-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-follow-auto.py
62 lines (52 loc) · 1.96 KB
/
gh-follow-auto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import requests
import os
import time
# Replace with your GitHub username and load token from environment
username = os.getenv('GITHUB_USERNAME')
token = os.getenv('GITHUB_TOKEN')
# Headers for authentication
headers = {
'Authorization': f'token {token}',
'Accept': 'application/vnd.github.v3+json'
}
# Helper function to handle pagination
def fetch_all(url):
results = []
while url:
response = requests.get(url, headers=headers)
if response.status_code != 200:
print(f"Failed to fetch: {url}")
break
results.extend(response.json())
url = response.links.get('next', {}).get('url') # Get next page URL
return results
# Fetch followers and following
followers_url = f'https://api.github.com/users/{username}/followers'
following_url = f'https://api.github.com/users/{username}/following'
followers = {user['login'] for user in fetch_all(followers_url)}
following = {user['login'] for user in fetch_all(following_url)}
# Identify users
not_following_back = following - followers
not_followed_back = followers - following
# Unfollow users not following back
for user in not_following_back:
unfollow_url = f'https://api.github.com/user/following/{user}'
response = requests.delete(unfollow_url, headers=headers)
if response.status_code == 204:
print(f"Unfollowed {user}")
else:
print(f"Failed to unfollow {user}")
time.sleep(1) # Delay between requests
# Follow users not followed back
for user in not_followed_back:
follow_url = f'https://api.github.com/user/following/{user}'
response = requests.put(follow_url, headers=headers)
if response.status_code == 204:
print(f"Followed {user}")
else:
print(f"Failed to follow {user}")
time.sleep(1) # Delay between requests
print("\nAccounts you're following but not following you back:")
print(not_following_back)
print("\nAccounts following you but you're not following back:")
print(not_followed_back)