-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoEndorse.py
326 lines (271 loc) · 10.8 KB
/
AutoEndorse.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
import sys
import os
import time
import datetime
from dotenv import load_dotenv
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium_stealth import stealth
import urllib.request
import zipfile
import tarfile
from colorama import init, Fore, Style
import os
import sys
import urllib.request
import zipfile
import tarfile
import subprocess
import shutil
# Initialise the colours
init()
RED = Fore.RED
ORANGE = Fore.RED + Fore.YELLOW
GREEN = Fore.GREEN
LIGHT_BLUE = Style.BRIGHT + Fore.BLUE
PURPLE = Style.BRIGHT + Fore.MAGENTA
ERASE = Style.RESET_ALL
# Load the environment variables from the .env file
load_dotenv()
# Get the username and password from environment variables
iterations = int(os.getenv("E_ITERATIONS"))
last_execution = os.getenv("E_LAST_EXECUTION")
start_index = int(os.getenv("E_START_INDEX"))
endorsements_left = int(os.getenv("E_ENDORSEMENTS_LEFT"))
language = os.getenv("E_LANGUAGE")
first_time = os.getenv("FIRST_TIME").lower() == "true"
lines = open("buddies.txt", "r").readlines()
endorse_button_text = "Endorse"
def download_and_extract_chromium(url, extract_to):
print(f"Downloading Chromium from {url}...")
file_name = url.split('/')[-1]
file_path = os.path.join(extract_to, file_name)
urllib.request.urlretrieve(url, file_path)
print("Extracting Chromium...")
if file_name.endswith('.zip'):
with zipfile.ZipFile(file_path, 'r') as zip_ref:
zip_ref.extractall(extract_to)
elif file_name.endswith('.tar.xz'):
with tarfile.open(file_path, 'r:xz') as tar_ref:
tar_ref.extractall(extract_to)
os.remove(file_path)
print("Chromium is ready.")
def scroll_to_element(driver, element):
actions = ActionChains(driver)
actions.move_to_element(element).perform()
def smooth_scroll(driver, scroll_duration=5):
# Get the total height of the page
total_height = driver.execute_script("return document.body.scrollHeight")
# Calculate the number of steps for smooth scrolling
steps = 50
step_height = total_height / steps
# Calculate the time to wait between each step
step_time = scroll_duration / steps
# Perform the smooth scroll
for i in range(steps):
scroll_to = (i + 1) * step_height
driver.execute_script(f"window.scrollTo(0, {scroll_to});")
time.sleep(step_time)
# language
def esp_translate():
global endorse_button_text
endorse_button_text = "Validar"
if language is None:
print("Please select a language with the number / Por favor seleccione un idioma con el numero:")
print("1. English")
print("2. Español")
choice = input("--> ")
if choice == "1":
language = "english"
elif choice == "2":
language = "spanish"
else:
print("Invalid selection. Please run the script again and select 1 or 2.")
sys.exit(1)
language = language.lower()
if language == "english":
print("You have selected English.")
elif language == "spanish":
print("Ha seleccionado Español.")
esp_translate()
else:
print("Invalid selection. Please run the script again and select 1 or 2.")
sys.exit(1)
if len(lines) <= start_index:
start_index = 0
# Get the current date
current_date = datetime.datetime.now().strftime("%Y-%m-%d")
# If the last execution date is the same as the current date and no endorsements left, do not execute the script
if last_execution == current_date and endorsements_left == 0:
sys.exit(1)
if last_execution != current_date:
with open(".env", "w") as f:
f.write("E_ITERATIONS=" + str(iterations) + "\n")
f.write("E_START_INDEX=" + str(start_index) + "\n")
f.write("E_LAST_EXECUTION=" + current_date + "\n")
f.write("E_ENDORSEMENTS_LEFT=" + "150" + "\n")
f.write("E_LANGUAGE=" + str(language) + "\n")
f.write("FIRST_TIME=" + str(first_time) + "\n")
endorsements_left = 150
current_endorsement_amount = endorsements_left
# probably some are just bloat
options = Options()
if sys.platform.startswith("win"):
options.add_argument("--user-data-dir=.\\config\\my-google-chrome")
elif sys.platform.startswith("linux"):
options.add_argument("--user-data-dir=./config/my-google-chrome")
elif sys.platform.startswith("darwin"):
options.add_argument("--user-data-dir=./config/my-google-chrome")
else:
print("Unknown operating system.")
sys.exit(1)
options.add_argument("--no-first-run")
options.add_argument("--no-default-browser-check")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--remote-debugging-port=9292")
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--ignore-certificate-errors")
options.add_argument("--disable-blink-features=AutomationControlled")
#options.add_experimental_option("excludeSwitches", ["enable-automation"])
#options.add_experimental_option('useAutomationExtension', False)
# Create a Chrome WebDriver instance with the specified path
driver = uc.Chrome(options=options)
# Remove navigator.webdriver flag
#driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
# Add custom user-agent
#driver.execute_cdp_cmd('Network.setUserAgentOverride', {
# "userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'})
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
if first_time:
driver.get("https://linkedin.com")
try:
signin = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, """//a[text()='
Sign in
']""")))
login = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "session_key")))
except:
pass
# Check if undefined and assign False is such
signin = locals().get("signin", False)
login = locals().get("login", False)
if signin or login:
print("Please, login on the opened browser")
else:
print(f"{RED}No login box found!{ERASE}")
print("Please, confirm that you are logged in and ready to start the script")
print("Type 'yes' to continue")
print("Type anything else to exit")
confirmation = input("--> ")
if (confirmation.lower() != "yes"):
print("Exiting...")
driver.quit()
sys.exit(1)
print(f"{GREEN}Starting...{ERASE}")
success_counter = 0
row_counter = 0
def make_number(input_str):
number = ""
for char in input_str:
if char.isdigit():
number += char
return int(number)
# Start the loop
for line in lines[start_index:]:
if success_counter >= iterations:
break
line = line.strip()
if line.startswith("http"):
last_line = line
driver.get(line + "/details/skills/")
else:
continue
row_counter += 1
delete_this_shi = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "pv-profile-sticky-header-v2__actions-container"))
)
driver.execute_script("arguments[0].parentNode.removeChild(arguments[0]);", delete_this_shi)
main_div = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "main"))
)
unclickable_button_counter = 0
endorsement_per_person = 0
while True:
js_code = """
document.addEventListener('orsedByViewer', function(event) {
event.preventDefault();
}, true);
"""
driver.execute_script(js_code)
while True:
try:
delete_this_shi_2 = WebDriverWait(driver, 4).until(
EC.presence_of_element_located((By.CLASS_NAME, "artdeco-tabpanel--hidden"))
)
class_to_remove = "artdeco-tabpanel--hidden"
driver.execute_script("arguments[0].classList.remove(arguments[1]);", delete_this_shi_2,
class_to_remove)
except:
break
try:
endorse_button = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH,
f"//button[contains(@class, 'artdeco-button')]//span[normalize-space()='{endorse_button_text}']/.."))
)
print(f"{GREEN}{current_endorsement_amount - endorsements_left + 1} endorsements done{ERASE}")
endorsement_per_person += 1
except:
if unclickable_button_counter > 4:
print(f"{RED}Could not find any buttons{ERASE}")
break
unclickable_button_counter += 1
try:
if endorsements_left == 0:
print(f"{RED}No endorsements left{ERASE}")
break
endorse_button.click()
endorsements_left -= 1
except:
try:
smooth_scroll(driver, 3)
scroll_to_element(driver, endorse_button)
except:
pass
if unclickable_button_counter > 4:
print(f"{RED}Could not click the endorse button{ERASE}")
break
unclickable_button_counter += 1
time.sleep(4)
if endorsements_left == 0:
print("You ran out of endorsements :(")
break
success_counter += 1
person_name = driver.title.split("|")[1].strip()
print(f"{PURPLE}{endorsement_per_person} endorsements were given to {person_name}{ERASE}")
print(f"{LIGHT_BLUE}{success_counter} Profiles endorsed{ERASE}")
print(f"Iterated through {PURPLE}{row_counter}{ERASE} profiles")
print(f"Successfully processed {GREEN}{success_counter}{ERASE} profiles")
# Write updated environment variables to .env file
with open(".env", "w") as f:
f.write("E_ITERATIONS=" + str(iterations) + "\n")
f.write("E_START_INDEX=" + str(int(os.environ["E_START_INDEX"]) + success_counter) + "\n")
f.write("E_LAST_EXECUTION=" + current_date + "\n")
f.write("E_ENDORSEMENTS_LEFT=" + str(endorsements_left) + "\n")
f.write("E_LANGUAGE=" + str(language) + "\n")
f.write("FIRST_TIME=" + str(first_time) + "\n")