Skip to content

Commit

Permalink
Add ability to choose whether to embed cover
Browse files Browse the repository at this point in the history
  • Loading branch information
nathom committed Apr 2, 2021
1 parent b9274a7 commit 4c82858
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
5 changes: 2 additions & 3 deletions streamrip/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ def download(self):
"keep_cover": self.config.session["keep_cover"],
"large_cover": self.config.session["metadata"]["large_cover"],
"folder_format": self.config.session["path_format"]["folder"],
"track_format": self.config.session["path_format"]["track"]
# TODO: fully implement this
# "embed_cover": self.config.session["metadata"]["embed_cover"],
"track_format": self.config.session["path_format"]["track"],
"embed_cover": self.config.session["metadata"]["embed_cover"],
}
logger.debug("Arguments from config: %s", arguments)
for item in self:
Expand Down
38 changes: 25 additions & 13 deletions streamrip/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import os
import re
import shutil
# import sys
from pprint import pformat
# from pprint import pprint
from tempfile import gettempdir
from typing import Any, Callable, Optional, Tuple, Union

Expand Down Expand Up @@ -251,8 +249,10 @@ def download_cover(self):

assert hasattr(self, "cover_url"), "must set cover_url attribute"

self.cover_path = os.path.join(self.folder, f"cover{hash(self.id)}.jpg")
self.cover_path = os.path.join(self.folder, f"cover{hash(self.meta.album)}.jpg")
logger.debug(f"Downloading cover from {self.cover_url}")
click.secho(f"\nDownloading cover art for {self!s}", fg='blue')

if not os.path.exists(self.cover_path):
tqdm_download(self.cover_url, self.cover_path)
else:
Expand Down Expand Up @@ -324,7 +324,7 @@ def tag(
self,
album_meta: dict = None,
cover: Union[Picture, APIC] = None,
embed_cover: bool = False,
embed_cover: bool = True,
):
"""Tag the track using the stored metadata.
Expand All @@ -336,6 +336,8 @@ def tag(
:type album_meta: dict
:param cover: initialized mutagen cover object
:type cover: Union[Picture, APIC]
:param embed_cover: Embed cover art into file
:type embed_cover: bool
"""
assert isinstance(self.meta, TrackMetadata), "meta must be TrackMetadata"
if not self._is_downloaded:
Expand Down Expand Up @@ -374,15 +376,17 @@ def tag(
for k, v in self.meta.tags(self.container):
audio[k] = v

if cover is None:
if embed_cover and cover is None:
assert hasattr(self, "cover")
cover = self.cover

if isinstance(audio, FLAC):
audio.add_picture(cover)
if embed_cover:
audio.add_picture(cover)
audio.save()
elif isinstance(audio, ID3):
audio.add(cover)
if embed_cover:
audio.add(cover)
audio.save(self.final_path, "v2_version=3")
else:
raise ValueError(f"Unknown container type: {audio}")
Expand Down Expand Up @@ -843,7 +847,8 @@ def download(
else:
tqdm_download(self.cover_urls["small"], cover_path)

if self.client.source != "deezer":
embed_cover = kwargs.get('embed_cover', True) # embed by default
if self.client.source != "deezer" and embed_cover:
cover = self.get_cover_obj(cover_path, quality)

for track in self:
Expand All @@ -856,7 +861,7 @@ def download(
track_format=kwargs.get("track_format", TRACK_FORMAT),
)
if kwargs.get("tag_tracks", True) and self.client.source != "deezer":
track.tag(cover=cover)
track.tag(cover=cover, embed_cover=embed_cover)

if not kwargs.get("keep_cover", True):
logger.debug(f"Removing cover at {cover_path}")
Expand Down Expand Up @@ -969,7 +974,6 @@ def load_meta(self, **kwargs):
:param kwargs:
"""
self.meta = self.client.get(self.id, "playlist")
self.name = self.meta.get("title")
self._load_tracks(**kwargs)

def _load_tracks(self, new_tracknumbers: bool = True):
Expand All @@ -979,6 +983,7 @@ def _load_tracks(self, new_tracknumbers: bool = True):
:type new_tracknumbers: bool
"""
if self.client.source == "qobuz":
self.name = self.meta['name']
tracklist = self.meta["tracks"]["items"]

def gen_cover(track): # ?
Expand All @@ -988,6 +993,7 @@ def meta_args(track):
return {"track": track, "album": track["album"]}

elif self.client.source == "tidal":
self.name = self.meta['title']
tracklist = self.meta["tracks"]

def gen_cover(track):
Expand All @@ -1001,6 +1007,7 @@ def meta_args(track):
}

elif self.client.source == "deezer":
self.name = self.meta['title']
tracklist = self.meta["tracks"]

def gen_cover(track):
Expand Down Expand Up @@ -1056,7 +1063,7 @@ def download(
for track in self:
track.download(parent_folder=folder, quality=quality, database=database)
if self.client.source != "deezer":
track.tag()
track.tag(embed_cover=kwargs.get('embed_cover', True))

@staticmethod
def _parse_get_resp(item: dict, client: ClientInterface):
Expand All @@ -1068,10 +1075,11 @@ def _parse_get_resp(item: dict, client: ClientInterface):
:param client:
:type client: ClientInterface
"""
print(item.keys())
if client.source == "qobuz":
return {
"name": item.get("name"),
"id": item.get("id"),
"name": item["name"],
"id": item['id'],
}
elif client.source == "tidal":
return {
Expand All @@ -1086,6 +1094,10 @@ def _parse_get_resp(item: dict, client: ClientInterface):

raise InvalidSourceError(client.source)

@property
def title(self):
return self.name

def __repr__(self) -> str:
"""Return a string representation of this Playlist object.
Useful for pprint and json.dumps.
Expand Down

0 comments on commit 4c82858

Please sign in to comment.