9 log = logging.getLogger("bot")
11 def get_radio_server_description(url):
14 p = re.compile('(https?\:\/\/[^\/]*)', re.IGNORECASE)
15 res = re.search(p, url)
16 base_url = res.group(1)
17 url_icecast = base_url + '/status-json.xsl'
18 url_shoutcast = base_url + '/stats?json=1'
21 r = requests.get(url_shoutcast, timeout=5)
23 title_server = data['servertitle']
25 # logging.info("TITLE FOUND SHOUTCAST: " + title_server)
26 except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.Timeout) as e:
27 error_traceback = traceback.format_exc()
28 error = error_traceback.rstrip().split("\n")[-1]
29 log.debug("radio: unsuccessful attempts on fetching radio description (shoutcast): " + error)
34 r = requests.get(url_icecast, timeout=5)
36 source = data['icestats']['source']
37 if type(source) is list:
39 title_server = source['server_name']
40 if 'server_description' in source:
41 title_server += ' - ' + source['server_description']
42 # logging.info("TITLE FOUND ICECAST: " + title_server)
44 except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.Timeout) as e:
45 error_traceback = traceback.format_exc()
46 error = error_traceback.rstrip().split("\n")[-1]
47 log.debug("radio: unsuccessful attempts on fetching radio description (icecast): " + error)
52 def get_radio_title(url):
54 r = requests.get(url, headers={'Icy-MetaData': '1'}, stream=True, timeout=5)
55 icy_metaint_header = int(r.headers['icy-metaint'])
56 r.raw.read(icy_metaint_header)
58 metadata_length = struct.unpack('B', r.raw.read(1))[0] * 16 # length byte
59 metadata = r.raw.read(metadata_length).rstrip(b'\0')
60 logging.info(metadata)
61 # extract title from the metadata
62 m = re.search(br"StreamTitle='([^']*)';", metadata)
67 except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as e: