Commit 619a9b59 authored by Fjen Undso's avatar Fjen Undso
Browse files

add paging support to playlists

parent 5ca0e80f
......@@ -38,7 +38,8 @@ class YouTubeBackend(pykka.ThreadingActor, backend.Backend):
class YouTubeLibraryProvider(backend.LibraryProvider):
def _get_duration(self, yttime):
# convert PT1H2M10S to 3730
"""Convert youtube time to milliseconds"""
# convert PT1H2M10S to 3730000
m = re.search('PT((?P<hours>\d+)H)?' +
'((?P<minutes>\d+)M)?' +
'((?P<seconds>\d+)S)?',
......@@ -48,7 +49,7 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
int(m.group('seconds') or 0)) * 1000
def _get_videos(self, ids):
"""Get list of youtube ids and return tracks"""
"""Process youtube ids and return tracks"""
logger.info("Get videos for '%s'", str(ids))
query = {
'part': 'id,snippet,contentDetails',
......@@ -73,21 +74,28 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
tracks.append(track)
return tracks
def _get_playlist(self, id, maxresults=30):
def _get_playlist(self, id, maxresults=100):
"""Process youtube playlist and return tracks"""
logger.info("Get Playlist for '%s'", str(id))
query = {
'part': 'id,snippet',
'fields': 'nextPageToken,items(snippet(resourceId(videoId)))',
'maxResults': maxresults,
'playlistId': id,
'key': yt_key,
'pageToken': '',
}
results = session.get(yt_api_endpoint + 'playlistItems', params=query).json()['items']
videoids = [x['snippet']['resourceId']['videoId'] for x in results]
page = ''
videoids = []
while page is not None and len(videoids) < maxresults:
query = {
'part': 'id,snippet',
'fields': 'nextPageToken,items(snippet(resourceId(videoId)))',
'maxResults': maxresults,
'playlistId': id,
'key': yt_key,
'pageToken': page,
}
results = session.get(yt_api_endpoint + 'playlistItems', params=query).json()['items']
videoids += [x['snippet']['resourceId']['videoId'] for x in results]
page = results['nextPageToken'] or None
return self._get_videos(videoids)
def _get_search(self, query, maxresults=20):
"""Process youtube search and return tracks"""
query = {
'part': 'id',
'fields': 'items(id)',
......@@ -102,6 +110,7 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
return self._get_videos(videoids)
def lookup(self, uri):
"""Process youtube playlist or id and return tracks"""
logger.info("Looking up YouTube for '%s'", uri)
for scheme in self.backend.uri_schemes:
if uri.startswith(scheme):
......@@ -118,6 +127,7 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
return self._get_videos([uri])
def search(self, query=None, uris=None, exact=False):
"""Search for query in youtube and return tracks"""
# TODO Support exact search
logger.info("Searching YouTube for query '%s'", query)
......@@ -136,6 +146,7 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
class YouTubePlaybackProvider(backend.PlaybackProvider):
def translate_uri(self, uri):
"""Translate youtube video url or id into ressource url"""
logger.info("Playing YouTube id: %s" % uri)
for scheme in self.backend.uri_schemes:
if uri.startswith(scheme):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment