v0.94.0: a playlist made from a Spotify or Apple Music link keeps the link's cover image (stored, not shown yet)

This commit is contained in:
2026-11-17 19:15:36 -08:00
parent 2069e1b1d0
commit 81b6fa509f
13 changed files with 287 additions and 13 deletions
+5
View File
@@ -853,6 +853,11 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal
API, so its `music.apple.com` page's `serialized-server-data` JSON is read
— measured to hold every song (143 of 143), with album and length each.
Private `/library/` playlists can't be read and aren't matched.
**The cover is kept** (`playlist_art.py`): a playlist made from a
Spotify/Apple link gets the link's cover in `<data_dir>/playlist_art/
<pid>.jpg|png` (synced, never in the playlist JSON, so no merge or
"edited" stamp; downloaded on a daemon thread when the playlist is made).
Nothing displays it yet.
**Several songs are identified as one batch** (Round 77,
`identify_batch.py` + `gui/identify_batch_dialog.py`): an import of more
than one song never pops a dialog mid-download. `IdentifyCollector` looks
+1 -1
View File
@@ -1,3 +1,3 @@
"""LinTunes — iTunes-style music library manager and player for Linux."""
__version__ = "0.93.0"
__version__ = "0.94.0"
+43 -3
View File
@@ -159,6 +159,44 @@ def parse_playlist_page(html: str) -> tuple[str, list[SpotifyTrack]]:
return name or "Apple Music", tracks
# The size a cover is asked for. Apple's artwork URLs are templates and
# scale server-side to whatever is asked.
COVER_SIZE = 1200
def _artwork(template: str) -> str:
if not template:
return ""
return (template.replace("{w}", str(COVER_SIZE))
.replace("{h}", str(COVER_SIZE)).replace("{f}", "jpg"))
def playlist_cover_url(html: str) -> str:
"""A playlist page's cover image URL, or "" when it has none."""
m = _SERVER_DATA.search(html or "")
try:
sections = json.loads(m.group(1))["data"][0]["data"]["sections"]
except (AttributeError, ValueError, KeyError, IndexError, TypeError):
return ""
for section in sections:
if section.get("itemKind") == "containerDetailHeaderLockup":
for item in section.get("items") or []:
url = (((item.get("artwork") or {}).get("dictionary") or {})
.get("url"))
if url:
return _artwork(url)
return ""
def lookup_cover_url(data: dict) -> str:
"""An iTunes Lookup answer's cover, at ``COVER_SIZE``, or ""."""
for r in (data or {}).get("results") or []:
url = r.get("artworkUrl100") or ""
if url and "100x100" in url:
return url.replace("100x100", f"{COVER_SIZE}x{COVER_SIZE}")
return ""
# ---- network ----
def _get(url: str, **params) -> requests.Response:
@@ -170,7 +208,8 @@ def _get(url: str, **params) -> requests.Response:
def fetch(kind: str, source_id: str,
storefront: str = "us") -> tuple[str, list[SpotifyTrack]]:
storefront: str = "us") -> tuple[str, list[SpotifyTrack], str]:
"""→ (the link's name, its songs, its cover image URL or "")."""
if kind == "playlist":
resp = _get(PLAYLIST_URL.format(storefront=storefront, id=source_id))
if resp.status_code == 404:
@@ -178,7 +217,8 @@ def fetch(kind: str, source_id: str,
"link (it may be private or deleted)")
if not resp.ok:
raise AppleMusicError(f"Apple Music answered {resp.status_code}")
return parse_playlist_page(resp.text)
return (*parse_playlist_page(resp.text),
playlist_cover_url(resp.text))
params = {"id": source_id, "country": storefront}
if kind == "album":
params["entity"] = "song"
@@ -190,4 +230,4 @@ def fetch(kind: str, source_id: str,
data = resp.json()
except ValueError as e:
raise AppleMusicError(f"Apple Music's answer couldn't be read: {e}")
return parse_lookup(data, kind)
return (*parse_lookup(data, kind), lookup_cover_url(data))
+8
View File
@@ -13,6 +13,7 @@ from PyQt6.QtGui import QAction, QKeySequence
from lintunes import (bpm_detect, device_sync, loudness, music_folder, shazam,
tagging, theme, url_import)
from lintunes import playlist_art
from lintunes.andtunes import layout as andtunes_layout
from lintunes.andtunes import install as andtunes_install
from lintunes.andtunes import sync as andtunes_sync
@@ -304,6 +305,7 @@ class MainWindow(QMainWindow):
# song lands, then the playlist's pid (made then, so a link that
# downloads nothing leaves no empty playlist behind).
self._url_new_name = ""
self._url_new_cover = ""
self._url_new_pid = ""
self._url_imported = 0
self._url_identify = True # False after "Stop Identifying"
@@ -1933,6 +1935,7 @@ class MainWindow(QMainWindow):
self._url_target = target if dialog.add_to_playlist() else None
self._url_inserted = 0
self._url_new_name = dialog.new_playlist_name()
self._url_new_cover = dialog.new_playlist_cover()
self._url_new_pid = ""
self._url_imported = 0
self._url_identify = True
@@ -2048,6 +2051,11 @@ class MainWindow(QMainWindow):
playlist = self._manager.create_playlist(self._url_new_name)
self._url_new_pid = playlist.persistent_id
self._url_new_name = ""
if self._url_new_cover: # kept for later; nothing shows it yet
playlist_art.fetch_in_background(
self._url_new_cover, self._manager.data_dir,
playlist.persistent_id)
self._url_new_cover = ""
self._manager.add_tracks_to_playlist(
self._url_new_pid, [t.track_id for t in tracks], undoable=False)
+12
View File
@@ -86,6 +86,7 @@ class UrlImportDialog(QDialog):
self._seen: set[str] = set() # ids already listed (a Mix loops)
self._just_song = False # "Just This Song" was pressed
self._list_name = "" # what the link's list is called
self._list_cover = "" # its cover image's URL
self.cookies_used = None # a browser the probe needed
layout = QVBoxLayout(self)
@@ -189,6 +190,10 @@ class UrlImportDialog(QDialog):
return ""
return self._make_name.text().strip()
def new_playlist_cover(self) -> str:
"""The cover image URL for the new playlist ("" for none)."""
return self._list_cover if self.new_playlist_name() else ""
def download_urls(self) -> list[str]:
"""What to hand yt-dlp: the ticked songs' own pages, the linked song
alone, or the link itself — never a list link the user didn't see."""
@@ -250,6 +255,7 @@ class UrlImportDialog(QDialog):
self._seen = set()
self._single = None
self._list_name = ""
self._list_cover = ""
self._url.setEnabled(False)
self._just.setVisible(bool(self._linked))
catalog = _CATALOGS.get(self._kind)
@@ -263,6 +269,8 @@ class UrlImportDialog(QDialog):
url, cookies_browser=self._cookies)
probe.entry.connect(self._on_entry)
probe.named.connect(self._on_named)
if hasattr(probe, "cover"):
probe.cover.connect(self._on_cover)
probe.finished.connect(self._on_probe_finished)
probe.failed.connect(self._on_probe_failed)
probe.cookies_used.connect(self._on_cookies_used)
@@ -303,6 +311,10 @@ class UrlImportDialog(QDialog):
if self._picking:
self._show_make()
def _on_cover(self, url: str):
if self.sender() is self._probe:
self._list_cover = url
def _show_make(self):
self._make.show()
self._make_name.show()
+95
View File
@@ -0,0 +1,95 @@
"""A playlist's own cover image (Round 130).
Spotify and Apple Music playlists (and albums) have a cover, and a playlist
made from one of their links in Import from URL keeps it. Nothing *shows* it
yet; this is only where it is kept.
One file per playlist, ``<data_dir>/playlist_art/<persistent_id>.jpg`` (or
``.png``), so it rides the Syncthing share with the playlists but stays out
of their JSON: a cover never makes a playlist look edited, and the conflict
merge never has to know about it. Only the machine that made the playlist
writes it. Writes are atomic (``.tmp`` → rename), like every JSON file.
A deleted playlist's cover is left where it is: undo brings the playlist
back with the same id, and a cover is a few hundred KB.
"""
import logging
import os
import threading
from pathlib import Path
import requests
log = logging.getLogger(__name__)
DIRNAME = "playlist_art"
TIMEOUT = 20
MAX_BYTES = 10 * 1024 * 1024
USER_AGENT = ("Mozilla/5.0 (X11; Linux x86_64; rv:131.0) "
"Gecko/20100101 Firefox/131.0")
_SUFFIXES = (".jpg", ".png")
def image_suffix(data: bytes) -> str | None:
"""".jpg" or ".png" by the bytes' own signature, else None."""
if data[:3] == b"\xff\xd8\xff":
return ".jpg"
if data[:8] == b"\x89PNG\r\n\x1a\n":
return ".png"
return None
def find(data_dir: Path, pid: str) -> Path | None:
"""The playlist's cover file, or None when it has none."""
folder = Path(data_dir) / DIRNAME
for suffix in _SUFFIXES:
path = folder / f"{pid}{suffix}"
if path.is_file():
return path
return None
def save(data_dir: Path, pid: str, data: bytes) -> Path:
"""Keep ``data`` as the playlist's cover, replacing any other. Raises
ValueError for bytes that aren't a JPEG or PNG."""
suffix = image_suffix(data)
if suffix is None:
raise ValueError("not a JPEG or PNG image")
if not pid or "/" in pid or pid.startswith("."):
raise ValueError(f"bad playlist id {pid!r}")
folder = Path(data_dir) / DIRNAME
folder.mkdir(parents=True, exist_ok=True)
path = folder / f"{pid}{suffix}"
tmp = path.with_name(path.name + ".tmp")
tmp.write_bytes(data)
os.replace(tmp, path)
for other in _SUFFIXES:
if other != suffix:
(folder / f"{pid}{other}").unlink(missing_ok=True)
return path
def download(url: str) -> bytes:
resp = requests.get(url, headers={"User-Agent": USER_AGENT},
timeout=TIMEOUT, stream=True)
resp.raise_for_status()
data = b""
for chunk in resp.iter_content(65536):
data += chunk
if len(data) > MAX_BYTES:
raise ValueError("cover image is too large")
return data
def fetch_in_background(url: str, data_dir: Path, pid: str):
"""Download ``url`` and keep it as the playlist's cover, on a daemon
thread. A failure is logged and otherwise ignored: the playlist is
complete without it."""
def run():
try:
path = save(data_dir, pid, download(url))
log.info("playlist art for %s saved to %s", pid, path)
except Exception as e:
log.warning("playlist art for %s from %s: %s", pid, url, e)
threading.Thread(target=run, daemon=True).start()
+18 -2
View File
@@ -163,6 +163,21 @@ def parse_embed(html: str) -> tuple[str, list[SpotifyTrack]]:
return name, tracks
def cover_url(html: str) -> str:
"""The embed page's cover image (the playlist's, album's or song's),
the largest it lists, or "" when it has none."""
m = _NEXT_DATA.search(html or "")
try:
entity = _find_entity(json.loads(m.group(1))) if m else None
except ValueError:
return ""
sources = ((entity or {}).get("coverArt") or {}).get("sources") or []
sources = [s for s in sources if isinstance(s, dict) and s.get("url")]
if not sources:
return ""
return max(sources, key=lambda s: s.get("width") or 0)["url"]
def search_query(track: SpotifyTrack) -> str:
return f"{track.artist} - {track.title}" if track.artist else track.title
@@ -212,7 +227,8 @@ def tag_fields(track: SpotifyTrack) -> dict:
# ---- network ----
def fetch(kind: str, source_id: str) -> tuple[str, list[SpotifyTrack]]:
def fetch(kind: str, source_id: str) -> tuple[str, list[SpotifyTrack], str]:
"""→ (the link's name, its songs, its cover image URL or "")."""
url = EMBED_URL.format(kind=kind, id=source_id)
try:
resp = requests.get(url, headers={"User-Agent": USER_AGENT},
@@ -224,4 +240,4 @@ def fetch(kind: str, source_id: str) -> tuple[str, list[SpotifyTrack]]:
"private or deleted)")
if not resp.ok:
raise SpotifyError(f"Spotify answered {resp.status_code}")
return parse_embed(resp.text)
return (*parse_embed(resp.text), cover_url(resp.text))
+15 -6
View File
@@ -619,6 +619,12 @@ class UrlImportWorker(_YtdlpRun):
# ---- Spotify ----
def _with_cover(result) -> tuple:
"""(name, songs, cover URL) from a fetch, which may leave the cover out
(the tests' canned fetches return only name and songs)."""
return (*result, "")[:3]
class SpotifyProbe(QObject):
"""``PlaylistProbe``'s signals for a Spotify link: reads its embed page
on a daemon thread and emits one entry per song, carrying the
@@ -626,6 +632,7 @@ class SpotifyProbe(QObject):
entry = pyqtSignal(dict)
named = pyqtSignal(str) # the album's or playlist's name
cover = pyqtSignal(str) # its cover image's URL
finished = pyqtSignal(int)
failed = pyqtSignal(str)
cookies_used = pyqtSignal(str) # never: Spotify needs no cookies
@@ -648,20 +655,22 @@ class SpotifyProbe(QObject):
threading.Thread(target=self._run, daemon=True).start()
def _lookup(self):
"""→ (is it a list, its name, its songs)."""
"""→ (is it a list, its name, its songs, its cover URL or "")."""
kind, source_id = spotify_link.spotify_source(self._url)
name, tracks = spotify_link.fetch(kind, source_id)
return kind != "track", name, tracks
return (kind != "track", *_with_cover(spotify_link.fetch(kind,
source_id)))
def _run(self):
try:
is_list, name, tracks = self._lookup()
is_list, name, tracks, cover = self._lookup()
except Exception as e:
self._busy = False
self.failed.emit(str(e))
return
if is_list:
self.named.emit(name)
if cover:
self.cover.emit(cover)
count = 0
for track in tracks:
if self._cancel.is_set():
@@ -681,8 +690,8 @@ class AppleMusicProbe(SpotifyProbe):
def _lookup(self):
kind, source_id, storefront = apple_music_link.apple_source(self._url)
name, tracks = apple_music_link.fetch(kind, source_id, storefront)
return kind != "song", name, tracks
return (kind != "song", *_with_cover(apple_music_link.fetch(
kind, source_id, storefront)))
class SpotifyImportWorker(UrlImportWorker):
+4 -1
View File
@@ -154,7 +154,7 @@ def window(qapp, tmp_path, fake, monkeypatch):
win.close()
def _import(window, qapp, monkeypatch, ids, name="Road Trip"):
def _import(window, qapp, monkeypatch, ids, name="Road Trip", cover=""):
from lintunes.gui import main_window as mw
chosen = [{"id": i, "title": f"Song {i}",
"url": f"https://www.youtube.com/watch?v={i}"} for i in ids]
@@ -169,6 +169,9 @@ def _import(window, qapp, monkeypatch, ids, name="Road Trip"):
return False
def new_playlist_name(self):
return name
def new_playlist_cover(self):
return cover
def chosen_entries(self):
return chosen
def spotify_tracks(self):
+74
View File
@@ -194,3 +194,77 @@ def test_an_apple_song_goes_straight_through(qapp, canned):
_pump(qapp, lambda: dialog.result() == dialog.DialogCode.Accepted)
assert [t.artist for t in dialog.spotify_tracks()] == ["Donald Fagen"]
assert dialog.new_playlist_name() == ""
# --------------------------------------------------------------------------
# D. the playlist's cover (kept, not shown yet)
from lintunes import playlist_art # noqa: E402
from tests.test_round109 import _import, _named, fake, window # noqa: E402,F401
JPEG = b"\xff\xd8\xff\xe0" + b"\0" * 32
PNG = b"\x89PNG\r\n\x1a\n" + b"\0" * 32
def test_cover_urls_are_read_off_every_kind_of_answer():
page = PLAYLIST.replace(
'"trackCount": 2}', '"trackCount": 2, "artwork": {"dictionary": '
'{"url": "https://is1-ssl.mzstatic.com/x/{w}x{h}bb.{f}"}}}')
assert apple_music_link.playlist_cover_url(page) == (
"https://is1-ssl.mzstatic.com/x/1200x1200bb.jpg")
assert apple_music_link.playlist_cover_url(PLAYLIST) == ""
assert apple_music_link.lookup_cover_url({"results": [
{"artworkUrl100": "https://a/b/100x100bb.jpg"}]}) == (
"https://a/b/1200x1200bb.jpg")
embed = ('<script id="__NEXT_DATA__" type="application/json">'
+ json.dumps({"entity": {
"type": "playlist", "name": "P",
"trackList": [{"title": "t"}],
"coverArt": {"sources": [
{"url": "https://i.scdn.co/small", "width": 64},
{"url": "https://i.scdn.co/big", "width": 640}]}}})
+ "</script>")
assert spotify_link.cover_url(embed) == "https://i.scdn.co/big"
assert spotify_link.cover_url("<html></html>") == ""
def test_a_cover_is_kept_per_playlist_and_replaced(tmp_path):
assert playlist_art.find(tmp_path, "ABCD1234") is None
playlist_art.save(tmp_path, "ABCD1234", JPEG)
assert playlist_art.find(tmp_path, "ABCD1234").name == "ABCD1234.jpg"
playlist_art.save(tmp_path, "ABCD1234", PNG) # png replaces jpg
assert playlist_art.find(tmp_path, "ABCD1234").name == "ABCD1234.png"
assert sorted(p.name for p in (tmp_path / "playlist_art").iterdir()) == [
"ABCD1234.png"]
with pytest.raises(ValueError):
playlist_art.save(tmp_path, "ABCD1234", b"<html>not an image")
with pytest.raises(ValueError):
playlist_art.save(tmp_path, "../x", JPEG)
def test_the_probe_passes_the_cover_on_for_a_new_playlist(qapp, monkeypatch):
monkeypatch.setattr(apple_music_link, "fetch", lambda *a: (
*parse_playlist_page(PLAYLIST), "https://img/cover.jpg"))
dialog = _dialog(qapp, PLAYLIST_URL)
dialog._on_import()
_pump(qapp, lambda: dialog._probe is None)
dialog._on_import()
assert dialog.new_playlist_cover() == "" # not ticked: no cover
dialog._make.setChecked(True)
assert dialog.new_playlist_cover() == "https://img/cover.jpg"
def test_the_new_playlist_gets_its_cover(window, qapp, fake, mp3_file,
monkeypatch):
monkeypatch.setenv("FAKE_YTDLP_AUDIO", str(mp3_file))
monkeypatch.setattr(playlist_art, "download", lambda url: JPEG)
saved = []
real_save = playlist_art.save
monkeypatch.setattr(playlist_art, "save",
lambda *a: saved.append(real_save(*a)))
manager = window._manager
_import(window, qapp, monkeypatch, ["aaa", "bbb"], cover="https://img/c")
[playlist] = _named(manager, "Road Trip")
_pump(qapp, lambda: saved)
assert playlist_art.find(manager.data_dir, playlist.persistent_id) \
== saved[0]
+3
View File
@@ -349,6 +349,9 @@ class TestWindow:
return True
def new_playlist_name(self):
return ""
def new_playlist_cover(self):
return ""
monkeypatch.setattr(mw, "UrlImportDialog", _Dialog)
window._show_playlist("a")
+6
View File
@@ -365,6 +365,9 @@ def test_only_the_chosen_songs_download(window, qapp, fake, mp3_file,
return False
def new_playlist_name(self):
return ""
def new_playlist_cover(self):
return ""
def chosen_entries(self):
return chosen
def spotify_tracks(self):
@@ -413,6 +416,9 @@ def test_one_song_gets_no_progress_window(window, qapp, fake, mp3_file,
return False
def new_playlist_name(self):
return ""
def new_playlist_cover(self):
return ""
def chosen_entries(self):
return []
def spotify_tracks(self):
+3
View File
@@ -349,6 +349,9 @@ def test_spotify_songs_import_tagged_and_go_to_identify(window, qapp, fake,
return False
def new_playlist_name(self):
return ""
def new_playlist_cover(self):
return ""
def chosen_entries(self):
return chosen
def spotify_tracks(self):