Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallbacks for Deezer + user uploaded tracks #787

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 111 additions & 86 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ simple-term-menu = { version = "^1.2.1", platform = 'darwin|linux' }
pick = { version = "^2", platform = 'win32|cygwin' }
windows-curses = { version = "^2.2.0", platform = 'win32|cygwin' }
Pillow = ">=9,<11"
deezer-py = "1.3.6"
deezer-py = {git = "https://github.com/OhMyMndy/deezer-py.git", rev = "fixes"}
pycryptodomex = "^3.10.1"
appdirs = "^1.4.4"
m3u8 = "^0.9.0"
Expand Down
26 changes: 25 additions & 1 deletion streamrip/client/deezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import binascii
import hashlib
import logging
import os

import deezer
from Cryptodome.Cipher import AES
Expand Down Expand Up @@ -36,7 +37,7 @@ class DeezerClient(Client):

def __init__(self, config: Config):
self.global_config = config
self.client = deezer.Deezer()
self.client = deezer.Deezer(os.getenv("DEEZER_ACCESS_TOKEN", None))
self.logged_in = False
self.config = config.session.deezer

Expand Down Expand Up @@ -70,6 +71,13 @@ async def get_track(self, item_id: str) -> dict:
except Exception as e:
raise NonStreamableError(e)

if not item["readable"]:
raise NonStreamableError(f"Track {item_id} not readable")

# User uploaded files might not have an album attached, because it does not exist in Deezer
if "album" not in item:
return item

album_id = item["album"]["id"]
try:
album_metadata, album_tracks = await asyncio.gather(
Expand Down Expand Up @@ -101,6 +109,22 @@ async def get_playlist(self, item_id: str) -> dict:
asyncio.to_thread(self.client.api.get_playlist_tracks, item_id),
)
pl_metadata["tracks"] = pl_tracks["data"]
for track in pl_tracks["data"]:
if not track["readable"]:
# We need to use gw instead of api
# gw has the fallback of a track, the api strangely enough, not
try:
item_fallbacks = await asyncio.to_thread(
self.client.gw.get_track_with_fallback, track["id"]
)
if "FALLBACK" in item_fallbacks:
track["id"] = item_fallbacks["FALLBACK"]["SNG_ID"]
track["readable"] = True
except Exception as e:
logger.error(
f"Error while getting fallbacks for track {track['id']}: {e}"
)

pl_metadata["track_total"] = len(pl_tracks["data"])
return pl_metadata

Expand Down
5 changes: 5 additions & 0 deletions streamrip/client/downloadable.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def __init__(self, session: aiohttp.ClientSession, info: dict):
qualities_available = [
i for i, size in enumerate(info["quality_to_size"]) if size > 0
]
# user uploaded tracks have negative ID's
if int(info.get("id", 0)) < 0:
# Always mp3
qualities_available = [1]

if len(qualities_available) == 0:
raise NonStreamableError(
"Missing download info. Skipping.",
Expand Down
14 changes: 10 additions & 4 deletions streamrip/metadata/album.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def format_folder_path(self, formatter: str) -> str:
"year": self.year,
"container": self.info.container,
}

return clean_filepath(formatter.format(**info))

@classmethod
Expand Down Expand Up @@ -457,9 +457,15 @@ def from_incomplete_deezer_track_resp(cls, resp: dict) -> AlbumMetadata | None:
album_id = album_resp["id"]
album = album_resp["title"]
covers = Covers.from_deezer(album_resp)
date = album_resp["release_date"]
year = date[:4]
albumartist = ", ".join(a["name"] for a in resp["contributors"])
date = album_resp.get("release_date")
if date is not None:
year = date[:4]
else:
year = None
if "contributors" in resp:
albumartist = ", ".join(a["name"] for a in resp["contributors"])
else:
albumartist = resp["artist"].get("name")
explicit = resp.get("explicit_lyrics", False)

info = AlbumInfo(
Expand Down
12 changes: 8 additions & 4 deletions streamrip/metadata/covers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ def from_qobuz(cls, resp):
@classmethod
def from_deezer(cls, resp):
c = cls()
c.set_cover_url("original", resp["cover_xl"])
c.set_cover_url("large", resp["cover_big"])
c.set_cover_url("small", resp["cover_medium"])
c.set_cover_url("thumbnail", resp["cover_small"])
if "cover_xl" in resp:
c.set_cover_url("original", resp["cover_xl"])
if "cover_big" in resp:
c.set_cover_url("large", resp["cover_big"])
if "cover_medium" in resp:
c.set_cover_url("small", resp["cover_medium"])
if "cover_small" in resp:
c.set_cover_url("thumbnail", resp["cover_small"])
return c

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions streamrip/metadata/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def from_deezer(cls, album: AlbumMetadata, resp) -> TrackMetadata | None:
work = None
title = typed(resp["title"], str)
artist = typed(resp["artist"]["name"], str)
tracknumber = typed(resp["track_position"], int)
discnumber = typed(resp["disk_number"], int)
tracknumber = typed(resp.get("track_position", 0), int)
discnumber = typed(resp.get("disk_number", 0), int)
composer = None
info = TrackInfo(
id=track_id,
Expand Down Expand Up @@ -212,7 +212,7 @@ def from_tidal(cls, album: AlbumMetadata, track) -> TrackMetadata:
discnumber=discnumber,
composer=None,
isrc=isrc,
lyrics=lyrics
lyrics=lyrics,
)

@classmethod
Expand Down