2022-11-20 01:00:54 -08:00
|
|
|
import logging
|
2022-12-02 21:43:51 -08:00
|
|
|
import json
|
2022-11-20 01:00:54 -08:00
|
|
|
import os
|
2022-12-02 21:43:51 -08:00
|
|
|
from pathlib import Path
|
2022-11-20 01:00:54 -08:00
|
|
|
|
|
|
|
import bottle
|
2022-12-02 21:43:51 -08:00
|
|
|
from bottle import HTTPResponse, template, static_file
|
2022-11-25 15:40:24 -08:00
|
|
|
from bottle.ext import sqlalchemy
|
2022-11-20 01:00:54 -08:00
|
|
|
|
2022-12-02 21:43:51 -08:00
|
|
|
import groove.db
|
2022-11-19 16:06:23 -08:00
|
|
|
from groove.auth import is_authenticated
|
2022-11-25 15:40:24 -08:00
|
|
|
from groove.db.manager import database_manager
|
2022-11-24 13:45:09 -08:00
|
|
|
from groove.playlist import Playlist
|
2022-12-02 21:43:51 -08:00
|
|
|
from groove.webserver import requests
|
|
|
|
|
2022-12-03 23:23:25 -08:00
|
|
|
# from groove.exceptions import APIHandlingException
|
2022-11-20 01:00:54 -08:00
|
|
|
|
|
|
|
server = bottle.Bottle()
|
2022-11-19 15:14:12 -08:00
|
|
|
|
2022-11-20 01:00:54 -08:00
|
|
|
|
2022-11-20 16:44:33 -08:00
|
|
|
def start(host: str, port: int, debug: bool) -> None: # pragma: no cover
|
2022-11-20 01:00:54 -08:00
|
|
|
"""
|
2022-11-20 09:28:00 -08:00
|
|
|
Start the Bottle app.
|
2022-11-20 01:00:54 -08:00
|
|
|
"""
|
2022-11-20 09:28:00 -08:00
|
|
|
logging.debug(f"Configuring sqllite using {os.environ.get('DATABASE_PATH')}")
|
2022-11-25 15:40:24 -08:00
|
|
|
|
|
|
|
with database_manager() as manager:
|
|
|
|
server.install(sqlalchemy.Plugin(
|
|
|
|
manager.engine,
|
2022-12-02 21:43:51 -08:00
|
|
|
groove.db.metadata,
|
2022-11-25 15:40:24 -08:00
|
|
|
keyword='db',
|
|
|
|
create=True,
|
|
|
|
commit=True,
|
|
|
|
))
|
|
|
|
logging.debug(f"Configuring webserver with host={host}, port={port}, debug={debug}")
|
|
|
|
server.run(
|
|
|
|
host=os.getenv('HOST', host),
|
|
|
|
port=os.getenv('PORT', port),
|
|
|
|
debug=debug,
|
|
|
|
server='paste',
|
|
|
|
quiet=True
|
|
|
|
)
|
2022-11-19 15:14:12 -08:00
|
|
|
|
|
|
|
|
|
|
|
@server.route('/')
|
|
|
|
def index():
|
|
|
|
return "Groovy."
|
2022-11-19 16:06:23 -08:00
|
|
|
|
|
|
|
|
2022-11-20 01:00:54 -08:00
|
|
|
@server.route('/build')
|
|
|
|
@bottle.auth_basic(is_authenticated)
|
|
|
|
def build():
|
2022-11-19 16:06:23 -08:00
|
|
|
return "Authenticated. Groovy."
|
2022-11-20 01:00:54 -08:00
|
|
|
|
|
|
|
|
2022-12-03 23:23:25 -08:00
|
|
|
@server.route('/static/<filepath:path>')
|
|
|
|
def server_static(filepath):
|
|
|
|
return static_file(filepath, root='static')
|
|
|
|
|
|
|
|
|
2022-12-02 21:43:51 -08:00
|
|
|
@bottle.auth_basic(is_authenticated)
|
|
|
|
@server.route('/build/search/playlist')
|
|
|
|
def search_playlist(slug, db):
|
|
|
|
playlist = Playlist(slug=slug, session=db, create_ok=False)
|
|
|
|
response = json.dumps(playlist.as_dict)
|
|
|
|
logging.debug(response)
|
|
|
|
return HTTPResponse(status=200, content_type='application/json', body=response)
|
|
|
|
|
|
|
|
|
|
|
|
@server.route('/track/<request>/<track_id>')
|
|
|
|
def serve_track(request, track_id, db):
|
|
|
|
|
|
|
|
expected = requests.encode([track_id], '/track')
|
|
|
|
if not requests.verify(request, expected):
|
|
|
|
return HTTPResponse(status=404, body="Not found")
|
|
|
|
|
|
|
|
track_id = int(track_id)
|
|
|
|
track = db.query(groove.db.track).filter(
|
|
|
|
groove.db.track.c.id == track_id
|
|
|
|
).one()
|
|
|
|
|
|
|
|
path = Path(os.environ['MEDIA_ROOT']) / Path(track['relpath'])
|
|
|
|
return static_file(path.name, root=path.parent)
|
|
|
|
|
|
|
|
|
2022-11-20 01:00:54 -08:00
|
|
|
@server.route('/playlist/<slug>')
|
2022-12-02 21:43:51 -08:00
|
|
|
def serve_playlist(slug, db):
|
2022-11-20 01:00:54 -08:00
|
|
|
"""
|
|
|
|
Retrieve a playlist and its entries by a slug.
|
|
|
|
"""
|
|
|
|
logging.debug(f"Looking up playlist: {slug}...")
|
2022-12-02 21:43:51 -08:00
|
|
|
playlist = Playlist(slug=slug, session=db, create_ok=False).load()
|
|
|
|
if not playlist.record:
|
|
|
|
logging.debug(f"Playist {slug} doesn't exist.")
|
2022-11-20 01:00:54 -08:00
|
|
|
return HTTPResponse(status=404, body="Not found")
|
2022-12-02 21:43:51 -08:00
|
|
|
logging.debug(f"Loaded {playlist.record}")
|
|
|
|
logging.debug(playlist.as_dict['entries'])
|
|
|
|
|
2022-12-03 23:23:25 -08:00
|
|
|
pl = playlist.as_dict
|
|
|
|
|
|
|
|
for entry in pl['entries']:
|
|
|
|
sig = requests.encode([str(entry['track_id'])], uri='/track')
|
|
|
|
entry['url'] = f"/track/{sig}/{entry['track_id']}"
|
2022-12-02 21:43:51 -08:00
|
|
|
|
|
|
|
template_path = Path(os.environ['TEMPLATE_PATH']) / Path('playlist.tpl')
|
2022-12-03 23:23:25 -08:00
|
|
|
body = template(
|
|
|
|
str(template_path),
|
|
|
|
url=requests.url(),
|
|
|
|
playlist=pl
|
|
|
|
)
|
|
|
|
return HTTPResponse(status=200, body=body)
|