-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoutube.py
656 lines (503 loc) · 25.4 KB
/
Youtube.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
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
import os
import google_auth_oauthlib.flow
from googleapiclient.discovery import build
import googleapiclient.errors
import streamlit as st
from pymongo import MongoClient
import pandas as pd
import mysql.connector
import numpy as np
from pandas.io import sql
from sqlalchemy import create_engine
# API Key: AIzaSyBAOevBsGQuErUKwIBSLdeX0ryfMS5mBu8
class youtube_harvest():
def __init__(self,api_key,youtube,channel_id):
self.api_key = api_key
self.youtube = youtube
self.channel_id = channel_id
def get_channel_stats(self):
request = self.youtube.channels().list(part = 'snippet,contentDetails,statistics',id = self.channel_id)
response = request.execute()
channel_info = dict(
channel_id = response['items'][0]['id'],
channel_name = response['items'][0]['snippet']['title'],
channel_description = response['items'][0]['snippet']['description'],
Subscribers = response['items'][0]['statistics']['subscriberCount'],
View_count = response['items'][0]['statistics']['viewCount'],
Total_videos = response['items'][0]['statistics']['videoCount'],
playlist_id = response['items'][0]['contentDetails']['relatedPlaylists']['uploads'])
return channel_info
def get_playlist_info(self,playlist_id):
request = self.youtube.playlistItems().list(part="snippet, contentDetails",playlistId = playlist_id)
response = request.execute()
video_ids = []
for i in response['items']:
video_ids.append(i['contentDetails']['videoId'])
next_page_token = response.get('nextPageToken')
while next_page_token is not None:
request = self.youtube.playlistItems().list(part="snippet, contentDetails",playlistId = playlist_id)
response = request.execute()
for i in response['items']:
video_ids.append(i['contentDetails']['videoId'])
next_page_token = response.get('nextPageToken')
return video_ids
def get_video_info(self,video_id):
vid_id = []
vid_name = []
view_count = []
like_count = []
fav_count = []
vid_duration = []
vid_des = []
vid_thumbnail = []
comm_count = []
chann_id = []
vid_publish = []
for i in range(len(video_id)):
request = self.youtube.videos().list(part = 'snippet,contentDetails,statistics',id = video_id[i])
response = request.execute()
vid_id.append(video_id[i])
vid_name.append(response['items'][0]['snippet']['title'])
view_count.append(response['items'][0]['statistics']['viewCount'])
like_count.append(response['items'][0]['statistics']['likeCount'])
fav_count.append(response['items'][0]['statistics']['favoriteCount'])
comm_count.append(response['items'][0]['statistics']['commentCount'])
vid_duration.append(response['items'][0]['contentDetails']['duration'])
vid_des.append(response['items'][0]['snippet']['description'])
vid_thumbnail.append(response['items'][0]['snippet']['thumbnails']['default']['url'])
chann_id.append(response['items'][0]['snippet']['channelId'])
vid_publish.append(response['items'][0]['snippet']['publishedAt'])
video_dict = dict(video_id = vid_id,
video_name = vid_name,
video_view_count = view_count,
video_like_count = like_count,
favourite_count = fav_count,
comment_count = comm_count,
duration = vid_duration,
desc = vid_des,
thumbnail = vid_thumbnail,
channel_ID = chann_id,
video_date = vid_publish
)
return video_dict
def get_playlists(self):
request = self.youtube.playlists().list(part = 'snippet,contentDetails',channelId = self.channel_id)
response = request.execute()
playlist_id = []
chann_id = []
playlist_name = []
for items in response['items']:
playlist_name.append(items['snippet']['title'])
playlist_id.append(items['id'])
chann_id.append(items['snippet']['channelId'])
play = dict(playlist_name = playlist_name,
playlist_id = playlist_id,
chann_id = chann_id)
return play
def get_comment_info(self,video_id):
vid_id = []
comment_id = []
comment_author = []
comment_text = []
comment_date = []
for i in range(len(video_id)):
try:
request = self.youtube.commentThreads().list(part = 'snippet',videoId = video_id[i])
response = request.execute()
if len(response['items']) > 0:
for j in response['items']:
comment_id.append(j['id'])
vid_id.append(j['snippet']['videoId'])
comment_text.append(j['snippet']['topLevelComment']['snippet']['textDisplay'])
comment_author.append(j['snippet']['topLevelComment']['snippet']['authorDisplayName'])
comment_date.append(j['snippet']['topLevelComment']['snippet']['publishedAt'])
except:
comment_id.append("")
vid_id.append(video_id[i])
comment_text.append("")
comment_author.append("")
comment_date.append("")
if len(vid_id)==0:
vid_id.append("")
comment_text.append("")
comment_author.append("")
comment_date.append("")
comment_id.append("")
comment = dict(comment_id = comment_id,
vid_id = vid_id,
comment_text = comment_text,
comment_author= comment_author,
comment_date= comment_date)
return comment
def send_to_mongodb(self,data_string,collection_name):
conn = MongoClient()
dbname = conn.get_database("Youtube")
collection = dbname[collection_name]
if collection_name == 'video':
df = pd.DataFrame(data_string)
collection.insert_many(df.to_dict('records'))
if collection_name == 'playlist':
df = pd.DataFrame(data_string)
collection.insert_many(df.to_dict('records'))
if collection_name == 'comment':
df = pd.DataFrame(data_string)
collection.insert_many(df.to_dict('records'))
else:
collection.insert_one(data_string)
def get_all_channel_names(self):
conn = MongoClient()
dbname = conn.get_database("Youtube")
collection = dbname['Final']
x = collection.find()
channel_names = []
for data in x:
channel_names.append(data['channel_stats']['channel_name'])
#remove duplicates if any
names = []
for x in channel_names:
if x not in names:
names.append(x)
return names
class migration():
def get_data_from_Mongo(self,channels):
conn = MongoClient()
dbname = conn.get_database("Youtube")
collection = dbname['Final']
x = collection.find()
channel_data = []
for data in x:
for i in channels:
if data['channel_stats']['channel_name'] == i:
channel_data.append(data)
return channel_data
def get_channel_data(self,channel_data):
channel_name = []
channel_id = []
channel_description = []
Subscribers = []
View_count = []
Total_videos = []
playlist_id = []
for i in range(len(channel_data)):
channel_name.append(channel_data[i]['channel_stats']['channel_name'])
channel_id.append(channel_data[i]['channel_stats']['channel_id'])
channel_description.append(channel_data[i]['channel_stats']['channel_description'])
Subscribers.append(int(channel_data[i]['channel_stats']['Subscribers']))
View_count.append(int(channel_data[i]['channel_stats']['View_count']))
Total_videos.append(int(channel_data[i]['channel_stats']['Total_videos']))
playlist_id.append(channel_data[i]['channel_stats']['playlist_id'])
df = pd.DataFrame(np.column_stack([channel_name,channel_id,channel_description,Subscribers,View_count,Total_videos,
playlist_id]),
columns= ['channel_name','channel_id','channel_description','Subscribers','View_count',
'Total_videos','playlist_id'] )
#df = pd.DataFrame(channel_name = channel_name, channel_id = channel_id,channel_description =channel_description,
#Subscribers = Subscribers, View_count =View_count, Total_videos =Total_videos,playlist_id = playlist_id)
return df
def get_playlist_data(self,channel_data):
playlist_id = []
playlist_name = []
channel_id = []
for i in range(len(channel_data)):
for j in range(len(channel_data[i]['playlists']['playlist_name'])):
playlist_name.append(channel_data[i]['playlists']['playlist_name'][j])
for k in range(len(channel_data[i]['playlists']['chann_id'])):
channel_id.append(channel_data[i]['playlists']['chann_id'][k])
for l in range(len(channel_data[i]['playlists']['playlist_id'])):
playlist_id.append(channel_data[i]['playlists']['playlist_id'][l])
df = pd.DataFrame(np.column_stack([playlist_id,playlist_name,channel_id]),
columns= ['playlist_id','playlist_name','channel_id']
)
return df
def get_comment_data(self,channel_data):
vid_id = []
comment_id = []
comment_author = []
comment_text = []
comment_date = []
for i in range(len(channel_data)):
for j in range(len(channel_data[i]['comments']['comment_id'])):
comment_id.append(channel_data[i]['comments']['comment_id'][j])
for k in range(len(channel_data[i]['comments']['vid_id'])):
vid_id.append(channel_data[i]['comments']['vid_id'][k])
for l in range(len(channel_data[i]['comments']['comment_author'])):
comment_author.append(channel_data[i]['comments']['comment_author'][l])
for m in range(len(channel_data[i]['comments']['comment_text'])):
comment_text.append(channel_data[i]['comments']['comment_text'][m])
for n in range(len(channel_data[i]['comments']['comment_date'])):
comment_date.append(channel_data[i]['comments']['comment_date'][n])
df = pd.DataFrame(np.column_stack([comment_id,vid_id,comment_author,comment_text,comment_date]),
columns= ['comment_id','vid_id','comment_author','comment_text','comment_date']
)
return df
def convert_duration(self,value):
if value.find('M') == -1:
return 1
else:
return int(value.split('T')[1].split('M')[0])
def get_video_data(self,channel_data):
video_id = []
video_name = []
video_view_count = []
video_like_count = []
favorite_count = []
duration = []
comment_count = []
channel_id = []
video_date = []
for i in range(len(channel_data)):
for j in range(len(channel_data[i]['video_stats']['video_id'])):
video_id.append(channel_data[i]['video_stats']['video_id'][j])
for k in range(len(channel_data[i]['video_stats']['video_name'])):
video_name.append(channel_data[i]['video_stats']['video_name'][k])
for l in range(len(channel_data[i]['video_stats']['video_view_count'])):
video_view_count.append(int(channel_data[i]['video_stats']['video_view_count'][l]))
for m in range(len(channel_data[i]['video_stats']['video_like_count'])):
video_like_count.append(int(channel_data[i]['video_stats']['video_like_count'][m]))
for n in range(len(channel_data[i]['video_stats']['favourite_count'])):
favorite_count.append(int(channel_data[i]['video_stats']['favourite_count'][n]))
for o in range(len(channel_data[i]['video_stats']['comment_count'])):
comment_count.append(int(channel_data[i]['video_stats']['comment_count'][o]))
for p in range(len(channel_data[i]['video_stats']['channel_ID'])):
channel_id.append(channel_data[i]['video_stats']['channel_ID'][p])
for b in range(len(channel_data[i]['video_stats']['duration'])):
value = channel_data[i]['video_stats']['duration'][b]
dur = self.convert_duration(value)
duration.append(dur)
for a in range(len(channel_data[i]['video_stats']['video_date'])):
value = channel_data[i]['video_stats']['video_date'][a]
video_date.append(value.split('T')[0])
df = pd.DataFrame(np.column_stack([video_id,
video_name,
video_view_count,
video_like_count,
favorite_count,
duration,
comment_count,
channel_id,
video_date]),columns=[ 'video_id',
'video_name',
'video_view_count',
'video_like_count',
'favorite_count',
'duration',
'comment_count',
'channel_id',
'video_date'])
return df
def migrate_to_sql(self,data,schema):
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="nishantsai8@P",
database = "youtube"
)
mycursor = mydb.cursor()
if schema == "Channel":
for row,col in data.iterrows():
channel_name = col[0]
channel_id = col[1]
channel_description = col[2]
subscribers = col[3]
view_count = col[4]
total_videos = col[5]
playlist_id = col[6]
query = """insert into channel (channel_name,channel_id,channel_description,subscribers,view_count,total_videos,playlist_id)
values (%s,%s,%s,%s,%s,%s,%s)"""
record = (channel_name,channel_id,channel_description,subscribers,view_count,total_videos,playlist_id)
mycursor.execute(query,record)
if schema == "Playlist":
for row,col in data.iterrows():
playlist_id = col[0]
playlist_name = col[1]
channel_id = col[2]
query = """insert into playlist (playlist_id,playlist_name,channel_id)
values (%s,%s,%s)"""
record = (playlist_id,playlist_name,channel_id)
mycursor.execute(query,record)
if schema == "Comment":
for row,col in data.iterrows():
comment_id = col[0]
vid_id = col[1]
comment_author = col[2]
comment_text = col[3]
comment_date = col[4]
query = """insert into comments (comment_id,vid_id,comment_author,comment_text,comment_date)
values (%s,%s,%s,%s,%s)"""
record = (comment_id,vid_id,comment_author,comment_text,comment_date)
mycursor.execute(query,record)
if schema == "Video":
for row,col in data.iterrows():
video_id = col[0]
video_name = col[1]
video_view_count = col[2]
video_like_count = col[3]
favorite_count = col[4]
duration = col[5]
comment_count = col[6]
channel_id = col[7]
video_date = col[8]
query = """insert into videos (video_id,
video_name,
video_view_count,
video_like_count,
favorite_count,
duration,
comment_count,
channel_id,
video_date)
values (%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
record = ( video_id,
video_name,
video_view_count,
video_like_count,
favorite_count,
duration,
comment_count,
channel_id,
video_date)
mycursor.execute(query,record)
mydb.commit()
mycursor.close()
class Analysis:
def query(self,selection):
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="nishantsai8@P",
database = "youtube"
)
#1
if selection == "What are the names of all the videos and their corresponding channels?":
query = """select video_name as "Video Name", channel_name as "Channel Name" from videos
left join channel on videos.channel_id = channel.channel_id"""
#2
if selection == "Which channels have the most number of videos, and how many videos do they have?":
query = """select channel_name as "Channel Name" ,count( distinct video_id) as "Total Videos" from channel
left join videos on channel.channel_id = videos.channel_id
group by channel_name
order by count(distinct video_id) DESC"""
#3
if selection == "What are the top 10 most viewed videos and their respective channels?":
query = """select video_name as "Video Name",
channel_name as "Channel Name",
sum(video_view_count) as "Views" from videos
left join channel on videos.channel_id = channel.channel_id
group by video_name, channel_name
order by sum(video_view_count) DESC
limit 10"""
#4
if selection == "How many comments were made on each video, and what are their orresponding video names?":
query = """select video_name as "Video Name",
sum(comment_count) as "Total Comments" from videos
group by video_name
order by sum(comment_count) DESC"""
#5
if selection == "Which videos have the highest number of likes, and what are their corresponding channel names?":
query = """select video_name as "Video Name",
channel_name as "Channel Name",
sum(video_like_count) as "Likes" from videos
left join channel on videos.channel_id = channel.channel_id
group by video_name, channel_name
order by sum(video_like_count) DESC"""
#6
if selection == "What is the total number of likes for each video, and what are their corresponding video names?":
query = """select video_name as "Video Name",
sum(video_like_count) as "Likes" from videos
group by video_name"""
#7
if selection == "What is the total number of views for each channel, and what are their corresponding channel names?":
query = """select channel_name as "Channel Name", view_count as "Total Views"
from channel"""
#8
if selection == "What are the names of all the channels that have published videos in the year 2022?":
query = """select distinct(channel_name) as "Channel Name" from
(select video_name as "Video Name",channel_name
from
videos left join channel on videos.channel_id = channel.channel_id
where Year(str_to_date(video_date,'%Y-%m-%d')) = 2022)a"""
#9
if selection == "What is the average duration of all videos in each channel, and what are their corresponding channel names?":
query = """select channel_name as "Channel Name", Truncate(avg(duration),1) as "Average Duration" from
channel left join videos on channel.channel_id = videos.channel_id
group by channel_name
order by avg(duration) desc"""
#10
if selection == "Which videos have the highest number of comments, and what are their corresponding channel names?":
query = """select channel_name as "Channel Name" , count(distinct comment_id) as "Total Comments"
from channel left join videos on channel.channel_id = videos.channel_id
left join comments on videos.video_id = comments.vid_id
group by channel_name
order by count(distinct comment_id) DESC"""
result = pd.read_sql(query,mydb)
return result
if __name__ == '__main__':
st.title("Youtube Data Harvesting Project")
api_key = "AIzaSyC7ktZA3hGYjmkxWQovcY1y86nhqUv9iRE"
title = st.text_input("Enter YouTube Channel ID")
a = st.button("Send Data to Mongo DB")
#All Channel IDs used in this Project
#1 UC2RqY29NIU7r1uUQwArZlIA
#2 UC-Rgo272A1RekreoF7lZkuQ
#3 UC-EcI50x2OawLjqdouNZPWQ
#4 UC-HTPRCwql-htDEFcEFvWyg
#5 UC--GIC9hgPZeJa6iZqXMZLw
#6 UC-0MCY2erzgB4WqpmEIGgpg
#7 UC-0PCGA_FSGdeyg16QLwEQw
#8 UC-_eVDj3aTFKoAYqSzcybkA
#9 UCjmTxX1jXZPvTwUQHR9tBhg
#10 UCeU5qTuBiqTU5z-hyL6WITQ
try:
channel_id = title
#channel_id = "UCo1iK1Y_rAr_CdiCU8hMaqA"
youtube = build('youtube','v3',developerKey=api_key)
data = youtube_harvest(api_key,youtube,channel_id)
channel_stats = data.get_channel_stats()
#print(channel_stats)
playlist_id = channel_stats['playlist_id']
playlist_stats = data.get_playlist_info(playlist_id=playlist_id)
#print(playlist_stats)
video_stats = data.get_video_info(video_id=playlist_stats)
#print(video_stats)
playlists = data.get_playlists()
#print(playlists)
comments = data.get_comment_info(video_id=playlist_stats)
except:
st.write("")
if a:
final = dict(channel_stats = channel_stats,
video_stats = video_stats,
playlists = playlists,
comments = comments)
st.write(final)
data.send_to_mongodb(final,"Final")
all_channels = data.get_all_channel_names()
selected_channels = st.multiselect("Select Channel(s) to Migrate to SQL:",all_channels)
b = st.button("Migrate to SQL Database")
if b:
migrate = migration()
mongo_db_info = migrate.get_data_from_Mongo(selected_channels)
channel = migrate.get_channel_data(mongo_db_info)
migrate.migrate_to_sql(channel,"Channel")
playlist = migrate.get_playlist_data(mongo_db_info)
migrate.migrate_to_sql(playlist,"Playlist")
comment = migrate.get_comment_data(mongo_db_info)
migrate.migrate_to_sql(comment,"Comment")
video = migrate.get_video_data(mongo_db_info)
migrate.migrate_to_sql(video,"Video")
queries = ("What are the names of all the videos and their corresponding channels?",
"Which channels have the most number of videos, and how many videos do they have?",
"What are the top 10 most viewed videos and their respective channels?",
"How many comments were made on each video, and what are their orresponding video names?",
"Which videos have the highest number of likes, and what are their corresponding channel names?",
"What is the total number of likes for each video, and what are their corresponding video names?",
"What is the total number of views for each channel, and what are their corresponding channel names?",
"What are the names of all the channels that have published videos in the year 2022?",
"What is the average duration of all videos in each channel, and what are their corresponding channel names?",
"Which videos have the highest number of comments, and what are their corresponding channel names?"
)
query_selected = st.selectbox("Select a Query: ",queries)
c = st.button("Get Results")
ana = Analysis()
if c:
result = ana.query(query_selected)
st.write(result)