from django.conf import settings from rest_framework import renderers from core.utils import beautify_artists, clean_quotes ANNOTATE_FORMAT = ( 'annotate:req_id="{}",' 'type="{}",' 'artist="{}",' 'title="{}",' 'game="{}",' 'replay_gain="{}":{}' ) def build_annotate_string(request_obj): ''' Takes the song request object and returns a Liquidsoap annotate string from the attributes. ''' song = request_obj['song'] if song['song_type'] == 'J': artist = settings.RADIO_NAME title = 'Jingle' game = settings.RADIO_NAME else: artist = beautify_artists(song['artists']) title = clean_quotes(song['title']) game = clean_quotes(song['game']) return ANNOTATE_FORMAT.format( request_obj['id'], song['song_type'], artist, title, game, song['replaygain'], song['path'] ) class LiquidsoapAnnotateRenderer(renderers.BaseRenderer): ''' Custom renderer to accept and respond with an "annotate" string specific to Liquidsoap requests. ''' media_type = 'application/vnd.liquidsoap.annotate' format = 'txt' def render(self, data, media_type=None, renderer_context=None): return data.encode(self.charset)