From aaa23e4034c363be5c7c8c367d2125dee416c691 Mon Sep 17 00:00:00 2001 From: ghostdanser <5584730+sjbrownrigg@users.noreply.github.com> Date: Mon, 17 Aug 2026 15:45:33 +0100 Subject: [PATCH 1/2] Stop cleanly on Ctrl-C Ctrl-C did not stop a download. Measured on a 16-track album: SIGINT was ignored, the process was still running ten seconds later and had to be killed outright -- which skips Main.__aexit__, so the __artwork directories it would have removed were left behind. That is where large numbers of stray __artwork directories in a library come from. Cancel the running task from a SIGINT handler instead, so the `async with Main(...)` blocks unwind normally and clean up after themselves. The process now exits promptly, __artwork is removed, and the message says what happened rather than showing a KeyboardInterrupt traceback. Partly-downloaded tracks are left behind for the next run to continue, so interrupting mid-album loses nothing: re-running the same command finishes it. Verified end to end -- interrupt a 16-track album, re-run, finish with 16 valid FLACs and no leftovers. --- streamrip/rip/cli.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/streamrip/rip/cli.py b/streamrip/rip/cli.py index 2b3f532e..4d7209a7 100644 --- a/streamrip/rip/cli.py +++ b/streamrip/rip/cli.py @@ -3,6 +3,7 @@ import logging import os import shutil +import signal import subprocess from functools import wraps from typing import Any @@ -26,7 +27,39 @@ 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 From 43c2966db39aad28d09b1167bfc28d4d89c2fa46 Mon Sep 17 00:00:00 2001 From: ghostdanser <5584730+sjbrownrigg@users.noreply.github.com> Date: Mon, 17 Aug 2026 15:53:34 +0100 Subject: [PATCH 2/2] Ask which albums to take before downloading an artist An artist url downloaded the entire discography with no chance to look at it first. Artist search is unreliable on both Qobuz and Tidal, so the url can easily be the wrong person entirely, and even the right one brings a lot nobody asked for -- the only way to stop it was Ctrl-C. Show the discography in the same scrolling multi-select menu `rip search` already uses, with the same SPACE/ENTER/ESC keys and the same preview pane: The Beatles - 88 releases SPACE - select, ENTER - download, ESC - exit 1. 1963 Please Please Me (Remastered) [14 trk] lossless 2. 1963 With The Beatles (Remastered) [14 trk] lossless ... 86. 2025 In My Life (Take 1) (single) [1 trk] hi-res To do that the menu moves into utils/menu.py and both callers use it, so the Windows `pick` fallback is no longer implemented only for search. The listing costs no extra requests: the artist response already carries the full album objects and ArtistMetadata was keeping only their ids. It now keeps the AlbumSummary that search_results already builds, which reads every source's field names and folds `version` into the name. Sorted, since sources return a discography in their own order: by release type then date, or as one chronological list. Set with cli.artist_album_sort, overridden for a run by `rip url --sort`. The config field is defaulted so older configs still load. Release type is only used where the source reports it. Tidal does; Qobuz leaves release_type null on every album, and guessing from the track count would put wrong labels on screen, since a four-track release is not reliably an EP. Unknown entries are unlabelled and sort with the albums, so "type" degrades to chronological rather than inventing groupings. AlbumSummary also gains an optional `quality`, defaulting to empty so existing construction is unaffected. Tidal lists the same release twice, once lossless and once hi-res, which is otherwise impossible to tell apart in a list. Search results show it in their preview pane too. And stop doubling the edition in the name: the version was appended even when the title already contained it, giving "Please Please Me (Remastered) (Remastered)". That affected `rip search` results as well. Only prompts when stdin is a terminal, so scripts and cron are unaffected, and `rip url --no-confirm` skips it. --- streamrip/config.py | 6 ++ streamrip/config.toml | 7 ++ streamrip/media/artist.py | 99 +++++++++++++++++++++++++++- streamrip/metadata/artist.py | 22 +++++-- streamrip/metadata/search_results.py | 39 ++++++++++- streamrip/rip/cli.py | 20 +++++- streamrip/rip/main.py | 53 ++++----------- streamrip/utils/menu.py | 63 ++++++++++++++++++ 8 files changed, 258 insertions(+), 51 deletions(-) create mode 100644 streamrip/utils/menu.py diff --git a/streamrip/config.py b/streamrip/config.py index bafa5687..01988160 100644 --- a/streamrip/config.py +++ b/streamrip/config.py @@ -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) diff --git a/streamrip/config.toml b/streamrip/config.toml index 029115bd..c3db4eca 100644 --- a/streamrip/config.toml +++ b/streamrip/config.toml @@ -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. diff --git a/streamrip/media/artist.py b/streamrip/media/artist.py index 854f6ea0..2aaa53ec 100644 --- a/streamrip/media/artist.py +++ b/streamrip/media/artist.py @@ -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 @@ -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( @@ -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 @@ -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) diff --git a/streamrip/metadata/artist.py b/streamrip/metadata/artist.py index 46b3ed04..32b9db09 100644 --- a/streamrip/metadata/artist.py +++ b/streamrip/metadata/artist.py @@ -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") @@ -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 @@ -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], + ) diff --git a/streamrip/metadata/search_results.py b/streamrip/metadata/search_results.py index 4b3011b2..715c2ed5 100644 --- a/streamrip/metadata/search_results.py +++ b/streamrip/metadata/search_results.py @@ -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" @@ -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") @@ -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) diff --git a/streamrip/rip/cli.py b/streamrip/rip/cli.py index 4d7209a7..6a679f00 100644 --- a/streamrip/rip/cli.py +++ b/streamrip/rip/cli.py @@ -21,6 +21,7 @@ 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 @@ -199,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 diff --git a/streamrip/rip/main.py b/streamrip/rip/main.py index 562773fc..df7047b0 100644 --- a/streamrip/rip/main.py +++ b/streamrip/rip/main.py @@ -9,6 +9,7 @@ from ..client import Client, DeezerClient, QobuzClient, SoundcloudClient, TidalClient from ..config import Config from ..console import console +from ..utils import menu from ..media import ( Media, Pending, @@ -191,47 +192,21 @@ async def search_interactive(self, source: str, media_type: str, query: str): return search_results = SearchResults.from_pages(source, media_type, pages) - if platform.system() == "Windows": # simple term menu not supported for windows - from pick import pick - - choices = pick( - search_results.results, - title=( - f"{source.capitalize()} {media_type} search.\n" - "Press SPACE to select, RETURN to download, CTRL-C to exit." - ), - multiselect=True, - min_selection_count=1, - ) - assert isinstance(choices, list) - - await self.add_all_by_id( - [(source, media_type, item.id) for item, _ in choices], - ) - + chosen_ind = menu.multi_select( + search_results.summaries(), + title=( + f"Results for {media_type} '{query}' from {source.capitalize()}\n" + "SPACE - select, ENTER - download, ESC - exit" + ), + previews=[r.preview() for r in search_results.results], + ) + if not chosen_ind: + menu.announce_nothing_chosen() else: - from simple_term_menu import TerminalMenu - - menu = TerminalMenu( - search_results.summaries(), - preview_command=search_results.preview, - preview_size=0.5, - title=( - f"Results for {media_type} '{query}' from {source.capitalize()}\n" - "SPACE - select, ENTER - download, ESC - exit" - ), - cycle_cursor=True, - clear_screen=True, - multi_select=True, + choices = search_results.get_choices(tuple(chosen_ind)) + await self.add_all_by_id( + [(source, item.media_type(), item.id) for item in choices], ) - chosen_ind = menu.show() - if chosen_ind is None: - console.print("[yellow]No items chosen. Exiting.") - else: - choices = search_results.get_choices(chosen_ind) - await self.add_all_by_id( - [(source, item.media_type(), item.id) for item in choices], - ) async def search_take_first(self, source: str, media_type: str, query: str): client = await self.get_logged_in_client(source) diff --git a/streamrip/utils/menu.py b/streamrip/utils/menu.py new file mode 100644 index 00000000..2842db56 --- /dev/null +++ b/streamrip/utils/menu.py @@ -0,0 +1,63 @@ +"""The scrolling multi-select menu used for anything the user has to pick from. + +`rip search` grew this first; artist downloads need exactly the same thing, so +it lives here rather than being written twice with the Windows fallback only +handled in one of them. +""" + +import platform + +from ..console import console + + +def multi_select( + entries: list[str], + title: str, + previews: list[str] | None = None, +) -> list[int]: + """Show `entries` and return the indices the user chose. + + Empty if they chose nothing or backed out. `previews`, when given, must be + the same length as `entries`. + """ + if not entries: + return [] + + if platform.system() == "Windows": + # simple-term-menu is unix only. + from pick import pick + + chosen = pick(entries, title=title, multiselect=True, min_selection_count=0) + if not chosen: + return [] + return [index for _, index in chosen] + + from simple_term_menu import TerminalMenu + + preview_command = None + if previews: + by_entry = dict(zip(entries, previews)) + + def preview_command(entry: str) -> str: # noqa: F811 + return by_entry.get(entry, "") + + menu = TerminalMenu( + entries, + title=title, + preview_command=preview_command, + preview_size=0.4, + cycle_cursor=True, + clear_screen=True, + multi_select=True, + show_multi_select_hint=True, + ) + chosen = menu.show() + if chosen is None: + return [] + if isinstance(chosen, int): + return [chosen] + return list(chosen) + + +def announce_nothing_chosen() -> None: + console.print("[yellow]No items chosen. Exiting.")