-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
69 lines (59 loc) · 2.22 KB
/
process.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
import imgkit
import json
from urllib.parse import urlparse
import os
with open('compareasonion.config.json', 'r') as cf:
config = json.load(cf)
options = config["imgkit"]["options"]
# additional options: 'width': 400, 'disable-smart-width': ''
alloweddomains = config["alloweddomains"]
imgkitconfig = imgkit.config(
wkhtmltoimage=config["imgkit"]["wkhtmltoimagepath"])
with open(config["inputfilepath"], 'r') as fp:
records = json.load(fp)
records_json = {}
for page_name in records:
print(f'Processing: {page_name}')
counter = 0
urls_json = []
for url in records[page_name]:
domain = ''
image_path = ''
error = ''
if url == '':
error = 'error: no page url provided'
try:
result = urlparse(url)
except Exception as e:
print(e)
error = f'error: urlparse > {url}'
domain = result.netloc
if not domain in alloweddomains:
error = f'error: invalid domain: {domain}'
if error == '':
if all([result.scheme, result.netloc, result.path]):
counter = counter + 1
page_name = page_name.replace(' ', '_')
image_path = f'{config["outputdir"]}/{page_name}_{str(counter)}.png'
if os.path.exists(image_path):
print('image exists')
else:
try:
print(url)
imgkit.from_url(url, image_path, options=options, config=imgkitconfig)
except Exception as e:
# if os.path.exists(image_path):
# os.remove(image_path)
# print('error occurred, image removed')
error = f'error: imgkit processing {url} > {e}'
else:
error = f'error: invalid url: {url}'
urls_json.append(
{"url": url, "image_path": image_path, "error": error})
records_json[page_name] = urls_json
with open(config["outputjsonpath"], 'w') as outfile:
json.dump(records_json, outfile)
cf.close()
fp.close()
outfile.close()
print('---OVER---')