"""Round 103: Connections ▸ Import Shazams…. - The Rabbit's Shazam history is read through root adb (and adbd is put back after), the -wal included. - Only Shazams not dealt with yet are offered; the library's songs come unticked; what's imported or passed over is remembered in the synced preferences, and a failure comes back next time. - The YouTube result is the original song: no live, cover, karaoke, clean, vinyl or sped-up take unless Shazam's title says so, and Apple's length decides between the rest. - One window shows every song's stage, and the tagging panel for each song as soon as it's imported. """ import os import sqlite3 import stat import sys import time from datetime import datetime from pathlib import Path import pytest from PyQt6.QtCore import QObject, Qt, pyqtSignal from lintunes import shazam, url_import from lintunes.fingerprint import IdentifyCandidate from lintunes.library_manager import LibraryManager from lintunes.models import Library from lintunes.models.track import Track from lintunes.preferences import Preferences from lintunes.shazam import ShazamSong def _song(rid="r1", title="Digital Love", artist="Daft Punk", **kw): return ShazamSong(request_id=rid, when=datetime(2026, 10, 1, 20, 15), title=title, artist=artist, **kw) def _pump(qapp, until, timeout=10.0): deadline = time.monotonic() + timeout while not until() and time.monotonic() < deadline: qapp.processEvents() time.sleep(0.01) assert until(), "timed out waiting" # -------------------------------------------------------------------------- # A. Shazam's database def _make_db(path: Path): db = sqlite3.connect(str(path)) db.executescript(""" create table tag (request_id text, timestamp integer, status text, track_key text); create table track (track_key text, track_title text, release_date text, cover_art text); create table artist (artist_id text, artist_name text); create table apple_artist_track (track_key text, artist_adam_id text); create table track_genre (track_key text, genre_id text, genre_type text); """) db.executemany("insert into tag values (?,?,?,?)", [ ("old", 1_700_000_000_000, "SUCCESSFUL", "k1"), ("new", 1_790_000_000_000, "SUCCESSFUL", "k2"), ("again", 1_795_000_000_000, "SUCCESSFUL", "k1"), ("untitled", 1_780_000_000_000, "UNSUBMITTED", None), ]) db.executemany("insert into track values (?,?,?,?)", [ ("k1", "Digital Love", "2001", "https://is1.mzstatic.com/x/724384960650_cover.jpg/400x400.jpg"), ("k2", "Hey Ya!", "2003-09-09", None), ]) db.executemany("insert into artist values (?,?)", [("a1", "Daft Punk"), ("a2", "Outkast")]) db.executemany("insert into apple_artist_track values (?,?)", [("k1", "a1"), ("k2", "a2")]) db.executemany("insert into track_genre values (?,?,?)", [("k1", "Electronic", "editorial"), ("k1", "1234", "mz")]) db.commit() db.close() def test_reads_shazams_newest_first_one_per_song(tmp_path): _make_db(tmp_path / "library.db") songs = shazam.read_shazams(tmp_path / "library.db") assert [s.request_id for s in songs] == ["again", "new", "old"] first = songs[0] assert (first.title, first.artist, first.year, first.genre) == ( "Digital Love", "Daft Punk", 2001, "Electronic") assert first.upc == "724384960650" assert songs[1].year == 2003 # The same song Shazamed twice is offered once: the newest. assert [s.request_id for s in shazam.dedupe(songs)] == ["again", "new"] class _Adb: """Answers adb the way the R1 does: shell until `adb root`.""" def __init__(self, refuse=False): self.root = False self.refuse = refuse self.calls = [] def __call__(self, adb, serial, *args, timeout=30): import subprocess self.calls.append(args) out = "" code = 0 if args == ("shell", "whoami"): out = "root\n" if self.root else "shell\n" elif args == ("root",): if self.refuse: out = "adbd cannot run as root in production builds\n" else: self.root = True out = "restarting adbd as root\n" elif args == ("unroot",): self.root = False elif args[0] == "pull": if not args[1].endswith("library.db"): code = 1 # no -wal on this device else: Path(args[2]).write_bytes(b"db") return subprocess.CompletedProcess(args, code, out, "") def test_pull_roots_for_the_copy_and_unroots_after(tmp_path, monkeypatch): fake = _Adb() monkeypatch.setattr(shazam, "_adb", fake) path = shazam.pull_database("adb", "R1", tmp_path) assert path == tmp_path / "library.db" and path.read_bytes() == b"db" pulls = [c[1] for c in fake.calls if c[0] == "pull"] assert pulls == [shazam.DEVICE_DB, shazam.DEVICE_DB + "-wal", shazam.DEVICE_DB + "-shm"] assert fake.calls[-1] == ("unroot",) and not fake.root def test_pull_leaves_an_already_root_adbd_alone(tmp_path, monkeypatch): fake = _Adb() fake.root = True monkeypatch.setattr(shazam, "_adb", fake) shazam.pull_database("adb", "R1", tmp_path) assert ("root",) not in fake.calls and ("unroot",) not in fake.calls def test_a_production_rom_says_why(tmp_path, monkeypatch): monkeypatch.setattr(shazam, "_adb", _Adb(refuse=True)) with pytest.raises(shazam.ShazamError, match="refused root"): shazam.pull_database("adb", "R1", tmp_path) # -------------------------------------------------------------------------- # B. what's been dealt with, and what the library has def test_handled_list_rides_the_synced_preferences(tmp_path): prefs = Preferences(tmp_path) assert shazam.handled_ids(prefs) == set() shazam.mark_handled(prefs, ["a", "b"], datetime(2026, 10, 19)) shazam.mark_handled(prefs, ["b", "c"]) again = Preferences(tmp_path) assert shazam.handled_ids(again) == {"a", "b", "c"} assert again.get("shazam_import")["handled"]["a"] == "2026-10-19" def test_the_skills_own_list_counts(tmp_path): state = tmp_path / "state.json" state.write_text('{"done": {"x": {"file": "a.mp3"}}}') assert shazam.legacy_handled(state) == {"x"} assert shazam.legacy_handled(tmp_path / "missing.json") == set() def test_library_matches_ignore_feat_and_remaster(): tracks = [Track(track_id=1, name="Hey Ya! (Radio Mix)", artist="Outkast"), Track(track_id=2, name="Digital Love (Remastered)", artist="Daft Punk")] songs = [_song("a"), _song("b", "Hey Ya! (Radio Mix) feat. Nobody", "OutKast"), _song("c", "One More Time")] shazam.match_library(songs, tracks) assert songs[0].in_library == "Daft Punk — Digital Love (Remastered)" assert songs[1].in_library == "Outkast — Hey Ya! (Radio Mix)" assert songs[2].in_library == "" # -------------------------------------------------------------------------- # C. the original song, not some other take of it @pytest.mark.parametrize("title, channel, why", [ ("Daft Punk - Digital Love (Live @ Coachella)", "", "live"), ("Digital Love - Daft Punk (guitar cover)", "", "cover"), ("Digital Love", "Sing King Karaoke", "karaoke"), ("Digital Love (Instrumental)", "", "karaoke"), ("Daft Punk - Digital Love (Clean)", "", "clean"), ("Digital Love played on a 1979 turntable", "", "vinyl"), ("Daft Punk - Digital Love [vinyl rip]", "", "vinyl"), ("digital love (sped up)", "", "altered"), ("Digital Love (slowed + reverb)", "", "altered"), ("Digital Love - Daft Punk REACTION", "", "other"), ("Daft Punk - Digital Love (Official Audio)", "Daft Punk", ""), ]) def test_not_the_song(title, channel, why): assert shazam.not_the_song(title, channel, "Digital Love") == why def test_a_live_shazam_wants_the_live_take(): assert shazam.not_the_song("Song (Live at Wembley)", "", "Song (Live at Wembley)") == "" assert shazam.not_the_song("Song (Remix)", "", "Song (Sasha Remix)") == "" def test_topic_and_the_right_length_win(): song = _song(canon_secs=301) hits = [ {"title": "Daft Punk - Digital Love (Official Video)", "channel": "Daft Punk", "duration": 265, "views": 36_000_000}, {"title": "Digital Love", "channel": "Daft Punk - Topic", "duration": 301, "views": 2_000_000}, {"title": "Daft Punk - Digital Love (Official Audio)", "channel": "Daft Punk", "duration": 302, "views": 21_000_000}, {"title": "Digital Love (full version) Live @Leeds", "channel": "Daft Funk Live", "duration": 309, "views": 1}, {"title": "Something Else", "channel": "x", "duration": 301}, ] ranked = shazam.rank_hits(hits, song) assert [h["channel"] for h in ranked] == ["Daft Punk - Topic", "Daft Punk"] # The video is 36 s short of Apple's length: another cut of the song. assert all("Video" not in h["title"] for h in ranked) def test_without_apples_length_youtube_order_and_words_decide(): song = _song() ranked = shazam.rank_hits( [{"title": "Digital Love (Official Video)", "channel": "Daft Punk", "duration": 265}, {"title": "Digital Love (Official Audio)", "channel": "Daft Punk", "duration": 302}, {"title": "Digital Love", "channel": "x", "duration": 20}], song) assert [h["duration"] for h in ranked] == [302, 265] def test_a_live_album_never_supplies_the_length(): song = _song() found = shazam.canonical_from_results([ {"trackName": "Digital Love", "artistName": "Daft Punk", "collectionName": "Alive 2007 (Live)", "trackTimeMillis": 400_000}, {"trackName": "Digital Love", "artistName": "Daft Punk", "collectionName": "Discovery", "trackTimeMillis": 301_000}, ], song) assert found == (301, "Discovery", "Daft Punk", True) def test_tags_take_apples_album_only_when_sure(): loose = _song(year=2001, genre="Electronic", album="Hits 2001", album_artist="Various") assert shazam.tag_fields(loose) == {"name": "Digital Love", "artist": "Daft Punk", "year": 2001, "genre": "Electronic"} sure = _song(album="Discovery", album_artist="Daft Punk", album_confident=True) assert shazam.tag_fields(sure)["album"] == "Discovery" def test_queries_drop_suffixes_then_try_soundcloud(): song = _song(title="Strobe (Club Edit)", artist="deadmau5") assert shazam.search_queries(song) == [ ("ytsearch", "deadmau5 Strobe (Club Edit)"), ("ytsearch", "deadmau5 Strobe"), ("scsearch", "deadmau5 Strobe (Club Edit)")] def test_new_marker_lines(): assert url_import.parse_line( "LTHIT abc\t301\t2000\thttps://y/watch?v=abc\tDaft Punk - Topic\t" "Digital Love\n") == ("hit", { "id": "abc", "duration": 301, "views": 2000, "url": "https://y/watch?v=abc", "channel": "Daft Punk - Topic", "title": "Digital Love"}) assert url_import.parse_line("LTPP ExtractAudio started\n") == ( "postprocess", "ExtractAudio", "started") cmd = url_import.build_command("https://x/v", "/tmp") assert any(a.startswith("postprocess:LTPP") for a in cmd) search = url_import.build_search_command("a b", 8, None, "scsearch") assert search[-1] == "scsearch8:a b" and "--flat-playlist" in search # -------------------------------------------------------------------------- # D. the worker, against a fake yt-dlp FAKE_YTDLP = '''#!/usr/bin/env python3 import os, sys args = sys.argv[1:] with open(os.environ["FAKE_YTDLP_LOG"], "a") as log: log.write("\\x1f".join(args) + "\\n") target = args[args.index("--") + 1] if "--flat-playlist" in args: query = target.split(":", 1)[1] if "Nowhere" in query: sys.exit(0) vid = query.split()[-1].lower() print(f"LTHIT {vid}live\\t\\t9\\thttps://y/watch?v={vid}live\\tx\\t" f"{query} (Live)", flush=True) print(f"LTHIT {vid}\\t\\t9\\thttps://y/watch?v={vid}\\tA - Topic\\t" f"{query}", flush=True) sys.exit(0) dest = args[args.index("-P") + 1] vid = target.rsplit("=", 1)[-1] print(f"LTSTART 1\\t1\\t{vid}\\t{vid}", flush=True) print("LTPROG 50.0%", flush=True) print("LTPP ExtractAudio started", flush=True) path = os.path.join(dest, f"{vid} [{vid}].mp3") with open(path, "wb") as f: f.write(open(os.environ["FAKE_YTDLP_AUDIO"], "rb").read()) print(f"LTFILE {path}", flush=True) ''' @pytest.fixture def fake(tmp_path, monkeypatch, mp3_file): bin_dir = tmp_path / "bin" bin_dir.mkdir() script = bin_dir / "yt-dlp" script.write_text(FAKE_YTDLP.replace("/usr/bin/env python3", sys.executable)) script.chmod(script.stat().st_mode | stat.S_IEXEC) log = tmp_path / "argv.log" monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ['PATH']}") monkeypatch.setenv("FAKE_YTDLP_LOG", str(log)) monkeypatch.setenv("FAKE_YTDLP_AUDIO", str(mp3_file)) monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config")) return lambda: ([line.split("\x1f") for line in log.read_text().splitlines()] if log.exists() else []) def test_worker_narrates_each_stage_and_skips_the_live_take(qapp, fake): songs = [_song("a", "Alpha", "A"), _song("n", "Nowhere", "Z")] worker = shazam.ShazamImportWorker(songs, lookup=lambda s: None) stages, landed, failed, done = [], [], [], [] worker.stage.connect(lambda rid, text: stages.append((rid, text))) worker.landed.connect(lambda rid, path: landed.append((rid, path))) worker.item_failed.connect(lambda rid, why: failed.append(rid)) worker.finished.connect(done.append) worker.start() _pump(qapp, lambda: done) a_stages = [text for rid, text in stages if rid == "a"] assert a_stages[:3] == ["Looking up…", "Searching YouTube…", "Downloading…"] assert "Downloading 50%" in a_stages and "Converting to mp3…" in a_stages assert a_stages[-1] == "Tagging…" [(rid, path)] = landed downloads = [argv for argv in fake() if "--flat-playlist" not in argv] assert downloads[0][-1] == "https://y/watch?v=alpha" # not alphalive from lintunes.tagging import read_tags tags = read_tags(path) assert (tags["name"], tags["artist"]) == ("Alpha", "A") # Nowhere: YouTube twice (no suffix to drop, so once), then SoundCloud. searched = [argv[-1] for argv in fake() if "--flat-playlist" in argv and "Nowhere" in argv[-1]] assert searched == ["ytsearch8:Z Nowhere", "scsearch8:Z Nowhere"] assert failed == ["n"] assert done[0]["downloaded"] == 1 and len(done[0]["errors"]) == 1 worker.cleanup() # -------------------------------------------------------------------------- # E. the preview and the import window class _Reader(QObject): status = pyqtSignal(str) read = pyqtSignal(list) failed = pyqtSignal(str) def __init__(self, songs=None, error=""): super().__init__() self._songs, self._error = songs, error def start(self): if self._error: self.failed.emit(self._error) else: self.read.emit(self._songs) def test_preview_offers_only_new_shazams(qapp): from lintunes.gui.shazam_import import ShazamPreviewDialog songs = [_song("a", "Alpha", "A"), _song("b", "Digital Love"), _song("c", "Gamma", "C")] owned = [Track(track_id=1, name="Digital Love", artist="Daft Punk")] dialog = ShazamPreviewDialog(_Reader(songs), {"c"}, owned, "Adds to the end of “Mix”") assert dialog._tree.topLevelItemCount() == 2 assert [s.request_id for s in dialog.chosen()] == ["a"] assert dialog.passed_over() == ["b"] # already owned, unticked assert dialog._ok.text() == "Import 1 Song" assert dialog._add.isEnabled() and dialog.add_to_playlist() dialog._tree.topLevelItem(0).setCheckState(0, Qt.CheckState.Unchecked) assert dialog._ok.text() == "Mark as Dealt With" assert dialog.passed_over() == ["a", "b"] def test_preview_with_nothing_new_or_no_device(qapp): from lintunes.gui.shazam_import import ShazamPreviewDialog dialog = ShazamPreviewDialog(_Reader([_song("a")]), {"a"}, [], None) assert "No new Shazams" in dialog._status.text() assert not dialog._ok.isEnabled() and not dialog._add.isEnabled() broken = ShazamPreviewDialog(_Reader(error="No Rabbit found"), set(), [], None) assert broken._status.text() == "No Rabbit found" assert not broken._ok.isEnabled() def _cand(name): return IdentifyCandidate(score=0.9, name=name, artist="A", year=1999) def test_window_tags_each_song_as_it_lands(qapp): from lintunes.gui import shazam_import as ui songs = [_song("a", "Alpha"), _song("b", "Beta"), _song("c", "Gamma"), _song("d", "Delta")] window = ui.ShazamImportWindow(songs) applied = [] window.apply_tags.connect(lambda tid, f: applied.append((tid, f))) assert window.status("a") == ui.WAITING assert window._stack.currentIndex() == 0 # placeholder window.set_stage("a", "Downloading 40%") window.imported("a", Track(track_id=1, name="Alpha")) assert window.status("a") == ui.IDENTIFYING window.identified("a", [_cand("Alpha!")]) assert window._stack.currentIndex() == 1 and window.status("a") == \ ui.TAGGING # A second song queues behind the one being tagged. window.imported("b", Track(track_id=2, name="Beta")) window.identified("b", [_cand("Beta!")]) assert window.status("b") == ui.READY and window.waiting_to_tag() == 2 # Nothing to propose: Shazam's tags stand. window.imported("c", Track(track_id=3, name="Gamma")) window.identified("c", []) assert window.status("c") == ui.KEPT window._apply_tags() assert applied == [(1, {"name": "Alpha!", "artist": "A", "year": 1999})] assert window.status("a") == ui.TAGGED and window.status("b") == \ ui.TAGGING window._keep_tags() assert window.status("b") == ui.KEPT and window.waiting_to_tag() == 0 assert window._stack.currentIndex() == 0 window.failed("d", "nothing found") window.finish(cancelled=False) assert window.status("d") == "Couldn't import: nothing found" assert window._summary.text() == ("Done: 3 of 4 songs imported · " "1 tagged · 1 couldn't be") def test_clicking_a_ready_row_tags_it_now(qapp): from lintunes.gui import shazam_import as ui window = ui.ShazamImportWindow([_song("a"), _song("b")]) for rid, tid in (("a", 1), ("b", 2)): window.imported(rid, Track(track_id=tid, name=rid)) window.identified(rid, [_cand(rid.upper())]) window._on_row_clicked(window._rows["b"], 0) assert window.status("b") == ui.TAGGING and window.status("a") == ui.READY window._keep_tags() assert window.status("a") == ui.TAGGING # -------------------------------------------------------------------------- # F. through the main window @pytest.fixture def window(qapp, tmp_path, fake, monkeypatch): from lintunes.gui import main_window as mw library = Library(music_folder=str(tmp_path / "media")) (tmp_path / "media").mkdir() manager = LibraryManager(library, tmp_path / "data") win = mw.MainWindow(manager, Preferences(tmp_path / "data")) monkeypatch.setattr(mw.andtunes_install, "adb_path", lambda: "/bin/adb") monkeypatch.setattr(shazam, "look_up", lambda song: None) monkeypatch.setattr(shazam, "legacy_handled", lambda: set()) yield win if win._url_worker is not None: win._url_worker.cancel() win.close() def test_import_shazams_end_to_end(window, qapp, monkeypatch): from lintunes.gui import main_window as mw songs = [_song("a", "Alpha", "A"), _song("n", "Nowhere", "Z"), _song("x", "Unwanted", "X")] class _Preview: def __init__(self, reader, handled, tracks, description, parent): pass def exec(self): return True def chosen(self): return songs[:2] def passed_over(self): return ["x"] def add_to_playlist(self): return False monkeypatch.setattr(mw, "ShazamPreviewDialog", _Preview) class _Identifier(QObject): finished = pyqtSignal(object) def identify(self, track, key): self.finished.emit({"track_id": track.track_id, "candidates": [ _cand("Alpha (AcoustID)"), IdentifyCandidate(score=0, source="filename", name="junk")]}) monkeypatch.setattr(mw, "TrackIdentifier", _Identifier) window._import_shazams() assert shazam.handled_ids(window._prefs) == {"x"} shazam_window = window._shazam_window assert shazam_window.isVisible() _pump(qapp, lambda: not window._url_worker.busy() and shazam_window._done) library = window._manager.library assert len(library.tracks) == 1 # Imported → remembered; not found → offered again next time. assert shazam.handled_ids(window._prefs) == {"x", "a"} assert shazam_window.status("a") == "Tagging below ▾" # Only the database's match is proposed, never the YouTube filename. assert shazam_window._panel._combo.count() == 1 shazam_window._apply_tags() [track] = library.tracks.values() assert track.name == "Alpha (AcoustID)" assert shazam_window.status("n").startswith("Couldn't import")