/
youtube.py
322 lines (252 loc) 路 11.5 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""
YouTube functions (playlist, mix)
"""
from flask import (
Blueprint, g, redirect, render_template, request, url_for
)
from newsic.functions import (
cache, get_locale, gettext, snippet_start_at, debug, read_config
)
import re
from urllib import request as urllib_request
from urllib.parse import quote
from json import loads, dumps
from random import randrange
bp = Blueprint('youtube', __name__)
# TODO: rename yt_grab
# TODO: remove "dangerous default value [] for video_ids as argument"
# TODO: split in two functions:
# first fetches video ids of a playlist -> store in list
# second takes the list of first one and requests more information
def yt_grab(video_ids=[], playlist_id=None):
"""
Fetch video info from YouTube based on a list of video IDs or a single playlist ID
"""
# * big function
# * two cases
# * 1: we don't have video ids: use a playlist id, grab ids and put em in a list
# * 2: we already have video ids (in a list), so use them
# * fetch first 50 video ids in a batch
# * fetch the rest
# * return video_list with id, type, title, start, end, length, blocked, allowed
max_results = 50 # YouTube API has a limit per request (50 videos in one batch is maximum)
if not video_ids:
video_ids = []
request_iteration = 1
next_page_token = ""
# fetch video ids from playlist
while True:
request_video_ids = (
"{}/playlistItems?part=contentDetails&playlistId={}"
+ "&fields=items/contentDetails/videoId,nextPageToken"
+ "&key={}&maxResults={}&pageToken={}").format(
read_config("YOUTUBE_API_URL"), playlist_id, read_config("YOUTUBE_API_KEY"),
max_results, next_page_token)
receive_video_ids = urllib_request.urlopen(request_video_ids)
json_video_ids = loads(receive_video_ids.read().decode())
for items in json_video_ids["items"]:
video_ids.append(items["contentDetails"]["videoId"])
if "nextPageToken" not in json_video_ids:
break
next_page_token = json_video_ids["nextPageToken"]
request_iteration = request_iteration + 1
else:
request_iteration = len(video_ids) % max_results
if len(video_ids) >= max_results:
request_iteration = request_iteration + 1
range_start = 0
range_end = max_results
video_list = []
while request_iteration > 0:
video_ids_part = ','.join(video_ids[range_start:range_end])
url = (
"{}/videos?part=contentDetails,snippet,status"
+ "&fields=items(contentDetails(countryRestriction,duration,regionRestriction),id,snippet/title,status(embeddable,privacyStatus,uploadStatus))"
+ "&id={}&key={}").format(
read_config("YOUTUBE_API_URL"), video_ids_part, read_config("YOUTUBE_API_KEY"))
response = urllib_request.urlopen(url)
data = loads(response.read().decode())
# fetch more details for every video
for video in data["items"]:
if data["items"]:
embeddable = video["status"]["embeddable"]
privacy_status = video["status"]["privacyStatus"]
privacy_public = privacy_status == "public"
privacy_unlisted = privacy_status == "unlisted"
upload_status = video["status"]["uploadStatus"]
upload_processed = upload_status == "processed"
debug((
"\n{} (ID: {}) \nprivacy: {} | "
+ "upload status: {} | embed status: {} ").format(
video["snippet"]["title"], video["id"],
privacy_status, upload_status, embeddable))
if (embeddable and upload_processed and
(privacy_public or privacy_unlisted)):
blocked = []
allowed = []
# find region restrictions (allowed or blocked)
if "regionRestriction" in video["contentDetails"]:
region_restriction = video["contentDetails"]["regionRestriction"]
if "blocked" in region_restriction:
for country in region_restriction["blocked"]:
blocked.append(country)
debug(("Blocked in these countries: {}").format(str(blocked)))
if "allowed" in region_restriction:
for country in region_restriction["allowed"]:
allowed.append(country)
debug(("Only allowed in these countries: {}").format(str(allowed)))
# convert YouTube's time format to hours, minutes and seconds
days = hours = minutes = seconds = 0
length_string = re.compile(
r"P(?P<d>\d*D)*T(?P<h>\d*H)*(?P<m>\d*M)*(?P<s>\d*S)*")
length = length_string.match(video["contentDetails"]["duration"])
if length.group("d"):
days = length.group("d").replace("D", "")
if length.group("h"):
hours = length.group("h").replace("H", "")
if length.group("m"):
minutes = length.group("m").replace("M", "")
if length.group("s"):
seconds = length.group("s").replace("S", "")
length_sec = (int(days) * 86400 + int(hours) * 60 * 60
+ int(minutes) * 60 + int(seconds))
#debug(("Length: {} seconds").format(length_sec))
start_time = snippet_start_at(length_sec)
end_time = start_time + int(read_config("SNIPPETLENGTH"))
if end_time > length_sec:
end_time = length_sec
# video_list = 0: id, 1: provider, 2: title, 3: start, 4: end, 5: length, 6: blocked, 7: allowed
video_list.append([
video["id"],
"youtube",
video["snippet"]["title"],
start_time,
end_time,
length_sec,
blocked,
allowed])
else:
debug("Embedding is not allowed, therefore this video was skipped.")
request_iteration = request_iteration - 1
range_start = range_start + max_results
range_end = range_end + max_results
return video_list
# TODO: rename yt_general_playlist_info
def yt_general_playlist_info(playlist_id):
"""
Fetch general information about a playlist (defined by given playlist_id)
"""
request_data = (
"{}/playlists?part=snippet&id={}"
+ "&fields=items(snippet(channelTitle,title))&key={}").format(
read_config("YOUTUBE_API_URL"), playlist_id, read_config("YOUTUBE_API_KEY"))
data_response = loads(urllib_request.urlopen(request_data).read().decode())
if data_response["items"]:
return data_response["items"][0]["snippet"]
# playlist is either private or empty
return False
@bp.route("/youtube/mix/<video_id>/")
@cache()
def mix_youtube(video_id):
"""
Fetch a playlist from YouTube based on a single video
TODO: improve performance
"""
api_get_video_title = (
"{}/videos?id={}&part=snippet"
+ "&fields=items/snippet/title&key={}").format(
read_config("YOUTUBE_API_URL"), video_id, read_config("YOUTUBE_API_KEY"))
response_title = urllib_request.urlopen(api_get_video_title)
data_title = loads(response_title.read().decode())
if data_title["items"]:
video_title = data_title["items"][0]["snippet"]["title"]
video_title_nospaces = video_title.replace(" ", "+")
max_results = 15
api_search_with_title = (
"{}/search?q={}&type=playlist&part=id"
+ "&fields=items/id/playlistId,pageInfo/totalResults"
+ "&maxResults={}&key={}").format(
read_config("YOUTUBE_API_URL"), quote(video_title_nospaces),
max_results, read_config("YOUTUBE_API_KEY"))
response_search = urllib_request.urlopen(api_search_with_title)
data_search = loads(response_search.read().decode())
if data_search["pageInfo"]["totalResults"] == 0:
return render_template(
"index.html",
getlocale=get_locale(),
error=gettext(u"No mix available for this video"),
bodyClass="home",
title=gettext(u"No mix available"))
randnum = randrange(max_results)
if data_search["pageInfo"]["totalResults"] < max_results:
randnum = randrange(data_search["pageInfo"]["totalResults"])
return redirect(
url_for(
"youtube.play_youtube",
lang_code=get_locale(),
youtube_playlist=data_search["items"][randnum]["id"]["playlistId"]))
return render_template(
"index.html",
getlocale=get_locale(),
error=gettext(u"No mix available for this video"),
bodyClass="home",
title=gettext(u"No mix available"))
@bp.route("/youtube/<youtube_playlist>/")
@cache()
def play_youtube(youtube_playlist):
"""
Fetch videos from YouTube
"""
# fetch general information about the playlist
# TODO: rename yt_general_playlist_info
general_info = yt_general_playlist_info(youtube_playlist)
if not general_info:
return render_template(
"index.html",
getlocale=get_locale(),
error=gettext(u"This playlist is either empty, private or non-existing"),
bodyClass="home",
title=gettext(u"Invalid playlist"))
# TODO: rename yt_grab
video_list = yt_grab(playlist_id=youtube_playlist)
return render_template(
"play.html",
getlocale=get_locale(),
videolist=video_list,
playlistTitle=general_info["title"],
playlistCreator=general_info["channelTitle"],
playlistVideoAmount=len(video_list),
playlistLength=int(float((len(video_list) * int(read_config("SNIPPETLENGTH"))) / 60)),
title=video_list[0][2] + " - " + general_info["title"])
@bp.route("/watch/")
def import_youtube_url():
"""
Receive https://trynewsic.tocsin.de/watch?v=<video_id>&list=<playlist_id>&index=<index>
links (same scheme as youtube URLs),
extract values and redirect to playlist view or mix
"""
video = request.args.get("v")
playlist = request.args.get("list")
index = request.args.get("index")
def append_index():
if int(index) - 1 > 0:
return ("#{}").format(int(index) - 1)
return ""
if(playlist or video and playlist):
debug("Found playlist id, redirect to playlist view")
return redirect(("{}{}").format(url_for(
"youtube.play_youtube",
lang_code=get_locale(),
youtube_playlist=playlist), append_index()))
if(video and not playlist):
debug("Found video id, redirect to mix")
return redirect(url_for(
"youtube.mix_youtube",
video_id=video))
debug("No music found")
return render_template(
"index.html",
getlocale=get_locale(),
error=gettext(u"No music found"),
bodyClass="home",
title=gettext(u"No music found"))