Take filters argument as a tuple of strings; Add minor changes

This commit is contained in:
vitiko98
2021-03-20 22:31:48 -04:00
parent c6af58204a
commit 4eadfdd36c
3 changed files with 48 additions and 23 deletions
+1 -1
View File
@@ -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,
+20 -6
View File
@@ -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.
+27 -16
View File
@@ -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: