-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhooks.py
201 lines (131 loc) · 5.75 KB
/
hooks.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
from pathlib import Path
try:
import undetected_chromedriver
except ImportError:
import sys
packages_path = Path.cwd() / "env" / "Lib" / "site-packages"
sys.path.insert(0, f"{packages_path}")
import undetected_chromedriver
from logger import logger
def before_search_hook(driver: undetected_chromedriver.Chrome) -> None:
"""Hook to be called before starting the search
At this point, only webdriver is created and proxy is installed if it is used.
No url is loaded yet. This can be used for loading other websites to create
some history before starting the search or other custom setup on driver.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
try:
logger.info("Executing before search hook...")
except Exception as exp:
logger.error(exp)
def captcha_seen_hook(driver: undetected_chromedriver.Chrome) -> None:
"""Hook to be called in case of a captcha
This is called at a point before after_query_sent_hook.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
try:
logger.info("Executing captcha seen hook...")
except Exception as exp:
logger.error(exp)
def after_query_sent_hook(driver: undetected_chromedriver.Chrome, search_query: str) -> None:
"""Hook to be called after submitting the search query
At this point, cookie dialogs are closed, captchas are solved, and search
query is written to search box and submitted.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
:type search_query: str
:param search_query: Search query
"""
try:
logger.info("Executing after query sent hook...")
except Exception as exp:
logger.error(exp)
def results_ready_hook(driver: undetected_chromedriver.Chrome) -> None:
"""Hook to be called when search results are ready
At this point, search query is executed and results are loaded. It is also
before random actions on search results page and starting to link collection.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
try:
logger.info("Executing search results ready hook...")
except Exception as exp:
logger.error(exp)
def after_search_hook(driver: undetected_chromedriver.Chrome) -> None:
"""Hook to be called after completing the search
At this point, searching/collecting ad links, non-ad links, and
shopping ad links if enabled is completed. Also, this is the point
before starting click actions.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
try:
logger.info("Executing after search hook...")
except Exception as exp:
logger.error(exp)
def before_ad_click_hook(driver: undetected_chromedriver.Chrome) -> None:
"""Hook to be called before clicking to ad link
At this point, ad links are found and ready to be clicked. It is also after
clicking shopping ads if it is enabled and there are any found.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
try:
logger.info("Executing before ad click hook...")
except Exception as exp:
logger.error(exp)
def after_ad_click_hook(driver: undetected_chromedriver.Chrome) -> None:
"""Hook to be called after clicking to ad link
At this point, the ad link is opened in a new tab and browser is switched
to this tab. It is before waiting and starting random actions on ad page.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
try:
logger.info("Executing after ad click hook...")
except Exception as exp:
logger.error(exp)
def after_clicks_hook(driver: undetected_chromedriver.Chrome) -> None:
"""Hook to be called after clicking the found links
At this point, clicking of the links is completed.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
try:
logger.info("Executing after clicks hook...")
except Exception as exp:
logger.error(exp)
def exception_hook(driver: undetected_chromedriver.Chrome) -> None:
"""Hook to be called in case of an exception
At this point, a screenshot is saved and exception is logged to file.
It can be used to investigate the exception before the browser is closed.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
try:
logger.info("Executing exception hook...")
except Exception as exp:
logger.error(exp)
def before_browser_close_hook(driver: undetected_chromedriver.Chrome) -> None:
"""Hook to be called before closing the browser instance
At this point, all actions are completed including the exception handling.
Cache and cookies are deleted and browser is closed after this hook is called.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
try:
logger.info("Executing before browser close hook...")
except Exception as exp:
logger.error(exp)
def after_browser_close_hook(driver: undetected_chromedriver.Chrome) -> None:
"""Hook to be called after closing the browser instance
At this point, all actions are completed including the exception handling.
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
try:
logger.info("Executing after browser close hook...")
except Exception as exp:
logger.error(exp)