TrackMania old videos archive tm.tilde.link

build/nav

+106
+106
build.py
··· 1 + #! /usr/bin/env python3 2 + 3 + import os 4 + import subprocess 5 + 6 + SECTIONS = { 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 + 17 + def card(name, web, raw): 18 + return f'''\ 19 + <div class="card"> 20 + <video controls> 21 + <source src="{web}" /> 22 + </video> 23 + <br /> 24 + <a href="{raw}">{name}</a> 25 + </div> 26 + ''' 27 + 28 + 29 + def 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 + 37 + def 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 + 46 + def body(sections): 47 + html = [] 48 + for section in sections.values(): 49 + html.append(f'<section id="{section["id"]}">') 50 + html.append(section['content']) 51 + html.append('</section>') 52 + return str().join(html) 53 + 54 + 55 + def 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 + 75 + def 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 + for category in categories: 83 + section = { 84 + 'id': category, 85 + 'label': SECTIONS.get(category, category), 86 + } 87 + content = [] 88 + raw_category = os.path.join(raw_root, category) 89 + for directory, directories, files in os.walk(raw_category): 90 + for file in files: 91 + relative = os.path.relpath(directory, raw_root) 92 + name, _ = os.path.splitext(file) 93 + raw_file = os.path.join('raw', relative, file) 94 + web_file = os.path.join('web', relative, f'{name}.mp4') 95 + content.append(card(name, web_file, raw_file)) 96 + section['content'] = os.linesep.join(content) 97 + sections[category] = section 98 + for id, section in sections.items(): 99 + section['content'] = nav(sections, id) + section['content'] 100 + index = os.path.join(root, 'index.html') 101 + with open(index, 'w') as i: 102 + i.write(html(body(sections))) 103 + 104 + 105 + if __name__ == '__main__': 106 + main()