forked from trav/lintunes
v0.73.0: Connections › Import Shazams… — the Rabbit's new Shazams, found on YouTube as the original song, tagged as they land
Reads Shazam's history over root adb (and unroots after), offers only Shazams not dealt with yet (a synced list), picks the plain studio take (no live/cover/karaoke/clean/vinyl/sped-up, Apple's length decides), and shows every song's stage with the Identify panel for each one as soon as it's imported, in the same window.
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
"""LinTunes — iTunes-style music library manager and player for Linux."""
|
||||
|
||||
__version__ = "0.72.1"
|
||||
__version__ = "0.73.0"
|
||||
|
||||
@@ -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()
|
||||
|
||||
+181
-1
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
+37
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user