2020-01-24 22:15:30 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"Creates a static site for redacted.life"
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
from collections import UserList
|
2020-01-25 19:10:25 +01:00
|
|
|
from datetime import datetime
|
2020-01-24 22:15:30 +01:00
|
|
|
import os
|
|
|
|
import os.path as path
|
|
|
|
import re
|
2020-01-25 19:10:25 +01:00
|
|
|
import subprocess
|
2020-01-24 22:15:30 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import jinja2
|
|
|
|
|
|
|
|
|
2020-01-25 19:10:25 +01:00
|
|
|
def gen_name(date, slug):
|
|
|
|
"Returns to file name"
|
|
|
|
return date.strftime("%Y-%m-%d-") + slug
|
|
|
|
|
|
|
|
|
2020-01-24 22:15:30 +01:00
|
|
|
class EpisodeList(UserList):
|
|
|
|
"Represents list of episodes"
|
|
|
|
def __init__(self, data, output, template):
|
|
|
|
super().__init__(data)
|
|
|
|
self.output = output
|
|
|
|
self.template = template
|
|
|
|
|
2020-01-25 19:10:25 +01:00
|
|
|
def sort(self, *_args, **_kwargs):
|
2020-01-24 22:15:30 +01:00
|
|
|
"Sorts the EpisodeList"
|
2020-01-25 19:10:25 +01:00
|
|
|
super().sort(key=lambda x: x.date, reverse=True)
|
2020-01-24 22:15:30 +01:00
|
|
|
|
2020-01-25 19:10:25 +01:00
|
|
|
def generate_thumbnails(self):
|
2020-01-24 22:15:30 +01:00
|
|
|
"Generates thumbnails for all the videos"
|
2020-01-25 19:10:25 +01:00
|
|
|
if not path.isdir(self.output + "assets"):
|
|
|
|
os.mkdir(self.output + "assets")
|
|
|
|
if not path.isdir(self.output + "assets/thumbnails"):
|
|
|
|
os.mkdir(self.output + "assets/thumbnails")
|
|
|
|
for episode in self.data:
|
|
|
|
location = (self.output + "assets/thumbnails/" +
|
|
|
|
gen_name(episode.date, episode.slug) + ".jpg")
|
|
|
|
episode.store_thumbnail(location)
|
2020-01-24 22:15:30 +01:00
|
|
|
|
|
|
|
def generate_atom(self):
|
|
|
|
"Generates the Atom feed"
|
|
|
|
|
|
|
|
def generate_site(self):
|
|
|
|
"Generates the entire "
|
|
|
|
|
|
|
|
|
|
|
|
class Episode:
|
|
|
|
"Represents one episode of podcast"
|
2020-01-25 19:10:25 +01:00
|
|
|
def __init__(self, date, slug, title, show_notes, video_src):
|
2020-01-24 22:15:30 +01:00
|
|
|
self.date = date
|
|
|
|
self.slug = slug
|
|
|
|
self.title = title
|
|
|
|
self.show_notes = show_notes
|
2020-01-25 19:10:25 +01:00
|
|
|
self.video = video_src
|
2020-01-24 22:15:30 +01:00
|
|
|
|
2020-01-25 19:10:25 +01:00
|
|
|
def render(self, template, thumbnail_src):
|
2020-01-24 22:15:30 +01:00
|
|
|
"Renders the Episode with the given template"
|
|
|
|
return template.render(
|
|
|
|
title=self.title,
|
|
|
|
show_notes=jinja2.Markup(self.show_notes),
|
|
|
|
thumbnail_src=thumbnail_src,
|
2020-01-25 19:10:25 +01:00
|
|
|
video_src=self.video
|
2020-01-24 22:15:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def store_thumbnail(self, location):
|
|
|
|
"Stores the thumbnail for given image at path"
|
2020-01-25 19:10:25 +01:00
|
|
|
args = ["ffmpeg", "-i", self.video, "-ss", "00:00:01.000", "-vframes",
|
|
|
|
"1", location]
|
|
|
|
subprocess.run(args, check=False)
|
2020-01-24 22:15:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"Main method"
|
2020-01-25 19:10:25 +01:00
|
|
|
root = path.dirname(sys.argv[0])
|
2020-01-24 22:15:30 +01:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("input_dir", help="Input directory")
|
|
|
|
parser.add_argument("output_dir", help="Output directory")
|
|
|
|
args = parser.parse_args()
|
|
|
|
input_dir = args.input_dir.rstrip("/") + "/"
|
|
|
|
output_dir = args.output_dir.rstrip("/") + "/"
|
|
|
|
|
|
|
|
# Input validation
|
|
|
|
if not all(path.isdir(i) for i in (input_dir, input_dir + "md",
|
|
|
|
input_dir + "videos")):
|
|
|
|
print("Invalid Input", file=sys.stderr)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not path.isdir(args.output_dir):
|
|
|
|
os.mkdir(args.output_dir)
|
|
|
|
|
|
|
|
template = jinja2.Environment(
|
2020-01-25 19:10:25 +01:00
|
|
|
loader=jinja2.FileSystemLoader(root),
|
2020-01-24 22:15:30 +01:00
|
|
|
autoescape=jinja2.select_autoescape("html")
|
|
|
|
).get_template("index.html")
|
|
|
|
|
|
|
|
podcast = EpisodeList([], output_dir, template)
|
|
|
|
|
2020-01-25 19:10:25 +01:00
|
|
|
split = re.compile(r"((?P<date>\d{4}-[01]?\d-[0123]?\d)-(?P<slug>.*).md)")
|
2020-01-24 22:15:30 +01:00
|
|
|
for file in os.listdir(input_dir + "md"):
|
|
|
|
match = split.match(file)
|
|
|
|
if not match:
|
|
|
|
print(f"Invalid filename: {file}", file=sys.stderr)
|
|
|
|
continue
|
2020-01-25 19:10:25 +01:00
|
|
|
date = datetime.strptime(match.group("date"), "%Y-%M-%d")
|
2020-01-24 22:15:30 +01:00
|
|
|
slug = match.group("slug")
|
2020-01-25 19:10:25 +01:00
|
|
|
with open(input_dir + "md/" + file) as episode:
|
|
|
|
title = episode.readline()
|
|
|
|
show_notes = episode.read()
|
|
|
|
video = input_dir + "videos/" + gen_name(date, slug) + ".mp4"
|
|
|
|
podcast.append(Episode(date, slug, title, show_notes, video))
|
2020-01-24 22:15:30 +01:00
|
|
|
|
|
|
|
podcast.sort()
|
2020-01-25 19:10:25 +01:00
|
|
|
podcast.generate_thumbnails()
|
2020-01-24 22:15:30 +01:00
|
|
|
podcast.generate_atom()
|
|
|
|
podcast.generate_site()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|