From 4eadfdd36cc157a2533e13f33683ddaefa7a05ef Mon Sep 17 00:00:00 2001 From: vitiko98 Date: Sat, 20 Mar 2021 22:31:48 -0400 Subject: [PATCH] Take filters argument as a tuple of strings; Add minor changes --- qobuz_dl_rewrite/config.py | 2 +- qobuz_dl_rewrite/core.py | 26 +++++++++++++++----- qobuz_dl_rewrite/downloader.py | 43 +++++++++++++++++++++------------- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/qobuz_dl_rewrite/config.py b/qobuz_dl_rewrite/config.py index ef3d949..c1b756c 100644 --- a/qobuz_dl_rewrite/config.py +++ b/qobuz_dl_rewrite/config.py @@ -49,7 +49,7 @@ class Config: self.tidal = {"enabled": True, "email": None, "password": None} self.deezer = {"enabled": True} self.downloads_database = None - self.filters = {"smart_discography": False, "albums_only": False} + self.filters = {"no_extras": False, "albums_only": False} self.downloads = {"folder": folder, "quality": quality} self.metadata = { "embed_cover": False, diff --git a/qobuz_dl_rewrite/core.py b/qobuz_dl_rewrite/core.py index 40a502d..7389f57 100644 --- a/qobuz_dl_rewrite/core.py +++ b/qobuz_dl_rewrite/core.py @@ -72,13 +72,27 @@ class QobuzDL: """ assert self.source in url, f"{url} is not a {self.source} source" url_type, item_id = self.parse_url(url) - item = MEDIA_CLASS[url_type](client=self.client, id=item_id) + self.handle_item(url_type, item_id) + + def handle_item(self, media_type, item_id): + arguments = { + "parent_folder": self.config.downloads["folder"], + "quality": self.config.downloads["quality"], + "embed_cover": self.config.metadata["embed_cover"], + } + + item = MEDIA_CLASS[media_type](client=self.client, id=item_id) + if isinstance(item, Artist): + filters_ = tuple(self.config.filters.keys()) + arguments["filters"] = filters_ + logger.debug( + "Added filter argument for artist/label: %s", filters_ + ) + + logger.debug("Arguments from config: %s", arguments) + item.load_meta() - item.download( - parent_folder=self.config.downloads["folder"], - quality=self.config.downloads["quality"], - embed_cover=self.config.metadata["embed_cover"], - ) + item.download(**arguments) def parse_url(self, url: str) -> Tuple[str, str]: """Returns the type of the url and the id. diff --git a/qobuz_dl_rewrite/downloader.py b/qobuz_dl_rewrite/downloader.py index bc45f2b..3a88bf4 100644 --- a/qobuz_dl_rewrite/downloader.py +++ b/qobuz_dl_rewrite/downloader.py @@ -5,7 +5,7 @@ import shutil from abc import ABC, abstractmethod from pprint import pprint, pformat from tempfile import gettempdir -from typing import Any, Callable, Optional, Union +from typing import Any, Callable, Optional, Union, Sequence, Tuple import click import requests @@ -622,6 +622,7 @@ class Album(Tracklist): "albumartist": resp.get("artist", {}).get("name"), "year": str(resp.get("release_date_original"))[:4], "version": resp.get("version"), + "release_type": resp.get("release_type", "n/a"), "cover_urls": resp.get("image"), "streamable": resp.get("streamable"), "quality": quality_id( @@ -1021,7 +1022,7 @@ class Artist(Tracklist): def download( self, parent_folder: str = "Downloads", - filters: Optional[Union[Callable, list]] = None, + filters: Optional[Tuple] = None, no_repeats: bool = False, quality: int = 6, embed_cover: bool = False, @@ -1029,14 +1030,14 @@ class Artist(Tracklist): """Download all albums in the discography. :param filters: Filters to apply to discography, see options below. - :type filters: Optional[Union[Callable, list]] + These only work for Qobuz. + :type filters: Optional[Tuple] :param no_repeats: Remove repeats :type no_repeats: bool :param quality: in (4, 5, 6, 7, 27) :type quality: int """ - # Sanitize first as os.path.join might cause conflicts - folder = sanitize_filepath(self.name) + folder = sanitize_filename(self.name) folder = os.path.join(parent_folder, folder) logger.debug("Artist folder: %s", folder) @@ -1047,19 +1048,14 @@ class Artist(Tracklist): else: final = self - if filters is not None and self.client.source == "qobuz": - if isinstance(filters, (tuple, list)): - for f in filters: - - def inter(album): - """Intermediate function to pass self into f""" - return f(self, album) - - final = filter(inter, final) - else: + if isinstance(filters, tuple) and self.client.source == "qobuz": + filters = [getattr(self, filter_) for filter_ in filters] + logger.debug("Filters: %s", filters) + for filter_ in filters: def inter(album): - return filters(self, album) + """Intermediate function to pass self into f""" + return filter_(self, album) final = filter(inter, final) @@ -1202,6 +1198,21 @@ class Artist(Tracklist): """ return TYPE_REGEXES["remaster"].search(album.title) is not None + @staticmethod + def albums_only(artist, album): + """Passed as a parameter by the user. + + >>> artist.download(filters=(albums_only)) + + This will download only remasterd albums. + + :param artist: usually self + :param album: the album to check + :type album: Album + :rtype: bool + """ + return album["release_type"] == "album" + # --------- Magic Methods -------- def __repr__(self) -> str: