eorzea-songbook/app.py
2023-06-06 18:46:12 +10:00

264 lines
4.3 KiB
Python

import flask
import sqlite3
app = flask.Flask('songbook')
# BLANK PAGE
# @app.route('/')
# def page():
# db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
# return flask.render_template('.jinja',
# )
# HOMEPAGE
@app.route('/')
def homepage():
return flask.render_template('home.jinja')
# SONG INDEX
@app.route('/song')
def song_redirect():
return flask.redirect("/index", code=308)
@app.route('/index')
def songindex():
db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
album_info = db.execute('''
select
id, name, date
from
album
order by
id
''').fetchall()
song_info = db.execute('''
select
song_id, album_id, track, song.name, song.name_jp
from
song_album
join
song
on
song_album.song_id=song.id
order by
album_id,
track
''').fetchall()
return flask.render_template('songindex.jinja',
album_info=album_info,
song_info=song_info
)
# MOTIF INDEX
@app.route('/motif')
def motifindex():
db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
category_info = db.execute('''
select
id, name
from
category
order by
id
''').fetchall()
motif_info = db.execute('''
select
id, name, category
from
motif
order by
id
''').fetchall()
return flask.render_template('motifindex.jinja',
category_info = category_info,
motif_info = motif_info
)
# ALBUM PAGES
@app.route('/album/<int:id>')
def albumpage(id):
db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
album_info = db.execute('''
select
name, date
from
album
where
id = ?
''',
(id,)
).fetchone()
song_info = db.execute('''
select
song_id, track, song.name, song.name_jp
from
song_album
join
song
on
song_album.song_id=song.id
where
album_id = ?
''',
(id,)
).fetchall()
return flask.render_template('album.jinja',
album_info=album_info,
song_info=song_info
)
# MOTIF PAGES
@app.route('/motif/<int:id>')
def motifpage(id):
db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
# query motif
name, = db.execute('''
select
name
from
motif
where
id = ?
''',
(id,)
).fetchone()
# query clips
clip_info = db.execute('''
select
song_id, motif_id, song.name
from
clip
join
song
on
clip.song_id=song.id
where
motif_id = ?
order by
song_id
''',
(id,)
).fetchall()
return flask.render_template('motif.jinja',
name=name,
clip_info=clip_info
)
# SONG PAGES
@app.route('/song/<int:id>')
def songpage(id):
db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
# query song name
name, = db.execute('''
select
name
from
song
where
id = ?
''',
(id,)
).fetchone()
# query album info
album_info = db.execute('''
select
album_id, track, album.name, album.code
from
song_album
join
album
on
song_album.album_id=album.id
where
song_id = ?
order by
album_id
''',
(id,)
).fetchall()
# query artist info
artist_info = db.execute('''
select
artist.name, artist.name_rm, credit.name
from
song_artist
join
artist
on
song_artist.artist_id=artist.id
join
credit
on
song_artist.credit_id=credit.id
where
song_id = ?
order by
credit_id,
artist.name_rm
''',
(id,)
).fetchall()
# query clip info
clip_info = db.execute('''
select
song_id, motif_id, motif.name
from
clip
join
motif
on
clip.motif_id=motif.id
where
song_id = ?
order by
song_id
''',
(id,)
).fetchall()
return flask.render_template('song.jinja',
name=name,
id=id,
album_info=album_info,
clip_info=clip_info,
artist_info=artist_info
)