[SSG] Add generate_site to generate episodes, copy videos, audios.

This commit is contained in:
Ceda EI 2020-01-26 03:24:59 +05:30
parent 97c898acdb
commit 8b9a367fb4
1 changed files with 44 additions and 8 deletions

52
nova.py
View File

@ -8,6 +8,7 @@ import os
import os.path as path import os.path as path
import re import re
import subprocess import subprocess
import shutil
import sys import sys
import jinja2 import jinja2
@ -44,18 +45,52 @@ class EpisodeList(UserList):
def generate_atom(self): def generate_atom(self):
"Generates the Atom feed" "Generates the Atom feed"
def generate_site(self): def generate_site(self, root):
"Generates the entire " "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",
"assets/audios",
"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")
audio = (self.output + "assets/audios/" +
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")
class Episode: class Episode:
"Represents one episode of podcast" "Represents one episode of podcast"
def __init__(self, date, slug, title, show_notes, video_src): def __init__(self, date, slug, title, show_notes, video_src, audo_src):
self.date = date self.date = date
self.slug = slug self.slug = slug
self.title = title self.title = title.strip()
self.show_notes = markdown.markdown(show_notes) self.show_notes = markdown.markdown(show_notes)
self.video = video_src self.video = video_src
self.audio = audo_src
def render(self, template, thumbnail_src): def render(self, template, thumbnail_src):
"Renders the Episode with the given template" "Renders the Episode with the given template"
@ -63,7 +98,7 @@ class Episode:
title=self.title, title=self.title,
show_notes=jinja2.Markup(self.show_notes), show_notes=jinja2.Markup(self.show_notes),
thumbnail_src=thumbnail_src, thumbnail_src=thumbnail_src,
video_src=self.video video_src=f"assets/videos/{path.basename(self.video)}"
) )
def store_thumbnail(self, location): def store_thumbnail(self, location):
@ -75,7 +110,7 @@ class Episode:
def main(): def main():
"Main method" "Main method"
root = path.dirname(sys.argv[0]) root = path.dirname(sys.argv[0]).rstrip("/") + "/"
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("input_dir", help="Input directory") parser.add_argument("input_dir", help="Input directory")
parser.add_argument("output_dir", help="Output directory") parser.add_argument("output_dir", help="Output directory")
@ -111,12 +146,13 @@ def main():
title = episode.readline() title = episode.readline()
show_notes = episode.read() show_notes = episode.read()
video = input_dir + "videos/" + gen_name(date, slug) + ".mp4" video = input_dir + "videos/" + gen_name(date, slug) + ".mp4"
podcast.append(Episode(date, slug, title, show_notes, video)) audio = input_dir + "audios/" + gen_name(date, slug) + ".mp3"
podcast.append(Episode(date, slug, title, show_notes, video, audio))
podcast.sort() podcast.sort()
podcast.generate_thumbnails() podcast.generate_thumbnails()
podcast.generate_atom() podcast.generate_atom()
podcast.generate_site() podcast.generate_site(root)
if __name__ == "__main__": if __name__ == "__main__":