-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannels.py
More file actions
620 lines (517 loc) · 26.8 KB
/
channels.py
File metadata and controls
620 lines (517 loc) · 26.8 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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import traceback
import sys
import datetime
import state
from utils import export_dict_to_excel
from utils import get_filename
from utils import preprocess_string
from utils import convert_to_local_zone
from utils import get_ids_from_file
from videos import create_video_metadata
from videos import get_video_transcript
from videos import write_transcript_to_file
from utils import get_filename_ordered
#*****************************************************************************************************
#This function gets the most recent activity of a channel along with the type of activity
#*****************************************************************************************************
def get_channel_activity(youtube, channel_id):
record = {}
try:
if state.under_quote_limit(state.state_yt, state.UNITS_ACTIVITIES_LIST):
requestActivities = youtube.activities().list(
part="snippet,contentDetails",
channelId=channel_id
)
state.state_yt = state.update_quote_usage(state.state_yt, state.UNITS_ACTIVITIES_LIST)
responseActivities = requestActivities.execute()
if len(responseActivities["items"])>0:
for item in responseActivities["items"]:
if "snippet" in item:
record["activityDate"] = item["snippet"].get("publishedAt","N/A")
record["activityType"] = item["snippet"].get("type","N/A")
actType = item["snippet"].get("title",None)
if not actType:
actType = item["snippet"].get("channelTitle","N/A")
record["activityTitle"] = preprocess_string(actType)
break
else:
record=-1
else:
record = -2
return record
except:
print("Error on getting channels activity ")
print(sys.exc_info()[0])
traceback.print_exc()
return record
#*****************************************************************************************************
#This function creates a dictionary with a channel's metadata (send it as parameter).
#This dictionary will be used to create a record on the output excel file
#*****************************************************************************************************
def create_channel_dict(youtube, item):
try:
record ={}
record["channelId"] = item["id"]
if "snippet" in item:
record["channel_title"] = preprocess_string(item["snippet"].get("title","N/A"))
record["channel_description"] = preprocess_string(item["snippet"].get("description","N/A"))
record["channel_url"] = "www.youtube.com/channel/" + item["id"]
record["channel_JoinDate"] = item["snippet"].get("publishedAt","N/A")
record["channel_country"] = item["snippet"].get("country","N/A")
if "statistics" in item:
record["channel_viewCount"] = item["statistics"].get("viewCount","N/A")
record["channel_subscriberCount"] = item["statistics"].get("subscriberCount","N/A")
record["channel_videoCount"] = item["statistics"].get("videoCount","N/A")
#last_activity_date = get_channel_activity(youtube, item["id"])
#record.update(last_activity_date)
except:
print("Error on creating channel dictionary ")
print(sys.exc_info()[0])
traceback.print_exc()
return record
#*****************************************************************************************************
#This function creates a dictionary that combines a video's and its creator (a channel) metadata
#The video's metadata is sent in the parameter item
#The information of the channel is located in channel_records which is a dictionary of channel's metadata
#This dictionary will be used to create a record on the output excel file
#*****************************************************************************************************
def create_video_and_creator_dict(item, channels_records):
try:
now = datetime.datetime.now()
current_datetime_str = now.strftime("%Y-%m-%d, %H:%M:%S")
videoId = item.get("id","N/A")
title = ""
if "snippet" in item:
title = preprocess_string(item["snippet"].get("title", "N/A"))
publishedDate = convert_to_local_zone(item["snippet"].get("publishedAt", None))
description = preprocess_string(item["snippet"].get("description", "N/A"))
channelId = item["snippet"].get("channelId", "N/A")
url = "youtu.be/" + videoId
if "statistics" in item:
views = item["statistics"].get("viewCount", "N/A")
likes = item["statistics"].get("likeCount","N/A")
favoriteCount = item["statistics"].get("favoriteCount","N/A")
commentsCount = item["statistics"].get("commentCount","N/A")
if "contentDetails" in item:
duration = item["contentDetails"].get("duration", "N/A")
transcript_dict = get_video_transcript(videoId)
transcript_filename=''
if transcript_dict['data']:
transcript_filename = write_transcript_to_file(transcript_dict["data"], title, videoId)
channel_info = None
if channels_records:
try:
channel_info = channels_records[channelId]
except:
channel_info = None
metadata = {
"videoId": videoId,
"video_title": title,
"video_url": url,
"video_publishedAt": publishedDate,
"video_scrappedAt": current_datetime_str,
"video_duration": duration,
"video_views": views,
"video_likes": likes,
"video_favoriteCount": favoriteCount,
"video_commentsCount": commentsCount,
"video_description": description,
"video_Transcript Language": transcript_dict["language"],
"video_Transcript Type" : transcript_dict["tr_type"],
"video_Downloaded Transcript" : transcript_filename,
"video_channelId": channelId
}
metadata.update(channel_info)
except:
print("Error on creating dict: \n")
print(item)
print("\n")
print(sys.exc_info()[0])
traceback.print_exc()
metadata = {
"videoId": "",
"video_title": "",
"video_url": "",
"video_publishedAt": "",
"video_scrappedAt": "",
"video_duration": "",
"video_views": "",
"video_likes": "",
"video_favoriteCount": "",
"video_commentsCount": "",
"video_description": "",
"video_channelId": "",
"video_Transcript Language": "",
"video_Transcript Type": "",
"video_Downloaded Transcript": ""
}
return metadata
#*****************************************************************************************************
#This function finds all the videos created by channel_id and return the metadata for these videos
#as a dictionary of dictionaries
#*****************************************************************************************************
def get_all_videos_by_a_channel(youtube, channel_id):
records = {}
nextPageToken = None
count = 1
pages = 1
try:
while True and pages<=state.MAX_PAGES_SEARCHES:
if state.under_quote_limit(state.state_yt, state.UNITS_SEARCH_LIST+state.UNITS_VIDEOS_LIST):
video_channels_request = youtube.search().list(
part="snippet",
channelId=channel_id,
type="video",
maxResults=state.MAX_SEARCH_RESULTS_PER_REQUEST,
order="date",
pageToken=nextPageToken
)
state.state_yt = state.update_quote_usage(state.state_yt, state.UNITS_SEARCH_LIST)
response_videos_channels= video_channels_request.execute()
# Obtain video_id for each video in the response
videos_ids = []
#There is at least one video to register
if len(response_videos_channels['items'])>0:
for item in response_videos_channels['items']:
videoId = item["id"].get("videoId", "N/A")
videos_ids.append(videoId)
# Request all videos
videos_request = youtube.videos().list(
part="contentDetails,snippet,statistics",
id=','.join(videos_ids)
)
state.state_yt = state.update_quote_usage(state.state_yt, state.UNITS_VIDEOS_LIST)
videos_response = videos_request.execute()
for item in videos_response['items']:
metadata = create_video_metadata(item)
print('Channel {} - Video {} {}'.format(channel_id,item["id"],count))
#pprint.pprint(metadata)
records[count] = metadata
count = count + 1
else:
records = -1
nextPageToken = response_videos_channels.get('nextPageToken')
pages = pages + 1
if not nextPageToken:
break;
else:
records = -2
return records
except:
print("Error on getting all videos by a channel ")
print(sys.exc_info()[0])
traceback.print_exc()
return records
#*****************************************************************************************************
#This function retrieves the channels' metadata for each channel in channel_ids
#The metadata is returned as a dictionary of dictionaries
#*****************************************************************************************************
def get_channels_metadata(youtube, channel_ids, export):
try:
if len(channel_ids)==0:
return {}
nextPageToken = None
while True:
records = {}
# Request all channels
channels_request = youtube.channels().list(
part="contentDetails,id,snippet,statistics,status,topicDetails",
id=','.join(channel_ids),
maxResults=state.MAX_CHANNELS_PER_REQUEST,
pageToken=nextPageToken
)
# Update quote usage
state.state_yt = state.update_quote_usage(state.state_yt, state.UNITS_CHANNELS_LIST)
channels_response = channels_request.execute()
for item in channels_response["items"]:
record = create_channel_dict(youtube, item)
records[item["id"]] =record
nextPageToken = channels_response.get('nextPageToken')
if not nextPageToken:
break;
except:
print("Error on getting channel metadata for channels ")
print(sys.exc_info()[0])
traceback.print_exc()
if export==True:
# Export info to excel
filename = get_filename('channels_metadata','xlsx')
export_dict_to_excel(records, 'output', filename)
print ("Output is in " + filename)
return records
#*****************************************************************************************************
#For a given list of videos ids (videos_ids), this function retrieves the metadata for each video and
#its creator.
#It returns a dictionary where each entry is a dictionary combining both metadata
#The functio also exports to excel the combined metadata
#*****************************************************************************************************
def get_videos_and_videocreators(youtube, videos_ids, prefix_name, start_index=None):
records = {}
count = 1
# We request at most 50 videos at the time to avoid breaking the API
slicing = True
if start_index:
start = start_index
else:
start = 0
original_videos_ids = videos_ids
retrieving_cost = state.total_requests_cost(len(videos_ids)-start,state.MAX_VIDEOS_PER_REQUEST,state.UNITS_VIDEOS_LIST)
print ("Retrieving {} videos' metadata with a total cost of {} units".format(len(videos_ids)-start,retrieving_cost))
#Add action to state
state.state_yt = state.add_action(state.state_yt, state.ACTION_RETRIEVE_VIDEOS)
state.state_yt = state.set_all_retrieved(state.state_yt, state.ALL_VIDEOS_RETRIEVED, False)
#Save videos_ids to be processed after if we run out of quote
state.state_yt = state.set_videos_ids_file(state.state_yt,videos_ids)
#Add index of the first video to be processed
state.state_yt = state.set_video_index(state.state_yt, start)
end=start
try:
while (slicing):
#The cost of retrieving videos and channels
retrieving_cost = state.UNITS_VIDEOS_LIST + state.UNITS_CHANNELS_LIST
#Check if there is available quote
if state.under_quote_limit(state.state_yt, retrieving_cost):
end = start + state.MAX_VIDEOS_PER_REQUEST
if end >= len(original_videos_ids):
end = len(original_videos_ids)
slicing = False
videos_ids = original_videos_ids[start:end]
# Request all videos
videos_request = youtube.videos().list(
part="contentDetails,snippet,statistics",
maxResults=state.MAX_VIDEOS_PER_REQUEST,
id=','.join(videos_ids)
)
# Update quote_usage
state.state_yt = state.update_quote_usage(state.state_yt, state.UNITS_VIDEOS_LIST)
videos_response = videos_request.execute()
# Get channel_id
channels_ids = []
for item in videos_response['items']:
channelId = item["snippet"].get("channelId", None)
channels_ids.append(channelId)
channels_ids = set(channels_ids)
channel_records = get_channels_metadata(youtube, channels_ids, False)
#Merge video and channel info in only one dictionary
for item in videos_response['items']:
metadata = create_video_and_creator_dict(item, channel_records)
print('{} - Video {}'.format(count, metadata["videoId"]))
#records[count] = metadata
records[metadata["videoId"]]=metadata
count = count + 1
start = end
# Add index of the first video to be processed
state.state_yt = state.set_video_index(state.state_yt, start)
else:
slicing = False
except:
print("Error on get_videos_and_videocreators")
print(sys.exc_info()[0])
traceback.print_exc()
# Export info to excel
if len(records)>0:
directory = 'output'
filename = get_filename_ordered(directory, prefix_name, 'xlsx')
filename_path = export_dict_to_excel(records, directory, filename)
print("Output: " + filename_path)
#Add output filename to the list of files to merge (in case the action was not completed)
state.add_filename_to_list(state.state_yt, state.LIST_VIDEOS_TO_MERGE, directory, filename)
#if state.under_quote_limit(state.state_yt):
#All videos have been retrieved
if end >= len(original_videos_ids):
#All the retrieval was completed sucessfully
state.state_yt = state.remove_action(state.state_yt, state.ACTION_RETRIEVE_VIDEOS)
state.state_yt = state.set_all_retrieved(state.state_yt, state.ALL_VIDEOS_RETRIEVED, True)
# export_channels_videos_for_network(records)
return records
#-----------------------------------------------------------------------------------------------------------------------
#This function extracts a list of videos ids from an excel file (The excel file must contain the column
#videoId with the videos' ids)
#Once extracted this list, the function then calls the function get_videos_and_videocreators to retrieve
#the videos and its creators' metadata.
#-----------------------------------------------------------------------------------------------------------------------
def get_videos_and_videocreators_from_file(youtube, filename, prefix, start_index=None):
try:
#Load file
videos_ids = get_ids_from_file(filename, "videoId")
if videos_ids:
prefix_name = "file_"+ prefix + "_videos_creators"
#Get data from YouTube API
get_videos_and_videocreators(youtube, videos_ids, prefix_name, start_index)
else:
print ("Video's ids couldn't be retrieved. Check input file.")
except:
print("Error on get_videos_and_videocreators_from_file")
print(sys.exc_info()[0])
traceback.print_exc()
#***********************************************************************************************************************
#Get last activity for a list of channels
#***********************************************************************************************************************
def get_channels_activity_from_file(youtube, filename, prefix, start_index=None):
try:
#Load file
records={}
print ("Retrieving the last activity of all the channels... \n")
channels_ids_ori = get_ids_from_file(filename, "channelId")
channels_ids = remove_duplicates(channels_ids_ori)
if channels_ids:
state.state_yt = state.add_action(state.state_yt, state.ACTION_RETRIEVE_CHANNELS_ACTIVITY)
state.state_yt = state.set_all_retrieved(state.state_yt, state.ALL_CHANNELS_RETRIEVED, False)
# Add index of the first channel to be processed
if not start_index:
start_index = 0
state.state_yt = state.set_channel_index(state.state_yt, start_index)
# Save channels_ids to be processed after if we run out of quote
state.state_yt = state.set_channels_ids_file(state.state_yt, channels_ids)
# Get data from YouTube API
# Get data from YouTube API
while (state.under_quote_limit(state.state_yt, state.UNITS_ACTIVITIES_LIST)) and (
start_index < len(channels_ids)):
id = channels_ids[start_index]
print ("Processing channel {}".format(id))
r = get_channel_activity(youtube, id)
#Run out of quote
if r and r==-2:
break
if r and r!=-1:
records[id] = r
start_index = start_index + 1
state.state_yt = state.set_channel_index(state.state_yt, start_index)
if len(records) > 0:
# Export info to excel
directory = 'output'
filename = get_filename_ordered(directory, prefix + "channels_activity", 'xlsx')
filename_path = export_dict_to_excel(records, directory, filename)
print("Output: " + filename_path)
# Add output filename to the list of files to merge (in case the action was not completed)
state.add_filename_to_list(state.state_yt, state.LIST_CHANNELS_TO_MERGE, directory, filename)
# All videos have been retrieved
if start_index >= len(channels_ids):
# All the retrieval were completed successfully
state.state_yt = state.remove_action(state.state_yt, state.ACTION_RETRIEVE_CHANNELS_ACTIVITY)
state.state_yt = state.set_all_retrieved(state.state_yt, state.ALL_CHANNELS_RETRIEVED, True)
else:
print("\nOut of . Not all the channels were processed. \n")
else:
print ("Channel's ids couldn't be retrieved. Check input file.")
except:
print("Error on get_channels_activity_from_file")
print(sys.exc_info()[0])
traceback.print_exc()
return
#***********************************************************************************************************************
#Get all videos for all the channels ids in a file
#***********************************************************************************************************************
def get_all_videos_by_all_channels_from_file(youtube, filename, prefix, start_index = None):
#Load file
try:
records={}
count=0
channels_ids_ori = get_ids_from_file(filename, "channelId")
channels_ids = remove_duplicates(channels_ids_ori)
if channels_ids:
print ("Retrieving all videos for all the channels in a given file...")
state.state_yt = state.add_action(state.state_yt, state.ACTION_RETRIEVE_CHANNELS_ALL_VIDEOS)
state.state_yt = state.set_all_retrieved(state.state_yt, state.ALL_CHANNELS_RETRIEVED, False)
# Add index of the first channel to be processed
if not start_index:
start_index = 0
state.state_yt = state.set_channel_index(state.state_yt, start_index)
# Save channels_ids to be processed after if we run out of quote
state.state_yt = state.set_channels_ids_file(state.state_yt, channels_ids)
#Get data from YouTube API
# Get data from YouTube API
maximum_retrieving_cost = state.UNITS_SEARCH_LIST*5+state.UNITS_VIDEOS_LIST*5
while (state.under_quote_limit(state.state_yt, maximum_retrieving_cost) and (start_index < len(channels_ids))):
id = channels_ids[start_index]
videos = get_all_videos_by_a_channel(youtube, id)
if videos and videos==-2: #We run out of quote
break
if videos and videos!=-1: #The channel doesn't have any updated videos
for video in videos.items():
video[1]["channelId"] = id
records[count] = video[1]
count = count + 1
else:
print ("Channel {} doesn't have uploaded videos.".format(id))
start_index = start_index + 1
state.state_yt = state.set_channel_index(state.state_yt, start_index)
if len(records) > 0:
#Export info to excel
directory = 'output'
filename = get_filename_ordered(directory, prefix + "all_videos_by_channels", 'xlsx')
filename_path = export_dict_to_excel(records, directory, filename)
print("Output: " + filename_path)
# Add output filename to the list of files to merge (in case the action was not completed)
state.add_filename_to_list(state.state_yt, state.LIST_CHANNELS_TO_MERGE, directory, filename)
# All videos have been retrieved
if start_index >= len(channels_ids):
# All the retrieval was completed successfully
state.state_yt = state.remove_action(state.state_yt, state.ACTION_RETRIEVE_CHANNELS_ALL_VIDEOS)
state.state_yt = state.set_all_retrieved(state.state_yt, state.ALL_CHANNELS_RETRIEVED, True)
else:
print("Out of quota. Not all the channels were processed.")
else:
print ("Channel's ids couldn't be retrieved. Check input file.")
except:
print("Error on get_all_videos_by_all_channels_from_file")
print(sys.exc_info()[0])
traceback.print_exc()
return
# **********************************************************************************************************************
# **********************************************************************************************************************
def remove_duplicates(list):
clean_list=[]
if list and len(list)>0:
for item in list:
if item not in clean_list:
clean_list.append(item)
return clean_list
# ***********************************************************************************************************************
# Get all videos for all the channels ids in a file
# ***********************************************************************************************************************
def get_metadata_channels_from_file(youtube, filename, prefix, start_index = None):
# Load file
try:
records = {}
channels_ids_ori = get_ids_from_file(filename, "channelId")
channels_ids = remove_duplicates(channels_ids_ori)
if channels_ids and len(channels_ids)>0:
state.state_yt = state.add_action(state.state_yt, state.ACTION_RETRIEVE_CHANNELS_METADATA)
state.state_yt = state.set_all_retrieved(state.state_yt, state.ALL_CHANNELS_RETRIEVED, False)
# Add index of the first channel to be processed
if not start_index:
start_index = 0
state.state_yt = state.set_channel_index(state.state_yt, start_index)
# Save channels_ids to be processed after if we run out of quote
state.state_yt = state.set_channels_ids_file(state.state_yt, channels_ids)
# Get data from YouTube API
while (state.under_quote_limit(state.state_yt, state.UNITS_CHANNELS_LIST)) and (start_index<len(channels_ids)):
id =channels_ids[start_index]
print ("Obtaining metadata for channel {}".format(id))
metadata = get_channels_metadata(youtube, [id], False)
records[id] = metadata[id]
start_index = start_index + 1
state.state_yt = state.set_channel_index(state.state_yt, start_index)
if len(records)>0:
# Export info to excel
directory = 'output'
filename = get_filename_ordered(directory,prefix + "channels_metadata", 'xlsx')
filename_path = export_dict_to_excel(records, directory, filename)
print("Output: " + filename_path)
# Add output filename to the list of files to merge (in case the action was not completed)
state.add_filename_to_list(state.state_yt, state.LIST_CHANNELS_TO_MERGE, directory, filename)
# All videos have been retrieved
if start_index >= len(channels_ids):
# All the retrieval was completed sucessfully
state.state_yt = state.remove_action(state.state_yt, state.ACTION_RETRIEVE_CHANNELS_METADATA)
state.state_yt = state.set_all_retrieved(state.state_yt, state.ALL_CHANNELS_RETRIEVED, True)
else:
print ("Out of . Not all the channels were processed.")
else:
print("Channel's ids couldn't be retrieved. Check input file.")
except:
print("Error on get_channels_metadata_from_file")
print(sys.exc_info()[0])
traceback.print_exc()
return