-
Notifications
You must be signed in to change notification settings - Fork 1
/
OnedrivePrivatePDFDownloader.py
342 lines (285 loc) · 10.9 KB
/
OnedrivePrivatePDFDownloader.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
import argparse
import logging
import os
import shutil
import tempfile
from contextlib import contextmanager
from time import sleep
import img2pdf
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
# if the class names are not up-to-date, you can use the browser inspector
# to get the new ones and add them here
CLASS_NAMES_TOTAL_PAGES = ["status_5a88b9b2"]
CLASS_NAMES_FILE_NAME = ["OneUpNonInteractiveCommandNewDesign_156f96ef"]
CLASS_NAMES_TOOLBAR = ["root_5a88b9b2"]
ARIA_LABELS_NEXT_PAGE = ["Vai alla pagina successiva."]
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s - %(message)s",
)
# avoid unnecessary logs
logging.getLogger("img2pdf").setLevel(logging.ERROR)
logging.getLogger("selenium").setLevel(logging.ERROR)
logging.getLogger("urllib3").setLevel(logging.ERROR)
logging.getLogger("PIL").setLevel(logging.ERROR)
def parse_arguments() -> argparse.Namespace:
"""Parse the arguments from the command line.
Returns:
argparse.Namespace: The parsed arguments.
"""
parser = argparse.ArgumentParser(
description="Export a PDF (also the protected ones) from an authenticated session.",
epilog="Made with ❤️ by @willnaoosmith and @Francesco146",
)
parser.add_argument(
"--browser",
"-b",
type=str,
choices=["firefox", "chrome"],
help="Browser to use (firefox or chrome)",
default="firefox",
metavar="BROWSER",
)
parser.add_argument(
"--profile-dir",
"-p",
type=str,
help="Path to the browser profile.",
default=None,
metavar="PATH",
)
parser.add_argument(
"--profile-name",
"-n",
type=str,
help="Profile name to use, only for Chrome.",
default=None,
metavar="PATH",
)
parser.add_argument(
"--keep-imgs",
"-k",
action="store_true",
help="Keep the images after the PDF creation",
default=False,
)
parser.add_argument(
"--debug",
"-d",
action="store_true",
help="Show debug messages",
default=False,
)
parser.add_argument(
"--output-file",
"-o",
type=str,
help="Specify the output file name",
required=False,
metavar="FILE",
)
parser.add_argument("url", type=str, help="URL of the PDF file")
return parser.parse_args()
def find_element(browser: webdriver, identifiers: list[str], by: By):
"""Find an element by one of the identifiers in the list.
Args:
browser (webdriver): Browser instance
identifiers (list[str]): List of identifiers to search
by (By): The method to use for finding the element
Raises:
NoSuchElementException: If no element is found
Returns:
WebElement: The found element
"""
for identifier in identifiers:
try:
match by:
case By.CLASS_NAME:
element = browser.find_element(by, identifier)
case By.XPATH:
element = browser.find_elements(
by, f"//button[@aria-label='{identifier}']"
)[-1]
case _:
raise ValueError(f"Unsupported method: {by}")
logging.debug(f"Element found using {by}: '{identifier}'")
return element
except NoSuchElementException | IndexError:
logging.debug(f"Element not found using {by}: '{identifier}'")
continue
raise NoSuchElementException(
f"No element found with any of the identifiers: {identifiers}"
)
def get_browser(args) -> webdriver:
"""Get the browser instance based on the arguments.
Args:
args (argparse.Namespace): Arguments from the command line
Raises:
ValueError: If the browser is not supported
Returns:
webdriver: Browser instance
"""
options = None
service = None
logging.info(f"Initializing browser: '{args.browser}'")
match args.browser:
case "firefox":
options = webdriver.FirefoxOptions()
service = FirefoxService(log_path=os.devnull)
if args.profile_dir:
options.profile = webdriver.FirefoxProfile(args.profile_dir)
return webdriver.Firefox(service=service, options=options)
case "chrome":
options = webdriver.ChromeOptions()
service = ChromeService(log_path=os.devnull)
if args.profile_dir and args.profile_name:
options.add_argument(f"user-data-dir={args.profile_dir}")
options.add_argument(f"--profile-directory={args.profile_name}")
return webdriver.Chrome(service=service, options=options)
case _:
raise ValueError(f"Unsupported browser: {args.browser}")
def hide_toolbar(browser, class_names) -> None:
"""Hide the toolbar by one of the class names in the list.
Args:
browser (WebDriver): Browser instance
class_names (list[str]): List of class names to search
Returns:
None
"""
for class_name in class_names:
try:
browser.execute_script(
f"document.getElementsByClassName('{class_name}')[0].style.visibility = 'hidden'"
)
logging.debug(f"Toolbar hidden using class name: '{class_name}'")
return
except (IndexError, NoSuchElementException):
logging.debug(f"Toolbar not found using class name: '{class_name}'")
continue
raise NoSuchElementException(
f"No toolbar found with any of the class names: {class_names}"
)
@contextmanager
def browser_context(args: argparse.Namespace):
"""Context manager to handle the browser session.
Args:
args (argparse.Namespace): Arguments from the command line
Yields:
webdriver: Browser instance
"""
browser = get_browser(args)
try:
yield browser
finally:
browser.quit()
print() # Add a new line after the browser is finally closed
logging.info("Browser session ended.")
def get_total_pages(browser: webdriver) -> int:
"""Get the total number of pages from the page counter or manually.
Args:
browser (webdriver): Browser instance
Returns:
int: The total number of pages
"""
try:
total_of_pages = int(
find_element(browser, CLASS_NAMES_TOTAL_PAGES, By.CLASS_NAME).text.replace(
"/", ""
)
)
logging.info(f"Total number of pages detected: {total_of_pages}")
except (ValueError, NoSuchElementException):
logging.warning(
"The page counter is not visible or the CLASS_NAME_TOTAL_PAGES is not up-to-date."
)
total_of_pages = int(input("Insert the total number of pages manually: "))
return total_of_pages
def get_output_filename(args: argparse.Namespace, browser: webdriver) -> str:
"""Get the output filename based on the arguments, the detected one or manually.
Args:
args (argparse.Namespace): Arguments from the command line
browser (webdriver): Browser instance
Returns:
str: The output filename
"""
if args.output_file:
filename = args.output_file
else:
try:
filename = find_element(browser, CLASS_NAMES_FILE_NAME, By.CLASS_NAME).text
logging.info(f"Detected file name: '{filename}'")
except NoSuchElementException:
logging.warning(
"The file name is not visible or the CLASS_NAME_FILE_NAME is not up-to-date."
)
filename = input(
"Insert the file name manually (with the extension e.g.: file.pdf): "
)
return filename
def main() -> None:
"""Main function to export the PDF file."""
args = parse_arguments()
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
with browser_context(args) as browser:
browser.get(args.url)
input(
"Make sure to authenticate and reach the PDF preview. "
"Once the file is loaded and the page counter is visible, press [ENTER] to start. "
"Keep the browser in the foreground for better results.\n> [ENTER] "
)
sleep(2)
total_of_pages = get_total_pages(browser)
filename = get_output_filename(args, browser)
logging.info(
f'Starting the export of the file "{filename}". '
"This might take a while depending on the number of pages."
)
files_list: list[str] = []
with tempfile.TemporaryDirectory() as temp_dir:
# Hide the toolbar for screenshots
try:
hide_toolbar(browser, CLASS_NAMES_TOOLBAR)
logging.info("Toolbar hidden for clean screenshots.")
except NoSuchElementException:
logging.warning(
"The toolbar is not visible or the CLASS_NAME_TOOLBAR is not up-to-date. "
"The screenshots might contain the toolbar or other errors might occur."
)
page_number = 1
while page_number <= total_of_pages:
sleep(5)
image_path = f"{temp_dir}/{str(page_number)}.png"
browser.find_element(By.CSS_SELECTOR, "canvas").screenshot(image_path)
files_list.append(image_path)
logging.info(
f"Page {str(page_number)} of {str(total_of_pages)} exported."
)
page_number += 1
try:
next_page_button = find_element(
browser, ARIA_LABELS_NEXT_PAGE, By.XPATH
)
except NoSuchElementException:
logging.error(
"Cannot find the next page button. it could be ARIA_LABEL_NEXT_PAGE is not "
"up-to-date or some race condition occurred. Please, update the tags and try again. Saving the obtained ones."
)
break
browser.execute_script("arguments[0].click();", next_page_button)
logging.info(f"Saving the file as '{filename}'.")
with open(filename, "wb") as out_file:
out_file.write(img2pdf.convert(files_list))
if args.keep_imgs:
keep_dir = f"{filename}_images"
os.makedirs(keep_dir, exist_ok=True)
for file_path in files_list:
shutil.copy(file_path, keep_dir)
logging.info(f"Images kept in directory '{keep_dir}'.")
logging.info("Temporary images removed.")
if __name__ == "__main__":
main()