forked from Chillwind132/AutoScreenshots_ARS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoScreenshots_ARS.py
69 lines (56 loc) · 2.37 KB
/
AutoScreenshots_ARS.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
from selenium import webdriver
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
import img2pdf
### Required: Python 3.9+ & Chrome installed & Chromedriver executable in $PATH ###
def save_screenshots(selection):
with open("list_urls.txt") as file:
for line in file:
URL = line
if selection == "1":
format = ".jpg"
else:
format = ".jp2"
mod_line = line.replace("/", "").replace(".html", "").replace(".", "").replace(
"https", "").replace("http", "").replace(":", "").replace("www", "")
file_name = path + "/output/" + mod_line.strip() + format
print(file_name)
options = webdriver.ChromeOptions()
options.headless = True
options.add_argument("--no-sandbox")
driver = webdriver.Chrome(options=options)
driver.get(URL)
def S(X): return driver.execute_script(
'return document.body.parentNode.scroll'+X)
driver.set_window_size(S('Width'), S('Height'))
driver.find_element_by_tag_name('body').screenshot(file_name)
driver.quit()
def convert_img_pdf():
for filename in os.listdir(output_path):
f_img = os.path.join(output_path, filename)
if os.path.isfile(f_img):
result = f_img[f_img.rindex('.')+1:]
print(result)
if result == "jp2":
f_out = f_img.removesuffix(".jp2") + ".pdf"
print(f_img, f_out)
with open(f_out, "wb") as f:
f.write(img2pdf.convert(f_img))
os.remove(f_img)
if __name__ == "__main__":
path = os.getcwd()
output_path = path + "/output/"
if os.path.exists(output_path):
print("Dir exists")
else:
os.makedirs(output_path)
selection = input(
"Would you like to convert URLs to images/PDFs? Input 1 to select images; Input 2 to select PDFs\n")
while selection != '1' and selection != '2':
print('Invalid input')
selection = input(
"Would you like to convert URLs to images/PDFs? Input 1 to select images; Input 2 to select PDFs\n")
save_screenshots(selection)
if selection == "2":
convert_img_pdf()