From 02696d69134a2345e17f8182c57837d42bffedb6 Mon Sep 17 00:00:00 2001 From: Pawky Languish Date: Wed, 27 Nov 2024 23:42:45 +0000 Subject: reformat (and add bandcamp) --- spotify.py | 76 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 40 insertions(+), 36 deletions(-) (limited to 'spotify.py') diff --git a/spotify.py b/spotify.py index f376bc1..1586784 100755 --- a/spotify.py +++ b/spotify.py @@ -3,20 +3,17 @@ from html.parser import HTMLParser from urllib.request import urlopen import json + class Spotify: def __init__(self): - self.ldjson=None + self.ldjson = None 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://open.spotify.com/" in i - ] + r = [i for i in str.split() if "https://open.spotify.com/" in i] r = list(dict.fromkeys(r)) n = 0 for i in r: @@ -28,51 +25,58 @@ class Spotify: class parseprop(HTMLParser): def __init__(self): - print("spotify parse init") + # print("spotify parse init") HTMLParser.__init__(self) - self.ldjson=False + 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 + 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_endtag(self, tag): + self.ldjson = False - def handle_data(self,data): - if self.ldjson: - Spotify.ldjson=data - return + def handle_data(self, data): + if self.ldjson: + Spotify.ldjson = data + return def spotify(self, url): url = url.rstrip("\x01") p = self.parseprop() data = urlopen(url).read().decode() p.feed(data) - irc_string = "[\x0304Youtube\x03] \x0307ERROR:\x0308 got no data from server! \x0315(check your URL for typos!)\x03" - ansi_string = "[\x1b[31mYoutube\x1b[0m] \x1b[33;2mERROR:\x1b[33;1m got no data from server! \x1b[37;2m(check your URL for typos!)\x1b[0m" - data=(json.loads(Spotify.ldjson)) - try: type=data["@type"] + irc_string = "[\x0304Spotify\x03] \x0307ERROR:\x0308 got no data from server! \x0315(check your URL for typos!)\x03" + ansi_string = "[\x1b[31mSpotify\x1b[0m] \x1b[33;2mERROR:\x1b[33;1m got no data from server! \x1b[37;2m(check your URL for typos!)\x1b[0m" + data = json.loads(Spotify.ldjson) + try: + type = data["@type"] except KeyError: - print(ansi_string) - return irc_string, True - id=data["@id"] - name=data["name"] - date=data["datePublished"] - artists=data["description"] - artists=artists.removeprefix(f'Listen to {name} on Spotify') - artists=artists.removeprefix('.').strip() - artists=artists.removeprefix('· ') - if artists.startswith("Song · "): artists=artists.removeprefix("Song · ") + print(ansi_string) + return irc_string, True + id = data["@id"] + name = data["name"] + date = data["datePublished"] + artists = data["description"] + artists = ( + artists.removeprefix(f"Listen to {name} on Spotify") + .removeprefix(".") + .strip() + .removeprefix("· ") + ) + if artists.startswith("Song · "): + artists = artists.removeprefix("Song · ") elif artists.startswith("Album · "): - artists=artists.removeprefix("Album · ") - artists=artists[::-1].split(" · ",1)[1][::-1] #removes the "10 songs" part from album - artists=artists.removesuffix(f" · {date[:4]}") #remove the year too - #print(type,id,name,"|"+artists+"|",date) + artists = artists.removeprefix("Album · ")[::-1].split(" · ", 1)[1][ + ::-1 + ] # removes the "10 songs" part from album + artists = artists.removesuffix(f" · {date[:4]}") # remove the year too irc_string = f"[\x0303Spotify\x03] \x02{name}\x02 by \x1d{artists}\x1d published on {date}" ansi_string = f"[\x1b[32mSpotify\x1b[0m] \x1b[1m{name}\x1b[0m by \x1b[03m{artists}\x1b[0m published on {date}" - #print(("Song: " if type=="MusicRecording" else "Album: " if type=="MusicAlbum" else f"Unknown type ({type}): ")+'"'+name+'"'+" by "+'"'+artists+'"'+" released on "+date) - #irc_string="dummy" + # print(("Song: " if type=="MusicRecording" else "Album: " if type=="MusicAlbum" else f"Unknown type ({type}): ")+'"'+name+'"'+" by "+'"'+artists+'"'+" released on "+date) + # irc_string="dummy" print(ansi_string) return irc_string, False -- cgit 1.4.1-2-gfad0