-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube_data_api_client.py
124 lines (101 loc) · 4.43 KB
/
youtube_data_api_client.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
#!/usr/bin/env python3
import os
import httplib2
import isodate
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
%s
with information from the API Console
https://console.developers.google.com/
For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
"""
class YoutubeDataApiClient():
def __init__(self, client_secrets_file, scopes):
self.__client = self.get_youtube_data_api_client(
client_secrets_file, scopes)
def get_youtube_data_api_client(self, client_secrets_file, scopes):
message = MISSING_CLIENT_SECRETS_MESSAGE % os.path.abspath(
os.path.join(os.path.dirname(__file__), client_secrets_file))
flow = flow_from_clientsecrets(client_secrets_file,
scope=scopes,
message=message)
storage = Storage('youtube-oauth2.json')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage)
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
def get_my_channel(self):
channels = self.__client.channels().list(
part='snippet,statistics',
mine=True,
fields='items(id,snippet(title,description),statistics(videoCount,viewCount,subscriberCount))'
).execute()
channel = channels['items'][0]
snippet = channel['snippet']
statistics = channel['statistics']
return {
'id': channel['id'],
'title': snippet['title'],
'description': snippet['description'],
'video_count': statistics['videoCount'],
'view_count': statistics['viewCount'],
'subscriber_count': statistics['subscriberCount'],
}
def get_my_video_ids(self):
search_list_request = self.__client.search().list(
part='id',
forMine=True,
type='video',
order='date',
maxResults=50,
fields='nextPageToken,items(id(videoId))'
)
video_ids = []
while search_list_request:
search_list_response = search_list_request.execute()
for video in search_list_response['items']:
video_ids.append(video['id']['videoId'])
search_list_request = self.__client.search().list_next(
previous_request=search_list_request,
previous_response=search_list_response)
return video_ids
def get_videos(self, video_ids):
videos = []
for ids in self.__chunks(video_ids, 50):
videos_list = self.__client.videos().list(
id=','.join(ids),
part='snippet,contentDetails,statistics',
fields='items(id,snippet(title,description,publishedAt),contentDetails(duration),statistics(viewCount,likeCount,dislikeCount,commentCount))'
).execute()
for item in videos_list['items']:
snippet = item['snippet']
details = item['contentDetails']
statistics = item['statistics']
# https://stackoverflow.com/a/16743442
duration = isodate.parse_duration(details['duration'])
videos.append({
'id': item['id'],
'title': snippet['title'],
'description': snippet['description'],
'published_at': snippet['publishedAt'],
'duration': int(duration.total_seconds()),
'view_count': int(statistics['viewCount']),
'like_count': int(statistics['likeCount']),
'dislike_count': int(statistics['dislikeCount']),
'comment_count': statistics['commentCount']
})
return videos
def __chunks(self, l, n):
# https://stackoverflow.com/a/312464
for i in range(0, len(l), n):
yield l[i:i + n]