-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretriever.py
More file actions
546 lines (448 loc) · 23.9 KB
/
retriever.py
File metadata and controls
546 lines (448 loc) · 23.9 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
import state
import pandas as pd
import shutil
from channels import get_videos_and_videocreators_from_file
from channels import get_metadata_channels_from_file
from channels import get_all_videos_by_all_channels_from_file
from channels import get_channels_activity_from_file
from comments import get_videos_comments_and_commenters_from_file
from services import build_service_api_key
from utils import get_filename
from utils import export_dataframe_to_excel
from utils import export_dict_to_csv
from utils import get_fullpath
from network import export_network_file
from datetime import datetime
from utils import get_api_key
import traceback
import sys
OUTPUT_DIRECTORY = "output"
OUTPUT_VIDEOS_FILE_NAME = "merged_videos_creators"
OUTPUT_COMMENTS_FILE_NAME = "merged_comments_commenters"
OUTPUT_SUB_COMMENTS_FILE_NAME = "merged_subcomments"
OUTPUT_CHANNEL_METADATA_FILE_NAME = "merged_channels_metadata"
OUTPUT_CHANNEL_ACTIVITY_FILE_NAME = "merged_channels_activity"
OUTPUT_CHANNEL_ALL_VIDEOS_FILE_NAME = "merged_channels_all_videos"
CHANNEL_METADATA_TEXT = "Metadata for all the channels"
CHANNEL_ACTIVITY_TEXT = "Last activity for all the channels"
CHANNEL_ALL_VIDEOS_TEXT = "All the videos for all the channels"
EXTENSION = "xlsx"
MSG_LIST = []
#-----------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------
def merge_csv_files(file_list,output_filename):
filename_path = ""
try:
if len(file_list)>0:
filename = file_list[0]
dataF = pd.read_csv(filename).T
i = 1
while (i < len(file_list)):
filename = file_list[i]
data = pd.read_csv(filename).T
dataF = pd.concat([dataF, data], axis=1, ignore_index=True)
i = i + 1
date_name = get_filename(output_filename, EXTENSION)
filename_path = export_dict_to_csv(dataF,OUTPUT_DIRECTORY,date_name)
except:
print ("Error when merging CSV files")
print(sys.exc_info()[0])
traceback.print_exc()
filename_path = "-1"
return filename_path
#-----------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------
def merge_excel_files(file_list, output_filename):
filename_path = ""
try:
if len(file_list)>0:
filename = file_list[0]
dataF = pd.read_excel(filename).T
i = 1
while (i < len(file_list)):
filename = file_list[i]
data = pd.read_excel(filename).T
dataF = pd.concat([dataF, data], axis=1, ignore_index=True)
i = i + 1
date_name = get_filename(output_filename, EXTENSION)
filename_path = export_dataframe_to_excel(dataF,OUTPUT_DIRECTORY,date_name)
except:
print ("Error when merging excel files")
print(sys.exc_info()[0])
traceback.print_exc()
filename_path = "-1"
return filename_path
#-----------------------------------------------------------------------------------------------------------------------
#Reset the quote to zero
#-----------------------------------------------------------------------------------------------------------------------
def reset_quote():
state_on_file = state.load_state_from_file()
if state_on_file:
state.state_yt = state_on_file
else:
state.state_yt = state.set_api_key(state.state_yt, get_api_key())
state.state_yt = state.set_quote_usage(state.state_yt, 0)
#state.print_state(state.state_yt)
#-----------------------------------------------------------------------------------------------------------------------
#This function will resume the retrieval of channel's metadata for a set of channels ids
#Starting in a particular index
#-----------------------------------------------------------------------------------------------------------------------
def resume_channels_metadata_retrievals(youtube, state_pr):
print ("Resuming channel's metadata retrievals...\n")
#Get filename with the videos ids to request
filename = state_pr[state.CHANNELS_IDS_FILE]
start_index = state_pr[state.CHANNEL_INDEX]
prefix = "state"
get_metadata_channels_from_file(youtube, filename, prefix, start_index)
#-----------------------------------------------------------------------------------------------------------------------
#This function will resume the retrieval of all videos by a channel for a set of channels ids
#Starting in a particular index
#-----------------------------------------------------------------------------------------------------------------------
def resume_channels_all_videos_retrievals(youtube, state_pr):
print ("Resuming all videos by a channel retrievals...\n")
#Get filename with the videos ids to request
filename = state_pr[state.CHANNELS_IDS_FILE]
start_index = state_pr[state.CHANNEL_INDEX]
prefix = "state"
get_all_videos_by_all_channels_from_file(youtube, filename, prefix, start_index)
#-----------------------------------------------------------------------------------------------------------------------
#This function will resume the retrieval of all videos by a channel for a set of channels ids
#Starting in a particular index
#-----------------------------------------------------------------------------------------------------------------------
def resume_channels_activity_retrievals(youtube, state_pr):
print ("Resuming latest activity for a list of channels... \n")
filename = state_pr[state.CHANNELS_IDS_FILE]
start_index = state_pr[state.CHANNEL_INDEX]
prefix = "state"
get_channels_activity_from_file(youtube, filename, prefix, start_index)
#-----------------------------------------------------------------------------------------------------------------------
#This function will resume the retrieval of videos for a set of videos ids
#Starting in a particular index
#-----------------------------------------------------------------------------------------------------------------------
def resume_video_retrievals(youtube, state_pr):
print ("Resuming video retrievals...\n")
#Get filename with the videos ids to request
filename = state_pr[state.VIDEOS_IDS_FILE]
start_index = state_pr[state.VIDEO_INDEX]
prefix = "state"
get_videos_and_videocreators_from_file(youtube, filename, prefix, start_index)
#-----------------------------------------------------------------------------------------------------------------------
#This function will resume the retrieval of comments for a set of videos ids
#Starting in a particular index
#-----------------------------------------------------------------------------------------------------------------------
def resume_comments_retrievals(youtube, state_pr):
# Get filename with the videos ids to request
print ("Resuming comments retrievals...\n")
filename = state_pr[state.VIDEOS_IDS_FILE]
start_index = state_pr[state.COMMENT_INDEX]
comments_count_filename = state_pr[state.COMMENTS_COUNT_FILE]
prefix = "state"
get_videos_comments_and_commenters_from_file(youtube, filename, prefix, comments_count_filename, start_index)
#-----------------------------------------------------------------------------------------------------------------------
#This function will merge the videos and comments files and create a network from them
#-----------------------------------------------------------------------------------------------------------------------
def output_retrievals():
print ("Merging files for output...")
success = True
try:
videos_merged_file = merge_excel_files(state.state_yt[state.LIST_VIDEOS_TO_MERGE], OUTPUT_VIDEOS_FILE_NAME)
state.state_yt[state.VIDEOS_MERGED]= videos_merged_file
comments_merged_file = merge_excel_files(state.state_yt[state.LIST_COMMENTS_TO_MERGE], OUTPUT_COMMENTS_FILE_NAME)
#sub_comments_merged_file = merge_csv_files(state.state_yt[state.LIST_SUBCOMMENTS_TO_MERGE], OUTPUT_SUB_COMMENTS_FILE_NAME)
state.state_yt[state.COMMENTS_MERGED] = comments_merged_file
if videos_merged_file!="-1" and comments_merged_file!="-1":
output_file = export_network_file("from_retrievals_", videosFilename=videos_merged_file.rstrip(),commentsFilename=comments_merged_file.rstrip())
state.state_yt[state.NETWORK_FILE] = output_file
state.remove_action(state.state_yt,state.ACTION_CREATE_NETWORK)
except:
success = False
print("Error on output_retrievals ")
print(sys.exc_info()[0])
traceback.print_exc()
return success
#-----------------------------------------------------------------------------------------------------------------------
#This function will merge channel retrievals
#-----------------------------------------------------------------------------------------------------------------------
def output_channel_retrievals(output_filename):
print ("Merging files for output...")
success = True
try:
channels_merged_file = merge_excel_files(state.state_yt[state.LIST_CHANNELS_TO_MERGE], output_filename)
state.state_yt[state. CHANNELS_MERGED]= channels_merged_file
except:
success = False
print("Error on output_channel_retrievals ")
print(sys.exc_info()[0])
traceback.print_exc()
return success
# -----------------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------------
def notify_user_on_file(nothing_to_retrieve=False, success=True):
try:
#Open file
name = get_filename("summary","txt")
fullname = get_fullpath("summaries", name)
fullname_plain = get_fullpath("summaries", "summary.txt")
f = open(fullname,'w+')
f.write("\n*****************************************************************\n")
f.write("Retrieving status\n")
now = datetime.now()
dt_string = now.strftime("%b %d %Y %H:%M:%S")
f.write(dt_string)
f.write("\n")
if len(state.state_yt["videos_files_to_merge"]) > 0 and (
state.ACTION_RETRIEVE_VIDEOS not in state.state_yt["actions"]):
f.write("\n** Sucessive video retrievals are located in: \n")
for p in state.state_yt["videos_files_to_merge"]:
f.write(p)
f.write("\n")
if len(state.state_yt["videos_merged"]) > 0:
if state.state_yt["videos_merged"] != "-1":
f.write("\n** File with merged retrievals' videos is located in: \n")
f.write(state.state_yt["videos_merged"])
f.write("\n")
else:
f.write("An error occurred when merging retrieved video files. \n")
if len(state.state_yt["comments_files_to_merge"]) > 0 and (
state.ACTION_RETRIEVE_COMMENTS not in state.state_yt["actions"]):
f.write("\n** Sucessive comments retrievals are located in: \n")
for p in state.state_yt["subcomments_files_to_merge"]:
f.write(p)
f.write("\n")
for p in state.state_yt["comments_files_to_merge"]:
f.write(p)
f.write("\n")
if len(state.state_yt["comments_merged"]) > 0:
if state.state_yt["comments_merged"] != "-1":
f.write("\n** File with merged retrievals' comments is located in: \n")
f.write(state.state_yt["comments_merged"])
f.write("\n")
else:
f.write("An error occurred when merging retrieved comments files. \n")
if state.state_yt["network_file"]:
if len(state.state_yt["network_file"]) > 0:
f.write("\n** Network file is located in: \n")
f.write(state.state_yt["network_file"])
f.write("\n")
f.write("\n")
if len(state.state_yt["actions"]) == 0:
if nothing_to_retrieve:
f.write("** No pending actions to retrieve \n")
else:
if success:
f.write("** Retrieving completed \n")
else:
f.write("** An error ocurred while retrieving and/or merging files. Please try the request again. \n")
else:
f.write("** The following actions will be completed when quota is available: \n")
if (state.ACTION_RETRIEVE_VIDEOS in state.state_yt["actions"]):
f.write("Retrieve videos \n")
if (state.ACTION_RETRIEVE_COMMENTS in state.state_yt["actions"]):
f.write("Retrieve comments \n")
if (state.ACTION_CREATE_NETWORK in state.state_yt["actions"]):
f.write("Build Network \n")
except:
f.write ("An error occurred while creating this summary. \n")
f.close()
shutil.copyfile(fullname,fullname_plain)
#-----------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------
def notify_user(nothing_to_retrieve=False, success=True):
print ("\n*******************************************************************************************")
print ("Retrieving status")
now = datetime.now()
dt_string = now.strftime("%b %d %Y %H:%M:%S")
print(dt_string)
state.print_quote_usage()
if len(state.state_yt["videos_files_to_merge"])>0 and (state.ACTION_RETRIEVE_VIDEOS not in state.state_yt["actions"]):
print ("\n** Sucessive video retrievals are located in: ")
for f in state.state_yt["videos_files_to_merge"]:
print (f)
if len(state.state_yt["videos_merged"])>0:
if state.state_yt["videos_merged"]!="-1":
print ("\n** File with merged retrievals' videos is located in: ")
print(state.state_yt["videos_merged"])
else:
print ("An error occurred when merging retrieved video files. \n")
if len(state.state_yt["comments_files_to_merge"])>0 and (state.ACTION_RETRIEVE_COMMENTS not in state.state_yt["actions"]):
print ("\n** Sucessive comments retrievals are located in: ")
for f in state.state_yt["subcomments_files_to_merge"]:
print (f)
for f in state.state_yt["comments_files_to_merge"]:
print(f)
if len(state.state_yt["comments_merged"])>0:
if state.state_yt["comments_merged"]!="-1":
print ("\n** File with merged retrievals' comments is located in: ")
print(state.state_yt["comments_merged"])
else:
print("An error occurred when merging retrieved comments files.")
if state.state_yt["network_file"]:
if len(state.state_yt["network_file"]) > 0:
print("\n** Network file is located in: ")
print(state.state_yt["network_file"])
print("\n")
if len(state.state_yt["actions"])==0:
if nothing_to_retrieve:
print ("** No pending actions to retrieve")
else:
if success:
print ("** Retrieving completed")
else:
print ("** An error ocurred while retrieving and/or merging files. Please try the request again.")
else:
print ("** The following actions will be completed when quota is available: ")
if (state.ACTION_RETRIEVE_VIDEOS in state.state_yt["actions"]):
print ("Retrieve videos ")
if (state.ACTION_RETRIEVE_COMMENTS in state.state_yt["actions"]):
print("Retrieve comments ")
if (state.ACTION_CREATE_NETWORK in state.state_yt["actions"]):
print("Build Network ")
# -----------------------------------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------------------------------
def notify_user_channel_on_file(text):
try:
#Open file
name = get_filename("summary","txt")
fullname = get_fullpath("summaries", name)
fullname_plain = get_fullpath("summaries", "summary.txt")
f = open(fullname,'w+')
f.write("\n*****************************************************************\n")
f.write("Retrieving status\n")
now = datetime.now()
dt_string = now.strftime("%b %d %Y %H:%M:%S")
f.write(dt_string)
f.write("\n")
f.write(text)
if len(state.state_yt[state.LIST_CHANNELS_TO_MERGE]) > 0:
f.write("\n** Sucessive channel's retrievals are located in: \n")
for fn in state.state_yt[state.LIST_CHANNELS_TO_MERGE]:
f.write(fn)
f.write("\n")
if len(state.state_yt[state.CHANNELS_MERGED]) > 0:
if state.state_yt[state.CHANNELS_MERGED] != "-1":
f.write("\n** File with merged channel's retrievals is located in: \n")
f.write(state.state_yt[state.CHANNELS_MERGED])
f.write("\n")
else:
f.write("An error occurred when merging retrieved channel's files. \n")
else:
f.write("** The following actions will be completed when quota is available: \n")
f.write(text)
except:
f.write ("An error occurred while creating this summary. \n")
f.write (sys.exc_info()[0])
traceback.print_exc()
f.close()
shutil.copyfile(fullname, fullname_plain)
#-----------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------
def notify_user_channel(text):
print ("\n*******************************************************************************************")
print ("Retrieving status")
now = datetime.now()
dt_string = now.strftime("%b %d %Y %H:%M:%S")
print(dt_string)
state.print_quote_usage()
print (text)
if len(state.state_yt[state.LIST_CHANNELS_TO_MERGE])>0:
print ("\n** Sucessive channel's retrievals are located in: ")
for f in state.state_yt[state.LIST_CHANNELS_TO_MERGE]:
print (f)
if len(state.state_yt[state.CHANNELS_MERGED])>0:
if state.state_yt[state.CHANNELS_MERGED]!="-1":
print ("\n** File with merged channel's retrievals is located in: ")
print(state.state_yt[state.CHANNELS_MERGED])
else:
print ("An error occurred when merging retrieved channel's files. \n")
else:
print("** The following actions will be completed when quota is available: ")
print (text)
#-----------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------
def resume_retrievals():
print ("************************************************************************************************")
now = datetime.now()
dt_string = now.strftime("%b %d %Y %H:%M:%S")
print(dt_string)
print ("Running Retriever ....\n")
#Load current state
state.state_yt = state.load_state_from_file()
print ("Current state: \n")
state.print_state(state.state_yt)
print ("************************************************************************************************")
print ("\n")
if not state.continue_to_retrieve(state.state_yt):
print ("Out of quota.")
return
#There is nothing to retrieve
if len(state.state_yt[state.LIST_ACTIONS])==0:
notify_user(nothing_to_retrieve=True)
notify_user_on_file(nothing_to_retrieve=True)
state.state_yt = state.clear_state(state.state_yt)
return
#Create youtube data api service
youtube = build_service_api_key()
#Actions run in a specific order
#For example, we cannot retrieve comments and then videos.
#First retrieve videos,then retrieve comments, and then build a network
#Channel retrieving actions are never in combination
if state.continue_to_retrieve(state.state_yt) and state.ACTION_RETRIEVE_CHANNELS_METADATA in state.state_yt[state.LIST_ACTIONS]:
resume_channels_metadata_retrievals(youtube, state.state_yt)
#Check if the retrieval finished
if state.ACTION_RETRIEVE_CHANNELS_METADATA not in state.state_yt[state.LIST_ACTIONS]:
print ("Call procedure to merge files")
success = output_channel_retrievals(OUTPUT_CHANNEL_METADATA_FILE_NAME)
notify_user_channel(CHANNEL_METADATA_TEXT)
notify_user_on_file(CHANNEL_METADATA_TEXT)
if state.state_yt[state.ALL_CHANNELS_RETRIEVED]:
state.clear_state(state.state_yt) # Quote usage remains and it will not be cleared out.
return
if state.continue_to_retrieve(state.state_yt) and state.ACTION_RETRIEVE_CHANNELS_ALL_VIDEOS in state.state_yt[
state.LIST_ACTIONS]:
resume_channels_all_videos_retrievals(youtube, state.state_yt)
# Check if the retrieval finished
if state.ACTION_RETRIEVE_CHANNELS_ALL_VIDEOS not in state.state_yt[state.LIST_ACTIONS]:
print("Call procedure to merge files")
success = output_channel_retrievals(OUTPUT_CHANNEL_ALL_VIDEOS_FILE_NAME)
notify_user_channel(CHANNEL_ALL_VIDEOS_TEXT)
notify_user_channel_on_file(CHANNEL_ALL_VIDEOS_TEXT)
if state.state_yt[state.ALL_CHANNELS_RETRIEVED]:
state.clear_state(state.state_yt) # Quote usage remains and it will not be cleared out.
return
if state.continue_to_retrieve(state.state_yt) and state.ACTION_RETRIEVE_CHANNELS_ACTIVITY in state.state_yt[
state.LIST_ACTIONS]:
resume_channels_activity_retrievals(youtube, state.state_yt)
# Check if the retrieval finished
if state.ACTION_RETRIEVE_CHANNELS_ACTIVITY not in state.state_yt[state.LIST_ACTIONS]:
print("\nCalling procedure to merge files \n")
success = output_channel_retrievals(OUTPUT_CHANNEL_ACTIVITY_FILE_NAME)
notify_user_channel(CHANNEL_ACTIVITY_TEXT)
notify_user_channel_on_file(CHANNEL_ACTIVITY_TEXT)
if state.state_yt[state.ALL_CHANNELS_RETRIEVED]:
state.clear_state(state.state_yt) # Quote usage remains and it will not be cleared out.
return
if state.continue_to_retrieve(state.state_yt) and state.ACTION_RETRIEVE_VIDEOS in state.state_yt[state.LIST_ACTIONS]:
resume_video_retrievals(youtube, state.state_yt)
if state.continue_to_retrieve(state.state_yt) and state.ACTION_RETRIEVE_COMMENTS in state.state_yt[state.LIST_ACTIONS]:
resume_comments_retrievals(youtube, state.state_yt)
if (len(state.state_yt[state.LIST_ACTIONS]) == 0) or (
(len(state.state_yt[state.LIST_ACTIONS]) == 1) and (state.ACTION_CREATE_NETWORK in state.state_yt[state.LIST_ACTIONS])):
# Only network action should remain, if everything has been processed.
success = output_retrievals()
notify_user(success=success)
notify_user_on_file(success=success)
state.clear_state(state.state_yt) #Quote usage remains and it will not be cleared out.
else:
notify_user()
notify_user_on_file()
if __name__ == "__main__":
reset_quote()
resume_retrievals()
#state.state_yt = state.load_state_from_file()
#print ("=======================================")
#state.print_state(state.state_yt)
#state.set_quote_usage(state.state_yt, 1000)
#state.add_action(state.state_yt, state.ACTION_RETRIEVE_VIDEOS)
#state.print_state(state.state_yt)
#reset_quote()
#state.print_state(state.state_yt)