media/scripts/generate-pages.py

167 lines
5.2 KiB
Python
Raw Normal View History

2016-08-25 01:26:29 +02:00
#!/usr/bin/env python3
import sys
import os
import shutil
import html
import yaml
def read(path):
with open(path) as f:
return f.read()
def write(path, text):
with open(path, 'w') as f:
f.write(text)
def load_settings(settings_file):
with open(settings_file) as f:
settings = {name: value
for name, value
in (line.split('=', 1) for line in
f.read().strip().split('\n'))}
return settings
2016-08-25 01:35:03 +02:00
2016-08-25 01:26:29 +02:00
def load_media(media_file):
with open(media_file) as f:
2019-10-09 18:57:19 +02:00
c = yaml.load(f, Loader=yaml.FullLoader)
c['date'] = c['date']
2016-08-25 01:26:29 +02:00
c['ident'] = ident(media_file)
return c
2016-08-25 01:35:03 +02:00
2016-08-25 01:26:29 +02:00
def ident(media_file):
return os.path.splitext(os.path.split(media_file)[1])[0]
def thumbnail_path(name):
return os.path.join(name, 'thumbnail.jpg')
def thumbnail_url(link):
return link + 'thumbnail.jpg'
def setup_directory_structure(settings, base_dir, template_dir):
site_new_dir = os.path.join(base_dir, 'site-new')
2017-06-25 23:54:11 +02:00
shutil.rmtree(site_new_dir, ignore_errors=True)
2016-08-25 01:26:29 +02:00
os.mkdir(site_new_dir)
style = read(os.path.join(template_dir, 'style.css'))
style_out = style.replace('{', '{{').replace('}', '}}') \
.replace('[', '{').replace(']', '}').format(**settings)
style_new_file = os.path.join(site_new_dir, 'style.css')
write(style_new_file, style_out)
2019-10-30 13:38:05 +01:00
shutil.copy(os.path.join(template_dir, 'favicon.png'), site_new_dir)
2016-08-25 01:26:29 +02:00
return site_new_dir
def publish_new_site(site_dir, site_new_dir):
shutil.rmtree(site_dir, ignore_errors=True)
shutil.move(site_new_dir, site_dir)
def generate_index_page(out_dir, base_html, section_html, medias):
sections = [generate_video_section_html(section_html, media)
for media in medias]
2016-08-25 01:35:03 +02:00
2016-08-25 01:26:29 +02:00
all_sections = '\n'.join(sections)
html_out = base_html.format(content=all_sections,
style_url='style.css')
out_file = os.path.join(out_dir, 'index.html')
write(out_file, html_out)
def generate_video_section_html(section_html, media):
title = media['title']
link = media['ident'] + '/'
thumbnail = thumbnail_url(link)
html_out = section_html.format(
title=html.escape(title),
thumbnail=thumbnail, link=link)
return html_out
2022-09-25 13:17:14 +02:00
def iframe_video(file_src):
return '''\
<iframe width="759" height="427" src="{url}" frameborder="0" allowfullscreen>
</iframe>
'''.format(url=file_src)
2016-08-25 01:26:29 +02:00
def extern_video(file_src):
2020-12-02 19:19:03 +01:00
url = 'https://media-metanohi-name.eu-central-1.linodeobjects.com/media/' + file_src
2016-08-25 01:26:29 +02:00
return '''\
2016-08-25 01:35:03 +02:00
<video src="{url}" controls height="427">
2016-08-25 01:26:29 +02:00
Your browser cannot play this video. You can download it here instead:
2016-08-25 01:35:03 +02:00
<a href="{url}">{url}</a>
2016-08-25 01:26:29 +02:00
</video>
2016-08-25 01:35:03 +02:00
'''.format(url=url)
2016-08-25 01:26:29 +02:00
def youtube_video(file_src):
2016-08-25 01:35:03 +02:00
yt_id = file_src
2016-08-25 01:26:29 +02:00
return '''\
<iframe type="text/html" width="760" height="427"
2016-08-25 01:35:03 +02:00
src="https://www.youtube.com/embed/{yt_id}?controls=2"
2016-08-25 01:26:29 +02:00
allowfullscreen frameborder="0">
</iframe>
2016-08-25 01:35:03 +02:00
'''.format(yt_id=yt_id)
2016-08-25 01:26:29 +02:00
def generate_video_page(base_dir, out_dir, base_html, showing_html, media):
title = media['title']
date = str(media['date'])
2016-08-25 02:47:29 +02:00
description = media['description'].rstrip()
2016-08-25 01:26:29 +02:00
name = media['ident']
video_dir = os.path.join(out_dir, name)
os.mkdir(video_dir)
thumbnail_file = thumbnail_path(video_dir)
thumbnail_file_local = os.path.join(
base_dir, 'thumbnails-small', name + '.jpg')
shutil.copyfile(thumbnail_file_local, thumbnail_file)
file_type, file_src = media['file'].split(':', 1)
actions = {
'extern': extern_video,
2022-09-25 13:20:00 +02:00
'iframe': iframe_video,
2016-08-25 01:26:29 +02:00
'youtube': youtube_video
}
video_html = actions[file_type](file_src)
2016-08-25 01:35:03 +02:00
2016-08-25 01:26:29 +02:00
html_inner = showing_html.format(
title=html.escape(title),
year=date[:4],
2016-08-25 01:26:29 +02:00
description=html.escape(description),
video=video_html)
html_out = base_html.format(content=html_inner,
style_url='../style.css')
out_file = os.path.join(video_dir, 'index.html')
write(out_file, html_out)
def main():
script_dir = os.path.dirname(__file__)
settings_file = os.path.join(script_dir, 'settings')
settings = load_settings(settings_file)
base_dir = os.path.split(script_dir)[0]
template_dir = os.path.join(base_dir, 'template')
base_html = read(os.path.join(template_dir, 'base.html'))
section_html = read(os.path.join(template_dir, 'section.html'))
showing_html = read(os.path.join(template_dir, 'showing.html'))
media_dir = os.path.join(base_dir, 'media')
media_files = [os.path.join(media_dir, media_file)
for media_file in os.listdir(media_dir)]
medias = [load_media(media_file) for media_file in media_files]
2020-12-12 17:06:19 +01:00
medias.sort(key=lambda m: (str(m['date']), m['ident']), reverse=True)
2016-08-25 01:26:29 +02:00
site_new_dir = setup_directory_structure(settings, base_dir, template_dir)
generate_index_page(site_new_dir, base_html, section_html, medias)
for media in medias:
generate_video_page(base_dir, site_new_dir, base_html, showing_html,
media)
site_dir = os.path.join(base_dir, 'site')
publish_new_site(site_dir, site_new_dir)
2016-08-25 01:35:03 +02:00
2016-08-25 01:26:29 +02:00
return 0
if __name__ == '__main__':
sys.exit(main())