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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
#!/usr/bin/env python3
from html.parser import HTMLParser
from urllib.request import urlopen
import json
class Spotify:
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://open.spotify.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("spotify 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:
Spotify.ldjson=data
return
def fmt_dur(dur):
h, m, s = 0, 0, 0
m = dur[2:].split("M")
s = int(m[1][:-1])
m = int(m[0])
if m >= 60:
h = m // 60
m = round((m / 60 - h) * 60)
return f"{h}h {m}m {s}s"
elif h == 0 and m == 0 and s == 0:
return "LIVE"
elif m == 0 and s != 0:
return f"{s}s"
elif s == 0:
return f"{m}m"
else:
return f"{m}m {s}s"
def spotify(self, url):
# self.util.mesg("dbg hello")
url = url.rstrip("\x01")
p = self.parseprop()
# use premature optimization? it should be SLIGHTLY faster
if self.premature_optimization:
url_h, data = urlopen(url), b""
# <body> appears on approximately line 21 or 22, so we read 24 lines to be safe (23-25 should be license comment)
# I tried to read byte amounts but it's hard to make sure no invalid utf8 bytes happen due to partial reads
for i in range(24):
data += url_h.readline()
data = data.decode() # bytes to utf-8
url_h.close()
else:
# just read all of the html
data = urlopen(url).read().decode()
# print(f"\x1b[31m my data is: {data}\x1b[0m")
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"
# print(ansi_string)
# return irc_string, True
#irc_string = f"[\x0303Youtube\x03] \x02{y['title']}\x02 ({y['duration']}) uploaded by \x1d{y['channelName']}\x1d on {y['uploadDate']}, {y['interactionCount']:,} views"
#ansi_string = f"[\x1b[32mYoutube\x1b[0m] \x1b[1m{y['title']}\x1b[0m ({y['duration']}) uploaded by \x1b[03m{y['channelName']}\x1b[0m on {y['uploadDate']}, {y['interactionCount']:,} views"
#print(ansi_string)
data=(json.loads(Spotify.ldjson))
#'@type': 'MusicRecording', '@id': 'https://open.spotify.com/track/7hoyhyjcZvOCC9Tv9JgRhr', 'url': 'https://open.spotify.com/track/7hoyhyjcZvOCC9Tv9JgRhr', 'name': 'YO HO (PEG THE POOPDECK)', 'description': 'Listen to YO HO (PEG THE POOPDECK) on Spotify. Song · TheDooo · 2023', 'datePublished': '2023-08-30'
#Listen to The Evolution of Tears on Spotify · Album · The Gentle Men · 2021 · 10 songs
type=data["@type"]
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 · ")
elif artists.startswith("Album · "):
artists=artists.removeprefix("Album · ")
#removes the "10 songs" part
artists=artists[::-1].split(" · ",1)[1][::-1]
artists=artists.removesuffix(f" · {date[:4]}")
#print(type,id,name,"|"+artists+"|",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"
return irc_string, False
if __name__ == "__main__":
import sys
Spotify.premature_optimization = False
Spotify.spotify(Spotify, sys.argv[1])
|