Handle station search globally

At least Denon AVRs don't require the API to return a 'Search' element
but use a hardcoded path for the search function.

Also, don't add a distinct menu item. Most AVRs don't display it
correctly either way.
This commit is contained in:
milaq 2019-08-21 23:37:16 +02:00
parent fbbf585bf0
commit 67ac5bd70a

View file

@ -8,13 +8,13 @@ import ycast.my_stations as my_stations
PATH_ROOT = 'ycast' PATH_ROOT = 'ycast'
PATH_SEARCH = 'search'
PATH_MY_STATIONS = 'my_stations' PATH_MY_STATIONS = 'my_stations'
PATH_RADIOBROWSER = 'radiobrowser' PATH_RADIOBROWSER = 'radiobrowser'
PATH_RADIOBROWSER_COUNTRY = 'country' PATH_RADIOBROWSER_COUNTRY = 'country'
PATH_RADIOBROWSER_LANGUAGE = 'language' PATH_RADIOBROWSER_LANGUAGE = 'language'
PATH_RADIOBROWSER_GENRE = 'genre' PATH_RADIOBROWSER_GENRE = 'genre'
PATH_RADIOBROWSER_POPULAR = 'popular' PATH_RADIOBROWSER_POPULAR = 'popular'
PATH_RADIOBROWSER_SEARCH = 'search'
my_stations_enabled = False my_stations_enabled = False
app = Flask(__name__) app = Flask(__name__)
@ -87,8 +87,10 @@ def get_paged_elements(items, requestargs):
def landing(path): def landing(path):
if request.args.get('token') == '0': if request.args.get('token') == '0':
return vtuner.get_init_token() return vtuner.get_init_token()
if request.args.get('search'):
return station_search()
page = vtuner.Page() page = vtuner.Page()
page.add(vtuner.Directory('Radiobrowser', url_for('radiobrowser_landing', _external=True), 5)) page.add(vtuner.Directory('Radiobrowser', url_for('radiobrowser_landing', _external=True), 4))
if my_stations_enabled: if my_stations_enabled:
page.add(vtuner.Directory('My Stations', url_for('my_stations_landing', _external=True), page.add(vtuner.Directory('My Stations', url_for('my_stations_landing', _external=True),
len(my_stations.get_category_directories()))) len(my_stations.get_category_directories())))
@ -123,7 +125,6 @@ def radiobrowser_landing():
len(radiobrowser.get_language_directories()))) len(radiobrowser.get_language_directories())))
page.add(vtuner.Directory('Most Popular', url_for('radiobrowser_popular', _external=True), page.add(vtuner.Directory('Most Popular', url_for('radiobrowser_popular', _external=True),
len(radiobrowser.get_stations_by_votes()))) len(radiobrowser.get_stations_by_votes())))
page.add(vtuner.Search('Search', url_for('radiobrowser_search', _external=True, path='')))
return page.to_string() return page.to_string()
@ -169,15 +170,16 @@ def radiobrowser_popular():
return get_stations_page(stations, request.args).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_SEARCH + '/')
@app.route('/' + PATH_ROOT + '/' + PATH_RADIOBROWSER + '/' + PATH_RADIOBROWSER_SEARCH + '<path:path>') def station_search():
def radiobrowser_search(path):
query = request.args.get('search') query = request.args.get('search')
if not query or len(query) < 3: if not query or len(query) < 3:
page = vtuner.Page() page = vtuner.Page()
page.add(vtuner.Previous(url_for('landing', _external=True))) page.add(vtuner.Previous(url_for('landing', _external=True)))
page.add(vtuner.Display("Search query too short.")) page.add(vtuner.Display("Search query too short."))
page.set_count(1)
return page.to_string() return page.to_string()
else: else:
# TODO: we also need to include 'my station' elements
stations = radiobrowser.search(query) stations = radiobrowser.search(query)
return get_stations_page(stations, request.args).to_string() return get_stations_page(stations, request.args).to_string()