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-25 22:54:59 +01:00
|
|
|
import shutil
|
2020-01-24 22:15:30 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import jinja2
|
2020-01-25 20:10:29 +01:00
|
|
|
import markdown
|
2020-01-24 22:15:30 +01:00
|
|
|
|
|
|
|
|
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"
|
2020-01-26 00:28:09 +01:00
|
|
|
def __init__(self, data, output, template, archives):
|
2020-01-24 22:15:30 +01:00
|
|
|
super().__init__(data)
|
|
|
|
self.output = output
|
|
|
|
self.template = template
|
2020-01-26 00:28:09 +01:00
|
|
|
self.archives = archives
|
2020-01-24 22:15:30 +01:00
|
|
|
|
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"
|
|
|
|
|
2020-01-26 00:28:09 +01:00
|
|
|
def generate_archives(self):
|
|
|
|
"Generates archives page"
|
|
|
|
with open(self.output + "archives.html", "w") as file:
|
|
|
|
episodes = [{
|
|
|
|
"slug": gen_name(i.date, i.slug) + ".html",
|
|
|
|
"title": i.title
|
|
|
|
} for i in self.data]
|
|
|
|
file.write(self.archives.render(episodes=episodes))
|
|
|
|
|
2020-01-25 22:54:59 +01:00
|
|
|
def generate_site(self, root):
|
|
|
|
"Generates the entire site"
|
|
|
|
# Generate CSS from SCSS
|
|
|
|
subprocess.run(["sass", "--update", f"{root}scss:{root}assets/css"],
|
|
|
|
check=True)
|
|
|
|
# Copy the existing assets
|
|
|
|
shutil.copytree(root + "assets", self.output + "assets",
|
|
|
|
dirs_exist_ok=True)
|
|
|
|
# Create the required directories
|
|
|
|
paths = [
|
|
|
|
"assets",
|
2020-01-26 03:00:02 +01:00
|
|
|
"assets/audio",
|
2020-01-25 22:54:59 +01:00
|
|
|
"assets/videos",
|
|
|
|
]
|
|
|
|
for directory in paths:
|
|
|
|
if not path.isdir(self.output + directory):
|
|
|
|
os.mkdir(self.output + directory)
|
|
|
|
|
|
|
|
# Render episodes and copy data
|
|
|
|
for episode in self.data:
|
|
|
|
html = f"{self.output}{gen_name(episode.date, episode.slug)}.html"
|
|
|
|
thumbnail = ("assets/thumbnails/" +
|
|
|
|
gen_name(episode.date, episode.slug) + ".jpg")
|
|
|
|
video = (self.output + "assets/videos/" +
|
|
|
|
gen_name(episode.date, episode.slug) + ".mp4")
|
2020-01-26 03:00:02 +01:00
|
|
|
audio = (self.output + "assets/audio/" +
|
2020-01-25 22:54:59 +01:00
|
|
|
gen_name(episode.date, episode.slug) + ".mp3")
|
|
|
|
shutil.copy2(episode.video, video)
|
|
|
|
shutil.copy2(episode.audio, audio)
|
|
|
|
with open(html, "w") as file:
|
|
|
|
file.write(episode.render(self.template, thumbnail))
|
|
|
|
|
|
|
|
last = self.data[-1]
|
|
|
|
last_name = self.output + gen_name(last.date, last.slug) + ".html"
|
|
|
|
shutil.copy2(last_name, self.output + "index.html")
|
2020-01-24 22:15:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Episode:
|
|
|
|
"Represents one episode of podcast"
|
2020-01-25 22:54:59 +01:00
|
|
|
def __init__(self, date, slug, title, show_notes, video_src, audo_src):
|
2020-01-24 22:15:30 +01:00
|
|
|
self.date = date
|
|
|
|
self.slug = slug
|
2020-01-25 22:54:59 +01:00
|
|
|
self.title = title.strip()
|
2020-01-25 20:10:29 +01:00
|
|
|
self.show_notes = markdown.markdown(show_notes)
|
2020-01-25 19:10:25 +01:00
|
|
|
self.video = video_src
|
2020-01-25 22:54:59 +01:00
|
|
|
self.audio = audo_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 22:54:59 +01:00
|
|
|
video_src=f"assets/videos/{path.basename(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 22:54:59 +01:00
|
|
|
root = path.dirname(sys.argv[0]).rstrip("/") + "/"
|
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
|
2020-01-26 00:28:09 +01:00
|
|
|
paths = [
|
|
|
|
input_dir,
|
|
|
|
input_dir + "md",
|
|
|
|
input_dir + "videos",
|
2020-01-26 03:00:02 +01:00
|
|
|
input_dir + "audio",
|
2020-01-26 00:28:09 +01:00
|
|
|
]
|
|
|
|
if not all(path.isdir(i) for i in paths):
|
2020-01-24 22:15:30 +01:00
|
|
|
print("Invalid Input", file=sys.stderr)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not path.isdir(args.output_dir):
|
|
|
|
os.mkdir(args.output_dir)
|
|
|
|
|
2020-01-26 00:28:09 +01:00
|
|
|
env = 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")
|
2020-01-26 00:28:09 +01:00
|
|
|
)
|
2020-01-24 22:15:30 +01:00
|
|
|
|
2020-01-26 00:28:09 +01:00
|
|
|
podcast = EpisodeList(
|
|
|
|
[],
|
|
|
|
output_dir,
|
|
|
|
env.get_template("index.html"),
|
|
|
|
env.get_template("archives.html")
|
|
|
|
)
|
2020-01-24 22:15:30 +01:00
|
|
|
|
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()
|
2020-01-26 00:28:09 +01:00
|
|
|
podcast.append(
|
|
|
|
Episode(
|
|
|
|
date,
|
|
|
|
slug,
|
|
|
|
title,
|
|
|
|
show_notes,
|
|
|
|
input_dir + "videos/" + gen_name(date, slug) + ".mp4",
|
2020-01-26 03:00:02 +01:00
|
|
|
input_dir + "audio/" + gen_name(date, slug) + ".mp3"
|
2020-01-26 00:28:09 +01:00
|
|
|
)
|
|
|
|
)
|
2020-01-24 22:15:30 +01:00
|
|
|
|
|
|
|
podcast.sort()
|
2020-01-25 19:10:25 +01:00
|
|
|
podcast.generate_thumbnails()
|
2020-01-26 00:28:09 +01:00
|
|
|
podcast.generate_archives()
|
2020-01-24 22:15:30 +01:00
|
|
|
podcast.generate_atom()
|
2020-01-25 22:54:59 +01:00
|
|
|
podcast.generate_site(root)
|
2020-01-27 00:13:39 +01:00
|
|
|
shutil.copytree(input_dir + "overrides", output_dir, dirs_exist_ok=True)
|
2020-01-24 22:15:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|