2023-04-20 19:21:59 +02:00
|
|
|
"""
|
|
|
|
Apple Music.
|
|
|
|
|
|
|
|
Scraping the website directly.
|
|
|
|
|
|
|
|
- Why not using Apple Music API?
|
|
|
|
- It requires Apple Developer Membership ($99 per year) to obtain a token.
|
|
|
|
|
|
|
|
"""
|
2024-06-02 14:50:07 -04:00
|
|
|
|
2023-04-20 19:21:59 +02:00
|
|
|
import json
|
|
|
|
import logging
|
2023-08-01 23:35:22 -04:00
|
|
|
|
2023-04-20 19:21:59 +02:00
|
|
|
import dateparser
|
|
|
|
|
2023-08-01 23:35:22 -04:00
|
|
|
from catalog.common import *
|
|
|
|
from catalog.models import *
|
2024-07-13 00:16:47 -04:00
|
|
|
from common.models.lang import (
|
2024-11-30 13:23:21 -05:00
|
|
|
SITE_DEFAULT_LANGUAGE,
|
|
|
|
SITE_PREFERRED_LANGUAGES,
|
2024-07-13 00:16:47 -04:00
|
|
|
detect_language,
|
|
|
|
)
|
|
|
|
from common.models.misc import uniq
|
2023-08-01 23:35:22 -04:00
|
|
|
|
|
|
|
from .douban import *
|
2023-04-20 19:21:59 +02:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@SiteManager.register
|
|
|
|
class AppleMusic(AbstractSite):
|
|
|
|
SITE_NAME = SiteName.AppleMusic
|
|
|
|
ID_TYPE = IdType.AppleMusic
|
2023-05-28 12:16:56 -04:00
|
|
|
URL_PATTERNS = [
|
|
|
|
r"https://music\.apple\.com/[a-z]{2}/album/[\w%-]+/(\d+)",
|
|
|
|
r"https://music\.apple\.com/[a-z]{2}/album/(\d+)",
|
|
|
|
r"https://music\.apple\.com/album/(\d+)",
|
2023-05-26 18:19:37 +02:00
|
|
|
]
|
2023-04-20 19:21:59 +02:00
|
|
|
WIKI_PROPERTY_ID = "?"
|
|
|
|
DEFAULT_MODEL = Album
|
2024-01-06 00:12:43 -05:00
|
|
|
headers = {
|
|
|
|
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:107.0) Gecko/20100101 Firefox/107.0",
|
|
|
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
2024-06-05 00:00:54 -04:00
|
|
|
"Accept-Language": BasicDownloader.get_accept_language(),
|
2024-01-06 00:12:43 -05:00
|
|
|
"Accept-Encoding": "gzip, deflate",
|
|
|
|
"Connection": "keep-alive",
|
|
|
|
"DNT": "1",
|
|
|
|
"Upgrade-Insecure-Requests": "1",
|
|
|
|
"Cache-Control": "no-cache",
|
|
|
|
}
|
2023-04-20 19:21:59 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def id_to_url(cls, id_value):
|
2023-05-28 12:16:56 -04:00
|
|
|
return f"https://music.apple.com/album/{id_value}"
|
|
|
|
|
2024-07-13 00:16:47 -04:00
|
|
|
def get_locales(self):
|
|
|
|
locales = {}
|
2024-11-30 13:23:21 -05:00
|
|
|
for lang in SITE_PREFERRED_LANGUAGES:
|
2024-07-27 03:22:27 -04:00
|
|
|
match lang:
|
2024-07-13 00:16:47 -04:00
|
|
|
case "zh":
|
|
|
|
locales.update({"zh": ["cn", "tw", "hk", "sg"]})
|
|
|
|
case "en":
|
|
|
|
locales.update({"en": ["us", "gb", "ca"]})
|
|
|
|
case "ja":
|
|
|
|
locales.update({"ja": ["jp"]})
|
|
|
|
case "ko":
|
|
|
|
locales.update({"ko": ["kr"]})
|
|
|
|
case "fr":
|
|
|
|
locales.update({"fr": ["fr", "ca"]})
|
|
|
|
if not locales:
|
|
|
|
locales = {"en": ["us"]}
|
|
|
|
return locales
|
2023-04-20 19:21:59 +02:00
|
|
|
|
|
|
|
def scrape(self):
|
2024-07-13 00:16:47 -04:00
|
|
|
matched_content = None
|
|
|
|
localized_title = []
|
|
|
|
localized_desc = []
|
|
|
|
for lang, locales in self.get_locales().items():
|
|
|
|
for loc in locales: # waterfall thru all locales
|
|
|
|
url = f"https://music.apple.com/{loc}/album/{self.id_value}"
|
|
|
|
try:
|
|
|
|
content = (
|
|
|
|
BasicDownloader(url, headers=self.headers).download().html()
|
|
|
|
)
|
|
|
|
_logger.info(f"got localized content from {url}")
|
|
|
|
elem = content.xpath(
|
|
|
|
"//script[@id='serialized-server-data']/text()"
|
|
|
|
)
|
|
|
|
txt: str = elem[0] # type:ignore
|
|
|
|
page_data = json.loads(txt)[0]
|
|
|
|
album_data = page_data["data"]["sections"][0]["items"][0]
|
|
|
|
title = album_data["title"]
|
|
|
|
brief = album_data.get("modalPresentationDescriptor", {}).get(
|
|
|
|
"paragraphText", ""
|
|
|
|
)
|
2024-07-27 03:22:27 -04:00
|
|
|
tl = detect_language(title + " " + brief)
|
|
|
|
localized_title.append({"lang": tl, "text": title})
|
2024-07-13 00:16:47 -04:00
|
|
|
if brief:
|
2024-07-27 03:22:27 -04:00
|
|
|
localized_desc.append({"lang": tl, "text": brief})
|
2024-11-30 13:23:21 -05:00
|
|
|
if lang == SITE_DEFAULT_LANGUAGE or not matched_content:
|
2024-07-13 00:16:47 -04:00
|
|
|
matched_content = content
|
|
|
|
break
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
if matched_content is None:
|
2023-05-28 12:16:56 -04:00
|
|
|
raise ParseError(self, f"localized content for {self.url}")
|
2024-07-13 00:16:47 -04:00
|
|
|
elem = matched_content.xpath("//script[@id='serialized-server-data']/text()")
|
2023-08-11 11:55:42 -04:00
|
|
|
txt: str = elem[0] # type:ignore
|
|
|
|
page_data = json.loads(txt)[0]
|
2023-04-20 19:21:59 +02:00
|
|
|
album_data = page_data["data"]["sections"][0]["items"][0]
|
|
|
|
title = album_data["title"]
|
2023-05-28 12:16:56 -04:00
|
|
|
brief = album_data.get("modalPresentationDescriptor")
|
|
|
|
brief = brief.get("paragraphText") if brief else None
|
2023-04-20 19:21:59 +02:00
|
|
|
artist_list = album_data["subtitleLinks"]
|
|
|
|
artist = [item["title"] for item in artist_list]
|
|
|
|
|
|
|
|
track_data = page_data["data"]["seoData"]
|
|
|
|
date_elem = track_data.get("musicReleaseDate")
|
2023-08-11 11:55:42 -04:00
|
|
|
release_datetime = dateparser.parse(date_elem.strip()) if date_elem else None
|
2023-04-20 19:21:59 +02:00
|
|
|
release_date = (
|
2023-08-11 11:55:42 -04:00
|
|
|
release_datetime.strftime("%Y-%m-%d") if release_datetime else None
|
2023-04-20 19:21:59 +02:00
|
|
|
)
|
2023-08-11 11:55:42 -04:00
|
|
|
|
2023-05-29 20:55:29 +02:00
|
|
|
track_list = [
|
|
|
|
f"{i}. {track['attributes']['name']}"
|
|
|
|
for i, track in enumerate(track_data["ogSongs"], 1)
|
|
|
|
]
|
2023-04-20 19:21:59 +02:00
|
|
|
duration_list = [
|
2023-08-01 23:35:22 -04:00
|
|
|
track["attributes"].get("durationInMillis", 0)
|
2023-04-20 19:21:59 +02:00
|
|
|
for track in track_data["ogSongs"]
|
|
|
|
]
|
2023-05-26 18:19:37 +02:00
|
|
|
duration = int(sum(duration_list))
|
2023-04-20 19:21:59 +02:00
|
|
|
genre = track_data["schemaContent"].get("genre")
|
|
|
|
if genre:
|
2023-05-26 18:19:37 +02:00
|
|
|
genre = [
|
|
|
|
genre[0]
|
2023-04-20 19:21:59 +02:00
|
|
|
] # apple treat "Music" as a genre. Thus, only the first genre is obtained.
|
|
|
|
|
2024-07-13 00:16:47 -04:00
|
|
|
images = matched_content.xpath("//source[@type='image/jpeg']/@srcset")
|
2023-08-11 11:55:42 -04:00
|
|
|
image_elem: str = images[0] if images else "" # type:ignore
|
2023-04-20 19:21:59 +02:00
|
|
|
image_url = image_elem.split(" ")[0] if image_elem else None
|
|
|
|
|
|
|
|
pd = ResourceContent(
|
|
|
|
metadata={
|
2024-07-13 00:16:47 -04:00
|
|
|
"localized_title": uniq(localized_title),
|
|
|
|
"localized_description": uniq(localized_desc),
|
2023-04-20 19:21:59 +02:00
|
|
|
"title": title,
|
2023-05-28 12:16:56 -04:00
|
|
|
"brief": brief,
|
2023-04-20 19:21:59 +02:00
|
|
|
"artist": artist,
|
|
|
|
"genre": genre,
|
|
|
|
"release_date": release_date,
|
|
|
|
"track_list": "\n".join(track_list),
|
|
|
|
"duration": duration,
|
|
|
|
"cover_image_url": image_url,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return pd
|