@@ -133,7 +133,7 @@ def followings(self):
133
133
def followings (self , value ):
134
134
self .__followings = value
135
135
136
- def getUsers (self , url = "" , maxAction = None ):
136
+ def getUsers (self , url = "" , maxAction = None , following = False ):
137
137
users = []
138
138
139
139
try :
@@ -167,7 +167,12 @@ def getUsers(self, url="", maxAction=None):
167
167
break
168
168
169
169
# Add username if it's not being followed already
170
- if not (user ["login" ] in self .followings ):
170
+ if (
171
+ not following
172
+ and not (user ["login" ] in self .followings )
173
+ or following
174
+ and (user ["login" ] in self .followings )
175
+ ):
171
176
users .append (user ["login" ])
172
177
173
178
# Check if we already have enough usernames
@@ -177,65 +182,73 @@ def getUsers(self, url="", maxAction=None):
177
182
178
183
return users
179
184
180
- def getFollowers (self , username = None ):
185
+ def getFollowers (self , username = None , following = None ):
181
186
if username == None :
182
187
username = self .username
183
- print ("\n Grabbing " + username + " 's followers\n " )
188
+ print (f "\n Grabbing { username } 's followers. \n " )
184
189
self .usersToAction .extend (
185
- self .getUsers ("https://api.github.com/users/" + username + "/followers" , self .maxAction )
190
+ self .getUsers (
191
+ url = f"https://api.github.com/users/{ username } /followers" ,
192
+ maxAction = self .maxAction ,
193
+ following = following ,
194
+ )
186
195
)
187
196
188
- def getFollowings (self , username = None , maxAction = None ):
197
+ def getFollowings (self , username = None ):
189
198
if username == None :
190
199
username = self .username
191
- print ("\n Grabbing " + username + " 's followings.\n " )
192
- self .followings .extend (self .getUsers ("https://api.github.com/users/" + username + " /following", maxAction ))
200
+ print (f "\n Grabbing { username } 's followings.\n " )
201
+ self .followings .extend (self .getUsers (url = f "https://api.github.com/users/{ username } /following" ))
193
202
194
203
def run (self , action ):
195
- # Users to follow/unfollow must not exceed the given max
196
- if self .maxAction != None :
197
- self .usersToAction = self .usersToAction [: min (len (self .usersToAction ), int (self .maxAction ))]
198
-
199
- # Start follow/unfollow
200
- print ("\n Starting to " + action + ".\n " )
201
- users = tqdm (
202
- self .usersToAction ,
203
- initial = 1 ,
204
- dynamic_ncols = True ,
205
- smoothing = True ,
206
- bar_format = "[PROGRESS] {n_fmt}/{total_fmt} |{l_bar}{bar}|" ,
207
- leave = False ,
208
- )
209
- for user in users :
204
+ if len (self .usersToAction ) == 0 :
205
+ print (f"Nothing to { action } " )
206
+ else :
210
207
211
- # Follow/unfollow user
212
- try :
213
- if action == "follow" :
214
- res = self .session .put ("https://api.github.com/user/following/" + user )
215
- else :
216
- res = self .session .delete ("https://api.github.com/user/following/" + user )
217
- except requests .exceptions .RequestException as e :
218
- raise SystemExit (e )
208
+ # Users to follow/unfollow must not exceed the given max
209
+ if self .maxAction != None :
210
+ self .usersToAction = self .usersToAction [: min (len (self .usersToAction ), int (self .maxAction ))]
219
211
220
- # Unsuccessful
221
- if res .status_code != 204 :
222
- sleepSeconds = random .randint (self .sleepSecondsLimitedMin , self .sleepSecondsLimitedMax )
223
- # Successful
224
- else :
225
- sleepSeconds = random .randint (self .sleepSecondsActionMin , self .sleepSecondsActionMax )
226
-
227
- # Sleep
228
- sleepSecondsObj = list (range (0 , sleepSeconds ))
229
- sleepSecondsBar = tqdm (
230
- sleepSecondsObj ,
212
+ # Start follow/unfollow
213
+ print (f"\n Starting to { action } .\n " )
214
+ users = tqdm (
215
+ self .usersToAction ,
216
+ initial = 1 ,
231
217
dynamic_ncols = True ,
232
218
smoothing = True ,
233
- bar_format = "[SLEEPING] {n_fmt}s/{total_fmt}s |{l_bar}{bar}|" ,
219
+ bar_format = "[PROGRESS] {n_fmt}/{total_fmt} |{l_bar}{bar}|" ,
220
+ leave = False ,
234
221
)
235
- for second in sleepSecondsBar :
236
- time .sleep (1 )
237
-
238
- print ("\n \n Finished " + action + "ing!" )
222
+ for user in users :
223
+
224
+ # Follow/unfollow user
225
+ try :
226
+ if action == "follow" :
227
+ res = self .session .put (f"https://api.github.com/user/following/{ user } " )
228
+ else :
229
+ res = self .session .delete (f"https://api.github.com/user/following/{ user } " )
230
+ except requests .exceptions .RequestException as e :
231
+ raise SystemExit (e )
232
+
233
+ # Unsuccessful
234
+ if res .status_code != 204 :
235
+ sleepSeconds = random .randint (self .sleepSecondsLimitedMin , self .sleepSecondsLimitedMax )
236
+ # Successful
237
+ else :
238
+ sleepSeconds = random .randint (self .sleepSecondsActionMin , self .sleepSecondsActionMax )
239
+
240
+ # Sleep
241
+ sleepSecondsObj = list (range (0 , sleepSeconds ))
242
+ sleepSecondsBar = tqdm (
243
+ sleepSecondsObj ,
244
+ dynamic_ncols = True ,
245
+ smoothing = True ,
246
+ bar_format = "[SLEEPING] {n_fmt}s/{total_fmt}s |{l_bar}{bar}|" ,
247
+ )
248
+ for second in sleepSecondsBar :
249
+ time .sleep (1 )
250
+
251
+ print (f"\n \n Finished { action } ing!" )
239
252
240
253
def follow (self ):
241
254
self .run ("follow" )
0 commit comments