fix: radio issue

This commit is contained in:
Terry Geng 2020-03-08 23:08:47 +08:00
parent d3fc12ef9c
commit e2f6de5066
3 changed files with 24 additions and 11 deletions

View File

@ -353,7 +353,7 @@ def cmd_play_radio(bot, user, text, command, parameter):
parameter = parameter.split()[0] parameter = parameter.split()[0]
url = util.get_url_from_input(parameter) url = util.get_url_from_input(parameter)
if url: if url:
music_wrapper = get_item_wrapper_from_scrap(bot, type='radio', url=url) music_wrapper = get_item_wrapper_from_scrap(bot, type='radio', url=url, user=user)
var.playlist.append(music_wrapper) var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string()) log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
@ -667,7 +667,7 @@ def cmd_last(bot, user, text, command, parameter):
if len(var.playlist) > 0: if len(var.playlist) > 0:
bot.interrupt() bot.interrupt()
var.playlist.point_to(len(var.playlist) - 1) var.playlist.point_to(len(var.playlist) - 1 - 1)
else: else:
bot.send_msg(constants.strings('queue_empty'), text) bot.send_msg(constants.strings('queue_empty'), text)
@ -775,7 +775,7 @@ def cmd_repeat(bot, user, text, command, parameter):
var.playlist.current_index + 1, var.playlist.current_index + 1,
music music
) )
log.info("bot: add to playlist: " + music.format_debug_string) log.info("bot: add to playlist: " + music.format_debug_string())
bot.send_msg(constants.strings("repeat", song=music.format_song_string(), n=str(repeat)), text) bot.send_msg(constants.strings("repeat", song=music.format_song_string(), n=str(repeat)), text)

View File

@ -22,12 +22,15 @@ def get_radio_server_description(url):
url_shoutcast = base_url + '/stats?json=1' url_shoutcast = base_url + '/stats?json=1'
title_server = None title_server = None
try: try:
r = requests.get(url_shoutcast, timeout=5) r = requests.get(url_shoutcast, timeout=10)
data = r.json() data = r.json()
title_server = data['servertitle'] title_server = data['servertitle']
return title_server return title_server
# logging.info("TITLE FOUND SHOUTCAST: " + title_server) # logging.info("TITLE FOUND SHOUTCAST: " + title_server)
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.Timeout) as e: except (requests.exceptions.ConnectionError,
requests.exceptions.HTTPError,
requests.exceptions.ReadTimeout,
requests.exceptions.Timeout) as e:
error_traceback = traceback.format_exc() error_traceback = traceback.format_exc()
error = error_traceback.rstrip().split("\n")[-1] error = error_traceback.rstrip().split("\n")[-1]
log.debug("radio: unsuccessful attempts on fetching radio description (shoutcast): " + error) log.debug("radio: unsuccessful attempts on fetching radio description (shoutcast): " + error)
@ -35,7 +38,7 @@ def get_radio_server_description(url):
return False # ? return False # ?
try: try:
r = requests.get(url_icecast, timeout=5) r = requests.get(url_icecast, timeout=10)
data = r.json() data = r.json()
source = data['icestats']['source'] source = data['icestats']['source']
if type(source) is list: if type(source) is list:
@ -45,7 +48,10 @@ def get_radio_server_description(url):
title_server += ' - ' + source['server_description'] title_server += ' - ' + source['server_description']
# logging.info("TITLE FOUND ICECAST: " + title_server) # logging.info("TITLE FOUND ICECAST: " + title_server)
return title_server return title_server
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.Timeout) as e: except (requests.exceptions.ConnectionError,
requests.exceptions.HTTPError,
requests.exceptions.ReadTimeout,
requests.exceptions.Timeout) as e:
error_traceback = traceback.format_exc() error_traceback = traceback.format_exc()
error = error_traceback.rstrip().split("\n")[-1] error = error_traceback.rstrip().split("\n")[-1]
log.debug("radio: unsuccessful attempts on fetching radio description (icecast): " + error) log.debug("radio: unsuccessful attempts on fetching radio description (icecast): " + error)
@ -58,7 +64,7 @@ def get_radio_title(url):
log.debug("radio: fetching radio server description") log.debug("radio: fetching radio server description")
try: try:
r = requests.get(url, headers={'Icy-MetaData': '1'}, stream=True, timeout=5) r = requests.get(url, headers={'Icy-MetaData': '1'}, stream=True, timeout=10)
icy_metaint_header = int(r.headers['icy-metaint']) icy_metaint_header = int(r.headers['icy-metaint'])
r.raw.read(icy_metaint_header) r.raw.read(icy_metaint_header)
@ -71,8 +77,13 @@ def get_radio_title(url):
title = m.group(1) title = m.group(1)
if title: if title:
return title.decode() return title.decode()
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as e: except (requests.exceptions.ConnectionError,
pass requests.exceptions.HTTPError,
requests.exceptions.ReadTimeout,
requests.exceptions.Timeout) as e:
error_traceback = traceback.format_exc()
error = error_traceback.rstrip().split("\n")[-1]
log.debug("radio: unsuccessful attempts on fetching radio title (icy): " + e)
return url return url
@ -125,6 +136,8 @@ class RadioItem(BaseItem):
dict['url'] = self.url dict['url'] = self.url
dict['title'] = self.title dict['title'] = self.title
return dict
def format_debug_string(self): def format_debug_string(self):
return "[radio] {name} ({url})".format( return "[radio] {name} ({url})".format(
name=self.title, name=self.title,

View File

@ -215,7 +215,7 @@ class MumbleBot:
# use the first word as a command, the others one as parameters # use the first word as a command, the others one as parameters
if len(message) > 0: if len(message) > 0:
command = message[0] command = message[0].lower()
parameter = '' parameter = ''
if len(message) > 1: if len(message) > 1:
parameter = message[1].rstrip() parameter = message[1].rstrip()