make it use flask and not be static pages anymore

This commit is contained in:
Effie 2023-06-04 16:39:25 +10:00
parent 6880b57844
commit 1f6d448c74
9 changed files with 150 additions and 72 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
/songbook.sqlite /songbook.sqlite
/public/ /static/clip/
/__pycache__/

43
app.py Normal file
View File

@ -0,0 +1,43 @@
import flask
import sqlite3
app = flask.Flask('songbook')
@app.route('/song/<int:id>')
def songpage(id):
db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
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 = ?
''',
(id,)
).fetchall()
return flask.render_template('song.jinja',
name=name,
id=id,
album_info=album_info
)

View File

@ -4,8 +4,8 @@ import glob
import shutil import shutil
import sox import sox
shutil.rmtree('public/clip') shutil.rmtree('static/clip')
os.makedirs('public/clip', exist_ok=True) os.makedirs('static/clip', exist_ok=True)
db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True) db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
@ -37,7 +37,7 @@ for song, motif, start, duration, album, track in db.execute('''
# get clip filename # get clip filename
destination = f'public/clip/{song:04}-{motif:03}.flac' destination = f'static/clip/{song:04}-{motif:03}.flac'
print(destination) print(destination)

View File

@ -1,54 +0,0 @@
import sqlite3
import jinja2
import os
import shutil
for dirname in [
"public/song",
"public/motif",
"public/album"
]:
os.makedirs(dirname, exist_ok=True)
environment = jinja2.Environment(loader=jinja2.FileSystemLoader("templates/"))
songplate = environment.get_template("songpage.jinja")
db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
# generate song pages
for id, name, name_jp in db.execute('''
select
id, name, name_jp
from
song
'''):
# 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 = ?
''',
(id,)
).fetchall()
print(album_info)
filename = f"public/song/{id:04}"
content = songplate.render(
name=name,
id=id,
album_info=album_info
)
with open(filename, mode="w") as file:
file.write(content)
print(f"... wrote {filename}")

View File

@ -3,10 +3,10 @@ eorzea-songbook.com/
index.html index.html
/song /song
/name /id
/motif /motif
/name /id
/album /album
/name /id

76
poetry.lock generated
View File

@ -1,3 +1,57 @@
[[package]]
name = "blinker"
version = "1.6.2"
description = "Fast, simple object-to-object and broadcast signaling"
category = "main"
optional = false
python-versions = ">=3.7"
[[package]]
name = "click"
version = "8.1.3"
description = "Composable command line interface toolkit"
category = "main"
optional = false
python-versions = ">=3.7"
[package.dependencies]
colorama = {version = "*", markers = "platform_system == \"Windows\""}
[[package]]
name = "colorama"
version = "0.4.6"
description = "Cross-platform colored terminal text."
category = "main"
optional = false
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
[[package]]
name = "flask"
version = "2.3.2"
description = "A simple framework for building complex web applications."
category = "main"
optional = false
python-versions = ">=3.8"
[package.dependencies]
blinker = ">=1.6.2"
click = ">=8.1.3"
itsdangerous = ">=2.1.2"
Jinja2 = ">=3.1.2"
Werkzeug = ">=2.3.3"
[package.extras]
async = ["asgiref (>=3.2)"]
dotenv = ["python-dotenv"]
[[package]]
name = "itsdangerous"
version = "2.1.2"
description = "Safely pass data to untrusted environments and back."
category = "main"
optional = false
python-versions = ">=3.7"
[[package]] [[package]]
name = "jinja2" name = "jinja2"
version = "3.1.2" version = "3.1.2"
@ -93,12 +147,31 @@ numpy = ">=1.9.0"
docs = ["sphinx (==1.2.3)", "sphinxcontrib-napoleon", "sphinx-rtd-theme", "numpydoc"] docs = ["sphinx (==1.2.3)", "sphinxcontrib-napoleon", "sphinx-rtd-theme", "numpydoc"]
tests = ["pytest", "pytest-cov", "pytest-pep8", "pysoundfile (>=0.9.0)"] tests = ["pytest", "pytest-cov", "pytest-pep8", "pysoundfile (>=0.9.0)"]
[[package]]
name = "werkzeug"
version = "2.3.4"
description = "The comprehensive WSGI web application library."
category = "main"
optional = false
python-versions = ">=3.8"
[package.dependencies]
MarkupSafe = ">=2.1.1"
[package.extras]
watchdog = ["watchdog (>=2.3)"]
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "e2e818660fff102eebe62dc32854ab0171cf72c277a25897ee4ee2f732e8498d" content-hash = "eb105df2967c9247e5d32c49b9996442ba31976fb4da0c99bffa003aa8242182"
[metadata.files] [metadata.files]
blinker = []
click = []
colorama = []
flask = []
itsdangerous = []
jinja2 = [] jinja2 = []
lml = [] lml = []
lxml = [] lxml = []
@ -107,3 +180,4 @@ numpy = []
pyexcel-io = [] pyexcel-io = []
pyexcel-odsr = [] pyexcel-odsr = []
sox = [] sox = []
werkzeug = []

View File

@ -9,6 +9,7 @@ python = "^3.10"
sox = "^1.4.1" sox = "^1.4.1"
Jinja2 = "^3.1.2" Jinja2 = "^3.1.2"
pyexcel-odsr = "^0.6.0" pyexcel-odsr = "^0.6.0"
Flask = "^2.3.2"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]

14
templates/base.jinja Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %} - Eorzea Songbook</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

View File

@ -1,12 +1,7 @@
<!DOCTYPE html> {% extends "base.jinja" %}
<html lang="en"> {% block title %}{{ name }}{% endblock %}
<head>
<title>title</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
{% 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>
<p>Artist: </p> <p>Artist: </p>
@ -16,6 +11,10 @@
<li>{{ album_name }} #{{ track }}</li> <li>{{ album_name }} #{{ track }}</li>
{% endfor %} {% endfor %}
</ul> </ul>
<p>blah</p>
<p>Clips: </p> <p>Clips: </p>
</body> <audio
</html> controls
src = "/static/clip/0001-001.flac">
</audio>
{% endblock %}