def recommended(): try: limit, response, page = int(request.args.get( 'limit', '0')), [], 'ajax/recommended/more' playlists, countryCode, nextPageToken = [ 'PLMC9KNkIncKvYin_USF1qoJQnIyMAfRxl', 'PLNCA1T91UH31_SnlMecke_9wsbc-5mamS', 'PLYVjGTi85afoMw4yMGHLTB99T8ZTTP0ZP' ], Repo.getRegion()['countryCode'], '' options = { 'part': 'id', 'maxResults': 50, 'order': 'relevance', 'key': 'AIzaSyDkNYRbreB8JHOggvjSznMIuw6TlvkHjGQ', 'channelId': 'UCk8vhgJslhfcLcwS5Q2KADw', 'type': 'playlist' } channel_playlists = YouTube.search(options) if channel_playlists.get('items') is not None: for item in channel_playlists.get('items'): playlistId = item['id'].get('playlistId') if playlistId is not None: playlists.append(playlistId) for playlist in playlists: playlist_tracks = YouTube.getPlaylistInfo(playlist, 8) if playlist_tracks['status'] == True: video = playlist_tracks.get('videos') if video is not None: response.append(playlist_tracks) topics = [{ 'topicId': '/m/04rlf', 'title': 'Music' }, { 'topicId': '/m/02lkt', 'title': 'EDM' }, { 'topicId': '/m/06by7', 'title': 'Rock' }] for topic in topics: options_topic = { 'part': 'snippet', 'maxResults': 8, 'key': 'AIzaSyDkNYRbreB8JHOggvjSznMIuw6TlvkHjGQ', 'regionCode': Repo.getRegion()['countryCode'], 'topicId': topic['topicId'] } playlist_topics = YouTube.search(options_topic) if playlist_topics['status'] == True: playlist_topics.update({'title': topic['title']}) response.append(playlist_topics) return render('recommended', response=response, page=page) except Exception as e: return render('error', code=500, message=str(e))
def recommend_more(): try: pageToken = request.args.get('pageToken', '') playlistId = request.args.get('playlistId', '') topicId = request.args.get('topicId', '') response, page = [], 'ajax/recommended/more' if len(playlistId.strip()) > 0: playlist_tracks = YouTube.getPlaylistInfo(playlistId, 8, pageToken) if playlist_tracks['status'] == True: if playlist_tracks.get('videos') is not None: response.append(playlist_tracks) elif len(topicId.strip()) > 0: options = { 'part': 'snippet', 'maxResults': 8, 'key': 'AIzaSyDkNYRbreB8JHOggvjSznMIuw6TlvkHjGQ', 'regionCode': Repo.getRegion()['countryCode'], 'topicId': topicId, 'pageToken': pageToken } playlist_topics = YouTube.search(options) if playlist_topics['status'] is True: if len(playlist_topics.get('videos', [])) > 0: response.append(playlist_topics) return render('recommended', response=response, page=page) except Exception as e: return render('error', code=500, message=str(e))
class YouTubeHandler: def __init__(self, app): self.app = app self.yt = YouTube() def __call__(self, params, **kwargs): command = params[0] print(kwargs["handler"].request.body) if command == "search": val = urllib.parse.unquote(params[1]) url = urllib.parse.urlparse(val) queryParams = urllib.parse.parse_qs(url.query) try: js = self.yt.search(url.path, token = queryParams.get("token",[None])[0], type = queryParams.get("type", ["video"])[0]) except Exception as e: return "not ok: " + str(e) items = [{"id": x["id"]["playlistId"] if "playlistId" in x["id"] else x["id"]["videoId"], "title": x["snippet"]["title"], "thumbnail": x["snippet"]["thumbnails"]["medium"]["url"], "published": x["snippet"]["publishedAt"]} for x in js["items"] if x["id"]["kind"] == "youtube#video" or x["id"]["kind"] == "youtube#playlist"] res = {"items": items} if "nextPageToken" in js: res["nextToken"] = js["nextPageToken"] if "prevPageToken" in js: res["prevToken"] = js["prevPageToken"] result = json.dumps(res, indent=1) return result elif command == "play": try: self.app.xbmc.openYoutubeVideo(params[1]) return "ok" except IndexError: return "not ok" elif command == "playlist": try: self.app.xbmc.openYoutubeList(params[1]) self.app.xbmc.StartPlaylist(1) return "ok" except IndexError: return "not ok" return "Not found"
def playlists(): try: pageToken = request.args.get('pageToken', '') options = { 'part': 'snippet', 'maxResults': 50, 'order': 'relevance', 'key': 'AIzaSyDkNYRbreB8JHOggvjSznMIuw6TlvkHjGQ', 'regionCode': Repo.getRegion()['countryCode'], 'channelId': 'UCk8vhgJslhfcLcwS5Q2KADw', 'pageToken': pageToken, 'type': 'playlist' } playlists = YouTube.search(options) return render( 'playlists', playlists=playlists) if playlists['status'] is True else render( 'error', code=404, message=playlists['message']) except Exception as e: return render('error', code=500, message=str(e))
def channel(channelId): try: order = request.args.get('order', 'date') options = { 'part': 'snippet', 'order': order, 'key': 'AIzaSyDkNYRbreB8JHOggvjSznMIuw6TlvkHjGQ', 'regionCode': Repo.getRegion()['countryCode'], 'channelId': channelId, 'type': 'video', 'maxResults': 50 } channel = YouTube.getChannel(channelId) if channel['status'] == True: search = YouTube.search(options) return render( 'channel', search=search, channel=channel, order=order) if search['status'] == True else render( 'error', code=404, message=search['message']) else: return render('error', code=400, message=channel['message']) except Exception as e: return render('error', code=500, message=str(e))
def search(): try: q = request.args.get('q', '') type = request.args.get('type', 'video,playlist,channel') pageToken = request.args.get('pageToken', '') duration = request.args.get('duration', '') maxResults = request.args.get('maxResults', '25') order = request.args.get('order', 'relevance') channelId = request.args.get('channelId', '') relatedToVideoId = request.args.get('relatedToVideoId', '') page = request.args.get('page') if len(q.strip()) > 0: if YouTube.validURL(q): v = YouTube.parseURL(q) if v is not False: if YouTube.validYouTubeURL(v): video = YouTube.getVideoInfo(v) return render( 'stream', type='video', core=video, json_core=json.dumps(video), relatedToVideoId=v ) if video['status'] == True else render( 'error', code=400, message=video['message']) else: return render( 'error', code=404, message="Couldn't find the YouTube video.") else: return render( 'error', code=400, message= "Couldn't find any videos related to the search.") options = { 'part': 'snippet', 'maxResults': maxResults, 'order': order, 'key': 'AIzaSyDkNYRbreB8JHOggvjSznMIuw6TlvkHjGQ', 'regionCode': Repo.getRegion()['countryCode'] } if len(q) > 0: options['q'] = str(urllib.quote_plus(q)) if len(duration.strip()) > 0: options['type'] = type = 'video' options['duration'] = duration.strip() if len(pageToken) > 0: options['pageToken'] = pageToken if len(type) > 0 and len(duration) == 0: options['type'] = type if len(channelId) > 0: options['channelId'] = channelId if type == 'video,playlist,channel': options['type'] = type = 'video' if len(relatedToVideoId) > 0: options['relatedToVideoId'] = relatedToVideoId options['type'] = type = 'video' if options.get('channelId') is not None or options.get( 'q') is not None or options.get( 'relatedToVideoId') is not None: search = YouTube.search(options) name = page if page is not None else 'list' return render(name, search=search, q=q, type=type, pageToken=pageToken, duration=duration, order=order, channelId=channelId, maxResults=maxResults, relatedToVideoId=relatedToVideoId ) if search['status'] == True else render( 'error', code=400, message=search['message']) else: return render('error', code=900, message="Please provide some input to search.") except Exception as e: return render('error', code=500, message=str(e))