-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.py
232 lines (194 loc) · 7.71 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
# coding=utf-8
import httplib2
from datetime import datetime, date, timedelta
import json
import apiclient
from os.path import expanduser
from unidecode import unidecode
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
LAST_RUN = 'lastRun'
VIDEOS = 'videos'
OAUTH_EXPIRES_FILE = expanduser("~/.youtube-oauth2-expires")
OAUTH_STORAGE_FILE = expanduser('~/.youtube-oauth2.json')
CLIENT_SECRETS_FILE = expanduser("~/.youtube-client-secrets.json")
SCOPE = "https://www.googleapis.com/auth/youtube"
SERVICE_NAME = "youtube"
API_VERSION = "v3"
def printJson(response):
print(json.dumps(response, indent=4, sort_keys=True))
class Channel:
def __init__(self, title, channelId):
self.title = unidecode(title)
self.channelId = channelId
def __repr__(self):
return u"%s %s" % (self.title, self.channelId)
class Video:
def __init__(self, title, channelTitle, videoId, publishedAt):
self.publishedAt = publishedAt
self.videoId = videoId
self.channelTitle = unidecode(channelTitle)
self.title = unidecode(title)
def __repr__(self):
return u"%s %s %s %s" % (self.publishedAt, self.channelTitle, self.title, self.videoId)
class YouTube:
def __init__(self):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, message="Missing secret file", scope=SCOPE)
storage = Storage(OAUTH_STORAGE_FILE)
credentials = storage.get()
expire = self.__getOauthExpire()
if credentials is None or credentials.invalid or datetime.now() > expire:
flags = argparser.parse_args()
credentials = run_flow(flow, storage, flags)
expire = datetime.now() + timedelta(seconds=(credentials.token_response["expires_in"]))
with open(OAUTH_EXPIRES_FILE, 'w') as file:
file.write(expire.strftime('%Y-%m-%d %H:%M:%S.%f'))
self.client = apiclient.discovery.build(SERVICE_NAME, API_VERSION, http=credentials.authorize(httplib2.Http()))
def __getOauthExpire(self):
try:
with open(OAUTH_EXPIRES_FILE, 'r') as file:
return datetime.strptime(file.read().replace('\n', ''), '%Y-%m-%d %H:%M:%S.%f')
except:
return datetime(1, 1, 1)
def addVideosToPlaylist(self, playlistTitle, playlistDescription, publishedAfter, historyFilename, channelFilter,
videoFilter):
playlistId = self.findOrCreatePlaylist(playlistTitle, playlistDescription)
historyFilename = expanduser(historyFilename)
history = self.__readHistoryFile(historyFilename, publishedAfter)
publishedAfter = history[LAST_RUN]
history[LAST_RUN] = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
print('Adding videos publised after %s' % publishedAfter)
channels = self.__getSubscriptionChannels()
historyVideos = history[VIDEOS]
historyVideosIds = set(map(lambda video: video['videoId'], historyVideos))
allVideos = []
for channel in channels:
if channelFilter(channel):
videos = self.__getNewVideos(channel, publishedAfter, historyVideosIds, videoFilter)
print(' %d new videos' % len(videos))
for video in videos:
historyVideos.append(video.__dict__)
allVideos.append(video)
self.__addToPlaylist(playlistId, sorted(allVideos, key=lambda video: video.publishedAt))
self.__writeHistoryFile(historyFilename, history)
def findOrCreatePlaylist(self, title, description):
# Find our playlist
playlists = self.client.playlists()
request = playlists.list(part='snippet,id', mine=True)
while request is not None:
response = request.execute()
for item in response['items']:
if item['snippet']['title'] == title:
return item['id']
request = playlists.list_next(request, response)
response = self.client.playlists().insert(
part="snippet,status",
body=dict(
snippet=dict(
title=title,
description=description),
status=dict(
privacyStatus="private"))
).execute()
return response['id']
def __readHistoryFile(self, filename, defaultPublishedAfter):
try:
with open(filename) as file:
history = json.load(file)
except:
history = {
LAST_RUN: defaultPublishedAfter.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
VIDEOS: []
}
return history
def __writeHistoryFile(self, filename, history):
history[VIDEOS] = sorted(history[VIDEOS], key=lambda video: video['publishedAt'])
with open(filename, 'w') as file:
json.dump(history, file, sort_keys=True, indent=2)
def __getSubscriptionChannels(self):
results = []
subscriptions = self.client.subscriptions()
request = subscriptions.list(part='snippet', mine=True)
while request is not None:
response = request.execute()
for item in response['items']:
snippet = item['snippet']
title = snippet['title']
channelId = snippet['resourceId']['channelId']
results.append(Channel(title, channelId))
request = subscriptions.list_next(request, response)
return results
def __getNewVideos(self, channel, publishedAfter, historyVideos, filter):
videos = []
search = self.client.search()
request = search.list(part='snippet', type='video', order='date', publishedAfter=publishedAfter,
channelId=channel.channelId)
print('Channel %s' % channel.title)
while request is not None:
response = request.execute()
items = response['items']
for item in items:
videoId = item['id']['videoId']
snippet = item['snippet']
title = snippet['title']
publishedAt = snippet['publishedAt']
channelTitle = snippet['channelTitle']
video = Video(title, channelTitle, videoId, publishedAt)
if videoId not in historyVideos and filter(channel, video):
print(' Adding video %s' % title)
videos.append(video)
if len(items) == 0:
# For some reason, if no items are returned, nextPage is always there.
break
request = search.list_next(request, response)
return videos
def __getPlaylistItems(self, playlistId):
playlistItems = self.client.playlistItems()
request = playlistItems.list(
part='snippet,id',
maxResults=50,
playlistId=playlistId)
videos = []
while request is not None:
response = request.execute()
for item in response['items']:
snippet = item['snippet']
id = snippet["resourceId"]["videoId"]
title = snippet['title']
channelTitle = snippet['channelTitle']
publishedAt = datetime.strptime(snippet['publishedAt'], '%Y-%m-%dT%H:%M:%S.%fZ')
videos.append(Video(title, channelTitle, id, publishedAt))
request = playlistItems.list_next(request, response)
return videos
def __addToPlaylist(self, playlistId, videos):
playlistItems = self.client.playlistItems()
existingVideos = set(map(lambda video: video.videoId, self.__getPlaylistItems(playlistId)))
for video in videos:
if video.videoId in existingVideos:
print("%s exists" % video.title)
existingVideos.remove(video.videoId)
continue
print("Adding %s" % video.title)
playlistItems.insert(
part='snippet',
body=dict(
snippet=dict(
playlistId=playlistId,
resourceId=dict(
kind='youtube#video',
videoId=video.videoId
)))
).execute()
def addVideoToPlaylist(self, playlistId, videoId):
playlistItems = self.client.playlistItems()
playlistItems.insert(
part='snippet',
body=dict(
snippet=dict(
playlistId=playlistId,
resourceId=dict(
kind='youtube#video',
videoId=videoId
)))
).execute()