summaryrefslogtreecommitdiff
path: root/modules/comic_generate.py
blob: de8fad874bba33928f23f6dfeade940ef1533d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import datetime
import email.utils
import json
import os.path
import shutil
import jinja2

from ..config import Config

META_FILE_NAME = "meta.json"
IMAGE_FILE_NAME = "comic.png"

PUBLISH_DATE_KEY = "publish_date"


class Issue:
    def __init__(self, path: str):
        self.path = path
        self.name = os.path.basename(path)

        with open(os.path.join(path, META_FILE_NAME)) as f:
            self.meta_data = json.load(f)


def get_issues(path: str, local: bool) -> list[Issue]:
    return_list = []

    for directory in os.listdir(path):
        if directory.endswith(".unpublished") and not local:
            continue

        return_list.append(Issue(os.path.join(path, directory)))

    return_list.sort(key=lambda post: post.meta_data[PUBLISH_DATE_KEY], reverse=True)

    return return_list

def generate(jinja_env: jinja2.Environment, output_root_path: str, local: bool):
    if os.path.exists(output_root_path):
        shutil.rmtree(output_root_path)

    os.mkdir(output_root_path)

    issues = get_issues(Config.POST_SOURCE_DIR, local)