From 5657ac43340ea3904929fb0684b915c690658d63 Mon Sep 17 00:00:00 2001 From: Josh Washburne Date: Mon, 20 May 2019 15:50:35 -0400 Subject: [PATCH] Added DJ control script. --- contrib/djcontrol/djcontrol.py | 111 +++++++++++++++++++++++++++++ contrib/djcontrol/requirements.txt | Bin 0 -> 164 bytes 2 files changed, 111 insertions(+) create mode 100644 contrib/djcontrol/djcontrol.py create mode 100644 contrib/djcontrol/requirements.txt diff --git a/contrib/djcontrol/djcontrol.py b/contrib/djcontrol/djcontrol.py new file mode 100644 index 0000000..9889cb6 --- /dev/null +++ b/contrib/djcontrol/djcontrol.py @@ -0,0 +1,111 @@ +''' +djcontrol.py + +This is the helper script that glues the webapi/database to the liquidsoap +application. We make RESTful requests here to get the next song and to report +when a song has been played. +''' + +import argparse +import json + +import requests + + +DJ_TOKEN = 'place_generated_token_here' + +API_URL = 'https://savepointradio.net/api/' + +RADIO_NAME = 'Save Point Radio' + +HEADERS = { + 'Content-Type': 'application/json; charset=utf-8', + 'Authorization': 'Token {}'.format(DJ_TOKEN) +} + +ANNOTATE = 'annotate:req_id="{}",type="{}",artist="{}",title="{}",game="{}":{}' + + +def clean_quotes(unclean_string): + ''' + Escapes quotes for use in the Liquidsoap parser. + ''' + return unclean_string.replace('"', '\\"') + + +def beautify_artists(artists): + ''' + Turns a list of one or more artists into a proper English listing. + ''' + output = ', ' + if len(artists) == 2: + output = ' & ' + return clean_quotes(output.join(artists)) + + +description = 'Lets the DJ control the radio.' + +parser = argparse.ArgumentParser(description=description) +subparsers = parser.add_subparsers(dest='command') + +parser_next = subparsers.add_parser('next', + help='Gets the next song from the radio.') +parser_played = subparsers.add_parser('played', + help='Tells the radio which song just played.') +parser_played.add_argument('request', + help='Song request ID number.', + nargs=1, + type=int) + +args = parser.parse_args() + +if args.command == 'next': + try: + r = requests.get(API_URL + 'next/', + headers=HEADERS, + timeout=5) + r.encoding = 'utf-8' + r.raise_for_status() + except requests.exceptions.HTTPError as errh: + print('Http Error: {}'.format(errh)) + except requests.exceptions.ConnectionError as errc: + print('Error Connecting: {}'.format(errc)) + except requests.exceptions.Timeout as errt: + print('Timeout Error: {}'.format(errt)) + except requests.exceptions.RequestException as err: + print('Error: {}'.format(err)) + else: + req = json.loads(r.text) + song = req['song'] + if song['song_type'] == 'J': + artist = RADIO_NAME + title = 'Jingle' + game = RADIO_NAME + else: + artist = beautify_artists(song['artists']) + title = clean_quotes(song['title']) + game = clean_quotes(song['game']) + print(ANNOTATE.format(req['id'], + song['song_type'], + artist, + title, + game, + song['path'])) +elif args.command == 'played': + try: + req_played = json.dumps({'song_request': args.request[0]}) + r = requests.post(API_URL + 'played/', + headers=HEADERS, + data=req_played, + timeout=5) + r.encoding = 'utf-8' + r.raise_for_status() + except requests.exceptions.HTTPError as errh: + print('Http Error: {}'.format(errh)) + print(r.text) + except requests.exceptions.ConnectionError as errc: + print('Error Connecting: {}'.format(errc)) + except requests.exceptions.Timeout as errt: + print('Timeout Error: {}'.format(errt)) + except requests.exceptions.RequestException as err: + print('Error: {}'.format(err)) diff --git a/contrib/djcontrol/requirements.txt b/contrib/djcontrol/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..175851aa05b26363d40bcd264aba83ad76f033b3 GIT binary patch literal 164 zcmX|)%L>9U5Jk^g@K;KcDl`lI7n@cL6u~@xU%eAS!ktCVxig=4WFu2a+?6XAJR^?4 zz@!9@9Ed!)$y;UDZW$YuQ|C$KDdX24nEP**{i10WwaTA*-LST8ZrZx*B^KSYz7zAD H`!wSVo