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:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user