Skip to content

Instantly share code, notes, and snippets.

Last active January 17, 2021 01:08
Show Gist options
  • Save mooware/4860529d59f296a62fb8fe882b943c29 to your computer and use it in GitHub Desktop.
Save mooware/4860529d59f296a62fb8fe882b943c29 to your computer and use it in GitHub Desktop.
Simple application to open a multitwitch (or similar site) for an SRL race
from bottle import *
from urllib.request import urlopen
import json
import re
DEFAULT_HOSTS = ['', '', '']
SRL_API = ""
SANITY_RE = re.compile('[0-9a-z]+')
<title>SRL Races</title>
% for race in races:
<a href="{{race['id']}}">#srl-{{race['id']}} ({{race['statetext']}})</a>
{{' - ' + race['goal'] if race['goal'] else ''}}
% for i, entrant in enumerate(race['entrants'].values()):
{{'/' if i != 0 else ''}}
% if entrant['twitch']:
<a href="{{entrant['twitch']}}">{{entrant['displayname']}}</a>
% else:
% end
% end
% for j, host in enumerate(hosts):
{{'/' if j != 0 else ''}}
<a href="{{host}}/{{race['id']}}">{{host}}</a>
% end
% end
def get_json(url):
req = urlopen(url)
reqdata =
return json.loads(reqdata)
def redirect_to_index():
# we need trailing slash for relative urls
return redirect('/srlmulti/')
def index():
data = get_json(SRL_API)
races = (r for r in data['races'] if r['state'] < RACE_STATE_COMPLETE)
return template(HTML_INDEX_TEMPLATE, races=races, hosts=DEFAULT_HOSTS)
def showmulti(race_id):
return showmulti_with_host(DEFAULT_HOSTS[0], race_id)
def showmulti_with_host(host, race_id):
race_id = race_id.strip()
if race_id.startswith('srl-'):
race_id = race_id[4:]
if not SANITY_RE.fullmatch(race_id):
abort(400, 'invalid format for race id')
# request might fail with exception, let it go through
data = get_json(SRL_API + race_id)
# srl api returns empty json object for invalid race id
if not data:
abort(404, "race id '" + race_id + "' not found")
streams = (v['twitch'] for v in data['entrants'].values() if v['twitch'])
return redirect('https://' + host + '/' + '/'.join(streams))
if __name__ == '__main__':
run(host='localhost', port=8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment