From b28be8ce1c80d8989c0c552f75038b8afebb7c8a Mon Sep 17 00:00:00 2001 From: nathom Date: Mon, 21 Jun 2021 00:07:17 -0700 Subject: [PATCH] Add support for deezer dynamic links Signed-off-by: nathom --- streamrip/constants.py | 1 + streamrip/core.py | 16 +++++++++++++++- streamrip/tracklists.py | 1 + streamrip/utils.py | 13 +++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/streamrip/constants.py b/streamrip/constants.py index f3d7dd5..c97818e 100644 --- a/streamrip/constants.py +++ b/streamrip/constants.py @@ -158,6 +158,7 @@ LASTFM_URL_REGEX = r"https://www.last.fm/user/\w+/playlists/\w+" QOBUZ_INTERPRETER_URL_REGEX = ( r"https?://www\.qobuz\.com/\w\w-\w\w/interpreter/[-\w]+/[-\w]+" ) +DEEZER_DYNAMIC_LINK_REGEX = r"https://deezer\.page\.link/\w+" YOUTUBE_URL_REGEX = r"https://www\.youtube\.com/watch\?v=[-\w]+" TIDAL_MAX_Q = 7 diff --git a/streamrip/core.py b/streamrip/core.py index 5cd8b28..335f5bf 100644 --- a/streamrip/core.py +++ b/streamrip/core.py @@ -32,6 +32,7 @@ from .constants import ( SOUNDCLOUD_URL_REGEX, URL_REGEX, YOUTUBE_URL_REGEX, + DEEZER_DYNAMIC_LINK_REGEX, ) from .db import MusicDB from .exceptions import ( @@ -42,7 +43,7 @@ from .exceptions import ( ParsingError, ) from .tracklists import Album, Artist, Label, Playlist, Tracklist -from .utils import extract_interpreter_url +from .utils import extract_interpreter_url, extract_deezer_dynamic_link logger = logging.getLogger("streamrip") @@ -82,6 +83,7 @@ class MusicDL(list): self.lastfm_url_parse = re.compile(LASTFM_URL_REGEX) self.interpreter_url_parse = re.compile(QOBUZ_INTERPRETER_URL_REGEX) self.youtube_url_parse = re.compile(YOUTUBE_URL_REGEX) + self.deezer_dynamic_url_parse = re.compile(DEEZER_DYNAMIC_LINK_REGEX) self.config: Config if config is None: @@ -329,6 +331,18 @@ class MusicDL(list): ) url = self.interpreter_url_parse.sub("", url) + dynamic_urls = self.deezer_dynamic_url_parse.findall(url) + if dynamic_urls: + click.secho( + "Extracting IDs from Deezer dynamic link. Use urls " + "of the form https://www.deezer.com/{country}/{type}/{id} for " + "faster processing.", + fg="yellow", + ) + parsed.extend( + ("deezer", *extract_deezer_dynamic_link(url)) for url in dynamic_urls + ) + parsed.extend(self.url_parse.findall(url)) # Qobuz, Tidal, Dezer soundcloud_urls = self.soundcloud_url_parse.findall(url) soundcloud_items = [self.clients["soundcloud"].get(u) for u in soundcloud_urls] diff --git a/streamrip/tracklists.py b/streamrip/tracklists.py index 2316189..d86d885 100644 --- a/streamrip/tracklists.py +++ b/streamrip/tracklists.py @@ -450,6 +450,7 @@ class Playlist(Tracklist): id=track.get("id"), meta=meta, cover_url=gen_cover(track), + part_of_tracklist=True, ) ) diff --git a/streamrip/utils.py b/streamrip/utils.py index 2a2de25..19f1c5e 100644 --- a/streamrip/utils.py +++ b/streamrip/utils.py @@ -337,6 +337,19 @@ def extract_interpreter_url(url: str) -> str: ) +deezer_id_link_regex = re.compile(r"https://www\.deezer\.com/[a-z]{2}/(\w+)/(\d+)") + + +def extract_deezer_dynamic_link(url: str) -> Tuple[str, str]: + session = gen_threadsafe_session({"User-Agent": AGENT}) + r = session.get(url) + match = deezer_id_link_regex.search(r.text) + if match: + return match.group(1), match.group(2) + + raise Exception("Unable to extract Deezer dynamic link.") + + def get_container(quality: int, source: str) -> str: """Get the file container given the quality.