This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathutils.py
433 lines (343 loc) · 17 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
"""
HAYAT BÖYLE ZATEN
Bu evin bir köpeği vardı;
Kıvır kıvırdı, adı Çinçon'du, öldü.
Bir de kedisi vardı: Maviş,
Kayboldu.
Evin kızı gelin oldu,
Küçük Bey sınıfı geçti.
Daha böyle acı, tatlı
Neler oldu bir yıl içinde!
Oldu ya, olanların hepsi böyle...
Hayat böyle zaten!..
-- Orhan Veli
"""
import os
import sys
import random
from pathlib import Path
from time import sleep
from typing import Optional
import requests
import undetected_chromedriver
from config import logger
from geolocation_db import GeolocationDB
from proxy import install_plugin
USER_AGENTS = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.5112.79 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Mozilla/5.0 (Linux; Android 10; SM-N960U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.5304.105 Mobile Safari/537.36",
"Mozilla/5.0 (Linux; Android 10; SM-N960U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.5735.57 Mobile Safari/537.36",
"Mozilla/5.0 (Linux; Android 10; SM-N960U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.5735.57 Mobile Safari/537.36",
"Mozilla/5.0 (Linux; Android 10; LM-X420) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.5735.57 Mobile Safari/537.36",
"Mozilla/5.0 (Linux; Android 10; LM-X420) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.5735.57 Mobile Safari/537.36",
"Mozilla/5.0 (Linux; Android 10; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.5304.105 Mobile Safari/537.36",
"Mozilla/5.0 (Linux; Android 10; LM-Q720) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.5304.105 Mobile Safari/537.36",
"Mozilla/5.0 (Linux; Android 10; LM-X420) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.5304.105 Mobile Safari/537.36",
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/107.0.5304.101 Mobile/15E148 Safari/604.1",
"Mozilla/5.0 (iPad; CPU OS 16_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/107.0.5304.101 Mobile/15E148 Safari/604.1",
"Mozilla/5.0 (iPad; CPU OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1"
]
def get_random_user_agent_string() -> str:
"""Get random user agent
:rtype: str
:returns: User agent string
"""
user_agent_string = random.choice(USER_AGENTS)
logger.debug(f"user_agent: {user_agent_string}")
return user_agent_string
def get_location(
geolocation_db_client: GeolocationDB, proxy: str, auth: Optional[bool] = False
) -> tuple[float, float]:
"""Get latitude and longitude of ip address
:type geolocation_db_client: GeolocationDB
:param geolocation_db_client: GeolocationDB instance
:type proxy: str
:param proxy: Proxy to get geolocation
:type auth: bool
:param auth: Whether authentication is used or not for proxy
:rtype: tuple
:returns: (latitude, longitude) pair for the given proxy IP
"""
proxies_header = {"http": f"http://{proxy}", "https": f"http://{proxy}"}
ip_address = ""
if auth:
for cycle in range(2):
try:
response = requests.get("https://api.ipify.org", proxies=proxies_header)
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.debug("Request will be resend after 60 seconds")
sleep(60)
else:
ip_address = proxy.split(":")[0]
if not ip_address:
logger.info(f"Couldn't verify IP address for {proxy}!")
logger.info("Geolocation won't be set")
return (None, None)
logger.info(f"Connecting with IP: {ip_address}")
db_result = geolocation_db_client.query_geolocation(ip_address)
latitude = None
longitude = None
if db_result:
latitude, longitude = db_result
logger.debug(f"Cached latitude and longitude for {ip_address}: ({latitude}, {longitude})")
return float(latitude), float(longitude)
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/", proxies=proxies_header, timeout=5
)
latitude, longitude = (
response.json().get("latitude"),
response.json().get("longitude"),
)
if not (latitude and longitude):
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 = (
response.json().get("latitude"),
response.json().get("longitude"),
)
if not (latitude and longitude):
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 = (
response.json()["latitude"],
response.json()["longitude"],
)
if not (latitude and longitude):
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
if latitude and longitude:
logger.debug(f"Latitude and longitude for {ip_address}: ({latitude}, {longitude})")
geolocation_db_client.save_geolocation(ip_address, latitude, longitude)
return (latitude, longitude)
else:
logger.error(f"Couldn't find latitude and longitude for {ip_address}!")
return (None, None)
def get_queries(query_file: Path) -> list[str]:
"""Get queries from file
:type query_file: Path
:param query_file: File containing queries
:rtype: list
:returns: List of queries
"""
filepath = Path(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 create_webdriver(
proxy: str, auth: bool, headless: bool, incognito: Optional[bool] = False
) -> undetected_chromedriver.Chrome:
"""Create Selenium Chrome webdriver instance
:type proxy: str
:param proxy: Proxy to use in ip:port or user:pass@host:port format
:type auth: bool
:param auth: Whether authentication is used or not for proxy
:type headless: bool
:param headless: Whether to use headless browser
:type incognito: bool
:param incognito: Whether to run in incognito mode
:rtype: undetected_chromedriver.Chrome
:returns: Selenium Chrome webdriver instance
"""
geolocation_db_client = GeolocationDB()
user_agent_str = get_random_user_agent_string()
chrome_options = undetected_chromedriver.ChromeOptions()
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--ignore-ssl-errors")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--disable-translate")
chrome_options.add_argument("--deny-permission-prompts")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--no-first-run")
chrome_options.add_argument("--no-service-autorun")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument(f"--user-agent={user_agent_str}")
# disable these features for saving some proxy bandwidth
optimization_features = [
"OptimizationGuideModelDownloading",
"OptimizationHintsFetching",
"OptimizationTargetPrediction",
"OptimizationHints",
"Translate",
"DownloadBubble",
"DownloadBubbleV2",
]
chrome_options.add_argument(f"--disable-features={','.join(optimization_features)}")
# disable WebRTC IP tracking
webrtc_preferences = {
"webrtc.ip_handling_policy": "disable_non_proxied_udp",
"webrtc.multiple_routes_enabled": False,
"webrtc.nonproxied_udp_enabled": False,
}
chrome_options.add_experimental_option("prefs", webrtc_preferences)
if incognito:
chrome_options.add_argument("--incognito")
if headless:
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--window-size=1920,1080")
multi_browser_flag_file = Path(".MULTI_BROWSERS_IN_USE")
multi_procs_enabled = multi_browser_flag_file.exists()
driver_exe_path = None
if multi_procs_enabled:
driver_exe_path = _get_driver_exe_path()
if proxy:
logger.info(f"Using proxy: {proxy}")
if auth:
if "@" not in proxy or proxy.count(":") != 2:
raise ValueError(
"Invalid proxy format! Should be in 'username:password@host:port' format"
)
username, password = proxy.split("@")[0].split(":")
host, port = proxy.split("@")[1].split(":")
install_plugin(chrome_options, host, int(port), username, password)
else:
chrome_options.add_argument(f"--proxy-server={proxy}")
driver = undetected_chromedriver.Chrome(
driver_executable_path=(
driver_exe_path if multi_procs_enabled and Path(driver_exe_path).exists() else None
),
options=chrome_options,
headless=headless,
user_multi_procs=multi_procs_enabled,
)
# set geolocation of the browser according to IP address
accuracy = 90
lat, long = get_location(geolocation_db_client, proxy, auth)
if lat and long:
driver.execute_cdp_cmd(
"Emulation.setGeolocationOverride",
{"latitude": lat, "longitude": long, "accuracy": accuracy},
)
response = requests.get(
f"http://timezonefinder.michelfe.it/api/0_{long}_{lat}", timeout=5
)
if response.status_code == 200:
timezone = response.json()["tz_name"]
logger.debug(f"Timezone of {proxy.split('@')[1] if auth else proxy}: {timezone}")
driver.execute_cdp_cmd("Emulation.setTimezoneOverride", {"timezoneId": timezone})
else:
driver = undetected_chromedriver.Chrome(
driver_executable_path=(
driver_exe_path if multi_procs_enabled and Path(driver_exe_path).exists() else None
),
options=chrome_options,
headless=headless,
user_multi_procs=multi_procs_enabled,
)
return driver
def _get_driver_exe_path() -> str:
"""Get the path for the chromedriver executable to avoid downloading and patching each time
:rtype: str
:returns: Absoulute path of the chromedriver executable
"""
platform = sys.platform
prefix = "undetected"
exe_name = "chromedriver%s"
if platform.endswith("win32"):
exe_name %= ".exe"
if platform.endswith(("linux", "linux2")):
exe_name %= ""
if platform.endswith("darwin"):
exe_name %= ""
if platform.endswith("win32"):
dirpath = "~/appdata/roaming/undetected_chromedriver"
elif "LAMBDA_TASK_ROOT" in os.environ:
dirpath = "/tmp/undetected_chromedriver"
elif platform.startswith(("linux", "linux2")):
dirpath = "~/.local/share/undetected_chromedriver"
elif platform.endswith("darwin"):
dirpath = "~/Library/Application Support/undetected_chromedriver"
else:
dirpath = "~/.undetected_chromedriver"
driver_exe_folder = os.path.abspath(os.path.expanduser(dirpath))
driver_exe_path = os.path.join(driver_exe_folder, "_".join([prefix, exe_name]))
return driver_exe_path