-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamscrobbler.py
More file actions
149 lines (124 loc) · 4.29 KB
/
streamscrobbler.py
File metadata and controls
149 lines (124 loc) · 4.29 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
import re
import urllib.request
import urllib.error
import urllib.parse
# this is the function you should call with the url to get all data sorted as a object in the return
def get_server_info(url):
if url.endswith(".pls") or url.endswith("listen.pls?sid=1"):
address = check_pls(url)
else:
address = url
if isinstance(address, str):
meta_interval = get_all_data(address)
else:
meta_interval = {"status": 0, "metadata": None}
return meta_interval
def get_all_data(address):
status = 0
request = urllib.request.Request(address)
user_agent = "iTunes/9.1.1"
request.add_header("User-Agent", user_agent)
request.add_header("icy-metadata", 1)
try:
response = urllib.request.urlopen(request, timeout=6)
headers = dict(response.info())
if "server" in headers:
shoutcast = headers["server"]
elif "X-Powered-By" in headers:
shoutcast = headers["X-Powered-By"]
elif "icy-notice1" in headers:
shoutcast = headers["icy-notice2"]
else:
shoutcast = True
if isinstance(shoutcast, bool) and shoutcast:
status = 1
metadata = shoutcast_check(response, headers, True)
elif "SHOUTcast" in shoutcast:
status = 1
metadata = shoutcast_check(response, headers, False)
elif "Icecast" or "137" or "StreamMachine" in shoutcast:
status = 1
metadata = shoutcast_check(response, headers, True)
else:
metadata = False
response.close()
return {"status": status, "metadata": metadata}
except urllib.error.HTTPError as e:
print((" Error, HTTPError = " + str(e.code)))
return {"status": status, "metadata": None}
except urllib.error.URLError as e:
print((" Error, URLError: " + str(e.reason)))
return {"status": status, "metadata": None}
except Exception as err:
print((" Error: " + str(err)))
return {"status": status, "metadata": None}
def check_pls(address):
try:
stream = None
response = urllib.request.urlopen(address, timeout=2)
for line in response:
if line.startswith(b"File1="):
stream = line.decode()
response.close()
if stream:
return stream[6:].strip("\n")
else:
return False
except Exception:
return False
def shoutcast_check(response, headers, is_old):
bitrate = None
contenttype = None
if "icy-br" in headers:
if is_old:
bitrate = headers["icy-br"].split(",")[0]
else:
bitrate = headers["icy-br"]
bitrate = bitrate.rstrip()
if "icy-metaint" in headers:
icy_metaint_header = headers["icy-metaint"]
else:
icy_metaint_header = None
if "Content-Type" in headers:
contenttype = headers["Content-Type"].rstrip()
elif "content-type" in headers:
contenttype = headers["content-type"].rstrip()
if icy_metaint_header:
metaint = int(icy_metaint_header)
read_buffer = metaint + 255
content = response.read(read_buffer)
start = "StreamTitle='"
end = "';"
try:
title = (
re.search(bytes("%s(.*)%s" % (start, end), "utf-8"), content[metaint:])
.group(1)
.decode("utf-8")
)
title = (
re.sub("StreamUrl='.*?';", "", title)
.replace("';", "")
.replace("StreamUrl='", "")
)
title = re.sub("&artist=.*", "", title)
title = re.sub("http://.*", "", title)
title.rstrip()
except Exception as err:
print(("songtitle error: " + str(err)))
title = content[metaint:].split(b"'")[1]
return {"song": title, "bitrate": bitrate, "contenttype": contenttype}
else:
print("No metaint")
return False
def strip_tags(text):
finished = 0
while not finished:
finished = 1
start = text.find("<")
if start >= 0:
stop = text[start:].find(">")
if stop >= 0:
text = text[:start] + text[start + stop + 1 :]
finished = 0
return text