Commit 8fd862d4 authored by Gerion Entrup's avatar Gerion Entrup
Browse files

backend: fix ytdl library lookup

parent a7dd4601
......@@ -21,6 +21,26 @@ session = requests.Session()
search_uri = 'youtube:search'
ytdl_id = 'ytdl_id'
cache = {}
cache_ids = []
cache_max = 50
def get_cache(y_id):
return cache[y_id]
def set_cache(tracks):
for track in tracks:
y_id = track.album.name
cache[y_id] = track
cache_ids.append(y_id)
if len(cach_ids) > cache_max:
del cache[cache_ids[0]]
cache_ids = cache_ids[1:]
def call_ytdl(uri):
yt_opts = {
......@@ -133,12 +153,14 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
name=ytdl['display_id'],
images=thumbnail
),
uri=self.backend.uri_schemes[0] + ':' + ytdl['url']
uri=self.backend.uri_schemes[0] + ':' + ytdl['display_id'] + ' ' + ytdl['url']
)
return track
def _get_ytdl_tracks(self, uri):
def _get_ytdl_tracks(self, uri, y_id=None):
"""Return a list of tracks given an ari based on ytdl."""
if y_id is not None:
return get_cache(y_id)
try:
yt_uri = call_ytdl(uri)
except:
......@@ -147,9 +169,10 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
tracks = []
for entry in yt_uri['entries']:
tracks.append(self._map_ytdl_track(entry))
return tracks
else:
return [self._map_ytdl_track(yt_uri)]
tracks = [self._map_ytdl_track(yt_uri)]
set_cache(tracks)
return tracks
def lookup(self, uri):
"""Process any uri supported by ytdl and return tracks"""
......@@ -168,6 +191,10 @@ class YouTubeLibraryProvider(backend.LibraryProvider):
elif 'http' not in uri:
logger.info("Detected Youtube-ID")
return self._get_tracks([uri])
elif uri.startswith(ytdl_id):
logger.info("Detected YTDL-ID, using cache")
y_id, uri = uri.split(' ')
return self._get_ytdl_tracks(uri, y_id=y_id)
else:
logger.info("Invoke youtube-dl")
return self._get_ytdl_tracks(uri)
......@@ -203,6 +230,8 @@ class YouTubePlaybackProvider(backend.PlaybackProvider):
if 'http' not in uri:
uri = self._resolve_url(uri)
if ' ' in uri:
uri = ' '.join(uri.split(' ')[1:])
return uri
def _resolve_url(self, url):
......
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