Skip to content

Commit

Permalink
Add ability to fetch single station element by id
Browse files Browse the repository at this point in the history
We don't know what's the correct format for the statxml.asp query.
So let's assume the API just returns a single station element.
  • Loading branch information
milaq committed Aug 19, 2019
1 parent 803964e commit b447ecc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
19 changes: 18 additions & 1 deletion ycast/server.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import logging

from flask import Flask, request, url_for
from flask import Flask, request, url_for, abort

import ycast.vtuner as vtuner
import ycast.radiobrowser as radiobrowser
import ycast.my_stations as my_stations
import ycast.generic as generic


PATH_ROOT = 'ycast'
Expand Down Expand Up @@ -80,12 +81,28 @@ def get_paged_elements(items, requestargs):
return items[offset:limit]


def get_station_by_id(stationid):
station_id_prefix = generic.get_stationid_prefix(stationid)
if station_id_prefix == my_stations.ID_PREFIX:
return my_stations.get_station_by_id(generic.get_stationid_without_prefix(stationid)).to_vtuner()
elif station_id_prefix == radiobrowser.ID_PREFIX:
return radiobrowser.get_station_by_id(generic.get_stationid_without_prefix(stationid)).to_vtuner()
return None


@app.route('/', defaults={'path': ''})
@app.route('/setupapp/<path:path>')
@app.route('/' + PATH_ROOT + '/', defaults={'path': ''})
def landing(path):
if request.args.get('token') == '0':
return vtuner.get_init_token()
if 'statxml.asp' in path and request.args.get('id'):
station = get_station_by_id(request.args.get('id'))
if station:
return station.to_string()
else:
logging.error("Could not get station with id '%s'", request.args.get('id'))
abort(404)
page = vtuner.Page()
page.add(vtuner.Directory('Radiobrowser', url_for('radiobrowser_landing', _external=True), 4))
if my_stations_enabled:
Expand Down
3 changes: 3 additions & 0 deletions ycast/vtuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,6 @@ def to_xml(self):
ET.SubElement(item, 'Relia').text = '3'
ET.SubElement(item, 'Bookmark').text = self.bookmark
return item

def to_string(self):
return XML_HEADER + ET.tostring(self.to_xml()).decode('utf-8')

0 comments on commit b447ecc

Please sign in to comment.