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
73
74
75
76
77
78
79
80
81
82
83
84
85
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])
|