diff options
Diffstat (limited to 'applemusic.py')
-rwxr-xr-x | applemusic.py | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/applemusic.py b/applemusic.py new file mode 100755 index 0000000..9e10ab9 --- /dev/null +++ b/applemusic.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 +from html.parser import HTMLParser +from urllib.request import urlopen +from urllib.parse import urlencode, urlparse +from json import loads as json_loads + + +class AppleMusic: + video_type = "" + + def mesg(self, msg, t=None): + self.util.mesg(msg, t) + + def match_urls(self, str): + r = [i for i in str.split() if "https://music.apple.com" in i] + r = list(dict.fromkeys(r)) + n = 0 + for i in r: + if not i.startswith("http"): + r.pop(n) + n += 1 + + return r + + class parseprop(HTMLParser): + def __init__(self): + # print("applemusic parse init") + HTMLParser.__init__(self) + self.ldjson = False + + def handle_starttag(self, tag, attrs): + if tag == "script" and ("type", "application/ld+json") in attrs: + self.ldjson = True + else: + self.ldjson = False + + def handle_endtag(self, tag): + self.ldjson = False + + def handle_data(self, data): + if self.ldjson: + AppleMusic.ldjson = data + return + + def is_album(self, url): + return "/album/" in url + + def applemusic_oembed(self, url): + url = urlparse(url) + url = url.scheme + "://" + url.netloc + url.path + url = f"https://music.apple.com/api/oembed?{urlencode([('url',url),('format','json')])}" + data = urlopen(url).read().decode() + # print(data) + data = json_loads(data) + try: + artist = data["author_name"] + title = data["title"].removesuffix(" by " + artist) + except KeyError: + title = "" + artist = "" + return title, artist + + def applemusic_ldjson(self, url): + p = self.parseprop() + data = urlopen(url).read().decode() + p.feed(data) + # print(AppleMusic.ldjson) + data = json_loads(AppleMusic.ldjson) + # print(data) + title = data["name"] + artists = data["audio"]["byArtist"] + artists = [i["name"] for i in artists] + artists = ", ".join(artists[:-1]) + " & " + artists[-1] + return title, artists, data + + def applemusic(self, url): + # self.util.mesg("dbg hello") + url = url.rstrip("\x01") + title = "" + artist = "" + irc_string = "[\x0304AppleMusic\x03] \x0307ERROR:\x0308 got no data from server! \x0315(check your URL for typos!)\x03" + ansi_string = "[\x1b[31mAppleMusic\x1b[0m] \x1b[33;2mERROR:\x1b[33;1m got no data from server! \x1b[37;2m(check your URL for typos!)\x1b[0m" + if self.is_album(url): + title, artist = self.applemusic_oembed(url) + else: + title, artist, data = self.applemusic_ldjson(url) + try: + type = data["@type"] + except KeyError: + print(ansi_string) + return irc_string, True + + if title == "": + print(ansi_string) + return irc_string, True + irc_string = ( + f"[\x0303AppleMusic\x03] \x02{title}\x02 uploaded by \x1d{artist}\x1d" + ) + ansi_string = f"[\x1b[32mAppleMusic\x1b[0m] \x1b[1m{title}\x1b[0m uploaded by \x1b[03m{artist}\x1b[0m" + # """ + # irc_string="dummy";ansi_string="dummy" + print(ansi_string) + return irc_string, False + + +if __name__ == "__main__": + import sys + + # AppleMusic.applemusic(AppleMusic, sys.argv[1]) + AppleMusic().applemusic(sys.argv[1]) |