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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
import json
import os
import time
from glob import glob
from itertools import batched
import jinja2
from PIL import ImageFile
from config import WILDCARD_LETTER, THUMBNAIL_CHUNK_SIZE, PROCS, GLOBALS
from imgutils import create_thumbnail_sheet, convert_cover, \
convert_chapter
ImageFile.LOAD_TRUNCATED_IMAGES = True
from minify_html import minify
from slugify import slugify
from tqdm import tqdm
from rcssmin import cssmin
from multiprocessing.pool import ThreadPool
env = jinja2.Environment(
undefined=jinja2.StrictUndefined,
)
env.filters["slug"] = slugify
env.filters["minify_css"] = cssmin
def render_index_pages(path, prefix, index_items, content_rating):
with open("assets/index.html.jinja") as f:
template = env.from_string(f.read())
index_parent_dir = f"{prefix}{('-' + content_rating.lower()) if content_rating else ''}"
def _work(task):
item, series = task
index_loc = item.replace(WILDCARD_LETTER, "_")
index_dir = str(os.path.join(path, index_parent_dir, index_loc))
os.makedirs(index_dir)
if prefix == "t":
series = sorted(series, key=lambda s: s["score"], reverse=True)
else:
series = sorted(series, key=lambda s: s["sort_name"])
thumbnails = [
{
"cover": glob(os.path.join(path, s["location"], "cover.*"))[0],
"name": s["name"],
"loc": s["location"]
}
for s in series
]
for i, chunk in enumerate(batched(thumbnails, THUMBNAIL_CHUNK_SIZE)):
sheet_name = f"{int(time.time()):x}{i:x}"
create_thumbnail_sheet(chunk, os.path.join(index_dir, sheet_name + ".webp"))
sheet_img = os.path.join(index_parent_dir, index_loc, sheet_name + ".webp")
for j, tn in enumerate(chunk):
offset_class = f"o{j:x}"
tn["offset"] = offset_class
tn["img"] = sheet_img
is_long_list = len(index_items) > 27
html = minify(template.render(
name=item,
thumbnails=thumbnails,
parent_loc=index_parent_dir,
loc=index_loc,
prefix=prefix,
index_pages=[
(
item.replace("-", " ") if is_long_list else item.upper(),
item,
os.path.join(index_parent_dir, item.replace(WILDCARD_LETTER, "_")),
len(series) > 0
)
for item, series in index_items.items()
],
tags=is_long_list,
rating=content_rating,
**GLOBALS
))
with open(os.path.join(index_dir, "index.html"), "w") as f:
f.write(html)
with ThreadPool(processes=PROCS) as pool:
tasks = list(index_items.items())
for _ in tqdm(pool.imap_unordered(_work, tasks), total=len(tasks)):
pass
def render_series_pages(path, index):
with open("assets/series.html.jinja") as f:
template = env.from_string(f.read())
def _work(series):
series_dir = str(os.path.join(path, series["location"]))
os.makedirs(series_dir)
convert_cover(series["id"] + "_c", series["cover"], series_dir)
html = minify(template.render(
series_name = series["name"],
loc = series["location"],
chapters = series["chapters"],
tags = list(filter(lambda t: t != "all", series["tags"])),
rating = series["rating"],
**GLOBALS
))
with open(os.path.join(series_dir, "index.html"), "w") as f:
#TODO: Debug
# f.write(f"<!-- rm -r data/{slugify(series['name'])} && echo '{series['_url']}' >> history.txt && echo '{series['_url']}' >> bl.txt -->")
f.write(html)
with ThreadPool(processes=PROCS) as pool:
for _ in tqdm(pool.imap_unordered(_work, index), total=len(index)):
pass
def render_chapter_pages(path, index):
with open("assets/chapter.html.jinja") as f:
template = env.from_string(f.read())
def _work(task):
series, chapter = task
chapter_dir = str(os.path.join(path, *chapter["location"].split("/")))
convert_chapter(
cache_key=f"{series['id']}.{chapter['index']:03d}",
images=chapter["images"],
output_folder=chapter_dir
)
images = [
os.path.basename(filename)
for filename in
sorted(glob(os.path.join(chapter_dir, "*.webp")))
]
html = minify(template.render(
series_name = series["name"],
series_loc = series["location"],
chapter_name = chapter["name"],
loc = chapter["location"],
prev_loc = chapter.get("prev"),
next_loc = chapter.get("next"),
images = images,
**GLOBALS
))
with open(os.path.join(chapter_dir, "index.html"), "w") as f:
f.write(html)
tasks = []
for series in index:
for chapter in series["chapters"]:
tasks.append((series, chapter))
with ThreadPool(processes=PROCS) as pool:
for _ in tqdm(pool.imap_unordered(_work, tasks), total=len(tasks)):
pass
def render_home(path):
with open("assets/home.html.jinja") as f:
template = env.from_string(f.read())
html = minify(template.render(
**GLOBALS
))
with open(os.path.join(path, "index.html"), "w") as f:
f.write(html)
with open("assets/manifest.json.jinja") as f:
template = env.from_string(f.read())
data = json.loads(template.render(
**GLOBALS
))
with open(os.path.join(path, "manifest.json"), "w") as f:
json.dump(data, f, sort_keys=True)
def render_style(path):
with open("assets/style.css.jinja") as f:
template = env.from_string(f.read())
css = cssmin(template.render(
**GLOBALS
))
with open(os.path.join(path, GLOBALS["style"]), "w") as f:
f.write(css)
|