Track Spotify origin (liked/playlist/top); make top tracks opt-in; clean Ctrl+C

- Tag each Spotify track with its origin (liked / from_playlist / from_top,
  combinable) so genuinely-saved tracks are distinguishable from ones only
  surfaced by the top-tracks endpoint.
- Top tracks are no longer added by default (they inflated "missing" with
  unowned listening stats). --include-top pulls them as full objects so they
  carry an ISRC for real ID matching.
- On --spotify-full, prune Spotify rows that are neither liked nor in a
  playlist (top-only / orphaned), cleaning an existing DB in one pass.
- Cached-ingest fallback honours include_top and prunes too.
- In-place SQLite migration adds the new columns (no DB rebuild needed).
- Ctrl+C exits cleanly (130) with a message instead of a traceback;
  incremental progress is already committed, so re-run to continue.
- Surface origin breakdown in the fetch log, match summary, and GUI.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Шурупов Илья Викторович 2026-06-16 00:42:44 +03:00
parent 6920b634c4
commit 48bf84bcb7
9 changed files with 177 additions and 34 deletions

25
main.py
View file

@ -9,6 +9,7 @@ Helpers: `enrich` (resume MBID resolution), `match` (print the summary).
"""
import argparse
import logging
import sys
def _setup_logging():
@ -20,7 +21,7 @@ def cmd_fetch(args):
from src.workflows import run_fetch
run_fetch(enrich_limit=args.enrich_limit, enrich=not args.no_enrich,
spotify_full=args.spotify_full)
spotify_full=args.spotify_full, include_top=args.include_top)
def cmd_display(args):
@ -59,6 +60,9 @@ def build_parser():
help="skip MusicBrainz enrichment; use MBIDs already on tracks")
fetch.add_argument("--spotify-full", action="store_true",
help="force a full Spotify resync (ignore snapshot/watermark)")
fetch.add_argument("--include-top", action="store_true",
help="also pull the top-tracks endpoint (off by default; "
"with --spotify-full, top-only tracks are otherwise pruned)")
sub.add_parser("display", help="open the Streamlit GUI")
@ -72,12 +76,19 @@ def build_parser():
def main():
args = build_parser().parse_args()
{
"fetch": cmd_fetch,
"display": cmd_display,
"enrich": cmd_enrich,
"match": cmd_match,
}[args.cmd](args)
try:
{
"fetch": cmd_fetch,
"display": cmd_display,
"enrich": cmd_enrich,
"match": cmd_match,
}[args.cmd](args)
except KeyboardInterrupt:
# Ctrl+C: stop cleanly, no traceback. Work already committed is kept
# (each step writes incrementally), so just re-run to continue.
print("\ninterrupted — partial progress saved; re-run to continue.",
file=sys.stderr)
sys.exit(130)
if __name__ == "__main__":