File size: 2,503 Bytes
a8e9b84 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import re
from typing import Union
import aiohttp
from bs4 import BeautifulSoup
from youtubesearchpython.__future__ import VideosSearch
class AppleAPI:
def __init__(self):
self.regex = r"^(https:\/\/music.apple.com\/)(.*)$"
self.base = "https://music.apple.com/in/playlist/"
async def valid(self, link: str):
if re.search(self.regex, link):
return True
else:
return False
async def track(self, url, playid: Union[bool, str] = None):
if playid:
url = self.base + url
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status != 200:
return False
html = await response.text()
soup = BeautifulSoup(html, "html.parser")
search = None
for tag in soup.find_all("meta"):
if tag.get("property", None) == "og:title":
search = tag.get("content", None)
if search is None:
return False
results = VideosSearch(search, limit=1)
for result in (await results.next())["result"]:
title = result["title"]
ytlink = result["link"]
vidid = result["id"]
duration_min = result["duration"]
thumbnail = result["thumbnails"][0]["url"].split("?")[0]
track_details = {
"title": title,
"link": ytlink,
"vidid": vidid,
"duration_min": duration_min,
"thumb": thumbnail,
}
return track_details, vidid
async def playlist(self, url, playid: Union[bool, str] = None):
if playid:
url = self.base + url
playlist_id = url.split("playlist/")[1]
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status != 200:
return False
html = await response.text()
soup = BeautifulSoup(html, "html.parser")
applelinks = soup.find_all("meta", attrs={"property": "music:song"})
results = []
for item in applelinks:
try:
xx = (((item["content"]).split("album/")[1]).split("/")[0]).replace(
"-", " "
)
except:
xx = ((item["content"]).split("album/")[1]).split("/")[0]
results.append(xx)
return results, playlist_id
|