Add JSON API for searches. Introduces new settings.py config option 'enable_search_route'

This commit is contained in:
dsc 2021-08-04 18:53:56 +02:00
parent 3ed9eee2f7
commit c0a1415eae
2 changed files with 49 additions and 1 deletions

View File

@ -3,7 +3,7 @@
from datetime import datetime
from typing import Tuple, Optional
from quart import request, render_template, abort
from quart import request, render_template, abort, jsonify
import settings
from ircradio.factory import app
@ -39,6 +39,52 @@ async def history():
return data
@app.route("/search")
async def search():
# search json api endpoint
# e.g: /search?name=test&limit=5&offset=0
if not settings.enable_search_route:
abort(404)
from ircradio.models import Song
name = request.args.get("name")
limit = request.args.get("limit", '20')
offset = request.args.get("offset", '0')
try:
limit = int(limit)
offset = int(offset)
except:
limit = 50
offset = 0
if not name or len(name) <= 2:
abort(404)
if limit > 50:
limit = 50
name = f"%{name}%"
try:
q = Song.select()
q = q.where((Song.added_by ** name) | (Song.title ** name))
q = q.order_by(Song.date_added.desc())
q = q.limit(limit).offset(offset)
results = [{
"added_by": s.added_by,
"karma": s.karma,
"id": s.id,
"title": s.title,
"utube_id": s.utube_id,
"date_added": s.date_added.strftime("%Y-%m-%d")
} for s in q]
except:
return jsonify([])
return jsonify(results)
@app.route("/library")
async def user_library():
from ircradio.models import Song

View File

@ -16,6 +16,8 @@ timezone = "Europe/Amsterdam"
dir_music = os.environ.get("DIR_MUSIC", os.path.join(cwd, "data", "music"))
enable_search_route = bool_env(os.environ.get("ENABLE_SEARCH_ROUTE", False))
irc_admins_nicknames = ["dsc_"]
irc_host = os.environ.get('IRC_HOST', 'localhost')
irc_port = int(os.environ.get('IRC_PORT', 6667))