4
4
from pytube import YouTube
5
5
import logging
6
6
from random import shuffle , randint
7
+ from botocore .vendored import requests
8
+ import json
7
9
logging .getLogger ('googleapiclient.discovery_cache' ).setLevel (logging .ERROR )
8
10
DEVELOPER_KEY = environ ['DEVELOPER_KEY' ]
9
11
YOUTUBE_API_SERVICE_NAME = 'youtube'
@@ -186,8 +188,16 @@ def on_intent(event):
186
188
# Dispatch to your skill's intent handlers
187
189
if intent_name == "SearchIntent" :
188
190
return search (intent , session )
191
+ elif intent_name == "PlaylistIntent" :
192
+ return search (intent , session )
193
+ elif intent_name == "ChannelIntent" :
194
+ return search (intent , session )
189
195
elif intent_name == "ShuffleIntent" :
190
196
return search (intent , session , shuffle_mode = True )
197
+ elif intent_name == "ShufflePlaylistIntent" :
198
+ return search (intent , session , shuffle_mode = True )
199
+ elif intent_name == "ShuffleChannelIntent" :
200
+ return search (intent , session , shuffle_mode = True )
191
201
elif intent_name == "AMAZON.HelpIntent" :
192
202
return get_help ()
193
203
elif intent_name == "AMAZON.CancelIntent" :
@@ -250,7 +260,7 @@ def illegal_action():
250
260
def do_nothing ():
251
261
return build_response ({})
252
262
253
- def youtube_search (query ):
263
+ def video_search (query ):
254
264
youtube = build (YOUTUBE_API_SERVICE_NAME , YOUTUBE_API_VERSION , developerKey = DEVELOPER_KEY )
255
265
search_response = youtube .search ().list (
256
266
q = query ,
@@ -263,6 +273,48 @@ def youtube_search(query):
263
273
videos .append (search_result ['id' ]['videoId' ])
264
274
return videos
265
275
276
+ def playlist_search (query ):
277
+ youtube = build (YOUTUBE_API_SERVICE_NAME , YOUTUBE_API_VERSION , developerKey = DEVELOPER_KEY )
278
+ search_response = youtube .search ().list (
279
+ q = query ,
280
+ part = 'id,snippet' ,
281
+ maxResults = 25 ,
282
+ type = 'playlist'
283
+ ).execute ()
284
+ playlist_id = search_response .get ('items' )[0 ]['id' ]['playlistId' ]
285
+ videos = []
286
+ data = {'nextPageToken' :'' }
287
+ while 'nextPageToken' in data and len (videos ) < 25 :
288
+ next_page_token = data ['nextPageToken' ]
289
+ data = json .loads (requests .get ('https://www.googleapis.com/youtube/v3/playlistItems?pageToken={}&part=snippet&playlistId={}&key={}' .format (next_page_token ,playlist_id ,DEVELOPER_KEY )).text )
290
+ for item in data ['items' ]:
291
+ try :
292
+ videos .append (item ['snippet' ]['resourceId' ]['videoId' ])
293
+ except :
294
+ pass
295
+ return videos
296
+
297
+ def channel_search (query ):
298
+ youtube = build (YOUTUBE_API_SERVICE_NAME , YOUTUBE_API_VERSION , developerKey = DEVELOPER_KEY )
299
+ search_response = youtube .search ().list (
300
+ q = query ,
301
+ part = 'id,snippet' ,
302
+ maxResults = 25 ,
303
+ type = 'channel'
304
+ ).execute ()
305
+ playlist_id = search_response .get ('items' )[0 ]['id' ]['channelId' ]
306
+ data = {'nextPageToken' :'' }
307
+ videos = []
308
+ while 'nextPageToken' in data and len (videos ) < 25 :
309
+ next_page_token = data ['nextPageToken' ]
310
+ data = json .loads (requests .get ('https://www.googleapis.com/youtube/v3/search?pageToken={}&part=snippet&channelId={}&key={}' .format (next_page_token ,playlist_id ,DEVELOPER_KEY )).text )
311
+ for item in data ['items' ]:
312
+ try :
313
+ videos .append (item ['id' ]['videoId' ])
314
+ except :
315
+ pass
316
+ return videos
317
+
266
318
def get_url_and_title (id ):
267
319
print ('Getting url for https://www.youtube.com/watch?v=' + id )
268
320
try :
@@ -278,7 +330,13 @@ def search(intent, session, shuffle_mode=False):
278
330
query = intent ['slots' ]['query' ]['value' ]
279
331
should_end_session = True
280
332
print ('Looking for: ' + query )
281
- videos = youtube_search (query )
333
+ intent_name = intent ['name' ]
334
+ if intent_name == "PlaylistIntent" or intent_name == "ShufflePlaylistIntent" :
335
+ videos = playlist_search (query )
336
+ elif intent_name == "ChannelIntent" or intent_name == "ShuffleChannelIntent" :
337
+ videos = channel_search (query )
338
+ else :
339
+ videos = video_search (query )
282
340
if shuffle_mode :
283
341
shuffle (videos )
284
342
next_url = None
@@ -375,13 +433,19 @@ def get_next_url_and_token(current_token, skip):
375
433
next_url = None
376
434
title = None
377
435
shuffle_mode = int (playlist ['s' ])
436
+ loop_mode = int (playlist ['l' ])
378
437
next_playing = int (playlist ['p' ])
379
438
number_of_videos = sum ('v' in i for i in playlist .keys ())
380
439
while next_url is None :
381
440
next_playing = next_playing + skip
382
441
if shuffle_mode and skip != 0 :
383
442
next_playing = randint (1 ,number_of_videos )
384
443
if next_playing < 0 :
444
+ if loop_mode :
445
+ next_playing = number_of_videos - 1
446
+ else :
447
+ next_playing = 0
448
+ if next_playing >= number_of_videos and loop_mode :
385
449
next_playing = 0
386
450
next_key = 'v' + str (next_playing )
387
451
if next_key not in playlist :
0 commit comments