-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradio_stream_reader.py
55 lines (42 loc) · 1.4 KB
/
radio_stream_reader.py
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
import json
from threading import active_count
from urllib.parse import unquote, urlparse
import sys
from http.client import HTTPConnection
from logger import logger
station = ("Yes 93.3", "http://mediacorp.rastream.com/933fm")
def listen_to_station(station, radio_streamer):
url = urlparse(station[1])
conn = HTTPConnection(url.hostname, timeout=4)
conn.request('get', url.path, headers={'Icy-MetaData': '1'})
res = conn.getresponse()
metaint = int(res.getheader('icy-metaint', '0'))
if not metaint:
logger.info('Endpoint does not support icy-metaint')
return
def parse(s):
return json.loads(s) if s.startswith('{') else s
while radio_streamer.running:
try:
res.read(metaint)
l = int(ord(res.read(1))) * 16
if l:
bytes = res.read(l)
txt = bytes.decode()
parts = [[parse(unquote(p.strip(' \'\"'))) for p in phrase.split('=')] for phrase in txt.strip().split(';')[:-1]]
obj = {'title': parts[0][1]}
try:
if 'current_song' in parts[1][1]:
obj['coverUrl'] = parts[1][1]['current_song']['coverUrl']
except Exception as e:
logger.error(e)
radio_streamer.messages.put(obj)
except Exception as e:
logger.info(e)
res.close()
break
if not res.closed:
res.close()
conn.close()
logger.info('closed producer thread {} {}'.format(active_count(), res.closed))
return