-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathytapi.py
More file actions
194 lines (148 loc) · 5.07 KB
/
ytapi.py
File metadata and controls
194 lines (148 loc) · 5.07 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
from apiclient.discovery import build
def create_service(service, version, key):
"""
Create a keyed google api service.
"""
return build(service, version, developerKey=key)
def get_channel_by_id(channel_id, ytservice):
response = ytservice.channels().list(
part='snippet',
id=channel_id,
).execute()
channels = response.get('items')
if channels:
channel = channels[0]
return ChannelSnippetResponseWrapper(channel)
def get_channel_by_username(username, ytservice):
response = ytservice.search().list(
part='snippet',
q=username,
type='channel'
).execute()
channels = response.get('items')
if channels:
channel = channels[0]
return ChannelSnippetResponseWrapper(channel)
def subscriptions(channel_id, ytservice):
"""
Generates the full subscription list for a channel.
"""
next_page_token = 'None'
while next_page_token:
if next_page_token == 'None':
next_page_token = None
response = ytservice.subscriptions().list(
part='snippet',
channelId=channel_id,
pageToken=next_page_token,
).execute()
channels = response.get('items')
next_page_token = response.get('nextPageToken')
for item in channels:
yield ChannelSnippetResponseWrapper(item)
def uploads(channel_id, ytservice):
# Get channels uploads playlist id
content_details_resp = ytservice.channels().list(
id=channel_id,
part='ContentDetails',
).execute()
content_details = one_item_response(content_details_resp)
uploads_playlist_id = playlists_from_content_details(content_details)['uploads']
# Get video ids from playlistItems
next_page_token = 'None'
while next_page_token:
if next_page_token == 'None':
next_page_token = None
playlist_resp = ytservice.playlistItems().list(
part='ContentDetails',
playlistId=uploads_playlist_id,
pageToken=next_page_token,
).execute()
next_page_token = playlist_resp.get('nextPageToken')
# Retreive video info
for item in playlist_resp['items']:
video_id = item['contentDetails']['videoId']
video_info = one_item_response(
ytservice.videos().list(
id=video_id,
part='snippet',
).execute()
)
yield VideoSnippetResponseWrapper(video_info)
def channel_from_snippet(resp):
"""
Convert a Youtube api snippet response into a dictionary of channel info.
Contains: name, image, description, channel_id
"""
return {
'name': resp['snippet']['title'],
'image': resp['snippet']['thumbnails']['high']['url'],
'description': resp['snippet']['description'],
'channel_id': resp['snippet']['resourceId']['channelId'],
}
def video_from_snippet(resp):
""" Convert a Youtube api snippet response into a dictionary of video info.
Contains: name, image, description, channel_id, video_id
"""
return {
'name': resp['snippet']['title'],
'image': resp['snippet']['thumbnails']['high']['url'],
'description': resp['snippet']['description'],
'channel_id': resp['snippet']['channelId'],
'date': resp['snippet']['publishedAt'],
'video_id': resp['id'],
}
def playlists_from_content_details(resp):
return resp['contentDetails']['relatedPlaylists']
def one_item_response(resp):
return resp['items'][0]
class ChannelSnippetResponseWrapper(object):
def __init__(self, resp):
self._resp = resp
@property
def name(self):
return self._resp['snippet']['title']
@property
def image(self):
return self._resp['snippet']['thumbnails']['high']['url']
@property
def description(self):
return self._resp['snippet']['description']
@property
def channel_id(self):
return self._resp['snippet']['resourceId']['channelId']
@property
def data(self):
return channel_from_snippet(self._resp)
def __str__(self):
return "<ChannelSnippet {}>".format(self.name)
def __repr__(self):
return str(self)
class VideoSnippetResponseWrapper(object):
def __init__(self, resp):
self._resp = resp
@property
def name(self):
return self._resp['snippet']['title']
@property
def image(self):
return self._resp['snippet']['thumbnails']['high']['url']
@property
def description(self):
return self._resp['snippet']['description']
@property
def channel_id(self):
return self._resp['snippet']['channelId']
@property
def video_id(self):
return self._resp['id']
@property
def date(self):
return self._resp['snippet']['publishedAt']
@property
def data(self):
return video_from_snippet(self._resp)
def __str__(self):
return "<VideoSnippet {} {}>".format(self.name, self.channel_id)
def __repr__(self):
return str(self)