tm/build.py
2023-01-11 10:25:19 +01:00

114 lines
3 KiB
Python
Executable file

#! /usr/bin/env python3
import os
import subprocess
SECTIONS = {
'drift': 'Drift',
'fs': 'FreeStyle',
'k': 'K projects',
'lol': 'LOL',
'pf': 'Press Forward',
'ta': 'Time Attack',
'trial': 'Trial',
}
def card(name, web, raw):
return f'''\
<div class="card">
<video controls="controls" preload="none">
<source src="{web}" />
</video>
<br />
<a href="{raw}">{name}</a>
</div>
'''
def a(href, text, active=False):
html = ['<a']
if active:
html.append(' class="active"')
html.append(f' href="{href}">{text}</a>')
return str().join(html)
def nav(sections, active):
html = ['<nav>']
for _, section in sorted(sections.items()):
id = section['id']
html.append(a(f"#{id}", section['label'], id == active))
html.append('</nav>')
return str().join(html)
def body(sections):
html = []
for _, section in sorted(sections.items()):
html.append(f'<section id="{section["id"]}">')
html.append(section['content'])
html.append('</section>')
return str().join(html)
def html(body):
return f'''\
<!DOCTYPE html><html><head>
<!----------------------------------------------------------------------------->
<meta charset="UTF-8" />
<link rel="stylesheet" href="index.css" />
<title>TrackMania vidz</title>
<!----------------------------------------------------------------------------->
</head></body>
<!----------------------------------------------------------------------------->
{body}
<!----------------------------------------------------------------------------->
</body></html>
'''
def main():
file = os.path.realpath(__file__)
root = os.path.dirname(file)
raw_root = os.path.join(root, 'raw')
web_root = os.path.join(root, 'web')
_, categories, _ = next(os.walk(raw_root))
sections = {
' ': {'id': 'home', 'label': '', 'content': str()},
}
for category in categories:
section = {
'id': category,
'label': SECTIONS.get(category, category),
}
content = []
raw_category = os.path.join(raw_root, category)
videos = {}
for directory, directories, files in os.walk(raw_category):
for file in files:
relative = os.path.relpath(directory, raw_root)
name, _ = os.path.splitext(file)
raw_file = os.path.join('raw', relative, file)
web_file = os.path.join('web', relative, f'{name}.mp4')
videos[name] = [web_file, raw_file]
for name, files in sorted(videos.items()):
web_file, raw_file = files
content.append(card(name, web_file, raw_file))
section['content'] = os.linesep.join([
'<div class="cards">', os.linesep.join(content), '</div>'])
sections[category] = section
for id, section in sections.items():
section['content'] = nav(sections, id) + section['content']
index = os.path.join(root, 'index.html')
with open(index, 'w') as i:
i.write(html(body(sections)))
if __name__ == '__main__':
main()