tm/build.py

114 lines
3 KiB
Python
Raw Normal View History

2023-01-10 22:06:09 +00:00
#! /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">
2023-01-11 08:43:07 +00:00
<video controls="controls" preload="none">
2023-01-10 22:06:09 +00:00
<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 = []
2023-01-10 22:26:13 +00:00
for _, section in sorted(sections.items()):
2023-01-10 22:06:09 +00:00
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))
2023-01-10 22:33:45 +00:00
sections = {
' ': {'id': 'home', 'label': '', 'content': str()},
}
2023-01-10 22:06:09 +00:00
for category in categories:
section = {
'id': category,
'label': SECTIONS.get(category, category),
}
content = []
raw_category = os.path.join(raw_root, category)
2023-01-11 09:02:12 +00:00
videos = {}
2023-01-10 22:06:09 +00:00
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')
2023-01-11 09:02:12 +00:00
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))
2023-01-11 09:25:19 +00:00
section['content'] = os.linesep.join([
'<div class="cards">', os.linesep.join(content), '</div>'])
2023-01-10 22:06:09 +00:00
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()