forked from TheDarkAssassins/Github-Follow-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow_API.py
65 lines (57 loc) · 2.73 KB
/
follow_API.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
63
64
65
import requests
import json
import argparse
import tqdm
import time
from base64 import b64encode
from datetime import datetime
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--token', help="Your GitHub Personal Access Token", required=True)
parser.add_argument('-m', '--my-username', help="Your GitHub Username", required=True)
parser.add_argument('-u', '--user-target', help="User to grab followers from")
parser.add_argument('-f', '--file', help="Follow users from a File pre-generated")
parser.add_argument('-mf', '--max-followers', help="Max Number of Followers to Follow")
args = parser.parse_args()
HEADERS = {"Authorization": "Basic " + b64encode(str(args.my_username + ":" + args.token).encode('utf-8')).decode('utf-8')}
res = requests.get("https://api.github.com/user", headers=HEADERS)
if res.status_code != 200:
print("Failure to Authenticate, please check Personal Access Token and Username!")
exit(1)
else:
print("Authentication Succeeded!")
sesh = requests.session()
sesh.headers.update(HEADERS)
if not args.file:
target = args.user_target
res = sesh.get("https://api.github.com/users/" + target + "/followers")
links_array = requests.utils.parse_header_links(res.headers['Link'].rstrip('>').replace('>,<', ',<'))
last_link = links_array[1]['url']
last_page = last_link.split('=')[-1]
users_to_follow = []
mnof = args.max_followers
print('Grabbing People to Follow\nThis may take a while... there are ' + str(last_page) + ' pages to go through.')
for page in tqdm.tqdm(range(1, int(last_page)), ncols=35, smoothing=True, bar_format='[PROGRESS] {n_fmt}/{total_fmt} | {bar}'):
res = sesh.get('https://api.github.com/users/' + target + "/followers?limit=100&page=" + str(page)).json()
for user in res:
users_to_follow.append(user['login'])
if mnof != None:
if len(users_to_follow) >= int(mnof):
break
filename = str(datetime.now().strftime('%m-%d-%YT%H-%M-%S')) + "-" + str(len(users_to_follow)) + "_FOLLOWED.json"
with open(filename, 'w+') as f:
json.dump(users_to_follow, f, indent=4)
else:
filename = args.file
with open(filename, 'r+') as f:
obj = json.load(f)
print("Starting to Follow Users... This WILL Take a while, we must avoid being rate-limited!")
for user in tqdm.tqdm(obj, ncols=35, smoothing=True, bar_format='[PROGRESS] {n_fmt}/{total_fmt} | {bar}'):
while True:
time.sleep(2)
res = sesh.put('https://api.github.com/user/following/' + user)
if res.status_code != 204:
print(res.status_code)
print("We may have been rate-limited, waiting until it stops!")
time.sleep(60)
else:
break