-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf_builder.py
executable file
·190 lines (153 loc) · 4.74 KB
/
pdf_builder.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
#!/usr/bin/env python3
# coding: utf-8
import tempfile
import os
from multiprocessing import Pool
import glob
import urllib.request
import subprocess
import html.parser
from functools import partial
# TODO: accept as parameter
URL_EXAMPLE = "https://www.keyforgegame.com/deck-details/f52ef95f-5ddb-463a-91c5-0dcdd0ed4b14"
CARDS_PATH = "./cards/"
CONVERT_PATH = '/usr/local/bin/convert'
OUTPUT_FILE = "./result.pdf"
JPEG_QUALITY = "94"
FILE_PATTERN = "[0-9][0-9][0-9]*"
USER_AGENT = (
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) ' +
'AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/35.0.1916.47 Safari/537.36'
)
DEFAULT_CROP_MARK_LENGTH = 24
DEFAULT_CROP_MARK_WIDTH = 2
class HTMLParser(html.parser.HTMLParser):
def __init__(self):
super(HTMLParser, self).__init__()
self.cards = []
def handle_starttag(self, tag, attrs):
NUMBER_CLASS = "card-table__deck-card-number"
self.in_card_number_span = (
(tag == 'span') and
("class", NUMBER_CLASS) in attrs
)
def handle_endtag(self, tag):
if self.in_card_number_span and tag == 'span':
self.in_card_number_span = False
def handle_data(self, data):
if self.in_card_number_span:
self.cards.append(int(data))
def rm(fname):
os.remove(fname)
def __run(*args):
try:
subprocess.check_output(args, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print(e.output)
raise
def convert(*params):
__run(CONVERT_PATH, *params)
def load_image_map():
images = {}
for fname in glob.glob(os.path.join(CARDS_PATH, FILE_PATTERN)):
fid = os.path.basename(fname)[:3]
images[int(fid)] = fname
return images
def get_deck_page(url):
req = urllib.request.Request(
url,
data=None,
headers={ 'User-Agent': USER_AGENT }
)
return urllib.request.urlopen(req).read().decode('utf-8')
def get_card_list(text):
parser = HTMLParser()
parser.feed(text)
return parser.cards
def get_temp_fname(suffix=".png"):
with tempfile.NamedTemporaryFile(suffix=suffix) as f:
return f.name
def build_page(crop_marks_file, page_cards):
assert len(page_cards) == 9, "Page should contain 9 cards"
A4_SIZE = "2480x3508"
BORDERED_SIZE = "2274x3174-12-12"
cs = page_cards
fname = get_temp_fname(".jpg")
convert(*[
"-size", A4_SIZE, "xc:white",
"(",
"(",
"(", cs[0], cs[1], cs[2], "+append", ")",
"(", cs[3], cs[4], cs[5], "+append", ")",
"(", cs[6], cs[7], cs[8], "+append", ")",
"-append",
")", crop_marks_file, "-gravity", "center", "-composite",
"(",
"-clone", "0",
"-set", "option:distort:viewport", BORDERED_SIZE,
"-virtual-pixel", "mirror",
"-distort", "SRT", "0,0 1,1 0",
")",
"(", "-clone", "0", ")",
"-delete", "0",
"-gravity", "center",
"-compose", "over",
"-composite",
"+repage",
")",
"-gravity", "center",
"-composite",
"-quality", JPEG_QUALITY,
fname
])
return fname
def build_crop_marks(w, h):
l = DEFAULT_CROP_MARK_LENGTH
crop_file_name = get_temp_fname()
convert(
"(",
"-size", f"{w}x{h}",
"xc:none",
"-fill", "red",
"-strokewidth", str(DEFAULT_CROP_MARK_WIDTH),
"-draw", (
f"line 0,0 0,{l} " +
f"line 0,0 {l},0 " +
f"line 0,{h - 1} 0,{h - l} " +
f"line 0,{h - 1} {l},{h - 1} " +
f"line {w - 1},0 {w - 1},{l} " +
f"line {w - 1},0 {w - l},0 " +
f"line {w - 1},{h - 1} {w - l},{h - 1} " +
f"line {w - 1},{h - 1} {w - 1},{h - l}"
),
")",
"-duplicate", "2", "+append",
"-duplicate", "2", "-append",
"+repage",
crop_file_name
)
return crop_file_name
def build_pdf(card_list):
assert len(card_list) == 36, "Deck should consist of 36 cards"
crop_marks_file = build_crop_marks(750, 1050)
fs = []
# TODO: add card backs
with Pool() as p:
fs = list(p.map(
partial(build_page, crop_marks_file),
zip(*[iter(card_list)]*9)
))
convert(*(fs + [OUTPUT_FILE]))
for f in fs:
rm(f)
rm(crop_marks_file)
def main():
# TODO: add logging
image_map = load_image_map()
html = get_deck_page(URL_EXAMPLE)
deck = get_card_list(html)
deck_images = list(map(lambda it: image_map[it], deck))
build_pdf(deck_images)
if __name__ == "__main__":
main()