diff options
author | Pawky Languish | 2024-11-27 23:42:45 +0000 |
---|---|---|
committer | Pawky Languish | 2024-11-27 23:42:45 +0000 |
commit | 02696d69134a2345e17f8182c57837d42bffedb6 (patch) | |
tree | 6971631fe41b5e5afc5d1ee46efb98236a6e347a | |
parent | 82b4772f1c0bbb9772c51598625958eef3676b4f (diff) |
reformat (and add bandcamp)
-rwxr-xr-x | bandcamp.py | 86 | ||||
-rw-r--r-- | config.py | 4 | ||||
-rwxr-xr-x | soundcloud.py | 37 | ||||
-rwxr-xr-x | spotify.py | 76 | ||||
-rwxr-xr-x | youtube.py | 16 |
5 files changed, 157 insertions, 62 deletions
diff --git a/bandcamp.py b/bandcamp.py new file mode 100755 index 0000000..7b32b3f --- /dev/null +++ b/bandcamp.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 +from html.parser import HTMLParser +from urllib.request import urlopen +import json + + +class Bandcamp: + + def __init__(self): + 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://" in i + and ("bandcamp.com/album/" in i or "bandcamp.com/track/" 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("bandcamp 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: + Bandcamp.ldjson = data + return + + def bandcamp(self, url): + url = url.rstrip("\x01") + p = self.parseprop() + data = urlopen(url).read().decode() + p.feed(data) + irc_string = "[\x0304BandCamp\x03] \x0307ERROR:\x0308 got no data from server! \x0315(check your URL for typos!)\x03" + ansi_string = "[\x1b[31mBandCamp\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(Bandcamp.ldjson) + # print(data) + # for i in data: + # print(i) + try: + type = data["@type"] + except KeyError: + print(ansi_string) + return irc_string, True + id = data["@id"] + name = data["name"] + date = data["datePublished"] + artists = data["byArtist"]["name"] + # 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 · ")[::-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"[\x0303BandCamp\x03] \x02{name}\x02 by \x1d{artists}\x1d published on {date}" + ansi_string = f"[\x1b[32mBandCamp\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) + print(ansi_string) + return irc_string, False + + +if __name__ == "__main__": + import sys + + Bandcamp.bandcamp(Bandcamp, sys.argv[1]) diff --git a/config.py b/config.py index 27cf5c4..28902ee 100644 --- a/config.py +++ b/config.py @@ -4,6 +4,7 @@ import importlib, sys if __name__ == "local_config": config = importlib.reload(sys.modules["config"]).config else: + class config: # dummy, so the local config can simply be a copy of this template ... @@ -13,7 +14,7 @@ class config(config): nick = "pawbot" username = "pawky_bot" # you should probably indicate yourself to be the owner of the bot, in username, or realname, or both - realname = "pawky's bot" + realname = "pawky's song-of-the-day bot, use ~help" source = "/home/pawky/git/sotdbot" # so far only used for ctcp response gitdir = "./" # where is the bot git dir? currently only used for version (git commit hash, assuming latest commit == latest version) @@ -65,6 +66,7 @@ class config(config): "chghost", # uh, same, I think? ] + # cap-notify draft/account-registration draft/channel-rename draft/persistence draft/read-marker echo-message ergo.chat/nope extended-join extended-monitor invite-notify labeled-response sasl=PLAIN,EXTERNAL server-time setname userhost-in-names znc.in/self-message # message-tags multi-prefix account-tag batch away-notify account-notify chghost diff --git a/soundcloud.py b/soundcloud.py index de0e930..ae17f9d 100755 --- a/soundcloud.py +++ b/soundcloud.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 -from urllib.parse import urlencode,urlparse +from urllib.parse import urlencode, urlparse from urllib.request import urlopen from json import loads as json_loads + class SoundCloud: video_type = "" @@ -10,11 +11,7 @@ class SoundCloud: self.util.mesg(msg, t) def match_urls(self, str): - r = [ - i - for i in str.split() - if "https://soundcloud.com" in i - ] + r = [i for i in str.split() if "https://soundcloud.com" in i] r = list(dict.fromkeys(r)) n = 0 for i in r: @@ -27,27 +24,31 @@ class SoundCloud: def soundcloud(self, url): # self.util.mesg("dbg hello") url = url.rstrip("\x01") - url=urlparse(url) - url=url.scheme+"://"+url.netloc+url.path + url = urlparse(url) + url = url.scheme + "://" + url.netloc + url.path url = f"https://soundcloud.com/oembed?{urlencode([('url',url),('format','json')])}" - data=urlopen(url).read().decode() - data=json_loads(data) - """ {'version': 1.0, 'type': 'rich', 'provider_name': 'SoundCloud', 'provider_url': 'https://soundcloud.com', 'height': 400, 'width': '100%', 'title': 'Doses And Mimosas - Cherub (Mocha Remix) by Mocha Music', 'description': 'One of my favorite songs ever with a dubstep twist! Hope you enjoy :)', 'thumbnail_url': 'https://i1.sndcdn.com/artworks-768QwHQ4tGr0P4wc-iH0Zww-t500x500.jpg', 'html': '<iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F1881169554&show_artwork=true"></iframe>', 'author_name': 'Mocha Music', 'author_url': 'https://soundcloud.com/mochamusic11' }""" - #print(data["title"].removesuffix(" by "+data["author_name"]),data["author_name"]) + data = urlopen(url).read().decode() + data = json_loads(data) + print(data) + # print(data["title"].removesuffix(" by "+data["author_name"]),data["author_name"]) try: - artist=data["author_name"];title=data["title"].removesuffix(" by "+artist) + artist = data["author_name"] + title = data["title"].removesuffix(" by " + artist) except KeyError: - title="";artist="" - print(title.removesuffix(" by "+artist),"|",artist) + title = "" + artist = "" + print(title.removesuffix(" by " + artist), "|", artist) if title == "": irc_string = "[\x0304SoundCloud\x03] \x0307ERROR:\x0308 got no data from server! \x0315(check your URL for typos!)\x03" ansi_string = "[\x1b[31mSoundCloud\x1b[0m] \x1b[33;2mERROR:\x1b[33;1m got no data from server! \x1b[37;2m(check your URL for typos!)\x1b[0m" print(ansi_string) return irc_string, True - irc_string = f"[\x0303SoundCloud\x03] \x02{title}\x02 uploaded by \x1d{artist}\x1d" + irc_string = ( + f"[\x0303SoundCloud\x03] \x02{title}\x02 uploaded by \x1d{artist}\x1d" + ) ansi_string = f"[\x1b[32mSoundCloud\x1b[0m] \x1b[1m{title}\x1b[0m uploaded by \x1b[03m{artist}\x1b[0m" - #""" - #irc_string="dummy";ansi_string="dummy" + # """ + # irc_string="dummy";ansi_string="dummy" print(ansi_string) return irc_string, False 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 diff --git a/youtube.py b/youtube.py index 475a36d..41dd18b 100755 --- a/youtube.py +++ b/youtube.py @@ -114,13 +114,15 @@ class YouTube: self.video_type = ( "clip" if self.is_clip(url) - else "shorts" - if self.is_ytshorts(url) - else "music" - if self.is_ytmusic(url) - else "embed" - if self.is_embed(url) - else "video" + else ( + "shorts" + if self.is_ytshorts(url) + else ( + "music" + if self.is_ytmusic(url) + else "embed" if self.is_embed(url) else "video" + ) + ) ) video_type = self.video_type if video_type == "embed": |