From b00f5278c479d2fb499cb1d205aa030eafac5e52 Mon Sep 17 00:00:00 2001 From: nathom Date: Fri, 9 Apr 2021 17:39:24 -0700 Subject: [PATCH] Formatting --- streamrip/core.py | 2 +- streamrip/downloader.py | 31 +++++++++++++++++-------------- streamrip/metadata.py | 22 ++++++++++------------ streamrip/utils.py | 8 ++++---- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/streamrip/core.py b/streamrip/core.py index 25690699..3888bc37 100644 --- a/streamrip/core.py +++ b/streamrip/core.py @@ -276,7 +276,7 @@ def handle_lastfm_urls(self, urls): pl.append(track) pl.loaded = True - click.secho(f"{tracks_not_found} tracks not found.", fg='yellow') + click.secho(f"{tracks_not_found} tracks not found.", fg="yellow") self.append(pl) def handle_txt(self, filepath: Union[str, os.PathLike]): diff --git a/streamrip/downloader.py b/streamrip/downloader.py index f7c29642..e3d6ad6e 100644 --- a/streamrip/downloader.py +++ b/streamrip/downloader.py @@ -3,8 +3,7 @@ import re import shutil import subprocess -import sys -from pprint import pformat, pprint +from pprint import pformat from tempfile import gettempdir from typing import Any, Callable, Optional, Tuple, Union @@ -33,11 +32,11 @@ from .utils import ( clean_format, decrypt_mqa_file, + ext, get_quality_id, safe_get, tidal_cover_url, tqdm_download, - ext, ) logger = logging.getLogger(__name__) @@ -314,9 +313,8 @@ def format_final_path(self) -> str: formatter = self.meta.get_formatter() logger.debug("Track meta formatter %s", pformat(formatter)) filename = clean_format(self.file_format, formatter) - self.final_path = ( - os.path.join(self.folder, filename)[:250].strip() - + ext(self.quality, self.client.source) + self.final_path = os.path.join(self.folder, filename)[:250].strip() + ext( + self.quality, self.client.source ) logger.debug("Formatted path: %s", self.final_path) @@ -404,11 +402,11 @@ def tag( logger.debug("Tagging file with %s container", self.container) audio = FLAC(self.final_path) elif self.quality <= 1: - if self.client.source == 'tidal': - self.container = 'AAC' + if self.client.source == "tidal": + self.container = "AAC" audio = MP4(self.final_path) else: - self.container = 'MP3' + self.container = "MP3" try: audio = ID3(self.final_path) except ID3NoHeaderError: @@ -425,7 +423,9 @@ def tag( if embed_cover and cover is None: assert hasattr(self, "cover_path") - cover = Tracklist.get_cover_obj(self.cover_path, self.quality, self.client.source) + cover = Tracklist.get_cover_obj( + self.cover_path, self.quality, self.client.source + ) if isinstance(audio, FLAC): if embed_cover: @@ -436,7 +436,7 @@ def tag( audio.add(cover) audio.save(self.final_path, "v2_version=3") elif isinstance(audio, MP4): - audio['covr'] = [cover] + audio["covr"] = [cover] audio.save() else: raise ValueError(f"Unknown container type: {audio}") @@ -616,7 +616,9 @@ def from_api(cls, item: dict, client: ClientInterface): return cls(client=client, **info) @staticmethod - def get_cover_obj(cover_path: str, quality: int, source: str) -> Union[Picture, APIC]: + def get_cover_obj( + cover_path: str, quality: int, source: str + ) -> Union[Picture, APIC]: """Given the path to an image and a quality id, return an initialized cover object that can be used for every track in the album. @@ -626,6 +628,7 @@ def get_cover_obj(cover_path: str, quality: int, source: str) -> Union[Picture, :type quality: int :rtype: Union[Picture, APIC] """ + def flac_mp3_cover_obj(cover): cover_obj = cover() cover_obj.type = 3 @@ -637,7 +640,7 @@ def flac_mp3_cover_obj(cover): if quality > 1: cover = Picture - elif source == 'tidal': + elif source == "tidal": cover = MP4Cover else: cover = APIC @@ -654,7 +657,7 @@ def flac_mp3_cover_obj(cover): return flac_mp3_cover_obj(cover) elif cover is MP4Cover: - with open(cover_path, 'rb') as img: + with open(cover_path, "rb") as img: return cover(img.read(), imageformat=MP4Cover.FORMAT_JPEG) raise InvalidQuality(f"Quality {quality} not allowed") diff --git a/streamrip/metadata.py b/streamrip/metadata.py index 8502785c..427efbb7 100644 --- a/streamrip/metadata.py +++ b/streamrip/metadata.py @@ -1,8 +1,6 @@ import json import logging import re -import sys -from pprint import pprint from typing import Generator, Optional, Tuple, Union from .constants import ( @@ -124,7 +122,7 @@ def add_album_meta(self, resp: dict): self.year = self.date[:4] self.copyright = resp.get("copyright") - self.albumartist = safe_get(resp, 'artist', 'name') + self.albumartist = safe_get(resp, "artist", "name") self.disctotal = resp.get("numberOfVolumes") self.isrc = resp.get("isrc") self.explicit = resp.get("explicit", False) @@ -133,9 +131,9 @@ def add_album_meta(self, resp: dict): elif self.__source == "deezer": self.album = resp.get("title") self.tracktotal = resp.get("track_total") - self.genre = safe_get(resp, 'genres', 'data') + self.genre = safe_get(resp, "genres", "data") self.date = resp.get("release_date") - self.albumartist = safe_get(resp, 'artist', 'name') + self.albumartist = safe_get(resp, "artist", "name") self.label = resp.get("label") # either 0 or 1 self.explicit = bool(resp.get("parental_warning")) @@ -156,23 +154,23 @@ def add_track_meta(self, track: dict): self._mod_title(track.get("version"), track.get("work")) self.composer = track.get("composer", {}).get("name") - self.tracknumber = track.get('track_number', 1) + self.tracknumber = track.get("track_number", 1) self.discnumber = track.get("media_number", 1) - self.artist = safe_get(track, 'performer', 'name') + self.artist = safe_get(track, "performer", "name") if self.artist is None: - self.artist = self.get('albumartist') + self.artist = self.get("albumartist") elif self.__source == "tidal": self.title = track.get("title").strip() self._mod_title(track.get("version"), None) - self.tracknumber = track.get('trackNumber', 1) + self.tracknumber = track.get("trackNumber", 1) self.discnumber = track.get("volumeNumber") self.artist = track.get("artist", {}).get("name") elif self.__source == "deezer": self.title = track.get("title").strip() self._mod_title(track.get("version"), None) - self.tracknumber = track.get('track_position', 1) + self.tracknumber = track.get("track_position", 1) self.discnumber = track.get("disk_number") self.artist = track.get("artist", {}).get("name") @@ -377,8 +375,8 @@ def __gen_mp4_tags(self) -> Tuple[str, Union[str, int, tuple]]: for k, v in MP4_KEY.items(): if k == "tracknumber": text = [(self.tracknumber, self.tracktotal)] - elif k == 'discnumber': - text = [(self.discnumber, self.get('disctotal', 1))] + elif k == "discnumber": + text = [(self.discnumber, self.get("disctotal", 1))] else: text = getattr(self, k) diff --git a/streamrip/utils.py b/streamrip/utils.py index ddb89df9..56a187c9 100644 --- a/streamrip/utils.py +++ b/streamrip/utils.py @@ -210,9 +210,9 @@ def decrypt_mqa_file(in_path, out_path, encryption_key): def ext(quality: int, source: str): if quality <= 1: - if source == 'tidal': - return '.m4a' + if source == "tidal": + return ".m4a" else: - return '.mp3' + return ".mp3" else: - return '.flac' + return ".flac"