Spotify: read the whole playlist so one wrong song shows WRONG PLAYLIST; refresh hourly and on unknown songs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-13 10:23:15 -04:00
co-authored by Claude Opus 5.5
parent 9a108b2508
commit 38b6d1b014
2 changed files with 22 additions and 10 deletions
+21 -9
View File
@@ -1595,29 +1595,39 @@ SPOTIFY_STATE = ('tell application "Spotify" to return (player state as text) &
def playlist_tracks(playlist_uri):
"""(set of track uris, complete?) from Spotify's public pages: the embed preview lists up to
100 songs, the playlist page says how many there are."""
"""(set of track uris, complete?) for a public playlist. The embed preview page lists the first
100 songs and carries an anonymous token, which reads the whole list from Spotify's playlist service."""
pid = playlist_uri.split(":")[-1]
req = lambda url: urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}),
timeout=10, context=_UNVERIFIED).read().decode("utf-8", "replace")
req = lambda url, **h: urllib.request.urlopen(urllib.request.Request(url, headers=dict({"User-Agent": "Mozilla/5.0"}, **h)),
timeout=10, context=_UNVERIFIED).read().decode("utf-8", "replace")
m = re.search(r'<script id="__NEXT_DATA__" type="application/json">(.*?)</script>',
req("https://open.spotify.com/embed/playlist/" + pid))
entity = json.loads(m.group(1))["props"]["pageProps"]["state"]["data"]["entity"]
tracks = {t["uri"] for t in entity.get("trackList", []) if t.get("uri")}
n = re.search(r'music:song_count" content="(\d+)"', req("https://open.spotify.com/playlist/" + pid))
return tracks, bool(n) and len(tracks) >= int(n.group(1))
state = json.loads(m.group(1))["props"]["pageProps"]["state"]
tracks = {t["uri"] for t in state["data"]["entity"].get("trackList", []) if t.get("uri")}
try:
token = state["settings"]["session"]["accessToken"]
full = json.loads(req("https://spclient.wg.spotify.com/playlist/v2/playlist/%s?from=0&length=1000" % pid,
Authorization="Bearer " + token, Accept="application/json"))
items = {i["uri"] for i in full.get("contents", {}).get("items", []) if i.get("uri")}
if items and len(items) >= int(full.get("length") or 0):
return items, True
except (OSError, ValueError, KeyError, TypeError) as e:
log.warning("Spotify full playlist: %s (using the first %d songs)", e, len(tracks))
return tracks, False
def poll_spotify(snap, cfg):
checks = [c for t in cfg["tiles"] for c in t.get("checks", []) if c["type"] == "spotify"]
if not checks:
return
if time.time() - snap.spotify_tracks_time > 6 * 3600: # the playlist changes weekly
def refresh():
try:
snap.spotify_tracks, snap.spotify_complete = playlist_tracks(checks[0]["playlist"])
except (OSError, ValueError, KeyError, AttributeError, TypeError) as e:
log.warning("Spotify playlist list: %s", e)
snap.spotify_tracks_time = time.time()
if time.time() - snap.spotify_tracks_time > 3600:
refresh()
# Never "tell" Spotify unless it is running: that would open it.
running = subprocess.run(["/usr/bin/pgrep", "-xq", "Spotify"], timeout=5).returncode == 0
if not running:
@@ -1630,6 +1640,8 @@ def poll_spotify(snap, cfg):
parts = (r.stdout.strip().split("|") + ["", "", "", ""])[:4]
snap.spotify = {"running": True, "state": parts[0], "track": parts[1], "name": parts[2], "artist": parts[3]}
if parts[1] and (not snap.spotify_recent or snap.spotify_recent[-1] != parts[1]):
if parts[1] not in snap.spotify_tracks and time.time() - snap.spotify_tracks_time > 60:
refresh() # a song just added to the playlist?
snap.spotify_recent.append(parts[1])