-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
626 lines (460 loc) · 19.1 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import sys
import json
import random
from datetime import datetime
from enum import Enum
from pathlib import Path
from time import sleep
from typing import Optional
try:
import pyautogui
import requests
import openpyxl
import undetected_chromedriver
from openpyxl.styles import Alignment, Font
except ImportError:
packages_path = Path.cwd() / "env" / "Lib" / "site-packages"
sys.path.insert(0, f"{packages_path}")
import pyautogui
import requests
import openpyxl
import undetected_chromedriver
from openpyxl.styles import Alignment, Font
from config_reader import config
from geolocation_db import GeolocationDB
from logger import logger
class Direction(Enum):
UP = "UP"
DOWN = "DOWN"
LEFT = "LEFT"
RIGHT = "RIGHT"
BOTH = "BOTH"
def get_random_user_agent_string() -> str:
"""Get random user agent
:rtype: str
:returns: User agent string
"""
user_agents = _get_user_agents(config.paths.user_agents)
user_agent_string = random.choice(user_agents)
logger.debug(f"user_agent: {user_agent_string}")
return user_agent_string
def _get_user_agents(user_agent_file: Path) -> list[str]:
"""Get user agents from file
:type user_agent_file: Path
:param user_agent_file: File containing user agents
:rtype: list
:returns: List of user agents
"""
filepath = Path(user_agent_file)
if not filepath.exists():
raise SystemExit(f"Couldn't find user agents file: {filepath}")
with open(filepath, encoding="utf-8") as useragentfile:
user_agents = [
user_agent.strip().replace("'", "").replace('"', "")
for user_agent in useragentfile.read().splitlines()
]
return user_agents
def get_location(geolocation_db_client: GeolocationDB, proxy: str) -> tuple[float, float, str, str]:
"""Get latitude, longitude, country code, and timezone of ip address
:type geolocation_db_client: GeolocationDB
:param geolocation_db_client: GeolocationDB instance
:type proxy: str
:param proxy: Proxy to get geolocation
:rtype: tuple
:returns: (latitude, longitude, country_code, timezone) tuple for the given proxy IP
"""
proxies_header = {"http": f"http://{proxy}", "https": f"http://{proxy}"}
ip_address = ""
if config.webdriver.auth:
for cycle in range(2):
try:
response = requests.get("https://api.ipify.org", proxies=proxies_header, timeout=5)
ip_address = response.text
if not ip_address:
raise Exception("Failed with https://api.ipify.org")
break
except Exception as exp:
logger.debug(exp)
try:
logger.debug("Trying with ipv4.webshare.io...")
response = requests.get(
"https://ipv4.webshare.io/", proxies=proxies_header, timeout=5
)
ip_address = response.text
if not ip_address:
raise Exception("Failed with https://ipv4.webshare.io")
break
except Exception as exp:
logger.debug(exp)
try:
logger.debug("Trying with ipconfig.io...")
response = requests.get(
"https://ipconfig.io/json", proxies=proxies_header, timeout=5
)
ip_address = response.json().get("ip")
if not ip_address:
raise Exception("Failed with https://ipconfig.io/json")
break
except Exception as exp:
logger.debug(exp)
if cycle == 1:
break
logger.info("Request will be resend after 60 seconds")
sleep(60)
sleep(get_random_sleep(0.5, 1))
else:
ip_address = proxy.split(":")[0]
if not ip_address:
logger.info(f"Couldn't verify IP address for {proxy}!")
logger.debug("Geolocation won't be set")
return (None, None, None, None)
logger.info(f"Connecting with IP: {ip_address}")
db_result = geolocation_db_client.query_geolocation(ip_address)
latitude = None
longitude = None
country_code = None
timezone = None
if db_result:
latitude, longitude, country_code = db_result
logger.debug(f"Cached latitude and longitude for {ip_address}: ({latitude}, {longitude})")
logger.debug(f"Cached country code for {ip_address}: {country_code}")
if not country_code:
try:
response = requests.get(f"https://ipapi.co/{ip_address}/json/", timeout=5)
country_code = response.json().get("country_code")
timezone = response.json().get("timezone")
logger.debug(f"Country code for {ip_address}: {country_code}")
except Exception:
try:
response = requests.get(
"https://ifconfig.co/json", proxies=proxies_header, timeout=5
)
country_code = response.json().get("country_iso")
timezone = response.json().get("time_zone")
except Exception:
logger.debug(f"Couldn't find country code for {ip_address}!")
return (float(latitude), float(longitude), country_code, timezone)
else:
retry_count = 0
max_retry_count = 5
sleep_seconds = 5
while retry_count < max_retry_count:
try:
response = requests.get(f"https://ipapi.co/{ip_address}/json/", timeout=5)
latitude, longitude, country_code, timezone = (
response.json().get("latitude"),
response.json().get("longitude"),
response.json().get("country_code"),
response.json().get("timezone"),
)
if not (latitude and longitude and country_code):
raise Exception("Failed with https://ipapi.co")
break
except Exception as exp:
logger.debug(exp)
logger.debug("Continue with ifconfig.co")
try:
response = requests.get(
"https://ifconfig.co/json", proxies=proxies_header, timeout=5
)
latitude, longitude, country_code, timezone = (
response.json().get("latitude"),
response.json().get("longitude"),
response.json().get("country_iso"),
response.json().get("time_zone"),
)
if not (latitude and longitude and country_code):
raise Exception("Failed with https://ifconfig.co/json")
break
except Exception as exp:
logger.debug(exp)
logger.debug("Continue with ipconfig.io")
try:
response = requests.get(
"https://ipconfig.io/json", proxies=proxies_header, timeout=5
)
latitude, longitude, country_code, timezone = (
response.json().get("latitude"),
response.json().get("longitude"),
response.json().get("country_iso"),
response.json().get("time_zone"),
)
if not (latitude and longitude and country_code):
raise Exception("Failed with https://ipconfig.io/json")
break
except Exception as exp:
logger.debug(exp)
logger.error(
f"Couldn't find latitude and longitude for {ip_address}! "
f"Retrying after {sleep_seconds} seconds..."
)
retry_count += 1
sleep(sleep_seconds)
sleep_seconds *= 2
sleep(0.5)
if latitude and longitude and country_code:
logger.debug(f"Latitude and longitude for {ip_address}: ({latitude}, {longitude})")
logger.debug(f"Country code for {ip_address}: {country_code}")
geolocation_db_client.save_geolocation(ip_address, latitude, longitude, country_code)
return (latitude, longitude, country_code, timezone)
else:
logger.error(f"Couldn't find latitude, longitude, and country_code for {ip_address}!")
return (None, None, None, None)
def get_queries() -> list[str]:
"""Get queries from file
:rtype: list
:returns: List of queries
"""
filepath = Path(config.paths.query_file)
if not filepath.exists():
raise SystemExit(f"Couldn't find queries file: {filepath}")
with open(filepath, encoding="utf-8") as queryfile:
queries = [
query.strip().replace("'", "").replace('"', "")
for query in queryfile.read().splitlines()
]
return queries
def get_domains() -> list[str]:
"""Get domains from file
:rtype: list
:returns: List of domains
"""
filepath = Path(config.paths.filtered_domains)
if not filepath.exists():
raise SystemExit(f"Couldn't find domains file: {filepath}")
with open(filepath, encoding="utf-8") as domainsfile:
domains = [
domain.strip().replace("'", "").replace('"', "")
for domain in domainsfile.read().splitlines()
]
logger.debug(f"Domains: {domains}")
return domains
def add_cookies(driver: undetected_chromedriver.Chrome) -> None:
"""Add cookies from cookies.txt file
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
filepath = Path.cwd() / "cookies.txt"
if not filepath.exists():
raise SystemExit("Missing cookies.txt file!")
logger.info(f"Adding cookies from {filepath}")
with open(filepath, encoding="utf-8") as cookie_file:
try:
cookies = json.loads(cookie_file.read())
except Exception:
logger.error("Failed to read cookies file. Check format and try again.")
raise SystemExit()
for cookie in cookies:
if cookie["sameSite"] == "strict":
cookie["sameSite"] = "Strict"
elif cookie["sameSite"] == "lax":
cookie["sameSite"] = "Lax"
else:
cookie["sameSite"] = "None" if cookie["secure"] else "Lax"
driver.add_cookie(cookie)
def solve_recaptcha(
apikey: str,
sitekey: str,
current_url: str,
data_s: str,
cookies: Optional[str] = None,
) -> Optional[str]:
"""Solve the recaptcha using the 2captcha service
:type apikey: str
:param apikey: API key for the 2captcha service
:type sitekey: str
:param sitekey: data-sitekey attribute value of the recaptcha element
:type current_url: str
:param current_url: Url that is showing the captcha
:type data_s: str
:param data_s: data-s attribute of the captcha element
:type cookies: str
:param cookies: Cookies to send 2captcha service
:rtype: str
:returns: Response code obtained from the service or None
"""
logger.info("Trying to solve captcha...")
api_url = "http://2captcha.com/in.php"
params = {
"key": apikey,
"method": "userrecaptcha",
"googlekey": sitekey,
"pageurl": current_url,
"data-s": data_s,
}
if cookies:
params["cookies"] = cookies
max_retry_count = 10
request_retry_count = 0
while request_retry_count < max_retry_count:
response = requests.get(api_url, params=params)
logger.debug(f"Response: {response.text}")
error_to_exit, error_to_continue, error_to_break = _check_error(response.text)
if error_to_exit:
raise SystemExit()
elif error_to_break:
request_id = response.text.split("|")[1]
logger.debug(f"request_id: {request_id}")
break
elif error_to_continue:
request_retry_count += 1
continue
sleep(15)
# check if the CAPTCHA has been solved
response_api_url = "http://2captcha.com/res.php"
params = {"key": apikey, "action": "get", "id": request_id}
response_retry_count = 0
captcha_response = None
while response_retry_count < max_retry_count:
response = requests.get(response_api_url, params=params)
logger.debug(f"Response: {response.text}")
error_to_exit, error_to_continue, error_to_break = _check_error(
response.text, request_type="res_php"
)
if error_to_exit:
raise SystemExit()
elif error_to_continue:
response_retry_count += 1
continue
elif error_to_break:
if "CAPCHA_NOT_READY" not in response.text:
captcha_response = response.text.split("|")[1]
return captcha_response
if not captcha_response:
logger.error("Failed to solve captcha!")
return captcha_response
def take_screenshot(driver: undetected_chromedriver.Chrome) -> None:
"""Save screenshot during exception
:type driver: undetected_chromedriver.Chrome
:param driver: Selenium Chrome webdriver instance
"""
now = datetime.now().strftime("%d-%m-%Y_%H:%M:%S")
filename = f"exception_ss_{now}.png"
if driver:
driver.save_screenshot(filename)
sleep(get_random_sleep(1, 1.5))
logger.info(f"Saved screenshot during exception as {filename}")
def generate_click_report(click_results: list[tuple[str, str, str]], report_date: str) -> None:
"""Update results file with new rows
:type click_results: list
:param click_results: List of (site_url, clicks, category, click_time, query) tuples
:type report_date: str
:param report_date: Date to query clicks
"""
click_report_file = Path(f"click_report_{report_date}.xlsx")
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.row_dimensions[1].height = 20
# add header
sheet["A1"] = "URL"
sheet["B1"] = "Query"
sheet["C1"] = "Clicks"
sheet["D1"] = "Time"
sheet["E1"] = "Category"
bold_font = Font(bold=True)
center_align = Alignment(horizontal="center", vertical="center")
for cell in ("A1", "B1", "C1", "D1", "E1"):
sheet[cell].font = bold_font
sheet[cell].alignment = center_align
# adjust column widths
sheet.column_dimensions["A"].width = 80
sheet.column_dimensions["B"].width = 25
sheet.column_dimensions["C"].width = 15
sheet.column_dimensions["D"].width = 20
sheet.column_dimensions["E"].width = 15
for result in click_results:
url, click_count, category, click_time, query = result
sheet.append((url, query, click_count, f"{report_date} {click_time}", category))
for column_letter in ("B", "C", "D", "E"):
sheet.column_dimensions[column_letter].alignment = center_align
workbook.save(click_report_file)
logger.info(f"Results were written to {click_report_file}")
def get_random_sleep(start: int, end: int) -> float:
"""Generate a random number from the given range
:type start: int
:pram start: Start value
:type end: int
:pram end: End value
:rtype: float
:returns: Randomly selected number rounded to 2 decimals
"""
return round(random.uniform(start, end), 2)
def _check_error(response_text: str, request_type: str = "in_php") -> tuple[bool, bool, bool]:
"""Check errors returned from requests to in.php or res.php endpoints
:type response_text: str
:param response_text: Response returned from the request
:request_type: str
:param request_type: Request type to differentiate error groups
:rtype: tuple
:returns: Flags for exit, continue, and break
"""
logger.debug("Checking error code...")
error_to_exit, error_to_continue, error_to_break = False, False, False
if request_type == "in_php":
if "ERROR_WRONG_USER_KEY" in response_text or "ERROR_KEY_DOES_NOT_EXIST" in response_text:
logger.error("Invalid API key. Please check your 2captcha API key.")
error_to_exit = True
elif "ERROR_ZERO_BALANCE" in response_text:
logger.error("You don't have funds on your account. Please load your account.")
error_to_exit = True
elif "ERROR_NO_SLOT_AVAILABLE" in response_text:
logger.error(
"The queue of your captchas that are not distributed to workers is too long."
)
logger.info("Waiting 5 seconds before sending new request...")
sleep(5)
error_to_continue = True
elif "IP_BANNED" in response_text:
logger.error(
"Your IP address is banned due to many frequent attempts to access the server"
)
error_to_exit = True
elif "ERROR_GOOGLEKEY" in response_text:
logger.error("Blank or malformed sitekey.")
error_to_exit = True
else:
logger.debug(response_text)
error_to_break = True
elif request_type == "res_php":
if "ERROR_WRONG_USER_KEY" in response_text or "ERROR_KEY_DOES_NOT_EXIST" in response_text:
logger.error("Invalid API key. Please check your 2captcha API key.")
error_to_exit = True
elif "ERROR_CAPTCHA_UNSOLVABLE" in response_text:
logger.error("Unable to solve the captcha.")
error_to_exit = True
elif "CAPCHA_NOT_READY" in response_text:
logger.info("Waiting 5 seconds before checking response again...")
sleep(5)
error_to_continue = True
else:
logger.debug(response_text)
error_to_break = True
else:
logger.error(f"Wrong request type: {request_type}")
return (error_to_exit, error_to_continue, error_to_break)
def get_locale_language(country_code: str) -> str:
"""Get locale language for the given country code
:type country_code: str
:param country_code: Country code for proxy IP
:rtype: str
:returns: Locale language for the given country code
"""
logger.debug(f"Getting locale language for {country_code}...")
with open("country_to_locale.json", "r") as locales_file:
locales = json.load(locales_file)
locale_language = locales.get(country_code, ["en"])
logger.debug(f"Locale language code for {country_code}: {locale_language[0]}")
return locale_language
def resolve_redirect(url: str) -> str:
"""Resolve any redirects and return the final destination URL
:type url: str
:param url: Input url to resolve
:rtype: str
:returns: Final destination URL
"""
try:
response = requests.get(url, allow_redirects=True)
return response.url
except requests.RequestException as exp:
logger.error(f"Error resolving URL redirection: {exp}")
return url