Skip to content

Commit

Permalink
Add option to set album tag to playlist name
Browse files Browse the repository at this point in the history
  • Loading branch information
nathom committed Apr 10, 2021
1 parent b00f527 commit ac02f79
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
3 changes: 3 additions & 0 deletions streamrip/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class Config:
"keep_embedded_cover": False,
"keep_downloaded_cover": True,
},
"metadata": {
"set_playlist_to_album": False,
},
"path_format": {"folder": FOLDER_FORMAT, "track": TRACK_FORMAT},
"check_for_updates": True,
"lastfm": {"source": "qobuz"},
Expand Down
4 changes: 3 additions & 1 deletion streamrip/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import re
import sys
import time
from getpass import getpass
from hashlib import md5
from string import Formatter
Expand Down Expand Up @@ -163,6 +162,9 @@ def download(self):
"embed_cover": self.config.session["artwork"]["embed"],
"embed_cover_size": self.config.session["artwork"]["embed_size"],
"download_cover_size": self.config.session["artwork"]["download_size"],
"set_playlist_to_album": self.config.session["metadata"][
"set_playlist_to_album"
],
}
logger.debug("Arguments from config: %s", arguments)
for item in self:
Expand Down
27 changes: 24 additions & 3 deletions streamrip/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,10 @@ def _parse_get_resp(resp: dict, client: ClientInterface) -> dict:
"bit_depth": 16,
"sampling_rate": 44100,
"tracktotal": resp.get("track_total") or resp.get("nb_tracks"),
"disctotal": max(track["disk_number"] for track in resp["tracks"]),
"disctotal": max(
track.get("disk_number") for track in resp.get("tracks", [{}])
)
or 1,
"explicit": bool(resp.get("explicit_content_lyrics")),
}

Expand Down Expand Up @@ -1088,7 +1091,8 @@ def load_meta(self, **kwargs):
:type new_tracknumbers: bool
:param kwargs:
"""
self.meta = self.client.get(id=self.id, media_type="playlist")
self.meta = self.client.get(self.id, media_type="playlist")
logger.debug(pformat(self.meta))
self._load_tracks(**kwargs)
self.loaded = True

Expand All @@ -1100,6 +1104,9 @@ def _load_tracks(self, new_tracknumbers: bool = True):
"""
if self.client.source == "qobuz":
self.name = self.meta["name"]
self.image = self.meta["images"]
self.creator = safe_get(self.meta, "owner", "name", default="Qobuz")

tracklist = self.meta["tracks"]["items"]

def gen_cover(track):
Expand All @@ -1110,10 +1117,13 @@ def meta_args(track):

elif self.client.source == "tidal":
self.name = self.meta["title"]
self.image = tidal_cover_url(self.meta["image"], 640)
self.creator = safe_get(self.meta, "creator", "name", default="TIDAL")

tracklist = self.meta["tracks"]

def gen_cover(track):
cover_url = tidal_cover_url(track["album"]["cover"], 320)
cover_url = tidal_cover_url(track["album"]["cover"], 640)
return cover_url

def meta_args(track):
Expand All @@ -1124,13 +1134,18 @@ def meta_args(track):

elif self.client.source == "deezer":
self.name = self.meta["title"]
self.image = self.meta["picture_big"]
self.creator = safe_get(self.meta, "creator", "name", default="Deezer")

tracklist = self.meta["tracks"]

def gen_cover(track):
return track["album"]["cover_medium"]

elif self.client.source == "soundcloud":
self.name = self.meta["title"]
self.image = self.meta.get("artwork_url").replace("large", "t500x500")
self.creator = self.meta["user"]["username"]
tracklist = self.meta["tracks"]

def gen_cover(track):
Expand Down Expand Up @@ -1185,10 +1200,16 @@ def download(
logger.debug(f"Parent folder {folder}")

self.download_message()
set_playlist_to_album = kwargs.get("set_playlist_to_album", False)
for i, track in enumerate(self):

if self.client.source == "soundcloud":
track.load_meta()

if set_playlist_to_album and hasattr(self, "image"):
track["album"] = self.name
track["albumartist"] = self.creator

if kwargs.get("new_tracknumbers", True):
track.meta["tracknumber"] = str(i + 1)

Expand Down
2 changes: 2 additions & 0 deletions streamrip/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TrackMetadata:
* tracknumber
* discnumber
* tracktotal
* disctotal
"""

Expand Down Expand Up @@ -67,6 +68,7 @@ def __init__(
self.cover = None
self.tracknumber = None
self.discnumber = None
self.disctotal = None

# not included in tags
self.explicit = False
Expand Down
6 changes: 2 additions & 4 deletions streamrip/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import base64
import logging
import logging.handlers as handlers
import os
import time
from string import Formatter
from typing import Hashable, Optional, Union

Expand Down Expand Up @@ -112,8 +110,8 @@ def tqdm_download(url: str, filepath: str, params: dict = None):
r = requests.get(url, allow_redirects=True, stream=True, params=params)
total = int(r.headers.get("content-length", 0))
logger.debug(f"File size = {total}")
if total < 1000 and not url.endswith("jpg"):
raise NonStreamable
if total < 1000 and not url.endswith("jpg") and not url.endswith("png"):
raise NonStreamable(url)

try:
with open(filepath, "wb") as file, tqdm(
Expand Down

0 comments on commit ac02f79

Please sign in to comment.