From 696e844c3029479c8f8801dfb569777291f55304 Mon Sep 17 00:00:00 2001 From: nathom Date: Mon, 5 Apr 2021 18:40:46 -0700 Subject: [PATCH] Misc fixes; formatting --- streamrip/clients.py | 4 +- streamrip/core.py | 3 +- streamrip/downloader.py | 88 ++++++++++++++++++++++------------------- streamrip/metadata.py | 2 - 4 files changed, 50 insertions(+), 47 deletions(-) diff --git a/streamrip/clients.py b/streamrip/clients.py index 67c45c9..3c2b45d 100644 --- a/streamrip/clients.py +++ b/streamrip/clients.py @@ -4,7 +4,6 @@ import json import logging import time from abc import ABC, abstractmethod -from pprint import pformat, pprint from typing import Generator, Sequence, Tuple, Union import click @@ -491,7 +490,7 @@ class TidalClient(ClientInterface): } resp = self._api_request(f"tracks/{track_id}/playbackinfopostpaywall", params) manifest = json.loads(base64.b64decode(resp["manifest"]).decode("utf-8")) - logger.debug(f"{pformat(manifest)}") + logger.debug(manifest) return { "url": manifest["urls"][0], "enc_key": manifest.get("keyId"), @@ -547,7 +546,6 @@ class TidalClient(ClientInterface): if "status" in resp and resp["status"] != 200: raise Exception(f"Device authorization failed {resp}") - logger.debug(pformat(resp)) self.device_code = resp["deviceCode"] self.user_code = resp["userCode"] self.user_code_expiry = resp["expiresIn"] diff --git a/streamrip/core.py b/streamrip/core.py index de3a6cb..5c7a372 100644 --- a/streamrip/core.py +++ b/streamrip/core.py @@ -4,7 +4,6 @@ import re import sys from getpass import getpass from hashlib import md5 -from pprint import pprint from string import Formatter from typing import Generator, Optional, Tuple, Union @@ -316,7 +315,7 @@ class MusicDL(list): def from_title(s): num = [] for char in s: - if char != '.': + if char != ".": num.append(char) else: break diff --git a/streamrip/downloader.py b/streamrip/downloader.py index 1223940..7f8a68a 100644 --- a/streamrip/downloader.py +++ b/streamrip/downloader.py @@ -3,13 +3,11 @@ import os import re import shutil import subprocess -import sys from pprint import pformat, pprint from tempfile import gettempdir from typing import Any, Callable, Optional, Tuple, Union import click -import requests from mutagen.flac import FLAC, Picture from mutagen.id3 import APIC, ID3, ID3NoHeaderError from pathvalidate import sanitize_filename, sanitize_filepath @@ -21,7 +19,6 @@ from .constants import ( EXT, FLAC_MAX_BLOCKSIZE, FOLDER_FORMAT, - SOUNDCLOUD_CLIENT_ID, TRACK_FORMAT, ) from .db import MusicDB @@ -99,8 +96,8 @@ class Track: self.sampling_rate = 44100 self.bit_depth = 16 - self._is_downloaded = False - self._is_tagged = False + self.downloaded = False + self.tagged = False for attr in ("quality", "folder", "meta"): setattr(self, attr, None) @@ -180,8 +177,8 @@ class Track: if database is not None: if self.id in database: - self._is_downloaded = True - self._is_tagged = True + self.downloaded = True + self.tagged = True click.secho( f"{self['title']} already logged in database, skipping.", fg="magenta", @@ -189,8 +186,8 @@ class Track: return False # because the track was not downloaded if os.path.isfile(self.format_final_path()): # track already exists - self._is_downloaded = True - self._is_tagged = True + self.downloaded = True + self.tagged = True click.secho(f"Track already downloaded: {self.final_path}", fg="magenta") return False @@ -235,32 +232,8 @@ class Track: return False elif self.client.source == "soundcloud": - if dl_info["type"] == "mp3": - temp_file += ".mp3" - # convert hls stream to mp3 - subprocess.call( - [ - "ffmpeg", - "-i", - dl_info["url"], - "-c", - "copy", - "-y", - temp_file, - "-loglevel", - "fatal", - ] - ) - elif dl_info["type"] == "original": - tqdm_download(dl_info["url"], temp_file) + temp_file = self._soundcloud_download(dl_info, temp_file) - # if a wav is returned, convert to flac - engine = converter.FLAC(temp_file) - temp_file = f"{temp_file}.flac" - engine.convert(custom_fn=temp_file) - - self.final_path = self.final_path.replace(".mp3", ".flac") - self.quality = 2 else: raise InvalidSourceError(self.client.source) @@ -275,7 +248,7 @@ class Track: logger.debug("Downloaded: %s -> %s", temp_file, self.final_path) - self._is_downloaded = True + self.downloaded = True if tag: self.tag() @@ -285,6 +258,36 @@ class Track: return True + def _soundcloud_download(self, dl_info: dict, temp_file: str) -> str: + if dl_info["type"] == "mp3": + temp_file += ".mp3" + # convert hls stream to mp3 + subprocess.call( + [ + "ffmpeg", + "-i", + dl_info["url"], + "-c", + "copy", + "-y", + temp_file, + "-loglevel", + "fatal", + ] + ) + elif dl_info["type"] == "original": + tqdm_download(dl_info["url"], temp_file) + + # if a wav is returned, convert to flac + engine = converter.FLAC(temp_file) + temp_file = f"{temp_file}.flac" + engine.convert(custom_fn=temp_file) + + self.final_path = self.final_path.replace(".mp3", ".flac") + self.quality = 2 + + return temp_file + def download_cover(self): """Downloads the cover art, if cover_url is given.""" @@ -378,13 +381,13 @@ class Track: :type embed_cover: bool """ assert isinstance(self.meta, TrackMetadata), "meta must be TrackMetadata" - if not self._is_downloaded: + if not self.downloaded: logger.info( "Track %s not tagged because it was not downloaded", self["title"] ) return - if self._is_tagged: + if self.tagged: logger.info( "Track %s not tagged because it is already tagged", self["title"] ) @@ -426,7 +429,7 @@ class Track: else: raise ValueError(f"Unknown container type: {audio}") - self._is_tagged = True + self.tagged = True def convert(self, codec: str = "ALAC", **kwargs): """Converts the track to another codec. @@ -445,7 +448,7 @@ class Track: :type codec: str :param kwargs: """ - if not self._is_downloaded: + if not self.downloaded: logger.debug("Track not downloaded, skipping conversion") click.secho("Track not downloaded, skipping conversion", fg="magenta") return @@ -775,12 +778,17 @@ class Album(Tracklist): "tracktotal": resp.get("numberOfTracks"), } elif client.source == "deezer": + if resp.get("release_date", False): + year = resp["release_date"][:4] + else: + year = None + return { "id": resp.get("id"), "title": resp.get("title"), "_artist": safe_get(resp, "artist", "name"), "albumartist": safe_get(resp, "artist", "name"), - "year": resp.get("release_date")[:4], + "year": year, # version not given by API "cover_urls": { sk: resp.get(rk) # size key, resp key diff --git a/streamrip/metadata.py b/streamrip/metadata.py index 2e94a4f..97b8c7b 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 (