fix apple music

This commit is contained in:
cloudwithax 2023-05-01 01:02:43 -04:00
parent f823786029
commit 665d6c13a3
No known key found for this signature in database
GPG Key ID: 5DBE54E45794983E
2 changed files with 54 additions and 25 deletions

View File

@ -20,7 +20,7 @@ if not discord.version_info.major >= 2:
"using 'pip install discord.py'",
)
__version__ = "2.4.1"
__version__ = "2.5a"
__title__ = "pomice"
__author__ = "cloudwithax"
__license__ = "GPL-3.0"

View File

@ -22,6 +22,9 @@ AM_URL_REGEX = re.compile(
AM_SINGLE_IN_ALBUM_REGEX = re.compile(
r"https?://music.apple.com/(?P<country>[a-zA-Z]{2})/(?P<type>album|playlist|song|artist)/(?P<name>.+)/(?P<id>.+)(\?i=)(?P<id2>.+)",
)
AM_SCRIPT_REGEX = re.compile(r'<script.*?src="(/assets/index-.*?)"')
AM_REQ_URL = "https://api.music.apple.com/v1/catalog/{country}/{type}s/{id}"
AM_BASE_URL = "https://api.music.apple.com"
@ -43,11 +46,37 @@ class Client:
if not self.session:
self.session = aiohttp.ClientSession()
async with self.session.get("https://music.apple.com/assets/index-e16a87ba.js") as resp:
# First lets get the raw response from the main page
resp = await self.session.get("https://music.apple.com")
if resp.status != 200:
raise AppleMusicRequestException(
f"Error while fetching results: {resp.status} {resp.reason}",
)
# Looking for script tag that fits criteria
text = await resp.text()
match = re.search(AM_SCRIPT_REGEX, text)
if not match:
raise AppleMusicRequestException(
"Could not find valid script URL in response.",
)
# Found the script file, lets grab our token
result = match.group(1)
asset_url = result
resp = await self.session.get("https://music.apple.com" + asset_url)
if resp.status != 200:
raise AppleMusicRequestException(
f"Error while fetching results: {resp.status} {resp.reason}",
)
text = await resp.text()
match = re.search('"(eyJ.+?)"', text)
if not match: