-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathchannel_data_extractor.py
48 lines (40 loc) · 1.48 KB
/
channel_data_extractor.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
import numpy as np
import pandas as pd
import sys
import yapi
import xml.etree.ElementTree as ET
def authenticate(api_key):
api = yapi.YoutubeAPI(api_key)
return api
def channel_data(api, channel_id):
# channel details
channel_details = api.get_channel_by_id(channel_id)
if len(channel_details.items) > 0:
country = None
if 'country' in channel_details.items[0].snippet:
country = channel_details.items[0].snippet.country
# Parse channel in details
yt_channel_dict = {
'channel_id': channel_id,
'comment_count': int(channel_details.items[0].statistics.commentCount),
'hiddenSubscriberCount': int(channel_details.items[0].statistics.hiddenSubscriberCount),
'subscriberCount': int(channel_details.items[0].statistics.subscriberCount),
'videoCount': int(channel_details.items[0].statistics.videoCount),
'viewCount': int(channel_details.items[0].statistics.viewCount),
'country': country,
'description': channel_details.items[0].snippet.description,
'title': channel_details.items[0].snippet.title,
'publishedAt': channel_details.items[0].snippet.publishedAt,
'kind': channel_details.items[0].kind,
'etag': str(channel_details.items[0].etag)
}
with open(str(channel_id) + '.json', 'w') as fp:
json.dump(yt_channel_dict, fp)
if __name__ == '__main__':
api_key = '<API_KEY>'
api = authenticate(api_key)
with open('channel_names.txt', 'r') as fr:
content = fr.read().split('\n')
for vals in content:
print(vals)
channel_data(api, vals)