Fix last.fm crash for tidal and deezer (#583)

This commit is contained in:
Nathan Thomas 2024-01-22 18:51:02 -08:00 committed by GitHub
parent 04f6881131
commit 56f9aac92a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 9 deletions

View File

@ -129,7 +129,9 @@ class DeezerClient(Client):
raise Exception(f"Invalid media type {media_type}")
response = search_function(query, limit=limit) # type: ignore
return [response]
if response["total"] > 0:
return [response]
return []
async def get_downloadable(
self,

View File

@ -121,7 +121,9 @@ class TidalClient(Client):
}
assert media_type in ("album", "track", "playlist", "video", "artist")
resp = await self._api_request(f"search/{media_type}s", params=params)
return [resp]
if len(resp["items"]) > 1:
return [resp]
return []
async def get_downloadable(self, track_id: str, quality: int):
params = {

View File

@ -254,12 +254,19 @@ class PendingLastfmPlaylist(Pending):
async def _make_query(
self,
query: str,
s: Status,
search_status: Status,
callback,
) -> tuple[str | None, bool]:
"""Try searching for `query` with main source. If that fails, try with next source.
"""Search for a track with the main source, and use fallback source
if that fails.
If both fail, return None.
Args:
query (str): Query to search
s (Status):
callback: function to call after each query completes
Returns: A 2-tuple, where the first element contains the ID if it was found,
and the second element is True if the fallback source was used.
"""
with ExitStack() as stack:
# ensure `callback` is always called
@ -267,7 +274,7 @@ class PendingLastfmPlaylist(Pending):
pages = await self.client.search("track", query, limit=1)
if len(pages) > 0:
logger.debug(f"Found result for {query} on {self.client.source}")
s.found += 1
search_status.found += 1
return (
SearchResults.from_pages(self.client.source, "track", pages)
.results[0]
@ -276,13 +283,13 @@ class PendingLastfmPlaylist(Pending):
if self.fallback_client is None:
logger.debug(f"No result found for {query} on {self.client.source}")
s.failed += 1
search_status.failed += 1
return None, False
pages = await self.fallback_client.search("track", query, limit=1)
if len(pages) > 0:
logger.debug(f"Found result for {query} on {self.client.source}")
s.found += 1
search_status.found += 1
return (
SearchResults.from_pages(
self.fallback_client.source,
@ -294,7 +301,7 @@ class PendingLastfmPlaylist(Pending):
), True
logger.debug(f"No result found for {query} on {self.client.source}")
s.failed += 1
search_status.failed += 1
return None, True
async def _parse_lastfm_playlist(