Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
nathom committed Apr 10, 2021
1 parent 9301757 commit b00f527
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion streamrip/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
31 changes: 17 additions & 14 deletions streamrip/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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__)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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}")
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand Down
22 changes: 10 additions & 12 deletions streamrip/metadata.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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)
Expand All @@ -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"))
Expand All @@ -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")

Expand Down Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions streamrip/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit b00f527

Please sign in to comment.