-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
325 lines (243 loc) · 9.2 KB
/
utils.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
import os
import time
import traceback
import random
import pathlib
from datetime import datetime
from PIL import Image
from bs4 import BeautifulSoup, NavigableString
from defines import WEBDRIVER_PATH
from defines import DOWNLOAD_PATH
from defines import COOKIES_PATH
from defines import TIMEOUT
from defines import DIRNAME_CHAR_COUNT
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
from selenium.webdriver.remote.webdriver import BaseWebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from pathlib import Path
from typing import Union
from typing import Callable
from typing import List
DEFAULT_REPEATER_ERRORS_TO_EXCEPT = (
NoSuchElementException,
StaleElementReferenceException,
WebDriverException
)
def prepare_file_name(file_name:str) -> str:
"""
Remove forbidden for file naming chars in windows
Parameters
----------
dir_name : str
Input string
Returns
-------
str
String without forbidden chars
"""
to_replace = ["\\", "/", ":", "*", "?", "\"", "<", ">", "|", "\n", "\r"]
for symbol in to_replace:
file_name = file_name.replace(symbol, "")
return file_name
def prepare_dir_name(dir_name:str, max_name_len:int = DIRNAME_CHAR_COUNT) -> str:
"""
Remove forbidden for dir naming chars in windows
Parameters
----------
dir_name : str
Input string
Returns
-------
str
String without forbidden chars
"""
dir_name = prepare_file_name(dir_name)
if len(dir_name) > max_name_len:
dir_name = f"{dir_name[:max_name_len]}"
to_replace = ["."]
for symbol in to_replace:
dir_name = dir_name.replace(symbol, "")
return dir_name.strip()
def get_inner_text(parent:BeautifulSoup) -> str:
"""
Gets text only inner the block, without texts inside her childs
Parameters
----------
parent : BeautifulSoup
Input element
Returns
-------
str
Text inside element
Examples
--------
html = \"\"\"
<div>
Some text inside block
<span>Some text inside child span</span>
</div>
\"\"\"
document = BeautifulSoup(html, "html.parser")
print(get_inner_text(document)) # Prints: Some text inside block
"""
return ''.join(parent.find_all(text=True, recursive=False)).strip()
# def get_inner_text(outer:BeautifulSoup):
# inner_text = [element for element in outer if isinstance(element, NavigableString)]
# return inner_text
def make_dirs_if_not_exists(path:Union[str, pathlib.Path]):
"""
Make dirst if not exists
Parameters
----------
path : str | pathlib.Path
Path (dirs)
"""
if not os.path.isdir(path):
os.makedirs(path)
return True
return False
def init():
"""
Create required dirs, check dependecies
"""
make_dirs_if_not_exists(DOWNLOAD_PATH)
make_dirs_if_not_exists(COOKIES_PATH)
def repeater(timeout:float,
retry:int = 5,
random_timeout_function:Callable[[], float] = random.random,
errors = DEFAULT_REPEATER_ERRORS_TO_EXCEPT):
"""
Repeat function 'retry' times
Parameters
----------
timeout : int | float
Times between tries.
retry : int
Count of tries.
random_timeout_function : Callable[[], float]
Time noise added to waiting.
errrors : Exception | Tuple[Exception]
Single error or list/tuple of exceptions that must be excepted
"""
def inner(function):
def wrapper(*args, **kwargs):
iteration = 0
ms_timeout = timeout / 1000
ms_random_timeout_function = lambda: random_timeout_function() / 1000
while ((retry is not None) and (iteration < retry)) or (retry is None):
iteration += 1
try:
return function(*args, **kwargs)
except errors as e:
# traceback.print_exc()
print(traceback.format_exc())
time.sleep(ms_timeout + ms_random_timeout_function())
raise Exception(f"Repeater was tried about {retry} times without results")
return wrapper
return inner
def close_tabs(driver:RemoteWebDriver, save_tabs:List[int] = [0]):
""" Close all tabs opened by extension """
wait = WebDriverWait(driver, TIMEOUT)
wait.until(
lambda driver: len(driver.window_handles) > 1
)
tabs_mask = [False for _ in range(len(driver.window_handles))]
for tab_index in save_tabs:
tabs_mask[tab_index] = True
pointer = 0
while pointer < len(driver.window_handles):
if tabs_mask[pointer]:
pointer += 1
continue
driver.switch_to.window(driver.window_handles[pointer])
driver.close()
tabs_mask.pop(pointer)
driver.switch_to.window(driver.window_handles[0])
def close_all_windows_except_main(driver:BaseWebDriver):
main_window = driver.window_handles[0]
for window in driver.window_handles[1:]:
driver.switch_to.window(window)
driver.close()
driver.switch_to.window(main_window)
def fullpage_screenshot(driver: RemoteWebDriver,
scrolling_element: WebElement,
removing_elements: List[WebElement] = [],
file: Union[str, Path] = DOWNLOAD_PATH / f"fullpage-screenshot_{datetime.now().strftime('%d-%m-%Y_%H-%M-%S')}.png",
time_delay:float = 0.2):
"""
Source: https://gist.github.com/PashaWNN/fc2ff352e54db7aee6f85dddca32a8f7
Saves full page screenshot from selenium webdriver
Originally modivicated from https://stackoverflow.com/questions/41721734/take-screenshot-of-full-page-with-selenium-python-with-chromedriver
Slightly refactored by jekahamster
:param driver: Selenium webdriber
:param scrolling_element: WebElement that shoud be scrolled
:param removing_elements: List of WebElements which should be deleted for image
:param file: Filepath to save screenshot
"""
for elem in removing_elements:
driver.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", elem)
total_width = int(scrolling_element.get_attribute("offsetWidth"))
total_height = int(scrolling_element.get_attribute("scrollHeight"))
viewport_width = int(scrolling_element.get_attribute("clientWidth"))
viewport_height = int(scrolling_element.get_attribute("clientHeight"))
rectangles = []
i = 0
while i < total_height:
j = 0
top_height = i + viewport_height
if top_height > total_height:
top_height = total_height
while j < total_width:
top_width = j + viewport_width
if top_width > total_width:
top_width = total_width
rectangles.append((j, i, top_width,top_height))
j += viewport_width
i += viewport_height
stitched_image = Image.new('RGB', (total_width, total_height))
previous = None
part = 0
for rectangle in rectangles:
if previous is not None:
driver.execute_script("arguments[0].scrollTo({0}, {1})".format(rectangle[0], rectangle[1]), scrolling_element)
time.sleep(time_delay)
file_name = "part_{0}.png".format(part)
# driver.get_screenshot_as_file(file_name)
scrolling_element.screenshot(file_name)
screenshot = Image.open(file_name)
if rectangle[1] + viewport_height > total_height:
offset = (rectangle[0], total_height - viewport_height)
else:
offset = (rectangle[0], rectangle[1])
stitched_image.paste(screenshot, offset)
del screenshot
os.remove(file_name)
part += 1
previous = rectangle
stitched_image.save(file)
def get_downloaded_file_name(driver, wait_time = None):
driver.execute_script("window.open()")
WebDriverWait(driver,10).until(EC.new_window_is_opened)
driver.switch_to.window(driver.window_handles[-1])
driver.get("chrome://downloads/")
endTime = time.time() + wait_time if wait_time else None
while True:
try:
fileName = driver.execute_script("return document.querySelector('#contentAreaDownloadsView .downloadMainArea .downloadContainer description:nth-of-type(1)').value")
if fileName:
return fileName
except:
pass
time.sleep(1)
if wait_time and time.time() > endTime:
break