-
Notifications
You must be signed in to change notification settings - Fork 0
/
f2e_alt_cover.py
319 lines (273 loc) · 9.46 KB
/
f2e_alt_cover.py
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python3
import argparse
import logging
import os
import re
import subprocess
import sys
__version__ = "1.0.0b"
log = logging.getLogger(__name__)
streamer = logging.StreamHandler()
formatter = logging.Formatter(
fmt="[{asctime}] [{levelname:^10}] {message}",
datefmt="%Y/%m/%d %H:%M:%S",
style="{",
)
streamer.setFormatter(formatter)
log.addHandler(streamer)
FONT_HELP_FMT = "{0}'s font to use on cover if needed. Recomended font: {1}."
FONT_SIZE_HELP_FMT = "{0}'s font size to use on cover if needed (Default: {1})."
DEFAULT_EXECUTABLE = "fimfic2epub.cmd" if sys.platform == "win32" else "fimfic2epub"
FIMFIC_STORY_URL_REGEX = r"https?://(?:www.)?fimfiction.net/story/(?P<ID>\d+)"
def get_api_response(story_id) -> dict:
import requests
url = f"https://www.fimfiction.net/api/story.php?story={story_id}"
log.debug("Getting story matadata from: %s", url)
data = requests.get(url).json()
log.debug("Got the following API response: %s", data)
return data["story"]
def logged_exit(return_code=0):
log.debug("Finished with return code of %s.", return_code)
exit(return_code)
COVER_SIZE = (1080, 1440)
COVER_BACKGROUND = (0, 0, 0)
COVER_FOREGROUND = (255, 255, 255)
SIDE_PADDING = 108
TITLE_POS = (SIDE_PADDING, 150)
AUTHOR_POS = (SIDE_PADDING, 1200)
LINE_SPACING = 1.2
def create_placeholder_cover(
*,
title,
author,
filename,
title_font,
title_font_size,
author_font,
author_font_size,
):
from PIL import Image, ImageDraw, ImageFont
log.debug("Loading fonts.")
title_font = ImageFont.truetype(title_font, size=title_font_size)
author_font = ImageFont.truetype(author_font, size=author_font_size)
im = Image.new("RGB", COVER_SIZE, COVER_BACKGROUND)
draw = ImageDraw.Draw(im, mode="RGB")
log.debug("Drawing rectangles.")
for pad in [12, 20]:
draw.rectangle(
[(pad, pad), (COVER_SIZE[0] - pad, COVER_SIZE[1] - pad)],
fill=COVER_BACKGROUND,
outline=COVER_FOREGROUND,
width=2,
)
max_width = COVER_SIZE[0] - SIDE_PADDING * 2
for (x, y), text, font in [
(TITLE_POS, title, title_font),
(AUTHOR_POS, author, author_font),
]:
log.debug("Drawing text %s with the font %s.", repr(text), repr(font))
# Centering algorithm from this gist:
# https://gist.github.com/pojda/8bf989a0556845aaf4662cd34f21d269
lines = []
line = []
words = text.split()
for word in words:
new_line = " ".join(line + [word])
size = font.getsize(new_line)
text_height = size[1] * LINE_SPACING
if size[0] <= max_width:
line.append(word)
else:
lines.append(line)
line = [word]
if line:
lines.append(line)
lines = [" ".join(line) for line in lines if line]
height = y
for line in lines:
total_size = font.getsize(line)
x_left = int(x + ((max_width - total_size[0]) / 2))
draw.text((x_left, height), line, fill=COVER_FOREGROUND, font=font)
height += text_height
log.debug("Saving cover image to %s", repr(filename))
im.save(filename)
def main():
parser = argparse.ArgumentParser(
description="A wrapper script that replaces the default cover of fimfic2epub.",
add_help=False,
)
parser.add_argument(
"-v",
"--version",
action="version",
version="%(prog)s, version " + __version__,
help="Print script's version and exit.",
)
parser.add_argument(
"-h",
"--help",
action="help",
default=argparse.SUPPRESS,
help="Show this help message and exit.",
)
parser.add_argument("--debug", action="store_true", help="Show debugging output.")
parser.add_argument(
"--image-dir",
default=os.getcwd(),
metavar="DIR",
help="Directory to store the cover image and serve it to "
"0.0.0.0 if necessary. Defaults to current directory.",
)
parser.add_argument(
"--title-font",
required=True,
metavar="FONT",
help=FONT_HELP_FMT.format("Title", "Montserrat-Bold"),
)
parser.add_argument(
"--title-font-size",
type=int,
default=100,
metavar="SIZE",
help=FONT_SIZE_HELP_FMT.format("Title", 100),
)
parser.add_argument(
"--author-font",
required=True,
metavar="FONT",
help=FONT_HELP_FMT.format("Author", "Montserrat-Regular"),
)
parser.add_argument(
"--author-font-size",
type=int,
default=50,
metavar="SIZE",
help=FONT_SIZE_HELP_FMT.format("Author", 50),
)
parser.add_argument(
"--wait",
type=int,
default=5,
help="Extra seconds to wait before executing fimfic2epub "
"if the server is started to ensure that it is ready (Default: 5).",
)
parser.add_argument(
"--fimfic2epub-executable",
default=DEFAULT_EXECUTABLE,
metavar="FILEPATH",
help="Location of the fimfic2epub executable "
f'(Default: "{DEFAULT_EXECUTABLE}").',
)
parser.add_argument(
"--fimfic2epub-dir",
metavar="DIR",
help='Forwarded into fimfic2epub as "--dir DIR".',
)
parser.add_argument(
"--fimfic2epub-extra-flags",
metavar="ARGS",
help="Flags to forward into fimfic2epub. "
'Take care with "-C <url>", it is added automatically when the story '
"doesn't have a cover. "
"If you want to define the directory use --fimfic2epub-dir instead.",
)
parser.add_argument(
"--fimfic2epub-filename",
metavar="FILENAME",
help="Filename of the epub that will be created, "
"forwarded into fimfic2epub itself.",
)
parser.add_argument("story", help="Fimfiction story to download with fimfic2epub.")
args = parser.parse_args()
log.setLevel(logging.DEBUG if args.debug else logging.INFO)
executable = args.fimfic2epub_executable
if not executable == DEFAULT_EXECUTABLE:
if not os.path.isfile(executable):
log.error("%s doesn't exist.", repr(executable))
logged_exit(1)
story_id = args.story
if not story_id.isnumeric():
m = re.match(FIMFIC_STORY_URL_REGEX, args.story)
if not m:
log.error("%s doesn't seem like a story ID nor a Fimfiction URL", story_id)
logged_exit(1)
story_id = m.groupdict()["ID"]
cmd = [executable, story_id]
output_name = args.fimfic2epub_filename
if output_name:
log.debug("Appending %s to fimfic2epub's execution command.", repr(output_name))
cmd.append(output_name)
output_dir = args.fimfic2epub_dir
if output_dir:
log.debug(
"Appending the flag --dir with value %s to fimfic2epub's "
"execution command.",
repr(output_dir),
)
for arg in reversed(["--dir", output_dir]):
cmd.insert(1, arg)
extra_flags = args.fimfic2epub_extra_flags
if extra_flags:
log.debug(
"Inserting the flags %s to fimfic2epub's execution command.",
repr(extra_flags),
)
import shlex
for arg in reversed(shlex.split(extra_flags)):
cmd.insert(1, arg)
cover_filename = f"{story_id}.jpeg"
cover_path = os.path.join(args.image_dir, cover_filename)
cover_file_exists = os.path.isfile(cover_path)
# Only making a request to the API when there isn't a generated cover.
cover_exists = False
if not cover_file_exists:
api_response = get_api_response(story_id)
cover_exists = bool(api_response.get("image", None))
if not cover_exists:
import time
log.info(
"Story of ID %s doesn't have a cover, fimfic2epub NEEDS one.", story_id
)
if not cover_file_exists:
log.info("Creating cover.")
create_placeholder_cover(
title=api_response["title"],
author=api_response["author"]["name"],
filename=cover_path,
title_font=args.title_font,
title_font_size=args.title_font_size,
author_font=args.author_font,
author_font_size=args.author_font_size,
)
else:
log.info("Skipping cover generation since it already exists.")
server = f"http://0.0.0.0:8000/{cover_filename}"
log.debug(
"Adding -C flag pointing to %s in fimfic2epub's execution command.",
repr(server),
)
for arg in reversed(["-C", server]):
cmd.insert(1, arg)
log.info("Serving the file in %s.", repr(server))
server_proc = subprocess.Popen(
[sys.executable, "-m", "http.server"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
cwd=args.image_dir,
)
log.debug("Waiting %s second(s).", args.wait)
time.sleep(args.wait)
log.info("Executing: %s", " ".join(map(lambda s: repr(s) if " " in s else s, cmd)))
try:
fimfic2epub_proc = subprocess.run(cmd)
logged_exit(fimfic2epub_proc.returncode)
finally:
if not cover_exists:
log.info("Closing server.")
server_proc.terminate()
if __name__ == "__main__":
try:
main()
except Exception:
log.exception("Unhandled exception!")
logged_exit(1)