forked from uaxe/geektime-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
377 lines (334 loc) · 12.8 KB
/
main.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import base64
import html
import json
import os
import subprocess
import sys
import re
import traceback
import click
import yaml
import requests
from concurrent.futures import ThreadPoolExecutor
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import staleness_of
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from pypdf import PdfWriter
from compress import __compress
requests.adapters.DEFAULT_RETRIES = 5
reqsession = requests.session()
requests.packages.urllib3.disable_warnings()
reqsession.keep_alive = False
reqsession.verify = False
retry_strategy = Retry(
total=3,
status_forcelist=[500, 502, 503, 504, 404],
allowed_methods=["HEAD", "GET", "OPTIONS", "POST"],
backoff_factor=0.5,
)
http_adapter = HTTPAdapter(
max_retries=retry_strategy,
pool_connections=200,
pool_maxsize=200,
)
reqsession.mount("http://", http_adapter)
reqsession.mount("https://", http_adapter)
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,"
"image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
}
def __send_devtools(driver, cmd, params={}):
resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
url = f"{driver.command_executor._client_config.remote_server_addr}{resource}"
body = json.dumps({"cmd": cmd, "params": params})
response = driver.command_executor._request("POST", url, body)
if not response:
raise Exception(response.get("value"))
return response.get("value")
def worker(driver, uri, timeout):
driver.get(uri)
print(f'Working on {uri}')
temp_height = 0
while True:
driver.execute_script("window.scrollBy(0,100)")
driver.implicitly_wait(0.2)
check_height = driver.execute_script(
"return document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;",
)
if check_height == temp_height:
break
temp_height = check_height
try:
WebDriverWait(driver, timeout).until(
staleness_of(driver.find_element(by=By.TAG_NAME, value="html")),
)
except TimeoutException:
calculated_print_options = {
"landscape": False,
"displayHeaderFooter": False,
"printBackground": True,
"preferCSSPageSize": True,
}
result = __send_devtools(
driver,
"Page.printToPDF",
calculated_print_options,
)
return base64.b64decode(result["data"])
def head(uri):
try:
resp = requests.head(uri, timeout=3, headers={'User-Agent': 'Mozilla/5.0'})
print(f'head status_code: {resp.status_code}, {uri}')
except Exception as e:
print(f'head error: {e}, {uri}')
@click.command("all_pdf")
@click.option(
"-i",
"--source",
required=True,
help="source is path",
)
@click.option(
"-o",
"--output",
default="logs",
help="output is dir",
)
@click.option(
"-t",
"--timeout",
type=int,
default=3,
help="get url with browser timeout",
)
@click.option(
"-c",
"--compress",
type=bool,
default=False,
help="PDF is compressed or not. Default value is False",
)
@click.option(
"-p",
"--power",
type=int,
default=0,
help="power of the compression. Default value is 0. This can be 0: default, 1: prepress, 2: printer, 3: ebook, 4: screen",
)
@click.option(
"-a",
"--port",
type=int,
default=8000,
help="mkdocs port. Default value is 8000",
)
def make_all_pdf(source, output, timeout, compress, power, port):
webdriver_options = Options()
webdriver_prefs = {}
webdriver_prefs["profile.default_content_settings"] = {"images": 2}
webdriver_options.add_argument("--headless")
webdriver_options.add_argument("--disable-gpu")
webdriver_options.add_argument("--no-sandbox")
webdriver_options.add_argument("--disable-dev-shm-usage")
webdriver_options.experimental_options["prefs"] = webdriver_prefs
service = Service(ChromeDriverManager().install())
host = f"http://127.0.0.1:{port}/"
for dirname, _, file_lst in os.walk(source):
for fname in file_lst:
if not fname in ["mkdocs.yml"]:
continue
for i in range(9):
os.popen("lsof -i:"+str(port)+" | grep -v 'PID' | awk '{print $2}' | xargs kill -9")
driver = webdriver.Chrome(service=service, options=webdriver_options)
try:
parts_dir = os.path.join(dirname, "parts")
pattern = r'https?://[^\s]+'
fpath = os.path.join(dirname, "mkdocs.yml")
data = yaml.safe_load(open(fpath))
print(f'dirname: {dirname}')
navs = data.get('nav')
proc = subprocess.Popen(
["mkdocs", "serve", "--no-livereload", "-a", f"127.0.0.1:{port}"],
cwd=dirname, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
part_dir = os.path.join(parts_dir, dirname)
if not os.path.exists(part_dir):
os.makedirs(part_dir, exist_ok=True)
pdfs = []
for nav in ["index.md"] + navs:
if not isinstance(nav, str):
raise ValueError(dirname + nav)
base_name = os.path.splitext(nav)[0]
target = os.path.join(part_dir, base_name.replace('%', "") + ".pdf")
uri = host if base_name == "index" else host + html.escape(base_name)
mk_path = os.path.join(dirname, "docs", base_name+'.md')
mk_data = open(mk_path).read()
matches = re.findall(pattern, mk_data)
images = []
for match in matches:
if 'static001.geekbang.org' not in match:
continue
match = match if match.count(')') <= 0 else match[:match.index(')')]
images.append(match)
if os.path.exists(target):
pdfs.append((target, base_name, mk_data, images))
continue
with ThreadPoolExecutor(max_workers=3) as executor:
for img_url in images:
executor.submit(head, img_url)
result = worker(driver, uri, timeout)
if compress:
__compress(result, target, power)
print(f"__compress {target}")
else:
with open(target, "wb") as file:
file.write(result)
print(f"writing {target}")
pdfs.append((target, base_name, mk_data, images))
proc.kill()
os.popen("lsof -i:" + str(port) + " | grep -v 'PID' | awk '{print $2}' | xargs kill -9")
merger = PdfWriter()
for (pdf, name, text, images) in pdfs:
merger.append(pdf)
pdf_base_name = os.path.basename(dirname).replace('%', "")
pdf_name = f"{pdf_base_name}.pdf"
pdf_path = os.path.join(dirname, pdf_name)
merger.write(pdf_path)
print(f"writing pdf {pdf_path}")
merger.close()
output_path = f'{output}/{os.path.basename(os.path.dirname(dirname))}/{pdf_name}'
if not os.path.exists(os.path.dirname(output_path)):
os.makedirs(os.path.dirname(output_path), exist_ok=True)
print(output_path)
os.rename(pdf_path, output_path)
break
except Exception as e:
print(f"exception: {e}, traceback: {traceback.format_exc()}")
if 'No such file or directory' in str(e):
driver.quit()
os.popen("lsof -i:" + str(port) + " | grep -v 'PID' | awk '{print $2}' | xargs kill -9")
break
finally:
driver.quit()
@click.command("pdf")
@click.option(
"-i",
"--source",
required=True,
help="source is path",
)
@click.option(
"-t",
"--timeout",
type=int,
default=3,
help="get url with browser timeout",
)
@click.option(
"-c",
"--compress",
type=bool,
default=False,
help="PDF is compressed or not. Default value is False",
)
@click.option(
"-p",
"--power",
type=int,
default=0,
help="power of the compression. Default value is 0. This can be 0: default, 1: prepress, 2: printer, 3: ebook, 4: screen",
)
@click.option(
"-a",
"--port",
type=int,
default=8000,
help="mkdocs port. Default value is 8000",
)
def make_pdf(source, timeout, compress, power, port):
webdriver_options = Options()
webdriver_prefs = {}
webdriver_prefs["profile.default_content_settings"] = {"images": 2}
webdriver_options.add_argument("--headless")
webdriver_options.add_argument("--disable-gpu")
webdriver_options.add_argument("--no-sandbox")
webdriver_options.add_argument("--disable-dev-shm-usage")
webdriver_options.experimental_options["prefs"] = webdriver_prefs
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=webdriver_options)
host = f"http://127.0.0.1:{port}/"
parts_dir = os.path.join(source, "parts")
pattern = r'https?://[^\s]+'
fpath = os.path.join(source, "mkdocs.yml")
data = yaml.safe_load(open(fpath))
proc = subprocess.Popen(["mkdocs", "serve", "--no-livereload"],
cwd=source,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
part_dir = os.path.join(parts_dir, source)
if not os.path.exists(part_dir):
os.makedirs(part_dir, exist_ok=True)
pdfs = []
navs = ["index.md"] + data.get('nav')
for nav in navs:
base_name = os.path.splitext(nav)[0]
target = os.path.join(part_dir, base_name.replace('%', "") + ".pdf")
uri = host if base_name == "index" else host + html.escape(base_name)
mk_path = os.path.join(source, "docs", nav)
mk_data = open(mk_path).read()
matches = re.findall(pattern, mk_data)
images = []
for match in matches:
if 'static001.geekbang.org' not in match:
continue
match = match if match.count(')') <= 0 else match[:match.index(')')]
if os.path.exists(target):
images.append(match)
else:
try:
resp = requests.head(match, timeout=3, headers={'User-Agent': 'Mozilla/5.0'})
print(f'head status_code: {resp.status_code}, {match}')
images.append(match)
except Exception as e:
print(f'head error: {e}, {match}')
if os.path.exists(target):
pdfs.append((target, base_name, mk_data, images))
continue
result = worker(driver, uri, timeout)
if compress:
__compress(result, target, power)
print(f"__compress {target}")
else:
with open(target, "wb") as file:
file.write(result)
print(f"writing {target}")
pdfs.append((target, base_name, mk_data, images))
proc.kill()
merger = PdfWriter()
for (pdf, name, text, images) in pdfs:
merger.append(pdf)
pdf_base_name = os.path.basename(source).replace('%','')
pdf_path = os.path.join(source, pdf_base_name + ".pdf")
merger.write(pdf_path)
print(f"writing pdf {pdf_path}")
merger.close()
@click.group(invoke_without_command=True)
@click.pass_context
def heya(ctx):
if ctx.invoked_subcommand is None:
click.echo(ctx.get_help())
heya.add_command(make_pdf)
heya.add_command(make_all_pdf)
def main() -> int:
return heya(auto_envvar_prefix="HEYA")
if __name__ == '__main__':
sys.exit(main())