diff --git a/nova.py b/nova.py index 7096555..172cca3 100755 --- a/nova.py +++ b/nova.py @@ -30,7 +30,7 @@ class EpisodeList(UserList): def sort(self, *_args, **_kwargs): "Sorts the EpisodeList" - super().sort(key=lambda x: x.date, reverse=True) + super().sort(key=lambda x: x.date, reverse=False) def generate_thumbnails(self): "Generates thumbnails for all the videos" @@ -88,20 +88,21 @@ class EpisodeList(UserList): 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") + last = self.data[-1] + last_name = f"{self.output}{gen_name(last.date, last.slug)}.html" + shutil.copy2(last_name, self.output + "index.html") class Episode: "Represents one episode of podcast" - def __init__(self, date, slug, title, show_notes, video_src, audio_src): + def __init__(self, date, slug, title, show_notes, video_src, audio_src, config): self.date = date self.slug = slug self.title = title.strip() self.show_notes = markdown.markdown(show_notes) self.video = video_src self.audio = audio_src + self.config = config def render(self, template, thumbnail_src): "Renders the Episode with the given template" @@ -130,10 +131,35 @@ def parse_args(): return input_dir, output_dir -def main(): +class ParseError(ValueError): + "Error raised while parsing a file" + + +def parse_file(file, array_keys=("categories")): + "Parses a file" + config = {} + kv_re = re.compile(r"(?P\w+):\s*(?P.*)") + while line := file.readline(): + if line.rstrip("\n") == "---": + break + if line.strip() == "": + continue + if match := kv_re.match(line): + if match.group("key").strip().lower() in array_keys: + config[match.group("key")] = [i.strip() for i in + match.group("value").split(",")] + else: + config[match.group("key")] = match.group("value") + else: + raise ParseError(f"Invalid line {line}") + + return (config, file.read()) + + +def main(args): "Main method" root = path.dirname(sys.argv[0]).rstrip("/") + "/" - input_dir, output_dir = parse_args() + input_dir, output_dir = args # Input validation paths = [ @@ -142,9 +168,9 @@ def main(): input_dir + "videos", input_dir + "audio", ] - if not all(path.isdir(i) for i in paths): - print("Invalid Input", file=sys.stderr) - return + if not all(path.isdir((fail := i)) for i in paths): + print(f"Invalid Input. {fail} is not a directory.", file=sys.stderr) + return 1 if not path.isdir(output_dir): os.mkdir(output_dir) @@ -170,16 +196,21 @@ def main(): date = datetime.strptime(match.group("date"), "%Y-%M-%d") slug = match.group("slug") with open(input_dir + "md/" + file) as episode: - title = episode.readline() - show_notes = episode.read() + try: + config, show_notes = parse_file(episode) + except ParseError as err: + print(f"Error while parsing file: {file}") + print(err) + return 2 podcast.append( Episode( date, slug, - title, + config["title"], show_notes, input_dir + "videos/" + gen_name(date, slug) + ".mp4", - input_dir + "audio/" + gen_name(date, slug) + ".mp3" + input_dir + "audio/" + gen_name(date, slug) + ".mp3", + config ) ) @@ -189,7 +220,8 @@ def main(): podcast.generate_atom() podcast.generate_site(root) shutil.copytree(input_dir + "overrides", output_dir, dirs_exist_ok=True) + return 0 if __name__ == "__main__": - main() + sys.exit(main(parse_args()))