forked from Correia-jpv/github-follow-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GithubAPIBot.py
329 lines (269 loc) · 10.1 KB
/
GithubAPIBot.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from base64 import b64encode
import datetime
import random
import requests
from requests.adapters import HTTPAdapter
import time
from tqdm import tqdm
from urllib.parse import parse_qs
from urllib.parse import urlparse
from urllib3.util import Retry
class GithubAPIBot:
# Constructor
def __init__(
self,
username: str,
token: str,
sleepSecondsActionMin: int,
sleepSecondsActionMax: int,
sleepSecondsLimitedMin: int,
sleepSecondsLimitedMax: int,
sleepHour=None,
sleepMinute=None,
sleepTime=None,
maxAction=None,
):
if not isinstance(username, str):
raise TypeError("Missing/Incorrect username")
if not isinstance(token, str):
raise TypeError("Missing/Incorrect token")
self.__username = username
self.__token = token
self.__sleepSecondsActionMin = sleepSecondsActionMin
self.__sleepSecondsActionMax = sleepSecondsActionMax
self.__sleepSecondsLimitedMin = sleepSecondsLimitedMin
self.__sleepSecondsLimitedMax = sleepSecondsLimitedMax
self.__sleepHour = sleepHour
self.__sleepMinute = sleepMinute
self.__sleepTime = sleepTime
self.__maxAction = maxAction
self.__usersToAction = []
self.__followings = []
# Requests' headers
HEADERS = {
"Authorization": "Basic " + b64encode(str(self.token + ":" + self.token).encode("utf-8")).decode("utf-8")
}
# Session
self.session = requests.session()
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
self.session.mount("https://", HTTPAdapter(max_retries=retries))
self.session.headers.update(HEADERS)
# Authenticate
try:
res = self.session.get("https://api.github.com/user")
except requests.exceptions.RequestException as e:
raise SystemExit(e)
if res.status_code == 404:
raise ValueError("\nFailure to Authenticate, please check Personal Access Token and Username!")
else:
print("\nSuccessful authentication.")
self.getFollowings()
# Getters & Setters
@property
def username(self):
return self.__username
@username.setter
def username(self, value):
self.__username = value
@property
def token(self):
return self.__token
@token.setter
def token(self, value):
self.__token = value
@property
def sleepSecondsActionMin(self):
return self.__sleepSecondsActionMin
@sleepSecondsActionMin.setter
def sleepSecondsActionMin(self, value):
self.__sleepSecondsActionMin = value
@property
def sleepSecondsActionMax(self):
return self.__sleepSecondsActionMax
@sleepSecondsActionMax.setter
def sleepSecondsActionMax(self, value):
self.__sleepSecondsActionMax = value
@property
def sleepSecondsLimitedMin(self):
return self.__sleepSecondsLimitedMin
@sleepSecondsLimitedMin.setter
def sleepSecondsLimitedMin(self, value):
self.__sleepSecondsLimitedMin = value
@property
def sleepSecondsLimitedMax(self):
return self.__sleepSecondsLimitedMax
@sleepSecondsLimitedMax.setter
def sleepSecondsLimitedMax(self, value):
self.__sleepSecondsLimitedMax = value
@property
def sleepHour(self):
return self.__sleepHour
@sleepHour.setter
def sleepHour(self, value):
self.__sleepHour = value
@property
def sleepMinute(self):
return self.__sleepMinute
@sleepMinute.setter
def sleepMinute(self, value):
self.__sleepMinute = value
@property
def sleepTime(self):
return self.__sleepTime
@sleepTime.setter
def sleepTime(self, value):
self.__sleepTime = value
@property
def maxAction(self):
return self.__maxAction
@maxAction.setter
def maxAction(self, value):
self.__maxAction = value
@property
def usersToAction(self):
return self.__usersToAction
@usersToAction.setter
def usersToAction(self, value):
self.__usersToAction = value
@property
def followings(self):
return self.__followings
@followings.setter
def followings(self, value):
self.__followings = value
def getUsers(self, url="", maxAction=None, following=False):
users = []
try:
res = self.session.get(url)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
# Get usernames from each page
page = 1
while True:
try:
res = self.session.get(url + "?page=" + str(page)).json()
except requests.exceptions.RequestException as e:
raise SystemExit(e)
for user in res:
# Check if we already have enough usernames
if maxAction != None:
if len(users) >= int(maxAction):
break
# Add username if it's not being followed already
if (
not following
and not (user["login"] in self.followings)
or following
and (user["login"] in self.followings)
):
users.append(user["login"])
# Check if we already have enough usernames
if maxAction != None:
if len(users) >= int(maxAction):
break
if res == []:
break
else:
page += 1
return users
def getFollowers(self, username=None, following=None):
if username == None:
username = self.username
print(f"\nGrabbing {username}'s followers.\n")
self.usersToAction.extend(
self.getUsers(
url=f"https://api.github.com/users/{username}/followers",
maxAction=self.maxAction,
following=following,
)
)
def getFollowings(self, username=None):
if username == None:
username = self.username
print(f"\nGrabbing {username}'s followings.\n")
self.followings.extend(self.getUsers(url=f"https://api.github.com/users/{username}/following"))
def run(self, action):
if len(self.usersToAction) == 0:
print(f"Nothing to {action}")
else:
# Users to follow/unfollow must not exceed the given max
if self.maxAction != None:
self.usersToAction = self.usersToAction[: min(len(self.usersToAction), int(self.maxAction))]
# Time for the bot to go to sleep
if self.sleepHour != None and self.sleepMinute != None and self.sleepTime != None:
sleepTime = nextSleepTime(int(self.__sleepHour), int(self.sleepMinute))
# Start follow/unfollow
print(f"\nStarting to {action}.\n")
users = tqdm(
self.usersToAction,
initial=1,
dynamic_ncols=True,
smoothing=True,
bar_format="[PROGRESS] {n_fmt}/{total_fmt} |{l_bar}{bar}|",
position=0,
leave=False,
)
for user in users:
# Set the bot to sleep at the set time
if self.sleepHour != None and self.sleepMinute != None and self.sleepTime != None:
timeNow = datetime.datetime.now()
if timeNow.timestamp() > sleepTime.timestamp():
sleepTime = nextSleepTime(int(self.__sleepHour), int(self.__sleepMinute))
timeNow += datetime.timedelta(hours=int(self.__sleepTime))
sleepUntil(timeNow.hour, random.randint(0, 59))
# Follow/unfollow user
try:
if action == "follow":
res = self.session.put(f"https://api.github.com/user/following/{user}")
else:
res = self.session.delete(f"https://api.github.com/user/following/{user}")
except requests.exceptions.RequestException as e:
raise SystemExit(e)
# Unsuccessful
if res.status_code != 204:
sleepSeconds = random.randint(self.sleepSecondsLimitedMin, self.sleepSecondsLimitedMax)
# Successful
else:
sleepSeconds = random.randint(self.sleepSecondsActionMin, self.sleepSecondsActionMax)
# Sleep
sleepSecondsObj = list(range(0, sleepSeconds))
sleepSecondsBar = tqdm(
sleepSecondsObj,
dynamic_ncols=True,
smoothing=True,
bar_format="[SLEEPING] {n_fmt}s/{total_fmt}s |{l_bar}{bar}|",
position=1,
leave=False,
)
for second in sleepSecondsBar:
time.sleep(1)
print(f"\n\nFinished {action}ing!")
def follow(self):
self.run("follow")
def unfollow(self):
self.run("unfollow")
def nextSleepTime(hour, minute):
timeNow = datetime.datetime.now()
future = datetime.datetime(timeNow.year, timeNow.month, timeNow.day, hour, minute)
if timeNow.timestamp() > future.timestamp():
future += datetime.timedelta(days=1)
return future
def sleepUntil(hour, minute):
t = datetime.datetime.today()
future = datetime.datetime(t.year, t.month, t.day, hour, minute)
if t.timestamp() >= future.timestamp():
future += datetime.timedelta(days=1)
print(f'\nSleeping... Waking up at {future.hour}:{future.minute}')
sleepSeconds = int((future-t).total_seconds())
sleepSecondsObj = list(range(0, sleepSeconds))
sleepSecondsBar = tqdm(
sleepSecondsObj,
dynamic_ncols=True,
smoothing=True,
bar_format="[SLEEPING] {n_fmt}s/{total_fmt}s |{l_bar}{bar}|",
position=2,
leave=False,
)
for second in sleepSecondsBar:
time.sleep(1)
print(f'\nWaking up...')