-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
75 lines (55 loc) · 1.86 KB
/
utils.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
from credentials import YT_API_KEY
from data_channel import CHANNELS_NAME, CHANNELS_ID
import time
from tqdm import tqdm
import requests
import pandas as pd
import json
import random
def get_url(channel_id):
url_base = 'https://youtube.googleapis.com/'
api_service_name = 'youtube/'
version = 'v3/'
params = 'channels?part=statistics&id='+channel_id+'&key='+YT_API_KEY
return (url_base + api_service_name + version + params)
def get_stats(channel_id):
url = get_url(channel_id)
channel_info = (requests.get(url).json())['items'][0]['statistics']
date = pd.to_datetime('today').strftime('%Y-%m-%d')
channel_stats = {
'createdAt': date,
'totalViews': int(channel_info['viewCount']),
'subscribers': int(channel_info['subscriberCount']),
'videoCount': int(channel_info['videoCount'])
}
return channel_stats
def create_df_with_info_channels():
dicc = {
'channelName' : CHANNELS_NAME,
'channelId' : CHANNELS_ID
}
file = pd.DataFrame(dicc)
file.to_csv('channels_to_analize.csv', index=False)
return (file)
def get_channels_statisticals(df_channels_info):
date = []
views = []
subscribers = []
video_count = []
channel_name = []
for i in tqdm(range(len(df_channels_info)), colour = 'green'):
info = get_stats(df_channels_info['channelId'][i])
video_count.append(info['videoCount'])
date.append(info['createdAt'])
views.append(info['totalViews'])
subscribers.append(info['subscribers'])
channel_name.append(df_channels_info['channelName'][i])
time.sleep(random.choice([1, 2, 3, 4]))
data = {
'createdAt': date,
'ChannelName': channel_name,
'totalViews': views,
'subscribers': subscribers,
'videoCount': video_count
}
return (pd.DataFrame(data))