-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate_book.py
303 lines (252 loc) · 9.14 KB
/
generate_book.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
import argparse
import os
import random
import tempfile
from PyPDF2 import PdfReader, PdfMerger
from src.generated_content_utils import load_json_files_from_path
from src.generate_pages import (
make_chapter_divider_page,
make_blank_page,
is_left_page,
set_ebook_version,
)
from src.generate_book_utils import (
latest_puzzle_folder,
sort_dicts_by_release_year,
create_dir,
create_puzzles_and_answers
)
parser = argparse.ArgumentParser()
parser.add_argument("--layout", required=True, choices=["ebook", "paperback"])
parser.add_argument("--input", default=None, type=str)
args = parser.parse_args()
IS_EBOOK = args.layout == "ebook"
IS_PAPERBACK = not IS_EBOOK
set_ebook_version(IS_EBOOK)
iteration_id = args.input if args.input else latest_puzzle_folder()
print(f'Generating {args.layout} from {iteration_id}')
content_source_directory = f'output/generated_puzzles/{iteration_id}'
movies = sort_dicts_by_release_year(
load_json_files_from_path(
f'{content_source_directory}/movies/'))
tv_shows = sort_dicts_by_release_year(
load_json_files_from_path(
f'{content_source_directory}/tv_shows/'))
books = sort_dicts_by_release_year(
load_json_files_from_path(
f'{content_source_directory}/books/'))
manuscript_output_dir = f'output/generated_manuscripts/{iteration_id}'
create_dir(manuscript_output_dir)
temp_output_dir = tempfile.mkdtemp()
create_dir(temp_output_dir)
# These pages are generated from code
blank_page = make_blank_page(f'{temp_output_dir}/blank.pdf')
title_page = make_chapter_divider_page('Emoji Puzzles', '🧩', f'{temp_output_dir}/title.pdf')
dedication_page = make_chapter_divider_page('Dedication', '❤️', f'{temp_output_dir}/dedication.pdf')
intro_page = make_chapter_divider_page('Intro', '👋', f'{temp_output_dir}/intro.pdf')
copyright_attributions = make_chapter_divider_page('Copyright ©', '⚖️', f'{temp_output_dir}/copyright_attributions.pdf')
movies_page = make_chapter_divider_page('Movies', '🎬', f'{temp_output_dir}/movies.pdf')
tv_shows_page = make_chapter_divider_page('TV Shows', '📺', f'{temp_output_dir}/tv_shows.pdf')
books_page = make_chapter_divider_page('Books', '📚', f'{temp_output_dir}/books.pdf')
answers_page = make_chapter_divider_page('Answers', '💡', f'{temp_output_dir}/answers.pdf')
# These pages were created manually on https://www.canva.com/ and included here
qr_code_page = 'static/qr-code.pdf'
toc_paperback = 'static/paperback-table-of-contents.pdf'
toc_ebook = 'static/ebook-table-of-contents.pdf'
how_to_play = 'static/how-to-play.pdf'
# Before building the book, figure out what page each Puzzle and Answer will land on
puzzle_page_lookup = {}
answer_page_lookup = {}
puzzle_to_answer = {}
# Ebooks + Paperback layouts are different.
# Paperback books need to have blank pages to ensure Left/Right page alignment
# So you'll see some IF statements treating them differently as the book is being built
if IS_EBOOK:
page_offset = len([
'title',
'copyright_attribution',
'dedication',
'QR Code', 'intro',
'how-to-play',
'Table of Contents',
'Movies Chapter Page',
])
else:
page_offset = len([
'title',
'copyright_attribution', 'dedication',
'QR Code', 'intro',
'blank', 'how-to-play',
'blank', 'Table of Contents',
'blank', 'Movies Chapter Page',
'blank'
])
if IS_EBOOK:
# Page numbers are just actual PDF natural page numbers
current_page_num = page_offset
else:
# "Page 1" is the first chapter title page, but the number is invisible
current_page_num = 1
# 😅 Please don't judge me too hard - the following code is very repetitive
# But I genuinely feel it's easier for layout to keep it this way - easier to change if needed
# Add all the Movie Puzzles
for item in movies:
title = item['title']
current_page_num += 1
puzzle_page_lookup[title] = current_page_num
if IS_PAPERBACK:
# If the last page was on the LEFT
if is_left_page(current_page_num):
# add 2 more for LEFT (RIGHT, LEFT)
current_page_num += 2
else:
# Add one for RIGHT (LEFT)
current_page_num += 1
# Hold space for a Chapter Page
current_page_num += 1
if IS_PAPERBACK:
# Add one so LEFT after chapter page can be blank
current_page_num += 1
# Add all the TV Shows
for item in tv_shows:
title = item['title']
current_page_num += 1
puzzle_page_lookup[title] = current_page_num
if IS_PAPERBACK:
# If the last page was on the LEFT
if is_left_page(current_page_num):
# add 2 more for LEFT (RIGHT, LEFT)
current_page_num += 2
else:
# Add one for RIGHT (LEFT)
current_page_num += 1
# Hold space for Chapter Page
current_page_num += 1
if IS_PAPERBACK:
# Add one so LEFT after chapter page can be blank
current_page_num += 1
# Add all the Books
for item in books:
title = item['title']
current_page_num += 1
puzzle_page_lookup[title] = current_page_num
if IS_PAPERBACK:
# If the last page was on the LEFT
if is_left_page(current_page_num):
# add 2 more for LEFT (RIGHT, LEFT)
current_page_num += 2
else:
# Add one for RIGHT (LEFT)
current_page_num += 1
# Hold space for Chapter Page
current_page_num += 1
if IS_PAPERBACK:
# Add one so LEFT after chapter page can be blank
current_page_num += 1
# Shuffle the answers to keep it interesting
all_items_for_answers = movies + tv_shows + books
random.shuffle(all_items_for_answers)
# Add the Answer pages
answer_pages = []
for item in all_items_for_answers:
title = item['title']
current_page_num += 1
answer_page_lookup[title] = current_page_num
answer_pdf_file = f'{temp_output_dir}//{title}-answer.pdf'
answer_pages.append(answer_pdf_file)
# Create PUZZLES and ANSWER PDFs for all categories
common_args = [puzzle_page_lookup, answer_page_lookup, temp_output_dir]
movie_puzzles = create_puzzles_and_answers(movies, '🎬', *common_args)
tv_show_puzzles = create_puzzles_and_answers(tv_shows, '📺', *common_args)
book_puzzles = create_puzzles_and_answers(books, '📚', *common_args)
# ASSEMBLE THE BOOK
merger = PdfMerger()
# TITLE PAGE
merger.append(PdfReader(open(title_page, 'rb')))
# Attributions + Dedication
merger.append(PdfReader(open(copyright_attributions, 'rb')))
merger.append(PdfReader(open(dedication_page, 'rb')))
# QR + INTRO
merger.append(PdfReader(open(qr_code_page, 'rb')))
merger.append(PdfReader(open(intro_page, 'rb')))
# BLANK + HOW TO PLAY
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb')))
merger.append(PdfReader(open(how_to_play, 'rb')))
# BLANK + TOC
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb')))
merger.append(PdfReader(open(toc_paperback, 'rb')))
else:
merger.append(PdfReader(open(toc_ebook, 'rb')))
# BLANK + MOVIES CHAPTER PAGE
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb')))
merger.append(PdfReader(open(movies_page, 'rb')))
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb')))
cur_page = 1
for puzzle_page in movie_puzzles:
merger.append(PdfReader(open(puzzle_page, 'rb')))
cur_page += 1
# Add a blank page to pad out to RIGHT if needed
if IS_PAPERBACK and is_left_page(cur_page):
merger.append(PdfReader(open(blank_page, 'rb'))) # RIGHT
cur_page += 1
# BLANK + TV SHOWS CHAPTER PAGE
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb'))) # LEFT
cur_page += 1
merger.append(PdfReader(open(tv_shows_page, 'rb'))) # RIGHT
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb')))
cur_page += 1
cur_page += 1
for puzzle_page in tv_show_puzzles:
merger.append(PdfReader(open(puzzle_page, 'rb')))
cur_page += 1
# Add a blank page to pad out to RIGHT if needed
if IS_PAPERBACK and is_left_page(cur_page):
merger.append(PdfReader(open(blank_page, 'rb'))) # RIGHT
cur_page += 1
# BLANK + BOOKS CHAPTER PAGE
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb'))) # LEFT
cur_page += 1
merger.append(PdfReader(open(books_page, 'rb'))) # RIGHT
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb')))
cur_page += 1
cur_page += 1
for puzzle_page in book_puzzles:
merger.append(PdfReader(open(puzzle_page, 'rb')))
cur_page += 1
# Add a blank page to pad out to RIGHT if needed
if IS_PAPERBACK and is_left_page(cur_page):
merger.append(PdfReader(open(blank_page, 'rb'))) # RIGHT
cur_page += 1
# BLANK + ANSWERS CHAPTER PAGE
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb')))
cur_page += 1
merger.append(PdfReader(open(answers_page, 'rb')))
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb')))
cur_page += 1
cur_page += 1
# Add answer pages
for answer_page in answer_pages:
merger.append(PdfReader(open(answer_page, 'rb')))
if IS_PAPERBACK:
merger.append(PdfReader(open(blank_page, 'rb')))
# Write the merged PDF
if IS_EBOOK:
output_name = 'generated-ebook.pdf'
else:
output_name = 'generated-paperback.pdf'
output_path = f'{manuscript_output_dir}/{output_name}'
output = open(output_path, 'wb')
merger.write(output)
output.close()
print(f'Book created at {output_path}')
os.system(f'open {output_path}')