Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions streamrip/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ class CliConfig:
progress_bars: bool
# The maximum number of search results to show in the interactive menu
max_search_results: int
# How to order the album list shown for an artist url.
# "type": albums first, then EPs, then singles, each oldest first.
# Sources that do not report a release type fall back to "date".
# "date": one chronological list.
# Defaulted so configs written before this existed still load.
artist_album_sort: str = "type"


@dataclass(slots=True)
Expand Down
7 changes: 7 additions & 0 deletions streamrip/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ text_output = true
progress_bars = true
# The maximum number of search results to show in the interactive menu
max_search_results = 100
# Order of the album list shown for an artist url.
# "type" albums first, then EPs, then singles; each oldest first
# "date" one chronological list, oldest first
# Sources that do not report a release type (Qobuz) behave the same either
# way. Ties break on title, then quality, then id, so the order does not
# change between runs. Override for one run with `rip url --sort date`.
artist_album_sort = "type"

[misc]
# Metadata to identify this config file. Do not change.
Expand Down
99 changes: 97 additions & 2 deletions streamrip/media/artist.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import asyncio
import dataclasses
import logging
import re
import sys
from dataclasses import dataclass
from typing import ClassVar

from ..client import Client
from ..config import Config, QobuzDiscographyFilterConfig
from ..console import console
from ..utils import menu
from ..db import Database
from ..exceptions import NonStreamableError
from ..metadata import ArtistMetadata
from ..metadata.artist import ArtistMetadata
from ..metadata.search_results import AlbumSummary
from .album import Album, PendingAlbum
from .media import Media, Pending

Expand All @@ -28,11 +33,20 @@ class Artist(Media):
albums: list[PendingAlbum]
client: Client
config: Config
summaries: list[AlbumSummary] = dataclasses.field(default_factory=list)

# Set to False by `rip url --no-confirm` for unattended runs.
confirm_selection: ClassVar[bool] = True
# "type" groups albums, then EPs, then singles, each oldest first.
# "date" is a single chronological run. Set by `rip url --sort`.
sort_by: ClassVar[str] = "type"

async def preprocess(self):
pass

async def download(self):
if not self._select_albums():
return
filter_conf = self.config.session.qobuz_filters
if filter_conf.repeats:
console.log(
Expand All @@ -42,6 +56,87 @@ async def download(self):
else:
await self._download_async(filter_conf)

def _select_albums(self) -> bool:
"""Let the user pick from the discography before anything downloads.

Artist search is unreliable on both Qobuz and Tidal, so an artist url
can easily be the wrong person entirely, and even the right one brings
a lot nobody asked for. Uses the same menu as `rip search`, so it looks
and behaves the same way. Returns False if nothing was chosen.
"""
if not (
self.confirm_selection
and self.summaries
and len(self.albums) > 1
and sys.stdin.isatty()
):
return True

# Sources return the discography in their own order, which is not
# useful for picking through 80-odd releases.
sort_by = self.sort_by
if sort_by not in ("type", "date"):
# A missing key is covered by the dataclass default; this is a
# value that is present but not one we understand. Say so rather
# than silently picking an order the user did not ask for.
logger.warning(
"Unknown cli.artist_album_sort %r, using 'type'. "
"Valid values are 'type' and 'date'.",
sort_by,
)
sort_by = "type"

# Unknown type ranks with albums, so a source that does not report one
# (Qobuz) just sorts chronologically rather than pretending.
rank = {"album": 0, "": 0, "ep": 1, "single": 2}

def sort_key(i: int):
s = self.summaries[i]
date = s.date_released or ""
# Dates arrive as ISO strings, which sort correctly as text, but
# can also be a bare year or the literal "Unknown". Anything
# unparseable goes last rather than landing in the middle.
date_key = date if date[:4].isdigit() else "9999"
# Title and quality break ties, so two editions of the same
# release always sit together. The id is a last resort that never
# ties, because sources do list the same release twice with every
# visible field identical -- without it those two could swap
# places between runs.
tail = (date_key, s.name.lower(), s.quality, str(s.id))
return (rank.get(s.release_type, 0), *tail) if sort_by == "type" else tail

order = sorted(range(len(self.summaries)), key=sort_key)

# The artist is the same on every row here, so show what actually
# distinguishes releases: year, size, quality and kind.
entries = []
for n, i in enumerate(order, 1):
s = self.summaries[i]
year = (s.date_released or "")[:4] or "----"
quality = f" {s.quality}" if s.quality else ""
kind = f" ({s.release_type})" if s.release_type in ("ep", "single") else ""
entries.append(
f"{n}. {year} {s.name[:48]}{kind} [{s.num_tracks} trk]{quality}"
)

chosen = menu.multi_select(
entries,
title=(
f"{self.name} - {len(self.summaries)} releases\n"
"SPACE - select, ENTER - download, ESC - exit"
),
previews=[self.summaries[i].preview() for i in order],
)

if not chosen:
menu.announce_nothing_chosen()
return False

keep = {self.summaries[order[i]].id for i in chosen}
self.albums = [a for a in self.albums if str(a.id) in keep]
console.print(f"[green]Downloading {len(self.albums)} release(s).\n")
return True

async def postprocess(self):
pass

Expand Down Expand Up @@ -205,4 +300,4 @@ async def resolve(self) -> Artist | None:
PendingAlbum(album_id, self.client, self.config, self.db)
for album_id in meta.album_ids()
]
return Artist(meta.name, albums, self.client, self.config)
return Artist(meta.name, albums, self.client, self.config, meta.albums)
22 changes: 16 additions & 6 deletions streamrip/metadata/artist.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import logging
from dataclasses import dataclass
from dataclasses import dataclass, field

from .search_results import AlbumSummary

logger = logging.getLogger("streamrip")

Expand All @@ -10,6 +12,10 @@
class ArtistMetadata:
name: str
ids: list[str]
# One per id, in the same order. The artist response already carries the
# full album objects, so describing a discography costs no extra requests
# -- and AlbumSummary already knows how to read one from any source.
albums: list[AlbumSummary] = field(default_factory=list)

def album_ids(self):
return self.ids
Expand All @@ -18,10 +24,14 @@ def album_ids(self):
def from_resp(cls, resp: dict, source: str) -> ArtistMetadata:
logger.debug(resp)
if source == "qobuz":
return cls(resp["name"], [a["id"] for a in resp["albums"]["items"]])
elif source == "tidal":
return cls(resp["name"], [a["id"] for a in resp["albums"]])
elif source == "deezer":
return cls(resp["name"], [a["id"] for a in resp["albums"]])
items = resp["albums"]["items"]
elif source in ("tidal", "deezer"):
items = resp["albums"]
else:
raise NotImplementedError

return cls(
resp["name"],
[str(a["id"]) for a in items],
[AlbumSummary.from_item(a) for a in items],
)
39 changes: 36 additions & 3 deletions streamrip/metadata/search_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ class AlbumSummary(Summary):
artist: str
num_tracks: str
date_released: str | None
# Best quality the source lists for this release, where it says. Tidal
# routinely lists the same album twice, once lossless and once hi-res,
# which is otherwise impossible to tell apart in a list.
quality: str = ""
# "album", "ep" or "single" where the source says so, otherwise empty.
# Tidal populates it; Qobuz leaves release_type null on every album, and
# guessing from the track count would put wrong labels on screen -- a
# four-track release is not reliably an EP.
release_type: str = ""

def media_type(self):
return "album"
Expand All @@ -123,14 +132,23 @@ def summarize(self) -> str:
return f"{clean(self.name)} by {clean(self.artist)}"

def preview(self) -> str:
return f"Date released:\n{self.date_released}\n\n{self.num_tracks} Tracks\n\nID: {self.id}"
quality = f"\n\n{self.quality}" if self.quality else ""
return (
f"Date released:\n{self.date_released}\n\n"
f"{self.num_tracks} Tracks{quality}\n\nID: {self.id}"
)

@classmethod
def from_item(cls, item: dict):
id = str(item["id"])
title = (item.get("title") or "").strip()
version = (item.get("version") or "").strip()
name = title + (" (" + version + ")" if version else "")
# Sources often already have the edition in the title, which gave
# "Please Please Me (Remastered) (Remastered)".
if version and version.lower() not in title.lower():
name = f"{title} ({version})"
else:
name = title
artist = (
item.get("performer", {}).get("name")
or item.get("artist", {}).get("name")
Expand Down Expand Up @@ -158,7 +176,22 @@ def from_item(cls, item: dict):
or item.get("year")
or "Unknown"
)
return cls(id, name, artist, str(num_tracks), date_released)
tags = (item.get("mediaMetadata") or {}).get("tags") or []
if "HIRES_LOSSLESS" in tags:
quality = "hi-res"
elif tags:
quality = "lossless"
elif item.get("maximum_bit_depth") and item.get("maximum_sampling_rate"):
quality = f"{item['maximum_bit_depth']}bit/{item['maximum_sampling_rate']}kHz"
else:
quality = ""

declared = str(item.get("type") or item.get("release_type") or "").lower()
release_type = declared if declared in ("album", "ep", "single") else ""

return cls(
id, name, artist, str(num_tracks), date_released, quality, release_type
)


@dataclass(slots=True)
Expand Down
55 changes: 53 additions & 2 deletions streamrip/rip/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import shutil
import signal
import subprocess
from functools import wraps
from typing import Any
Expand All @@ -20,13 +21,46 @@
from ..config import DEFAULT_CONFIG_PATH, Config, OutdatedConfigError, set_user_defaults
from ..console import console
from ..utils.ssl_utils import get_aiohttp_connector_kwargs
from ..media.artist import Artist
from .main import Main


def coro(f):
@wraps(f)
def wrapper(*args, **kwargs):
return asyncio.run(f(*args, **kwargs))
async def run():
# Ctrl-C used to be ignored until whatever was in flight finished,
# so people force-killed it -- which skips the cleanup in
# Main.__aexit__ and strews __artwork directories through the
# library. Cancel the task instead, so the `async with Main(...)`
# blocks unwind normally and clean up after themselves.
task = asyncio.current_task()
loop = asyncio.get_running_loop()

def stop():
console.print(
"\n[yellow]Stopping after the current downloads finish... "
"(Ctrl-C again to force)"
)
if task is not None:
task.cancel()

try:
loop.add_signal_handler(signal.SIGINT, stop)
except (NotImplementedError, RuntimeError):
# Windows, or no running loop to attach to; fall back to the
# default KeyboardInterrupt behaviour.
pass

return await f(*args, **kwargs)

try:
return asyncio.run(run())
except (asyncio.CancelledError, KeyboardInterrupt):
console.print("[green]Stopped. Partly-downloaded tracks were left")
console.print(
"[green]resumable -- re-run the same command to continue."
)

return wrapper

Expand Down Expand Up @@ -166,16 +200,33 @@ def rip(

@rip.command()
@click.argument("urls", nargs=-1, required=True)
@click.option(
"--no-confirm",
is_flag=True,
help="For artist urls, take every album instead of asking which ones.",
)
@click.option(
"--sort",
"sort_by",
type=click.Choice(["type", "date"]),
default=None,
help="Order the artist album list. Overrides cli.artist_album_sort.",
)
@click.pass_context
@coro
async def url(ctx, urls):
async def url(ctx, urls, no_confirm, sort_by):
"""Download content from URLs."""
if ctx.obj["config"] is None:
return

if no_confirm:
Artist.confirm_selection = False

try:
with ctx.obj["config"] as cfg:
cfg: Config
# The flag wins for this run; otherwise take the configured order.
Artist.sort_by = sort_by or cfg.session.cli.artist_album_sort
updates = cfg.session.misc.check_for_updates
if updates:
# Run in background
Expand Down
Loading