add support for start and howmany request arguments

seems like some AVRs use these for paging.
also unify request extraction logic on the way.
This commit is contained in:
milaq 2019-08-12 15:22:46 +02:00
parent 4fb591a18b
commit 4a2ecd9821

View file

@ -43,16 +43,16 @@ def get_stations(config):
return return
def get_directories_page(subdir, directories, startitems, enditems): def get_directories_page(subdir, directories, requestargs):
page = vtuner.Page() page = vtuner.Page()
if len(directories) == 0: if len(directories) == 0:
page.add(vtuner.Display("No entries found.")) page.add(vtuner.Display("No entries found."))
return page return page
offset = 0 offset = 0
limit = len(directories) limit = len(directories)
if startitems and enditems: if get_element_offset(requestargs) is not None and get_element_limit(requestargs) is not None:
offset = int(startitems) - 1 offset = get_element_offset(requestargs)
limit = int(enditems) limit = get_element_limit(requestargs)
if offset > len(directories): if offset > len(directories):
offset = len(directories) offset = len(directories)
if limit > len(directories): if limit > len(directories):
@ -63,16 +63,16 @@ def get_directories_page(subdir, directories, startitems, enditems):
return page return page
def get_stations_page(stations, startitems, enditems): def get_stations_page(stations, requestargs):
page = vtuner.Page() page = vtuner.Page()
if len(stations) == 0: if len(stations) == 0:
page.add(vtuner.Display("No stations found.")) page.add(vtuner.Display("No stations found."))
return page return page
offset = 0 offset = 0
limit = len(stations) limit = len(stations)
if startitems and enditems: if get_element_offset(requestargs) is not None and get_element_limit(requestargs) is not None:
offset = int(startitems) - 1 offset = get_element_offset(requestargs)
limit = int(enditems) limit = get_element_limit(requestargs)
if offset > len(stations): if offset > len(stations):
offset = len(stations) offset = len(stations)
if limit > len(stations): if limit > len(stations):
@ -83,6 +83,24 @@ def get_stations_page(stations, startitems, enditems):
return page return page
def get_element_offset(requestargs):
if requestargs.get('startitems'):
return int(requestargs.get('startitems')) - 1
elif requestargs.get('start'):
return int(requestargs.get('start')) - 1
else:
return None
def get_element_limit(requestargs):
if requestargs.get('enditems'):
return int(requestargs.get('enditems'))
elif requestargs.get('start') and requestargs.get('howmany'):
return int(requestargs.get('start')) - 1 + int(requestargs.get('howmany'))
else:
return None
# TODO: vtuner doesn't do https (e.g. for logos). make an icon cache # TODO: vtuner doesn't do https (e.g. for logos). make an icon cache
@ -140,33 +158,31 @@ def radiobrowser_landing():
@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_COUNTRY + '/') @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_COUNTRY + '/')
def radiobrowser_countries(): def radiobrowser_countries():
directories = radiobrowser.get_countries() directories = radiobrowser.get_countries()
return get_directories_page('radiobrowser_country_stations', directories, return get_directories_page('radiobrowser_country_stations', directories, request.args).to_string()
request.args.get('startitems'), request.args.get('enditems')).to_string()
@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_COUNTRY + '/<directory>') @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_COUNTRY + '/<directory>')
def radiobrowser_country_stations(directory): def radiobrowser_country_stations(directory):
stations = radiobrowser.get_stations_by_country(directory) stations = radiobrowser.get_stations_by_country(directory)
return get_stations_page(stations, request.args.get('startitems'), request.args.get('enditems')).to_string() return get_stations_page(stations, request.args).to_string()
@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_GENRE + '/') @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_GENRE + '/')
def radiobrowser_genres(): def radiobrowser_genres():
directories = radiobrowser.get_genres() directories = radiobrowser.get_genres()
return get_directories_page('radiobrowser_genre_stations', directories, return get_directories_page('radiobrowser_genre_stations', directories, request.args).to_string()
request.args.get('startitems'), request.args.get('enditems')).to_string()
@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_GENRE + '/<directory>') @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_GENRE + '/<directory>')
def radiobrowser_genre_stations(directory): def radiobrowser_genre_stations(directory):
stations = radiobrowser.get_stations_by_genre(directory) stations = radiobrowser.get_stations_by_genre(directory)
return get_stations_page(stations, request.args.get('startitems'), request.args.get('enditems')).to_string() return get_stations_page(stations, request.args).to_string()
@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_POPULAR + '/') @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_POPULAR + '/')
def radiobrowser_popular(): def radiobrowser_popular():
stations = radiobrowser.get_stations_by_votes() stations = radiobrowser.get_stations_by_votes()
return get_stations_page(stations, request.args.get('startitems'), request.args.get('enditems')).to_string() return get_stations_page(stations, request.args).to_string()
@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_SEARCH, defaults={'path': ''}) @app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_SEARCH, defaults={'path': ''})
@ -185,4 +201,4 @@ def radiobrowser_search(path):
return page.to_string() return page.to_string()
else: else:
stations = radiobrowser.search(query) stations = radiobrowser.search(query)
return get_stations_page(stations, request.args.get('startitems'), request.args.get('enditems')).to_string() return get_stations_page(stations, request.args).to_string()