orders database queries and adds motif types

This commit is contained in:
Effie 2023-06-06 14:34:34 +10:00
parent 6c8a9b1da0
commit 1d39f61d48
8 changed files with 156 additions and 14 deletions

103
app.py
View File

@ -3,6 +3,100 @@ import sqlite3
app = flask.Flask('songbook') app = flask.Flask('songbook')
# BLANK PAGE
# @app.route('/album/<int:id>')
# def page(id):
# db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
# return flask.render_template('motif.jinja',
# )
# 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>') @app.route('/song/<int:id>')
def songpage(id): def songpage(id):
@ -25,7 +119,7 @@ def songpage(id):
album_info = db.execute(''' album_info = db.execute('''
select select
track, album.name, album.code album_id, track, album.name, album.code
from from
song_album song_album
join join
@ -34,6 +128,8 @@ def songpage(id):
song_album.album_id=album.id song_album.album_id=album.id
where where
song_id = ? song_id = ?
order by
album_id
''', ''',
(id,) (id,)
).fetchall() ).fetchall()
@ -55,6 +151,9 @@ def songpage(id):
song_artist.credit_id=credit.id song_artist.credit_id=credit.id
where where
song_id = ? song_id = ?
order by
credit_id,
artist.name_rm
''', ''',
(id,) (id,)
).fetchall() ).fetchall()
@ -72,6 +171,8 @@ def songpage(id):
clip.motif_id=motif.id clip.motif_id=motif.id
where where
song_id = ? song_id = ?
order by
song_id
''', ''',
(id,) (id,)
).fetchall() ).fetchall()

View File

@ -10,8 +10,8 @@ cur = db.cursor()
with open('db.sql') as file: with open('db.sql') as file:
db.executescript(file.read()) db.executescript(file.read())
data = pyexcel_odsr.get_data('reference.ods') data = pyexcel_odsr.get_data('reference.ods', skip_empty_rows=True)
clipdata = pyexcel_odsr.get_data('reference_clips.ods') clipdata = pyexcel_odsr.get_data('reference_clips.ods', skip_empty_rows=True)
# import song # import song
cur.executemany( cur.executemany(
@ -37,18 +37,16 @@ cur.executemany(
data['Credit'][1:] data['Credit'][1:]
) )
print(clipdata['Motif'][1:])
# import motif # import motif
cur.executemany( cur.executemany(
'insert into motif(id,name) values(?, ?)', 'insert into motif(id,name,type) values(?, ?, ?)',
clipdata['Motif'][1:] ((row[0],row[1],'' if len(row)<3 else row[2]) for row in clipdata['Motif'][1:])
) )
# import clip # import clip
cur.executemany( cur.executemany(
'insert into clip(start_ms,duration_ms,song_id,motif_id) values (?, ?, ?, ?)', 'insert into clip(start_ms,duration_ms,song_id,motif_id,feature) values (?, ?, ?, ?, ?)',
clipdata['Clip'][1:] ((row[0],row[1],row[2],row[3],0 if len(row)<5 else row[4]) for row in clipdata['Clip'][1:])
) )
# import song x album # import song x album

4
db.sql
View File

@ -31,7 +31,8 @@ create table credit(
create table motif( create table motif(
id int primary key, id int primary key,
name text not null name text not null,
type int not null
); );
create table clip( create table clip(
@ -39,6 +40,7 @@ create table clip(
motif_id int not null references motif(id), motif_id int not null references motif(id),
start_ms int not null, start_ms int not null,
duration_ms int not null, duration_ms int not null,
feature int not null,
primary key(song_id,motif_id) primary key(song_id,motif_id)
); );

Binary file not shown.

Binary file not shown.

16
templates/album.jinja Normal file
View File

@ -0,0 +1,16 @@
{% extends "base.jinja" %}
{% block title %}{{ album_info[0] }}{% endblock %}
{% block content %}
<h1>{{ album_info[0] }}</h1>
<p>Released: {{ album_info[1] }}
<!-- List songs -->
<ul>
{% for id, track, name, name_jp in song_info %}
<li><a href="/song/{{ id }}">#{{ track }}: {{name}} / {{name_jp}}</li></a>
{% endfor %}
</ul>
{% endblock %}

22
templates/motif.jinja Normal file
View File

@ -0,0 +1,22 @@
{% extends "base.jinja" %}
{% block title %}{{ name }}{% endblock %}
{% block content %}
<h1> Motif: {{ name }} </h1>
<!-- Clips -->
<p>Clips: </p>
<ul>
{% for song_id, motif_id, song in clip_info %}
<li>
<audio
controls
src = "/static/clip/{{ '%04d' % song_id }}-{{ '%03d' % motif_id }}.flac">
</audio>
<a href="/song/{{ song_id }}">{{ song }}</a>
</li>
{% endfor %}
{% endblock %}

View File

@ -2,7 +2,7 @@
{% block title %}{{ name }}{% endblock %} {% block title %}{{ name }}{% endblock %}
{% block content %} {% block content %}
<h1>Now Playing: {{ id }}. {{ name }} ({% for _, _, album_code in album_info %}{{ album_code }}{% if not loop.last %}, {% endif %}{% endfor %}) <h1>Now Playing: {{ id }}. {{ name }} ({% for _, _, _, album_code in album_info %}{{ album_code }}{% if not loop.last %}, {% endif %}{% endfor %})
</h1> </h1>
<!-- Artist info --> <!-- Artist info -->
@ -18,11 +18,13 @@
<p>Album: </p> <p>Album: </p>
<ul> <ul>
{% for track, album_name, album_code in album_info %} {% for id, track, album_name, album_code in album_info %}
<li>{{ album_name }} #{{ track }}</li> <li><a href="/album/{{ id }}">{{ album_name }}</a> #{{ track }}</li>
{% endfor %} {% endfor %}
</ul> </ul>
<!-- Clips -->
<p>Clips: </p> <p>Clips: </p>
<ul> <ul>
{% for song_id, motif_id, motif in clip_info %} {% for song_id, motif_id, motif in clip_info %}
@ -30,7 +32,8 @@
<audio <audio
controls controls
src = "/static/clip/{{ '%04d' % song_id }}-{{ '%03d' % motif_id }}.flac"> src = "/static/clip/{{ '%04d' % song_id }}-{{ '%03d' % motif_id }}.flac">
</audio> {{ motif }} </audio>
<a href = "/motif/{{ motif_id }}"> {{ motif }} </a>
</li> </li>
{% endfor %} {% endfor %}