forked from coskundeniz/ad_clicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
141 lines (104 loc) · 5.46 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
import re
import random
import subprocess
from time import sleep
import requests
from random_user_agent.user_agent import UserAgent
from random_user_agent.params import SoftwareName, OperatingSystem, Popularity, SoftwareType
from config import logger
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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36",
]
def get_random_user_agent_string() -> str:
"""Get random user agent
If returned value from random_user_agent package has Chrome version less than 90,
choice user agent string from the values defined in USER_AGENTS list.
:rtype: str
:returns: User agent string
"""
software_names = [SoftwareName.CHROME.value]
operating_systems = [
OperatingSystem.MAC.value,
OperatingSystem.LINUX.value,
OperatingSystem.WINDOWS.value,
]
software_types = [SoftwareType.WEB_BROWSER.value, SoftwareType.APPLICATION.value]
popularity = [Popularity.POPULAR.value, Popularity.COMMON.value, Popularity.AVERAGE.value]
user_agent_rotator = UserAgent(
software_names=software_names,
operating_systems=operating_systems,
software_types=software_types,
popularity=popularity,
limit=1000,
)
user_agents = user_agent_rotator.get_user_agents()
selected_versions = []
for item in user_agents:
user_agent_str = item["user_agent"]
if re.search("Chrome\/\s*v*(\d+)", user_agent_str):
major_version = int(re.search("Chrome\/\s*v*(\d+)", user_agent_str).group(1))
if major_version > 70:
selected_versions.append((user_agent_str, major_version))
if selected_versions:
user_agent_string, chrome_version = sorted(
selected_versions, key=lambda x: x[1], reverse=True
)[0]
if chrome_version < 90:
user_agent_string = random.choice(USER_AGENTS)
logger.debug(f"user_agent: {user_agent_string}")
return user_agent_string
def get_location(ip_address: str) -> tuple[float, float]:
"""Get latitude and longitude of ip address
:type ip_address: str
:param ip_address: IP address to get geolocation
:rtype: tuple
:returns: (latitude, longitude) pair for the given IP
"""
retry_count = 0
while retry_count < 5:
try:
response = requests.get(f"https://ipapi.co/{ip_address}/json/", timeout=2).json()
latitude, longitude = response.get("latitude"), response.get("longitude")
if not (latitude or longitude):
# try a different api
response = requests.get(
f"https://geolocation-db.com/json/{ip_address}", timeout=2
).json()
latitude, longitude = response.get("latitude"), response.get("longitude")
if latitude == "Not found":
latitude = longitude = None
if latitude and longitude:
logger.debug(f"Latitude and longitude for {ip_address}: ({latitude}, {longitude})")
return latitude, longitude
except requests.RequestException as exp:
logger.error(exp)
logger.error("Couldn't find latitude and longitude! Retrying after a second...")
retry_count += 1
sleep(1)
def get_installed_chrome_version() -> int:
"""Get major version for the Chrome installed on the system
:rtype: int
:returns: Chrome major version
"""
major_version = None
try:
result = subprocess.run(["google-chrome", "--version"], capture_output=True)
major_version = int(str(result.stdout).split(" ")[-2].split(".")[0])
logger.debug(f"Installed Chrome version: {major_version}")
except subprocess.SubprocessError:
logger.error("Failed to get Chrome version! Latest version will be used.")
return major_version