diff --git a/CLAUDE.md b/CLAUDE.md index 7dbf3f7..487c6eb 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -727,6 +727,28 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal cancel `_url_batch` on teardown, or a late lookup opens a modal dialog in the next test. +- **`lintunes/shazam.py`** + **`gui/shazam_import.py`** — Connections › + Import Shazams… (Round 103), the `/shazam-import` skill inside the app. + Shazam's `library.db` is only reachable through **root adbd** + (`ShazamReader`: `adb root`, pull the db **and its -wal** — the newest + Shazams live there — then `adb unroot` if it was the one that rooted). + Which Shazams are dealt with is the synced pref `shazam_import.handled` + (request id → date; the skill's own state file counts too): imported, + unticked in the preview, or already in the library (`match_library`) — + never a failure, so that comes back next time. Picking the result: + `not_the_song` rejects live/cover/karaoke/instrumental/clean/vinyl/ + turntable/sped-up/remix… unless Shazam's title says it too; then Apple's + length (iTunes Search, never a live album or a hits collection) rejects + anything >20 s off, and Topic > the artist's own channel > a stranger's + "(Official Audio)" (measured: the other order picked fan uploads over + Kendrick's and Outkast's own). `ShazamImportWorker` is a + `UrlImportWorker` (it owns `_url_worker`/`_url_progress`, so busy/cancel/ + quit come free) narrating `stage` per song — the `LTPP` postprocess + marker is what says "Converting". `ShazamImportWindow` is the song list + *and* an embedded `identify_dialog.IdentifyPanel` per song as soon as it's + imported (its own one-at-a-time `TrackIdentifier` queue; filename + candidates are dropped, since the file already wears Shazam's tags). + - **`lintunes/mpris.py`** — registers `org.mpris.MediaPlayer2.lintunes` over D-Bus so the desktop's media keys / now-playing popup control playback. Spacebar and arrow keys are handled locally via `MainWindow.eventFilter`, which **ignores diff --git a/lintunes/__init__.py b/lintunes/__init__.py index e43a5f7..ffdc20b 100644 --- a/lintunes/__init__.py +++ b/lintunes/__init__.py @@ -1,3 +1,3 @@ """LinTunes — iTunes-style music library manager and player for Linux.""" -__version__ = "0.72.1" +__version__ = "0.73.0" diff --git a/lintunes/gui/identify_dialog.py b/lintunes/gui/identify_dialog.py index 1e11ae6..236351b 100644 --- a/lintunes/gui/identify_dialog.py +++ b/lintunes/gui/identify_dialog.py @@ -6,10 +6,13 @@ dropdown of alternate candidates (other albums/releases the recording appears on). Passive like AlbumArtDialog: candidates arrive pre-parsed, nothing is written until the user accepts, and the caller applies ``result_fields()`` through LibraryManager afterwards. + +The proposal itself is ``IdentifyPanel``, a plain widget, so Import Shazams +can show it inside its own window while songs are still downloading. """ from PyQt6.QtWidgets import ( QCheckBox, QComboBox, QDialog, QDialogButtonBox, QGridLayout, QLabel, - QLineEdit, QPushButton, QSpinBox, QVBoxLayout, + QLineEdit, QPushButton, QSpinBox, QVBoxLayout, QWidget, ) # (field, label, is_number, spinbox max) @@ -25,16 +28,16 @@ _FIELDS = [ ] -class IdentifyDialog(QDialog): - def __init__(self, track, candidates, remaining: int = 0, parent=None): +class IdentifyPanel(QWidget): + """One track's proposals: the candidate drop-down and the field grid.""" + + def __init__(self, track, candidates, parent=None): super().__init__(parent) - self.setWindowTitle("Identify Track") self._track = track self._candidates = candidates - # True when the user pressed "Stop Identifying" on a queued batch. - self.cancel_all = False layout = QVBoxLayout(self) + layout.setContentsMargins(0, 0, 0, 0) self._header = QLabel() self._header.setWordWrap(True) layout.addWidget(self._header) @@ -78,20 +81,6 @@ class IdentifyDialog(QDialog): grid.addWidget(editor, row, 3) layout.addLayout(grid) - buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok - | QDialogButtonBox.StandardButton.Cancel) - buttons.button(QDialogButtonBox.StandardButton.Ok).setText( - "Apply Tags") - if remaining > 0: - noun = "track" if remaining == 1 else "tracks" - stop_btn = QPushButton(f"Stop Identifying ({remaining} {noun} left)") - buttons.addButton(stop_btn, - QDialogButtonBox.ButtonRole.ActionRole) - stop_btn.clicked.connect(self._stop_all) - buttons.accepted.connect(self.accept) - buttons.rejected.connect(self.reject) - layout.addWidget(buttons) - self._combo.currentIndexChanged.connect(self._fill_from_candidate) self._fill_from_candidate(0) @@ -134,10 +123,6 @@ class IdentifyDialog(QDialog): check.setChecked(differs) editor.setEnabled(differs) - def _stop_all(self): - self.cancel_all = True - self.reject() - # ---- result ---- def result_fields(self) -> dict: @@ -153,3 +138,44 @@ class IdentifyDialog(QDialog): if value: fields[field] = value return fields + + +class IdentifyDialog(QDialog): + """``IdentifyPanel`` as its own window, with Apply / Cancel and, for a + queue, Stop Identifying.""" + + def __init__(self, track, candidates, remaining: int = 0, parent=None): + super().__init__(parent) + self.setWindowTitle("Identify Track") + # True when the user pressed "Stop Identifying" on a queued batch. + self.cancel_all = False + + layout = QVBoxLayout(self) + self.panel = IdentifyPanel(track, candidates, self) + layout.addWidget(self.panel) + # What the tests reach into. + self._header = self.panel._header + self._combo = self.panel._combo + self._checks = self.panel._checks + self._editors = self.panel._editors + + buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok + | QDialogButtonBox.StandardButton.Cancel) + buttons.button(QDialogButtonBox.StandardButton.Ok).setText( + "Apply Tags") + if remaining > 0: + noun = "track" if remaining == 1 else "tracks" + stop_btn = QPushButton(f"Stop Identifying ({remaining} {noun} left)") + buttons.addButton(stop_btn, + QDialogButtonBox.ButtonRole.ActionRole) + stop_btn.clicked.connect(self._stop_all) + buttons.accepted.connect(self.accept) + buttons.rejected.connect(self.reject) + layout.addWidget(buttons) + + def _stop_all(self): + self.cancel_all = True + self.reject() + + def result_fields(self) -> dict: + return self.panel.result_fields() diff --git a/lintunes/gui/main_window.py b/lintunes/gui/main_window.py index cb7a5ee..5f8f14e 100644 --- a/lintunes/gui/main_window.py +++ b/lintunes/gui/main_window.py @@ -10,7 +10,8 @@ from PyQt6.QtWidgets import ( from PyQt6.QtCore import QRect, Qt, QEvent, QObject, QTimer, pyqtSignal from PyQt6.QtGui import QAction, QKeySequence -from lintunes import bpm_detect, device_sync, loudness, music_folder, theme, url_import +from lintunes import (bpm_detect, device_sync, loudness, music_folder, shazam, + theme, url_import) from lintunes.andtunes import layout as andtunes_layout from lintunes.andtunes import install as andtunes_install from lintunes.andtunes import sync as andtunes_sync @@ -45,6 +46,7 @@ from lintunes.gui.playlist_view import PlaylistView from lintunes.gui.transport import TransportBar from lintunes.gui.url_import_dialog import UrlImportDialog from lintunes.gui.url_import_progress import UrlImportProgress +from lintunes.gui.shazam_import import ShazamImportWindow, ShazamPreviewDialog from lintunes.gui import web_upload_progress as web_progress from lintunes.gui.info_dialog import InfoDialog from lintunes.gui.music_folder_dialog import MusicFolderDialog @@ -282,6 +284,12 @@ class MainWindow(QMainWindow): # Several songs are looked up quietly while they download, and # reviewed together once the last one has landed. self._url_batch = None # identify_batch.IdentifyCollector + # Import Shazams runs as a URL import (it owns _url_worker and + # _url_progress), with its own window and its own lookup queue, so + # Identify proposals appear in that window as songs land. + self._shazam_window = None + self._shazam_identify: list = [] # (request id, track) to look up + self._shazam_identifier = None # the TrackIdentifier running # Popups waiting for LinTunes to be the app in front (they must # never land on top of another app). self._popups_waiting: list = [] @@ -531,6 +539,8 @@ class MainWindow(QMainWindow): self._install_action = self._add_action( connections_menu, "Install andTunes on Rabbit…", "", self._install_andtunes) + self._shazam_action = self._add_action( + connections_menu, "Import Shazams…", "", self._import_shazams) # "Sync Playlist to Rabbit (Auxio)" was retired in Round 51, once # andTunes could play what it syncs. device_sync's helpers stay: the # export and andTunes code import them. @@ -612,6 +622,7 @@ class MainWindow(QMainWindow): andtunes_install.bundled_apk() is not None and not (self._install_worker is not None and self._install_worker.busy())) + self._shazam_action.setEnabled(self._busy_worker() is None) # The self-updater ships code but not dependencies, so a machine that # has pulled this round hasn't necessarily installed pychromecast (or @@ -1696,6 +1707,7 @@ class MainWindow(QMainWindow): self._url_progress.close() self._url_progress.deleteLater() self._url_progress = None + self._shazam_window = None if len(chosen) > 1: self._url_progress = UrlImportProgress(chosen, self) self._url_progress.stop_requested.connect( @@ -1898,6 +1910,174 @@ class MainWindow(QMainWindow): self, "Import from URL", f"Couldn't import from that link:\n\n{message}")) + # ---- Import Shazams (Connections menu) ---- + + def _import_shazams(self): + if self._busy_worker() is not None: + self.statusBar().showMessage( + "Wait for the current transfer to finish first", 6000) + return + missing = [name for name, ok in ( + ("adb", andtunes_install.adb_path() is not None), + ("yt-dlp", url_import.ytdlp_available()), + ("ffmpeg", exporter.web_support.ffmpeg_available())) if not ok] + if missing: + QMessageBox.warning( + self, "Import Shazams", + f"Importing Shazams needs {', '.join(missing)}, which " + f"{'is' if len(missing) == 1 else 'are'} not installed.\n\n" + "adb: Fedora's “android-tools” package\n" + "yt-dlp: pip install --user yt-dlp (or your distro's " + "package)\nffmpeg: your distro's “ffmpeg” package") + return + target = self._url_import_target() + handled = shazam.handled_ids(self._prefs) | shazam.legacy_handled() + dialog = ShazamPreviewDialog( + shazam.ShazamReader(), handled, + self._manager.library.tracks.values(), + target.description if target else None, self) + if not dialog.exec() or self._ensure_music_dir() is None: + return + # Unticked and already-owned songs are dealt with now; the others + # only once they're imported, so a failure is offered again. + shazam.mark_handled(self._prefs, dialog.passed_over()) + chosen = dialog.chosen() + if not chosen: + return + self._url_target = target if dialog.add_to_playlist() else None + self._url_inserted = 0 + self._url_imported = 0 + self._url_identify = True + self._url_note = "" + if self._url_batch is not None: + self._url_batch.cancel() + self._url_batch = None + self._shazam_identify = [] + + worker = shazam.ShazamImportWorker( + chosen, self, + cookies_browser=load_config().get(url_import.COOKIES_KEY)) + if self._url_progress is not None: + self._url_progress.close() + self._url_progress.deleteLater() + window = ShazamImportWindow(chosen, self) + window.stop_requested.connect(self._confirm_cancel_url_import) + window.apply_tags.connect(self._apply_shazam_tags) + self._url_progress = self._shazam_window = window + worker.stage.connect(window.set_stage) + worker.item_failed.connect(window.failed) + worker.cookies_used.connect(self._remember_cookies_browser) + worker.item_started.connect(self._on_shazam_started) + worker.progress.connect(self._sync_progress.setValue) + worker.landed.connect(self._on_shazam_landed) + worker.finished.connect(self._on_shazam_finished) + worker.failed.connect(self._on_shazam_failed) + self._url_worker = worker + self._sync_label.setText("Importing Shazams…") + self._sync_progress.setRange(0, 100) + self._sync_progress.setValue(0) + self._sync_cancel.show() + self._sync_label.show() + self._sync_progress.show() + window.show() + worker.start() + + def _on_shazam_started(self, index: int, count: int, _rid: str, + title: str): + short = title if len(title) <= 40 else title[:39] + "…" + self._sync_label.setText(f"Shazam {index} of {count} · {short}") + self._sync_progress.setValue(0) + + def _on_shazam_landed(self, rid: str, path: str): + """One song is downloaded and wearing Shazam's tags: import it, then + look it up for the window's tagging panel.""" + window = self._shazam_window + if window is not None: + window.importing(rid) + target = self._url_target + pid, position = "", None + if target is not None and target.pid in self._manager.library.playlists: + pid = target.pid + if target.position is not None: + position = target.position + self._url_inserted + imported = self.import_files([Path(path)], pid, position) + if pid: + self._url_inserted += len(imported) + self._url_imported += len(imported) + if imported: + shazam.mark_handled(self._prefs, [rid]) + if window is not None: + window.imported(rid, imported[0] if imported else None) + if imported: + self._shazam_identify.append((rid, imported[0])) + if self._shazam_identifier is None: + self._shazam_identify_next() + + def _shazam_identify_next(self): + """One lookup at a time (AcoustID allows three a second).""" + self._shazam_identifier = None + while self._shazam_identify: + rid, track = self._shazam_identify.pop(0) + if track.track_id in self._manager.library.tracks: + break + else: + return + window = self._shazam_window + identifier = TrackIdentifier() + identifier.finished.connect( + lambda result, r=rid, w=window: + self._on_shazam_identified(w, r, result)) + self._shazam_identifier = identifier + identifier.identify(track, + self._prefs.acoustid.get("api_key", "").strip()) + + def _on_shazam_identified(self, window, rid: str, result: dict): + # The file already carries Shazam's own title and artist, which beat + # anything read off a YouTube upload's name: only real matches. + candidates = [c for c in result.get("candidates", []) + if c.source != "filename"] + if window is not None and window is self._shazam_window: + window.identified(rid, candidates) + self._shazam_identify_next() + + def _apply_shazam_tags(self, track_id: int, fields: dict): + if track_id in self._manager.library.tracks: + # The edit funnel writes the tags, may relocate the file and + # records one undo step. + self._manager.edit_track_fields(track_id, fields) + + def _on_shazam_finished(self, summary: dict): + self._finish_url_import() + window = self._shazam_window + if window is not None: + window.finish(summary["cancelled"]) + if window.waiting_to_tag() and not window.isVisible(): + # Hidden mid-run, and the status-bar line that brought it + # back is gone: songs still waiting to be tagged reopen it. + self._when_app_active(window.show) + count = self._url_imported + if summary["cancelled"]: + msg = f"Shazam import stopped — {count} song(s) imported" + else: + msg = f"Imported {count} Shazam(s)" + sounds.play_done(self._prefs) + target = self._url_target + playlist = (self._manager.library.playlists.get(target.pid) + if target is not None else None) + if playlist is not None and count: + msg += f" into “{playlist.name}”" + if summary["errors"]: + msg += f" · {len(summary['errors'])} couldn't be found" + self.statusBar().showMessage(msg, 12000) + + def _on_shazam_failed(self, message: str): + self._finish_url_import() + if self._shazam_window is not None: + self._shazam_window.finish(False) + self._when_app_active(lambda: QMessageBox.warning( + self, "Import Shazams", + f"Couldn't import the Shazams:\n\n{message}")) + def _confirm_cancel_url_import(self): box = QMessageBox(self) box.setWindowTitle("Stop downloading") diff --git a/lintunes/gui/shazam_import.py b/lintunes/gui/shazam_import.py new file mode 100644 index 0000000..aafc97b --- /dev/null +++ b/lintunes/gui/shazam_import.py @@ -0,0 +1,390 @@ +"""The two windows of Connections ▸ Import Shazams… (Round 103). + +``ShazamPreviewDialog`` reads the Rabbit's Shazams (``shazam.ShazamReader``) +and lists the ones not dealt with yet, ticked, with those the library already +has below them, unticked. Import takes the ticked ones, and "Add to the +current playlist" uses the same rule as Import from URL +(``url_import.resolve_target``). + +``ShazamImportWindow`` is the whole run in one place: every song with where +it's up to (looking up, searching, downloading, converting, tagging, +importing, identifying), and below them the Identify proposal for each song +as soon as it's imported — so tagging starts while the rest download. +Non-modal; Hide only hides it, and the status-bar line brings it back. +""" +from PyQt6.QtCore import Qt, pyqtSignal +from PyQt6.QtWidgets import ( + QCheckBox, QDialog, QDialogButtonBox, QGroupBox, QHBoxLayout, + QHeaderView, QLabel, QPushButton, QSplitter, QStackedWidget, + QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget, +) + +from lintunes import shazam +from lintunes.gui.identify_dialog import IdentifyPanel + +ROLE_SONG = Qt.ItemDataRole.UserRole + + +def _when(song) -> str: + return song.when.strftime("%Y-%m-%d %H:%M") + + +class ShazamPreviewDialog(QDialog): + def __init__(self, reader, handled: set, tracks, + target_description: str | None, parent=None): + super().__init__(parent) + self.setWindowTitle("Import Shazams") + self.resize(720, 520) + self._handled = set(handled) + self._tracks = list(tracks) + self._reader = reader + + layout = QVBoxLayout(self) + self._status = QLabel("Reading Shazams from the Rabbit…") + self._status.setWordWrap(True) + layout.addWidget(self._status) + + self._tree = QTreeWidget() + self._tree.setColumnCount(3) + self._tree.setHeaderLabels(["Song", "Shazamed", "Note"]) + self._tree.setRootIsDecorated(False) + self._tree.setUniformRowHeights(True) + header = self._tree.header() + header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch) + header.setSectionResizeMode(1, QHeaderView.ResizeMode.ResizeToContents) + header.setSectionResizeMode(2, QHeaderView.ResizeMode.ResizeToContents) + header.setStretchLastSection(False) + self._tree.itemChanged.connect(self._refresh_button) + layout.addWidget(self._tree, 1) + + note = QLabel("Unticked songs won't be offered again. A song that " + "can't be found or downloaded will be.") + note.setEnabled(False) # the greyed-hint idiom + note.setWordWrap(True) + layout.addWidget(note) + + # Import from URL's checkbox, the same way round. + self._add = QCheckBox("and add to current playlist?") + layout.addWidget(self._add) + hint = QLabel(target_description + or "Select or play a playlist to add songs to it.") + hint.setEnabled(False) + hint.setWordWrap(True) + hint.setContentsMargins(22, 0, 0, 0) + layout.addWidget(hint) + if target_description is None: + self._add.setEnabled(False) + else: + self._add.setChecked(True) + + self._buttons = QDialogButtonBox( + QDialogButtonBox.StandardButton.Ok + | QDialogButtonBox.StandardButton.Cancel) + self._ok = self._buttons.button(QDialogButtonBox.StandardButton.Ok) + self._ok.setEnabled(False) + self._ok.setText("Import") + self._buttons.accepted.connect(self.accept) + self._buttons.rejected.connect(self.reject) + layout.addWidget(self._buttons) + + reader.status.connect(self._status.setText) + reader.read.connect(self._on_read) + reader.failed.connect(self._on_failed) + reader.start() + + # ---- the reader ---- + + def _on_read(self, songs: list): + fresh = [s for s in songs if s.request_id not in self._handled] + shazam.match_library(fresh, self._tracks) + new = [s for s in fresh if not s.in_library] + owned = [s for s in fresh if s.in_library] + self._tree.blockSignals(True) + for song in new + owned: + item = QTreeWidgetItem([song.label(), _when(song), + f"In your library: {song.in_library}" + if song.in_library else "New"]) + item.setData(0, ROLE_SONG, song) + item.setToolTip(0, song.label()) + item.setFlags(item.flags() | Qt.ItemFlag.ItemIsUserCheckable) + item.setCheckState(0, Qt.CheckState.Unchecked if song.in_library + else Qt.CheckState.Checked) + self._tree.addTopLevelItem(item) + self._tree.blockSignals(False) + if not fresh: + earlier = len(songs) + self._status.setText( + "No new Shazams since the last import" + + (f" ({earlier} earlier ones already dealt with)." + if earlier else ".")) + else: + text = f"{len(new)} new Shazam{'' if len(new) == 1 else 's'}" + if owned: + text += (f", and {len(owned)} you already have (unticked)") + self._status.setText(text + ".") + self._refresh_button() + + def _on_failed(self, message: str): + self._status.setText(message) + + def _items(self): + return [self._tree.topLevelItem(i) + for i in range(self._tree.topLevelItemCount())] + + def _refresh_button(self, *_): + count = len(self.chosen()) + self._ok.setEnabled(count > 0 or bool(self._items())) + self._ok.setText(f"Import {count} Song{'' if count == 1 else 's'}" + if count else "Mark as Dealt With" + if self._items() else "Import") + + # ---- the answer ---- + + def chosen(self) -> list: + return [item.data(0, ROLE_SONG) for item in self._items() + if item.checkState(0) == Qt.CheckState.Checked] + + def passed_over(self) -> list[str]: + """Ids of the songs left unticked, never to be offered again.""" + return [item.data(0, ROLE_SONG).request_id for item in self._items() + if item.checkState(0) != Qt.CheckState.Checked] + + def add_to_playlist(self) -> bool: + return self._add.isEnabled() and self._add.isChecked() + + +# Row statuses the window itself sets (the worker narrates the rest). +WAITING = "Waiting" +IMPORTING = "Importing…" +IDENTIFYING = "Identifying track…" +READY = "Ready to tag ▸" +TAGGING = "Tagging below ▾" +TAGGED = "Tagged" +KEPT = "Imported with Shazam's tags" +NOT_IMPORTED = "Downloaded, but couldn't be imported" +SKIPPED = "Skipped" + + +class ShazamImportWindow(QDialog): + stop_requested = pyqtSignal() + apply_tags = pyqtSignal(int, dict) # track id, fields + + def __init__(self, songs: list, parent=None): + super().__init__(parent) + self.setWindowTitle("Importing Shazams") + self.setModal(False) + self.resize(780, 720) + self._rows: dict[str, QTreeWidgetItem] = {} + self._tracks: dict[str, object] = {} + self._candidates: dict[str, list] = {} + self._pending: list[str] = [] # ids ready to tag, in order + self._current = "" # the id being tagged below + self._panel: IdentifyPanel | None = None + self._done = False + + layout = QVBoxLayout(self) + self._summary = QLabel() + layout.addWidget(self._summary) + + split = QSplitter(Qt.Orientation.Vertical) + self._tree = QTreeWidget() + self._tree.setColumnCount(2) + self._tree.setHeaderLabels(["Song", "Status"]) + self._tree.setRootIsDecorated(False) + self._tree.setUniformRowHeights(True) + header = self._tree.header() + header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch) + header.setSectionResizeMode(1, QHeaderView.ResizeMode.Interactive) + header.resizeSection(1, 230) + header.setStretchLastSection(False) + for song in songs: + item = QTreeWidgetItem([song.label(), WAITING]) + item.setToolTip(0, f"{song.label()}\nShazamed {_when(song)}") + self._tree.addTopLevelItem(item) + self._rows[song.request_id] = item + self._tree.itemClicked.connect(self._on_row_clicked) + split.addWidget(self._tree) + + box = QGroupBox("Tag the imported songs") + box_layout = QVBoxLayout(box) + self._stack = QStackedWidget() + self._placeholder = QLabel( + "Each song shows up here to tag as soon as it's imported.") + self._placeholder.setAlignment(Qt.AlignmentFlag.AlignCenter) + self._placeholder.setEnabled(False) + self._placeholder.setWordWrap(True) + self._stack.addWidget(self._placeholder) + page = QWidget() + page_layout = QVBoxLayout(page) + page_layout.setContentsMargins(0, 0, 0, 0) + self._panel_slot = QVBoxLayout() + page_layout.addLayout(self._panel_slot) + page_layout.addStretch(1) + row = QHBoxLayout() + self._queue_note = QLabel() + self._queue_note.setEnabled(False) + row.addWidget(self._queue_note, 1) + self._keep = QPushButton("Keep Shazam's Tags") + self._keep.setAutoDefault(False) # Return applies + self._keep.clicked.connect(self._keep_tags) + self._apply = QPushButton("Apply Tags") + self._apply.setDefault(True) + self._apply.clicked.connect(self._apply_tags) + row.addWidget(self._keep) + row.addWidget(self._apply) + page_layout.addLayout(row) + self._stack.addWidget(page) + box_layout.addWidget(self._stack) + split.addWidget(box) + split.setStretchFactor(0, 1) + split.setStretchFactor(1, 1) + layout.addWidget(split, 1) + + buttons = QHBoxLayout() + buttons.addStretch(1) + self._stop = QPushButton("Stop") + self._stop.clicked.connect(self.stop_requested) + self._hide = QPushButton("Hide") + self._hide.setToolTip("The import carries on. Click the progress in " + "the status bar to see this again.") + self._hide.clicked.connect(self.close) + buttons.addWidget(self._stop) + buttons.addWidget(self._hide) + layout.addLayout(buttons) + self._refresh_summary() + + # ---- fed by MainWindow ---- + + def status(self, rid: str) -> str: + item = self._rows.get(rid) + return item.text(1) if item is not None else "" + + def set_stage(self, rid: str, text: str): + item = self._rows.get(rid) + if item is None: + return + if item.text(1) == WAITING: + self._tree.scrollToItem(item) + item.setText(1, text) + item.setToolTip(1, text) + self._refresh_summary() + + def failed(self, rid: str, reason: str): + self.set_stage(rid, f"Couldn't import: {reason}") + + def importing(self, rid: str): + self.set_stage(rid, IMPORTING) + + def imported(self, rid: str, track, identifying: bool = True): + if track is None: + self.set_stage(rid, NOT_IMPORTED) + return + self._tracks[rid] = track + self.set_stage(rid, IDENTIFYING if identifying else KEPT) + + def identified(self, rid: str, candidates: list): + """The lookup is back. Nothing worth proposing leaves Shazam's tags + on; anything else queues for the panel.""" + if rid not in self._tracks: + return + if not candidates: + self.set_stage(rid, KEPT) + return + self._candidates[rid] = candidates + self._pending.append(rid) + self.set_stage(rid, READY) + if not self._current: + self._show_next() + else: + self._refresh_queue_note() + + def finish(self, cancelled: bool): + self._done = True + leftover = SKIPPED if cancelled else "Not found" + for rid, item in self._rows.items(): + text = item.text(1) + if text == WAITING or (rid not in self._tracks and not + text.startswith(("Couldn't", + "Downloaded"))): + self.set_stage(rid, leftover) + self._stop.setEnabled(False) + self._hide.setText("Close") + self._refresh_summary() + + def waiting_to_tag(self) -> int: + return len(self._pending) + (1 if self._current else 0) + + # ---- the tagging panel ---- + + def _show(self, rid: str): + if self._current and self._current != rid: + # The one on screen goes back in the queue, at the front. + self._pending.insert(0, self._current) + self.set_stage(self._current, READY) + if rid in self._pending: + self._pending.remove(rid) + self._current = rid + if self._panel is not None: + self._panel.setParent(None) + self._panel.deleteLater() + self._panel = IdentifyPanel(self._tracks[rid], self._candidates[rid]) + self._panel_slot.addWidget(self._panel) + self._stack.setCurrentIndex(1) + self.set_stage(rid, TAGGING) + self._refresh_queue_note() + + def _show_next(self): + self._current = "" + if self._pending: + self._show(self._pending[0]) + return + if self._panel is not None: + self._panel.setParent(None) + self._panel.deleteLater() + self._panel = None + self._placeholder.setText( + "All imported songs are tagged." if self._done else + "Each song shows up here to tag as soon as it's imported.") + self._stack.setCurrentIndex(0) + + def _refresh_queue_note(self): + n = len(self._pending) + self._queue_note.setText(f"{n} more waiting" if n else "") + + def _on_row_clicked(self, item, _column): + for rid, row in self._rows.items(): + if row is item and rid in self._pending: + self._show(rid) + return + + def _apply_tags(self): + rid = self._current + if not rid or self._panel is None: + return + fields = self._panel.result_fields() + if fields: + self.apply_tags.emit(self._tracks[rid].track_id, fields) + self.set_stage(rid, TAGGED if fields else KEPT) + self._show_next() + + def _keep_tags(self): + if self._current: + self.set_stage(self._current, KEPT) + self._show_next() + + def _refresh_summary(self): + statuses = [item.text(1) for item in self._rows.values()] + total = len(statuses) + imported = len(self._tracks) + tagged = statuses.count(TAGGED) + problems = sum(1 for s in statuses + if s.startswith(("Couldn't", "Downloaded")) + or s == "Not found") + text = f"{imported} of {total} songs imported" + if tagged: + text += f" · {tagged} tagged" + if problems: + text += f" · {problems} couldn't be" + if self._done: + text = "Done: " + text + self._summary.setText(text) diff --git a/lintunes/preferences.py b/lintunes/preferences.py index 609e7ce..d394329 100644 --- a/lintunes/preferences.py +++ b/lintunes/preferences.py @@ -78,6 +78,12 @@ DEFAULTS = { "folder_ids": [], "copy_art": True, }, + # Connections ▸ Import Shazams: the Rabbit's Shazams already dealt with + # (imported, unticked, or already owned), request id → date. Synced, so + # the other desktop doesn't offer them again either. + "shazam_import": { + "handled": {}, + }, # Which machine runs friend sharing (Cassette): {machine_id, hostname}. # Synced so the other machine can name it; see lintunes/cassette/host.py. "cassette_host": None, diff --git a/lintunes/shazam.py b/lintunes/shazam.py new file mode 100644 index 0000000..27593ce --- /dev/null +++ b/lintunes/shazam.py @@ -0,0 +1,724 @@ +"""Connections ▸ Import Shazams… (Round 103): the songs trav Shazamed on the +Rabbit, found on YouTube and imported, ported from the ``/shazam-import`` +skill (``~/.claude/skills/shazam-import``). + +Shazam keeps its history in ``library.db`` inside its app sandbox. MTP +doesn't show it, ``run-as`` is refused (the app isn't debuggable) and +``adb backup`` is refused (no allowBackup). The R1's ROM is a userdebug GSI, +so root adbd is the way in: ``ShazamReader`` runs ``adb root``, pulls the +database **and its -wal** (the newest Shazams sit in the write-ahead log and +are invisible without it), and puts adbd back with ``adb unroot`` if it was +the one that rooted it. + +Which Shazams have been dealt with lives in the synced preferences +(``shazam_import.handled``, request id → date), so the other desktop doesn't +offer them again either. A song is handled once it's imported, unticked on +purpose, or already in the library; one that failed to download is not, so +it comes back next time. + +Picking the YouTube result is the point of the feature. "The original song" +means: not live, not a cover, not karaoke or an instrumental, not a clean +(censored) edit, not a vinyl rip filmed on a turntable, not sped up, slowed +or remixed — unless the Shazam title itself says so. Every such word rejects +a result outright. Then **length** decides: the iTunes Search API (no key, +``art_search``'s endpoint) knows the recording's canonical duration, and a +result that disagrees by more than 20 s is another take. A live album must +never supply that length, or the filter steers toward the concert. Among the +survivors, a ``- Topic`` channel (YouTube's own audio-only upload of the +release) beats the artist's own channel beats a stranger's "(Official +Audio)", and a music video, which often carries a skit, counts against. +Measured on real searches (Round 103): without the artist's channel ahead +of "Audio", fan uploads beat Kendrick's and Outkast's own. + +Shaped like ``spotify_link``: pure helpers first (tested against a synthetic +database and canned results), then adb and the network. +""" +import json +import logging +import os +import re +import shutil +import sqlite3 +import subprocess +import tempfile +import threading +import time +import unicodedata +from dataclasses import dataclass, field +from datetime import datetime +from pathlib import Path + +import requests +from PyQt6.QtCore import QObject, pyqtSignal + +from lintunes import tagging, url_import + +log = logging.getLogger(__name__) + +DEVICE_DB = "/data/user/0/com.shazam.android/databases/library.db" +ITUNES_SEARCH = "https://itunes.apple.com/search" +ITUNES_LOOKUP = "https://itunes.apple.com/lookup" +USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64)" +TIMEOUT = 20 + +# YouTube results listed per search. +SEARCH_RESULTS = 8 +# Results downloaded per song before giving up on it. +TRIES = 3 +# A downloaded file this far from its listed length was cut short. +TRUNCATED_SLACK = 20 + +# The preferences key: {"handled": {request id: "YYYY-MM-DD"}}. +PREFS_KEY = "shazam_import" + +# Where the /shazam-import skill kept the ones it fetched. Read once, so the +# first in-app import doesn't offer them all again. +LEGACY_STATE = Path(os.environ.get( + "XDG_STATE_HOME", str(Path.home() / ".local" / "state"))) \ + / "lintunes-shazam-import" / "state.json" + + +@dataclass +class ShazamSong: + request_id: str + when: datetime + title: str + artist: str = "" + year: int | None = None + genre: str = "" + upc: str = "" # the release barcode in the artwork URL + in_library: str = "" # "Artist — Title" of a library match + # Filled in by the lookup: Apple's length and album for the recording. + canon_secs: int | None = None + album: str = "" + album_artist: str = "" + album_confident: bool = False + + def label(self) -> str: + return f"{self.artist} — {self.title}" if self.artist else self.title + + +def fold(text: str) -> str: + text = unicodedata.normalize("NFC", text or "").casefold() + return " ".join(re.sub(r"[^\w\s]+", " ", text).split()) + + +_STOP = {"the", "a", "an", "of", "and", "version", "original", "feat"} + + +def toks(text: str) -> set[str]: + return {w for w in fold(text).split() if w not in _STOP and len(w) > 1} + + +# ---- reading Shazam's database ---- + +def _year(text) -> int | None: + m = re.match(r"\s*(\d{4})", str(text or "")) + return int(m.group(1)) if m else None + + +def read_shazams(db_path) -> list[ShazamSong]: + """Every Shazam with a title, newest first. A Shazam of the same song + twice is two rows; ``dedupe`` keeps the newest.""" + db = sqlite3.connect(str(db_path)) + try: + rows = db.execute(""" + select t.request_id, t.timestamp, tr.track_key, tr.track_title, + tr.release_date, tr.cover_art + from tag t left join track tr on tr.track_key = t.track_key + order by t.timestamp desc""").fetchall() + artists: dict[str, list[str]] = {} + for key, name in db.execute(""" + select aat.track_key, a.artist_name + from apple_artist_track aat + join artist a on a.artist_id = aat.artist_adam_id"""): + artists.setdefault(key, []).append(name) + genres: dict[str, list[str]] = {} + # 'mz' genres are opaque Apple ids; 'editorial' ones are words. + for key, genre in db.execute( + "select track_key, genre_id from track_genre " + "where genre_type = 'editorial'"): + genres.setdefault(key, []).append(genre) + finally: + db.close() + + songs = [] + for rid, ts, key, title, release, art in rows: + if not title: + continue + m = re.search(r"/(\d{11,13})_cover", art or "") + songs.append(ShazamSong( + request_id=str(rid), + when=datetime.fromtimestamp((ts or 0) / 1000), + title=title, + artist=", ".join(dict.fromkeys(artists.get(key) or [])), + year=_year(release), + genre=", ".join(dict.fromkeys(genres.get(key) or [])), + upc=m.group(1) if m else "")) + return songs + + +def dedupe(songs: list[ShazamSong]) -> list[ShazamSong]: + """One row per song (the newest Shazam of it), in the order given.""" + seen, out = set(), [] + for song in songs: + key = key_of(song.artist, song.title) + if key not in seen: + seen.add(key) + out.append(song) + return out + + +# ---- what's been dealt with ---- + +def handled_ids(prefs) -> set[str]: + return set((prefs.get(PREFS_KEY) or {}).get("handled", {})) + + +def mark_handled(prefs, request_ids, when: datetime | None = None): + """Add ids to the synced list, in one save.""" + data = dict(prefs.get(PREFS_KEY) or {}) + handled = dict(data.get("handled", {})) + stamp = (when or datetime.now()).strftime("%Y-%m-%d") + added = False + for rid in request_ids: + if rid not in handled: + handled[rid] = stamp + added = True + if added: + data["handled"] = handled + prefs.set(PREFS_KEY, data) + + +def legacy_handled(path: Path = LEGACY_STATE) -> set[str]: + """Ids the /shazam-import skill already fetched on this machine.""" + try: + return set(json.loads(path.read_text(encoding="utf-8")) + .get("done", {})) + except (OSError, ValueError, AttributeError): + return set() + + +# ---- already in the library? ---- + +_PARENS = re.compile(r"[\(\[]\s*(feat|ft|with|remaster|remastered|original|" + r"official|explicit|bonus|deluxe|mono|stereo|single " + r"version|album version|radio edit)\b[^\)\]]*[\)\]]", re.I) +_FEAT = re.compile(r"\s+(feat\.?|ft\.?|featuring)\s+.*$", re.I) + + +def key_of(artist: str, title: str) -> tuple[str, str]: + t = _FEAT.sub(" ", _PARENS.sub(" ", title or "")) + t = re.sub(r"^the\s+", "", fold(t)) + return fold(artist), t + + +def match_library(songs: list[ShazamSong], tracks) -> None: + """Set ``in_library`` on every song the library already has: the same + artist and title once "(feat. …)"/"(Remastered)" are stripped, or a + title sharing 80 % of its words with an artist in common.""" + exact, index = {}, [] + for t in tracks: + a, n = key_of(t.artist, t.name) + if not n: + continue + exact.setdefault((a, n), t) + index.append((toks(t.name), toks(t.artist), t)) + for song in songs: + hit = exact.get(key_of(song.artist, song.title)) + if hit is None: + nt, at = toks(song.title), toks(song.artist) + best, best_score = None, 0.0 + for ltok, latok, t in index: + if not nt or not (nt & ltok): + continue + ts = len(nt & ltok) / len(nt | ltok) + ars = (len(at & latok) / len(at | latok) + if at and latok else 0.0) + score = ts * .7 + ars * .3 + if ts >= 0.8 and (ars > 0 or not at) and score > best_score: + best, best_score = t, score + hit = best + if hit is not None: + song.in_library = (f"{hit.artist} — {hit.name}" if hit.artist + else hit.name) + + +# ---- choosing the YouTube result ---- + +# Each rejects a result whose title (or, for the first few, channel) says +# it — unless the Shazam title says so too: a Shazam of a live recording +# wants the live recording. +_NOT_THE_SONG = { + "live": r"live|concert|tiny desk|boiler room|festival|unplugged|" + r"rehearsal|soundcheck|in.studio|session|acoustic", + "cover": r"cover(ed)?( by)?|tribute|in the style of|made famous by|" + r"originally (performed|by)", + "karaoke": r"karaoke|instrumental|backing track|no vocals|" + r"without vocals|minus one|sing ?along|sing king", + "clean": r"clean( version| edit)?|censored|radio clean", + "vinyl": r"vinyl|turntable|record player|needle ?drop|phonograph|" + r"gramophone|\d{2} ?rpm", + "altered": r"sped up|speed up|slowed|reverb|nightcore|8d( audio)?|" + r"bass ?boost(ed)?|remix|mashup|lo-?fi|chopped|screwed|" + r"extended|loop|1 hour|10 hours", + "other": r"reaction|reacts?|tutorial|lesson|how to play|" + r"drum cam|guitar|piano version|full album|dj set|mix ?tape|" + r"megamix|lyrics? breakdown|explained|review|teaser|snippet|" + r"preview|short", +} +_NOT_THE_SONG_RE = {why: re.compile(rf"\b({pattern})\b", re.I) + for why, pattern in _NOT_THE_SONG.items()} +# Words about the *upload*, which a channel name can carry too. +_CHANNEL_CHECKED = ("cover", "karaoke") + +_VIDEO = re.compile(r"\b(official (music )?video|music video|mv|videoclip)\b", + re.I) +_AUDIO = re.compile(r"(official audio|full audio|\(audio\)|\[audio\]|" + r"\bvisuali[sz]er\b|\bhq audio\b|\bfull song\b|" + r"\baudio oficial\b)", re.I) +_LYRICS = re.compile(r"\b(lyrics?|lyric video|letra)\b", re.I) + + +def not_the_song(title: str, channel: str, wanted: str) -> str: + """Why a result isn't the plain studio recording, or ''.""" + for why, regex in _NOT_THE_SONG_RE.items(): + if regex.search(wanted or ""): + continue + if regex.search(title or ""): + return why + if why in _CHANNEL_CHECKED and regex.search(channel or ""): + return why + return "" + + +def score_hit(hit: dict, song: ShazamSong) -> tuple[float | None, str]: + """(score, why) for one search result; score None means rejected.""" + title, channel = hit.get("title") or "", hit.get("channel") or "" + reason = not_the_song(title, channel, song.title) + if reason: + return None, reason + want = toks(song.title) + overlap = len(want & toks(title)) / len(want) if want else 0 + if overlap < 0.6: + return None, "title mismatch" + + score, why = overlap * 20, [] + if channel.endswith("- Topic"): + score += 40 + why.append("Topic") + # The artist's own channel counts for more than a stranger's "(Audio)": + # a label's upload titled just the song is the release's own audio. + if toks(song.artist) & toks(channel): + score += 25 + why.append("artist's channel") + if _AUDIO.search(title): + score += 15 + why.append("audio") + if fold(title) in (fold(song.title), + fold(f"{song.artist} {song.title}")): + score += 10 + why.append("just the title") + if _LYRICS.search(title): + score += 3 + why.append("lyrics") + if _VIDEO.search(title): + score -= 15 + why.append("music video") + + length = hit.get("duration") + if song.canon_secs and length: + off = abs(length - song.canon_secs) + if off <= 3: + score += 45 + why.append("exact length") + elif off <= 8: + score += 30 + why.append("±8 s") + elif off <= 20: + score += 8 + why.append("±20 s") + else: + return None, f"length off by {off:.0f} s" + elif length and (length < 45 or length > 1500): + return None, "implausible length" + score += min(hit.get("views") or 0, 5_000_000) / 1_000_000 + return score, "+".join(why) or "-" + + +def rank_hits(hits: list[dict], song: ShazamSong) -> list[dict]: + """The results that are the song, best first (YouTube's order breaks a + tie). Each gets ``score`` and ``why``.""" + scored = [] + for position, hit in enumerate(hits): + score, why = score_hit(hit, song) + if score is not None: + scored.append((-score, position, {**hit, "score": score, + "why": why})) + return [hit for *_k, hit in sorted(scored, key=lambda x: x[:2])] + + +def search_queries(song: ShazamSong) -> list[tuple[str, str]]: + """(yt-dlp search prefix, query) in the order tried: YouTube as Shazam + names it, YouTube without "(… Remix)"-style suffixes (what finds a DJ + edit), then SoundCloud.""" + plain = f"{song.artist} {song.title}".strip() + bare = re.sub(r"\s*[\(\[].*?[\)\]]", "", plain).strip() + queries = [("ytsearch", plain)] + if bare and bare != plain: + queries.append(("ytsearch", bare)) + queries.append(("scsearch", plain)) + return queries + + +_COMPILATION = re.compile(r"\b(greatest hits|best of|the hits|collection|" + r"anthology|essential|essentials|definitive|" + r"gold|now that's what)\b", re.I) + + +def canonical_from_results(results: list[dict], song: ShazamSong): + """The iTunes Search result that is this recording → (seconds, album, + album artist, confident), or None. A live album never supplies it.""" + want, want_artist = toks(song.title), toks(song.artist) + best, best_score = None, -1.0 + for item in results: + got = toks(item.get("trackName", "")) + if not got: + continue + score = len(want & got) / len(want | got) if want else 0 + if want_artist & toks(item.get("artistName", "")): + score += 0.5 + if "live" in got and "live" not in want: + score -= 0.6 + collection = fold(item.get("collectionName", "")).split() + if (("live" in collection or "concert" in collection) + and "live" not in want): + score -= 0.8 + # The album it came out on, not a hits collection it's also on. + if _COMPILATION.search(item.get("collectionName", "")): + score -= 0.3 + if song.year and _year(item.get("releaseDate")) == song.year: + score += 0.3 + if score > best_score: + best, best_score = item, score + if not best or not best.get("trackTimeMillis"): + return None + return (best["trackTimeMillis"] // 1000, best.get("collectionName", ""), + best.get("artistName", ""), best_score >= 0.9) + + +def tag_fields(song: ShazamSong) -> dict: + """What Shazam (and Apple, when it was sure) knows, as Track fields for + ``tagging.write_tags``. Apple's album only when its match was confident + or the barcode named it: a loose match hands back a compilation.""" + fields = {"name": song.title, "artist": song.artist} + if song.year: + fields["year"] = song.year + if song.genre: + fields["genre"] = song.genre + if song.album and (song.album_confident or song.upc): + fields["album"] = song.album + if song.album_artist: + fields["album_artist"] = song.album_artist + return {k: v for k, v in fields.items() if v} + + +# ---- network ---- + +def _itunes(url: str, **params) -> list[dict]: + resp = requests.get(url, params=params, timeout=TIMEOUT, + headers={"User-Agent": USER_AGENT}) + resp.raise_for_status() + return resp.json().get("results", []) + + +def look_up(song: ShazamSong) -> None: + """Fill in Apple's length and album. Failing is fine: the search then + runs without a length filter.""" + try: + found = canonical_from_results(_itunes( + ITUNES_SEARCH, term=f"{song.artist} {song.title}".strip(), + entity="song", media="music", limit=8), song) + except (requests.RequestException, ValueError) as e: + log.info("shazam: iTunes lookup for %s failed: %s", song.label(), e) + found = None + if found is not None: + song.canon_secs, song.album, song.album_artist, \ + song.album_confident = found + if not song.album and song.upc: + try: + for item in _itunes(ITUNES_LOOKUP, upc=song.upc, entity="song", + limit=30): + if item.get("wrapperType") == "collection": + song.album = item.get("collectionName", "") + song.album_artist = item.get("artistName", "") + break + except (requests.RequestException, ValueError): + pass + + +def file_seconds(path) -> float | None: + try: + import mutagen + audio = mutagen.File(str(path)) + return audio.info.length if audio is not None else None + except Exception: + return None + + +# ---- adb ---- + +class ShazamError(Exception): + pass + + +def _adb(adb: str, serial: str, *args, timeout: int = 30): + return subprocess.run([adb, "-s", serial, *args], capture_output=True, + text=True, timeout=timeout) + + +def pull_database(adb: str, serial: str, dest: Path, status=lambda s: None): + """Copy Shazam's database (+ -wal, -shm) into ``dest`` → its path. + Roots adbd for the copy if it wasn't already, and unroots it after.""" + def whoami(): + try: + return _adb(adb, serial, "shell", "whoami").stdout.strip() + except subprocess.SubprocessError: + return "" + + rooted_here = False + if whoami() != "root": + status("Asking the Rabbit for root adb…") + out = _adb(adb, serial, "root") + said = (out.stdout + out.stderr).strip() + if "cannot run as root" in said: + raise ShazamError( + "The Rabbit refused root adb (“" + said + "”), and Shazam's " + "history can't be read without it.") + rooted_here = True + # adbd restarts: the device drops off and comes back. + deadline = time.monotonic() + 30 + while whoami() != "root": + if time.monotonic() > deadline: + raise ShazamError("The Rabbit didn't come back as root after " + "adb root. Unplug it, plug it back in, and " + "try again.") + try: + _adb(adb, serial, "wait-for-device", timeout=10) + except subprocess.SubprocessError: + pass + time.sleep(0.5) + try: + status("Copying Shazam's history…") + for suffix in ("", "-wal", "-shm"): + out = _adb(adb, serial, "pull", DEVICE_DB + suffix, + str(dest / f"library.db{suffix}"), timeout=60) + if out.returncode != 0 and suffix == "": + raise ShazamError( + "Couldn't copy Shazam's history off the Rabbit — is " + "Shazam installed?\n\n" + (out.stderr or out.stdout) + .strip()) + finally: + if rooted_here: + # Don't leave the device with a root adbd. + try: + _adb(adb, serial, "unroot", timeout=15) + except subprocess.SubprocessError: + pass + return dest / "library.db" + + +class ShazamReader(QObject): + """Reads the Rabbit's Shazams on a daemon thread. ``read`` carries every + Shazam (newest first, one per song); ``status`` narrates for the + preview dialog; ``failed`` says why there's nothing.""" + + status = pyqtSignal(str) + read = pyqtSignal(list) + failed = pyqtSignal(str) + + def start(self): + threading.Thread(target=self._run, daemon=True).start() + + def _run(self): + from lintunes.andtunes import install + temp = Path(tempfile.mkdtemp(prefix="lintunes-shazam-")) + try: + adb = install.adb_path() + if adb is None: + raise ShazamError("Reading Shazams needs adb, which isn't " + "installed (Fedora: android-tools).") + self.status.emit("Looking for the Rabbit…") + serial = install.rabbit_serial(adb) + if serial is None: + raise ShazamError( + "No Rabbit found over adb. Plug it in, unlock it, and " + "allow USB debugging if it asks.") + db = pull_database(adb, serial, temp, self.status.emit) + songs = dedupe(read_shazams(db)) + except (ShazamError, OSError, sqlite3.Error, + subprocess.SubprocessError) as e: + self.failed.emit(str(e)) + return + finally: + shutil.rmtree(temp, ignore_errors=True) + self.read.emit(songs) + + +# ---- the import ---- + +class ShazamImportWorker(url_import.UrlImportWorker): + """``UrlImportWorker``'s threading, cancel and cookie retry, for a list + of Shazams: each is looked up on iTunes, searched for, the best result + downloaded (the next if that fails or comes back short), and Shazam's + tags written in before ``landed``. + + ``stage`` (request id, text) narrates each song for the window: + Looking up, Searching, Downloading n %, Converting, Tagging. + ``landed`` is (request id, mp3 path). ``item_failed`` is (request id, + reason). ``finished`` as UrlImportWorker's; ``failed`` only when nothing + landed because YouTube wanted a signed-in browser, or yt-dlp can't run. + """ + + stage = pyqtSignal(str, str) + landed = pyqtSignal(str, str) + + def __init__(self, songs, parent=None, *, cookies_browser=None, + lookup=look_up): + super().__init__([], parent, cookies_browser=cookies_browser) + self._songs = list(songs) + self._lookup = lookup + self._song: ShazamSong | None = None + self._mode = "" + self._query = ("", "") + self._hits: list[dict] = [] + self._files: list[str] = [] + + def _command(self, cookies_browser): + if self._mode == "search": + prefix, query = self._query + return url_import.build_search_command( + query, SEARCH_RESULTS, cookies_browser, prefix) + return url_import.build_command(self._query[1], self.temp_dir, + cookies_browser) + + def _on_parsed(self, parsed): + kind = parsed[0] + rid = self._song.request_id if self._song else "" + if self._mode == "search": + if kind == "hit": + self._hits.append(parsed[1]) + return True + return False + if kind == "progress": + self.progress.emit(parsed[1]) + self.stage.emit(rid, f"Downloading {parsed[1]}%") + elif kind == "postprocess" and parsed[1:] == ("ExtractAudio", + "started"): + self.stage.emit(rid, "Converting to mp3…") + elif kind == "file" and not self._cancel.is_set(): + self._files.append(parsed[1]) + return True + return False + + def _on_error(self, error): + pass # reported per song + + def _search(self, song): + """→ (results best first, ERROR lines of the last search), or None + when yt-dlp couldn't run.""" + errors = [] + for prefix, query in search_queries(song): + if self._cancel.is_set(): + break + self._mode, self._query, self._hits = "search", (prefix, + query), [] + result = self._run_with_retry() + if result is None: + return None + errors = result[1] + ranked = rank_hits(self._hits, song) + if ranked: + return ranked, errors + return [], errors + + def _download(self, hit): + """→ (path or '', why not), or None when yt-dlp couldn't run.""" + self._mode, self._query, self._files = "download", ("", hit["url"]), [] + result = self._run_with_retry() + if result is None: + return None + if not self._files: + errors = result[1] + return "", errors[-1] if errors else "download failed" + path = self._files[-1] + length, listed = file_seconds(path), hit.get("duration") + if length is None: + return "", "the download isn't a playable file" + if listed and abs(length - listed) > TRUNCATED_SLACK: + Path(path).unlink(missing_ok=True) + return "", (f"the download was cut short ({length:.0f} s of " + f"{listed} s)") + return path, "" + + def _run(self): + landed, errors, bot_errors = 0, [], [] + for song in self._songs: + if self._cancel.is_set(): + break + rid = self._song_started(song) + self.stage.emit(rid, "Looking up…") + self._lookup(song) + if self._cancel.is_set(): + break + self.stage.emit(rid, "Searching YouTube…") + searched = self._search(song) + if searched is None: + return + hits, run_errors = searched + path, reason = "", "" + if not hits: + reason = (run_errors[-1] if run_errors else + "nothing found that's just the song (no studio " + "version on YouTube or SoundCloud)") + if run_errors and url_import.is_bot_check(run_errors[-1]): + bot_errors = run_errors + for hit in hits[:TRIES]: + if self._cancel.is_set(): + break + self.stage.emit(rid, "Downloading…") + got = self._download(hit) + if got is None: + return + path, reason = got + if path: + log.info("shazam: %s ← %s (%s)", song.label(), + hit["url"], hit.get("why")) + break + if self._cancel.is_set() and not path: + break + if not path: + errors.append(f"{song.label()}: {reason}") + self.item_failed.emit(rid, reason) + continue + self.stage.emit(rid, "Tagging…") + try: + tagging.write_tags(path, tag_fields(song)) + except Exception as e: # untagged still beats not imported + log.warning("shazam tags for %s: %s", path, e) + landed += 1 + self.landed.emit(rid, path) + cancelled = self._cancel.is_set() + self._busy = False + if landed == 0 and not cancelled and bot_errors: + self._fail_nothing(bot_errors, self._cookies, 1, "download") + return + self.finished.emit({"downloaded": landed, "errors": errors, + "cancelled": cancelled}) + + def _song_started(self, song) -> str: + self._song = song + index = self._songs.index(song) + 1 + self.item_started.emit(index, len(self._songs), song.request_id, + song.label()) + return song.request_id diff --git a/lintunes/url_import.py b/lintunes/url_import.py index e7c2504..5c2b60b 100644 --- a/lintunes/url_import.py +++ b/lintunes/url_import.py @@ -67,6 +67,10 @@ START = "LTSTART" FILE = "LTFILE" PROGRESS = "LTPROG" ENTRY = "LTENTRY" +# A search result with the channel and view count too (Import Shazams), and +# a postprocessor starting or finishing (ExtractAudio = the mp3 conversion). +HIT = "LTHIT" +POSTPROCESS = "LTPP" # What YouTube says when it wants a signed-in browser. Matched on a fragment @@ -168,6 +172,10 @@ def build_command(urls, dest_dir, "--print", f"after_move:{FILE} %(filepath)s", "--progress", "--newline", "--progress-template", f"download:{PROGRESS} %(progress._percent_str)s", + # Printed even under --print's quiet: says when the conversion runs. + "--progress-template", + f"postprocess:{POSTPROCESS} %(progress.postprocessor)s " + f"%(progress.status)s", "--", *urls, ] @@ -187,6 +195,21 @@ def build_probe_command(url: str, ] +def build_search_command(query: str, results: int, + cookies_browser: str | None = None, + prefix: str = "ytsearch") -> list[str]: + """A search, listed without downloading: one HIT line per result, with + the channel (a ``- Topic`` channel is YouTube's own audio-only upload) + and the view count that a plain ENTRY line leaves out.""" + return [ + "yt-dlp", "--flat-playlist", *_cookie_args(cookies_browser), + "--print", + f"{HIT} %(id)s\t%(duration|)s\t%(view_count|)s\t%(webpage_url|)s\t" + f"%(channel|)s\t%(title)s", + "--", f"{prefix}{results}:{query}", + ] + + def _cookie_args(browser: str | None) -> list[str]: return ["--cookies-from-browser", browser] if browser else [] @@ -208,7 +231,8 @@ def _duration(text: str) -> int | None: def parse_line(line: str): """One line of yt-dlp output → ("start", i, n, id, title) | ("file", path) | ("progress", percent) | ("entry", {id, duration, url, - title}) | None for everything else.""" + title}) | ("hit", {id, duration, views, url, channel, title}) | + ("postprocess", name, status) | None for everything else.""" line = line.rstrip("\r\n") if line.startswith(START + " "): parts = line[len(START) + 1:].split("\t", 3) @@ -222,6 +246,18 @@ def parse_line(line: str): return None return ("entry", {"id": parts[0], "duration": _duration(parts[1]), "url": parts[2], "title": parts[3]}) + if line.startswith(HIT + " "): + parts = line[len(HIT) + 1:].split("\t", 5) + if len(parts) != 6 or not parts[3].strip(): + return None + return ("hit", {"id": parts[0], "duration": _duration(parts[1]), + "views": _int(parts[2], 0), "url": parts[3], + "channel": parts[4], "title": parts[5]}) + if line.startswith(POSTPROCESS + " "): + parts = line[len(POSTPROCESS) + 1:].split() + if len(parts) != 2: + return None + return ("postprocess", parts[0], parts[1]) if line.startswith(FILE + " "): path = line[len(FILE) + 1:] return ("file", path) if path.strip() else None diff --git a/tests/test_round103.py b/tests/test_round103.py new file mode 100644 index 0000000..4fe3163 --- /dev/null +++ b/tests/test_round103.py @@ -0,0 +1,537 @@ +"""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")