TrackMania old videos archive
tm.tilde.link
1#! /usr/bin/env python3
2
3import os
4import subprocess
5
6SECTIONS = {
7 'drift': 'Drift',
8 'fs': 'FreeStyle',
9 'k': 'K projects',
10 'lol': 'LOL',
11 'pf': 'Press Forward',
12 'ta': 'Time Attack',
13 'trial': 'Trial',
14}
15
16
17def card(name, web, raw):
18 return f'''\
19<div class="card">
20<video controls="controls" preload="none">
21<source src="{web}" />
22</video>
23<br />
24<a href="{raw}">{name}</a>
25</div>
26'''
27
28
29def a(href, text, active=False):
30 html = ['<a']
31 if active:
32 html.append(' class="active"')
33 html.append(f' href="{href}">{text}</a>')
34 return str().join(html)
35
36
37def nav(sections, active):
38 html = ['<nav>']
39 for _, section in sorted(sections.items()):
40 id = section['id']
41 html.append(a(f"#{id}", section['label'], id == active))
42 html.append('</nav>')
43 return str().join(html)
44
45
46def body(sections):
47 html = []
48 for _, section in sorted(sections.items()):
49 html.append(f'<section id="{section["id"]}">')
50 html.append(section['content'])
51 html.append('</section>')
52 return str().join(html)
53
54
55def html(body):
56 return f'''\
57<!DOCTYPE html><html><head>
58<!----------------------------------------------------------------------------->
59
60<meta charset="UTF-8" />
61<link rel="stylesheet" href="index.css" />
62<title>TrackMania vidz</title>
63
64<!----------------------------------------------------------------------------->
65</head></body>
66<!----------------------------------------------------------------------------->
67
68{body}
69
70<!----------------------------------------------------------------------------->
71</body></html>
72'''
73
74
75def main():
76 file = os.path.realpath(__file__)
77 root = os.path.dirname(file)
78 raw_root = os.path.join(root, 'raw')
79 web_root = os.path.join(root, 'web')
80 _, categories, _ = next(os.walk(raw_root))
81 sections = {
82 ' ': {'id': 'home', 'label': '⋅', 'content': str()},
83 }
84 for category in categories:
85 section = {
86 'id': category,
87 'label': SECTIONS.get(category, category),
88 }
89 content = []
90 raw_category = os.path.join(raw_root, category)
91 videos = {}
92 for directory, directories, files in os.walk(raw_category):
93 for file in files:
94 relative = os.path.relpath(directory, raw_root)
95 name, _ = os.path.splitext(file)
96 raw_file = os.path.join('raw', relative, file)
97 web_file = os.path.join('web', relative, f'{name}.mp4')
98 videos[name] = [web_file, raw_file]
99 for name, files in sorted(videos.items()):
100 web_file, raw_file = files
101 content.append(card(name, web_file, raw_file))
102 section['content'] = os.linesep.join([
103 '<div class="cards">', os.linesep.join(content), '</div>'])
104 sections[category] = section
105 for id, section in sections.items():
106 section['content'] = nav(sections, id) + section['content']
107 index = os.path.join(root, 'index.html')
108 with open(index, 'w') as i:
109 i.write(html(body(sections)))
110
111
112if __name__ == '__main__':
113 main()