-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_manager.py
executable file
·444 lines (372 loc) · 17.5 KB
/
task_manager.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
from multiprocessing import Process, Queue, current_process
from enum import Enum
from tqdm import tqdm
from collections import defaultdict
import json
import tweepy
import os
import queue
class TaskType(Enum):
"""
This defines the types of tasks that the TaskManager can perform.
"""
tweet_details = 0
retweets = 1
followers = 2
twohup_followers = 3
followees = 4
timeline = 5
user_details = 6
class TaskManager:
"""
The TaskManager allows scheduling of different type of Twitter data
tasks in a queue, which are executed in parallel by multiple processes.
Instance Variables:
- The tasks_pending queue stores all the pending tasks in a FIFO queue.
- The tasks_pending_dict is a dictionary that stores the set of tasks
pending corresponding to each task type.
- The folder paths corresponding to where the different types of
information will be stored are also defined.
"""
def __init__(self, base_folder_path, twitter_folder_path,
ignore_list=True, **args):
self.base_folder_path = base_folder_path
self.twitter_folder_path = twitter_folder_path
self.timeline_folder_path = twitter_folder_path + 'timelines/'
if not os.path.exists(self.timeline_folder_path):
os.makedirs(self.timeline_folder_path)
self.follower_folder_path = twitter_folder_path + 'followers/'
if not os.path.exists(self.follower_folder_path):
os.makedirs(self.follower_folder_path)
self.followee_folder_path = twitter_folder_path + 'followees/'
if not os.path.exists(self.followee_folder_path):
os.makedirs(self.followee_folder_path)
self.tweet_details_folder_path = twitter_folder_path + 'tweet_details/'
if not os.path.exists(self.tweet_details_folder_path):
os.makedirs(self.tweet_details_folder_path)
self.retweets_folder_path = twitter_folder_path + 'retweets/'
if not os.path.exists(self.retweets_folder_path):
os.makedirs(self.retweets_folder_path)
self.user_details_folder_path = twitter_folder_path + 'user_details/'
if not os.path.exists(self.user_details_folder_path):
os.makedirs(self.user_details_folder_path)
self.tasks_pending = Queue()
self.tasks_pending_dict = defaultdict(set)
self.ignore_list = ignore_list
def do_task(self, api):
"""
Queries the tasks_pending queue to fetch a new task if available,
and executes the tasks based on the TaskType.
"""
while True:
try:
object_id, task_type = self.tasks_pending.get_nowait()
except queue.Empty:
break
else:
try:
if task_type == TaskType.tweet_details:
self._get_tweet_details(object_id, api)
elif task_type == TaskType.retweets:
self._get_retweets(object_id, api)
elif task_type == TaskType.followers:
self._get_followers(object_id, api)
elif task_type == TaskType.followees:
self._get_followees(object_id, api)
elif task_type == TaskType.timeline:
self._get_timelines(object_id, api)
elif task_type == TaskType.user_details:
self._get_user_details(object_id, api)
except Exception as e:
print("\nError: Unable to complete " + str(task_type) +
" for id: " + str(object_id) + " - " + str(e) + '\n')
continue
finally:
print("\nProcessed: " + str(task_type) + " for id " +
str(object_id) + " is processed by " +
current_process().name + ".\nTasks left: " +
str(self.tasks_pending.qsize()) + '\n')
if object_id in self.tasks_pending_dict[task_type]:
self.tasks_pending_dict[task_type].remove(object_id)
return True
def run_tasks(self, apis):
"""
Create processes for parallel execution - each process will use
one API key to accomplish one task at a time.
"""
processes = []
for idx in range(len(apis)):
current_api = apis[idx]
# api_object = dill.dumps(current_api)
p = Process(target=self.do_task, args=(current_api,))
processes.append(p)
p.start()
# Avoiding deadlock
for idx, p in enumerate(processes):
while True:
running = p.is_alive()
if not self.tasks_pending.empty():
self.do_task(apis[idx])
else:
if not running:
break
# Completing processes
print("Waiting for processes to finish...")
for p in processes:
p.join()
def _get_tweet_details(self, tweet_id, api):
print("Getting tweet details of tweet {}".format(tweet_id))
tweet_details = api.get_status(tweet_id, tweet_mode='extended')
print("Writing the details of {} to file...".format(tweet_id))
with open(self.tweet_details_folder_path + '/' + str(tweet_id) +
'.json', 'w') as fw:
json.dump(tweet_details._json, fw)
return tweet_details
def _get_retweets(self, tweet_id, api):
print("Getting retweets of tweet {}".format(tweet_id))
retweets = api.retweets(tweet_id, 200, tweet_mode='extended')
print("Writing the {0} retweets of {1} to file".format(
len(retweets), tweet_id))
retweets_arr = []
for retweet in retweets:
retweets_arr.append(json.dumps(retweet._json))
with open(self.retweets_folder_path + str(tweet_id) +
'.json', 'w') as fw:
json.dump(retweets_arr, fw)
def _get_followers(self, user_id, api):
user_obj = api.get_user(user_id)
if self.add_user_to_ignore_list(user_obj):
return
user_id = user_obj.id_str
all_followers = self.get_all_followers(user_id)
followers_added = []
followers_subtracted = []
followers_current = set()
print("Getting followers of user {}".format(user_id))
try:
for follower in tqdm(tweepy.Cursor(
api.followers_ids, id=user_id).items(), unit="followers"):
followers_current.add(follower)
except Exception as e:
print("Error while fetching user followers: " + str(e))
else:
followers_added = [item for item in followers_current
if item not in all_followers]
followers_subtracted = [item for item in all_followers
if item not in followers_current]
followers = {'followers_added': followers_added,
'followers_subtracted': followers_subtracted}
print("Writing followers for user {}. Added: {}, Subtracted: {}"
.format(user_id, len(followers_added),
len(followers_subtracted)))
with open(self.follower_folder_path + str(user_id) +
'.json', 'w') as fw:
json.dump(followers, fw)
def _get_followees(self, user_id, api):
user_obj = api.get_user(user_id)
if self.add_user_to_ignore_list(user_obj):
return
user_id = user_obj.id_str
all_followees = self.get_all_followees(user_id)
followees_added = []
followees_subtracted = []
followees_current = set()
print("Getting followees of user {}".format(user_id))
try:
for followee in tqdm(tweepy.Cursor(
api.friends_ids, id=user_id).items(), unit="followees"):
followees_current.add(followee)
except Exception as e:
print("Error while fetching user followees: " + str(e))
else:
followees_added = [item for item in followees_current
if item not in all_followees]
followees_subtracted = [item for item in all_followees
if item not in followees_current]
followees = {'followees_added': followees_added,
'followees_subtracted': followees_subtracted}
print("Writing followees for user {}. Added: {}, Subtracted: {}"
.format(user_id, len(followees_added),
len(followees_subtracted)))
with open(self.followee_folder_path + str(user_id) +
'.json', 'w') as fw:
json.dump(followees, fw)
def _get_timelines(self, user_id, api):
user_obj = api.get_user(user_id)
if self.add_user_to_ignore_list(user_obj):
return
user_id = user_obj.id_str
last_tweet_id = self.get_last_tweet_id(user_id)
tweets_arr = []
print("Fetching timelines for user {}".format(user_id))
try:
if last_tweet_id != -1:
for tweet in tqdm(tweepy.Cursor(
api.user_timeline, id=user_id,
since_id=int(last_tweet_id),
tweet_mode='extended')
.items(), unit="tweets"):
tweets_arr.append(json.dumps(tweet._json))
else:
for tweet in tweepy.Cursor(
api.user_timeline, id=user_id,
tweet_mode='extended').items():
tweets_arr.append(json.dumps(tweet._json))
except Exception as e:
print("Error while fetching user timeline: " + str(e))
else:
print("Writing {} tweets of user {}"
.format(len(tweets_arr), user_id))
if (len(tweets_arr) != 0):
with open(self.timeline_folder_path + str(user_id) +
'.json', 'w') as fw:
json.dump(tweets_arr, fw)
def _get_user_details(self, user_id, api):
print("Getting user details of user {}".format(user_id))
user_obj = api.get_user(user_id)
user_id = user_obj.id_str
if os.path.exists(self.user_details_folder_path +
str(user_id) + '.json'):
print("Already fetched user details for user {}".format(user_id))
with open(self.user_details_folder_path + '/' + str(user_id) +
'.json') as f:
return json.load(f)
print("Writing the user object of {} to file...".format(user_id))
with open(self.user_details_folder_path + '/' + str(user_id) +
'.json', 'w') as fw:
json.dump(user_obj._json, fw)
return user_obj
def get_tweet_details(self, tweet_ids):
for tweet_id in tweet_ids:
if not os.path.exists(self.tweet_details_folder_path +
str(tweet_id) + '.json') \
and tweet_id not in \
self.tasks_pending_dict[TaskType.tweet_details]:
self.tasks_pending_dict[TaskType.tweet_details].add(tweet_id)
self.tasks_pending.put((tweet_id, TaskType.tweet_details))
def get_retweets(self, tweet_ids):
for tweet_id in tweet_ids:
if not os.path.exists(self.retweets_folder_path +
str(tweet_id) + ".json") \
and tweet_id not in \
self.tasks_pending_dict[TaskType.retweets]:
self.tasks_pending_dict[TaskType.retweets].add(tweet_id)
self.tasks_pending.put((tweet_id, TaskType.retweets))
def get_followers(self, user_ids):
for user_id in user_ids:
if not os.path.exists(self.follower_folder_path +
str(user_id) + ".json") \
and user_id not in \
self.tasks_pending_dict[TaskType.followers]:
self.tasks_pending_dict[TaskType.followers].add(user_id)
self.tasks_pending.put((user_id, TaskType.followers))
def get_followees(self, user_ids):
for user_id in user_ids:
if not os.path.exists(self.followee_folder_path +
str(user_id) + ".json") \
and user_id not in \
self.tasks_pending_dict[TaskType.followees]:
self.tasks_pending_dict[TaskType.followees].add(user_id)
self.tasks_pending.put((user_id, TaskType.followees))
def get_timelines(self, user_ids):
for user_id in user_ids:
if not os.path.exists(self.timeline_folder_path +
str(user_id) + ".json") \
and user_id not in \
self.tasks_pending_dict[TaskType.timeline]:
self.tasks_pending_dict[TaskType.timeline].add(user_id)
self.tasks_pending.put((user_id, TaskType.timeline))
def get_user_details(self, user_ids):
for user_id in user_ids:
if not os.path.exists(self.user_details_folder_path +
str(user_id) + '.json') \
and user_id not in \
self.tasks_pending_dict[TaskType.user_details]:
self.tasks_pending_dict[TaskType.user_details].add(user_id)
self.tasks_pending.put((user_id, TaskType.user_details))
def get_all_followers(self, user_id):
"""
Parses through the already fetched followers at previous timesteps
to find the complete list of followers of a user.
"""
all_followers = set()
time_folder_list = sorted(os.listdir(self.base_folder_path),
reverse=True)
for time_folder in time_folder_list:
if '.txt' in time_folder:
continue
followers_folder = self.base_folder_path + time_folder + \
'/twitter/followers/'
followers_file = followers_folder + str(user_id) + '.json'
if os.path.exists(followers_file):
print("Existing file found for user" + str(user_id) +
"in folder " + str(time_folder))
with open(followers_file) as f:
data = json.load(f)
all_followers.update(data['followers_added'])
for item in data['followers_subtracted']:
if item in all_followers:
all_followers.remove(item)
return all_followers
def get_all_followees(self, user_id):
"""
Parses through the already fetched followees at previous timesteps
to find the complete list of followees of a user.
"""
all_followees = set()
time_folder_list = sorted(os.listdir(self.base_folder_path),
reverse=True)
for time_folder in time_folder_list:
if '.txt' in time_folder:
continue
followees_folder = self.base_folder_path + time_folder + \
'/twitter/followees/'
followees_file = followees_folder + str(user_id) + '.json'
if os.path.exists(followees_file):
print("Existing file found for user" + str(user_id) +
"in folder " + str(time_folder))
with open(followees_file) as f:
data = json.load(f)
all_followees.update(data['followees_added'])
for item in data['followees_subtracted']:
if item in all_followees:
all_followees.remove(item)
return all_followees
def get_last_tweet_id(self, user_id):
"""
Parses through the already fetched timelines at previous timesteps
to find the last tweet_id fetched from a user's timeline.
"""
time_folder_list = sorted(os.listdir(self.base_folder_path),
reverse=True)
for time_folder in time_folder_list:
if '.txt' in time_folder:
continue
timelines_folder = self.base_folder_path + time_folder + \
'/twitter/timelines/'
timelines_file = timelines_folder + str(user_id) + '.json'
if os.path.exists(timelines_file):
print("Timeline file found for user" + str(user_id) +
"in folder " + str(time_folder))
with open(timelines_file) as f:
data = json.load(f)
last_tweet = json.loads(data[0])
last_tweet_id = int(last_tweet["id"])
return last_tweet_id
return -1
def add_user_to_ignore_list(self, user_obj):
"""
An user-level ignore list is maintined so that celebrity like users
are not processed, thereby avoiding exceedance of Twitter API rate
limits quickly.
"""
if not self.ignore_list:
return False
if user_obj.followers_count > 20000 or user_obj.friends_count > 20000:
print("IgnoreList: The user has more than 20000 " +
"followers/followees, ignoring.")
with open(self.base_folder_path +
'user_ignore_list.txt', 'a+') as fw:
fw.write(str(user_obj.id_str) + '\n')
return True
return False