-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
1328 lines (1145 loc) · 56.7 KB
/
run.py
File metadata and controls
1328 lines (1145 loc) · 56.7 KB
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from env_bootstrap import ensure_venv
ensure_venv()
# Imports
import os
import sys
import time
import json
import random
import pathlib
from urllib.parse import quote, urlencode
from datetime import datetime
from pprint import pprint
from dotenv import load_dotenv
# Load .env file (credentials, etc.)
load_dotenv()
# Selenium Imports
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
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.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException, WebDriverException, ElementNotInteractableException
# -------------------------------------------------------------------------
# CONFIGURATION & SETTINGS
# -------------------------------------------------------------------------
def load_json_config(config_file="search_config.json"):
"""Load configuration from search_config.json."""
if os.path.exists(config_file):
try:
with open(config_file, 'r') as f:
return json.load(f)
except Exception as e:
print(f"Warning: failed to load {config_file}: {e}")
return {}
def get_config_value(config_data, keys, default=None):
value = config_data
for key in keys:
if isinstance(value, dict) and key in value:
value = value[key]
else:
return default
return value
# Load configuration
config_file_path = "search_config.json"
config = load_json_config(config_file_path)
# Global Settings
logs_folder_path = get_config_value(config, ["logging", "log_file"], "logs/bot.log")
if '/' in logs_folder_path:
logs_folder_path = os.path.dirname(logs_folder_path) + '/'
else:
logs_folder_path = 'logs/'
run_in_background = get_config_value(config, ["browser", "headless_mode"], False)
disable_extensions = False
safe_mode = True
stealth_mode = get_config_value(config, ["anti_detection", "use_stealth_mode"], False)
click_gap = get_config_value(config, ["delays_seconds", "min_delay"], 1.0)
# Credentials: env vars take priority, then config fallback
username = os.environ.get('LINKEDIN_USERNAME') or get_config_value(config, ["linkedin", "credentials", "username"], '')
password = os.environ.get('LINKEDIN_PASSWORD') or get_config_value(config, ["linkedin", "credentials", "password"], '')
if username:
print(f"Loaded credentials for: {username}")
else:
print("WARNING: No credentials found. Set LINKEDIN_USERNAME/LINKEDIN_PASSWORD in .env or search_config.json")
# -------------------------------------------------------------------------
# HELPERS
# -------------------------------------------------------------------------
def make_directories(paths: list[str]) -> None:
'''
Function to create missing directories
'''
for path in paths:
path = os.path.expanduser(path)
path = path.replace("//","/")
if '.' in os.path.basename(path):
path = os.path.dirname(path)
if not path: continue
try:
if not os.path.exists(path):
os.makedirs(path, exist_ok=True)
except Exception as e:
print(f'Error while creating directory "{path}": ', e)
def find_default_profile_directory() -> str | None:
home = pathlib.Path.home()
if sys.platform.startswith('win'):
paths = [
os.path.expandvars(r"%LOCALAPPDATA%\Google\Chrome\User Data"),
os.path.expandvars(r"%USERPROFILE%\AppData\Local\Google\Chrome\User Data"),
os.path.expandvars(r"%USERPROFILE%\Local Settings\Application Data\Google\Chrome\User Data")
]
elif sys.platform.startswith('linux'):
paths = [
str(home / ".config" / "google-chrome"),
str(home / ".var" / "app" / "com.google.Chrome" / "data" / ".config" / "google-chrome"),
]
elif sys.platform == 'darwin':
paths = [
str(home / "Library" / "Application Support" / "Google" / "Chrome")
]
else:
return None
for path_str in paths:
if os.path.exists(path_str):
return path_str
return None
def print_lg(*msgs, end: str = "\n", pretty: bool = False, flush: bool = True):
try:
log_file = os.path.join(logs_folder_path, "log.txt")
for message in msgs:
pprint(message) if pretty else print(message, end=end, flush=flush)
with open(log_file, 'a+', encoding="utf-8") as file:
file.write(str(message) + end)
except Exception as e:
print(f"Logging failed: {e}")
def buffer(speed: int=0) -> None:
if speed<=0: return
elif speed <= 1 and speed < 2:
time.sleep(random.randint(6,10)*0.1)
elif speed <= 2 and speed < 3:
time.sleep(random.randint(10,18)*0.1)
else:
time.sleep(random.randint(18,round(speed)*10)*0.1)
def manual_login_retry(is_logged_in_func, limit: int = 2) -> None:
count = 0
while not is_logged_in_func():
print_lg("Seems like you're not logged in!")
message = 'Please Log In manually in the browser.\nOnce you have successfully logged in, press Enter in this terminal to continue...'
if count > limit:
message = 'If you\'re seeing this message even after you logged in, press Enter to force continue. Seems like auto login confirmation failed!'
count += 1
print_lg(f"\n!!! ACTION REQUIRED !!!\n{message}\n")
input("Press Enter to continue...")
if count > limit: return
# -------------------------------------------------------------------------
# FINDERS & CLICKERS
# -------------------------------------------------------------------------
def scroll_to_view(driver, element, top: bool = False):
if top:
return driver.execute_script('arguments[0].scrollIntoView();', element)
return driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth" });', element)
def is_checked(element) -> bool:
try:
if element.get_attribute("aria-checked") == "true" or element.get_attribute("checked") == "true":
return True
if element.tag_name == "input" and element.is_selected():
return True
return False
except: return False
def wait_span_click(driver, text: str, time_wait: float=5.0, click: bool=True, scroll: bool=True, scrollTop: bool=False):
if text:
try:
xpath = f'.//span[normalize-space(.)="{text}"] | .//label[normalize-space(.)="{text}"] | .//div[normalize-space(.)="{text}"]'
button = WebDriverWait(driver,time_wait).until(EC.presence_of_element_located((By.XPATH, xpath)))
if scroll: scroll_to_view(driver, button, scrollTop)
if click:
if not is_checked(button):
button.click()
buffer(click_gap)
return button
except Exception:
return False
def text_input_by_ID(driver, id_val: str, value: str, time_wait: float=5.0):
field = WebDriverWait(driver, time_wait).until(EC.presence_of_element_located((By.ID, id_val)))
field.send_keys(Keys.CONTROL + "a")
field.send_keys(value)
def find_by_class(driver, class_name: str, time_wait: float=5.0):
return WebDriverWait(driver, time_wait).until(EC.presence_of_element_located((By.CLASS_NAME, class_name)))
def try_xp(driver, xpath: str, click: bool=True):
try:
if click:
driver.find_element(By.XPATH, xpath).click()
return True
else:
return driver.find_element(By.XPATH, xpath)
except: return False
def try_linkText(driver, linkText: str):
try: return driver.find_element(By.LINK_TEXT, linkText)
except: return False
# -------------------------------------------------------------------------
# BROWSER & LOGIN LOGIC
# -------------------------------------------------------------------------
if stealth_mode:
try:
import undetected_chromedriver as uc
except ImportError:
print("Stealth mode requires 'undetected-chromedriver'. Installing it now...")
try:
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "undetected-chromedriver"])
import undetected_chromedriver as uc
except Exception as e:
print(f"Failed to install undetected-chromedriver: {e}")
stealth_mode = False
# -------------------------------------------------------------------------
# BROWSER & LOGIN LOGIC
# -------------------------------------------------------------------------
def setup_browser():
make_directories([logs_folder_path])
if stealth_mode:
options = uc.ChromeOptions()
else:
options = Options()
if run_in_background:
options.add_argument("--headless=new")
if disable_extensions:
options.add_argument("--disable-extensions")
# Stability options
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
# # # # # # # # # # options.add_argument("--remote-debugging-pipe") # Causes Chrome to crash in some environments # Causes Chrome to crash in some environments # Causes Chrome to crash in some environments # Causes Chrome to crash in some environments # Causes Chrome to crash in some environments # Causes Chrome to crash in some environments # Causes Chrome to crash in some environments # Causes Chrome to crash in some environments # Causes Chrome to crash in some environments # Causes Chrome to crash in some environments
if safe_mode:
print_lg("Safe Mode: Logging in with a guest profile.")
else:
# NOTE: To use your own profile, you must close ALL other Chrome instances first.
# However, to be stable and avoid 'SessionNotCreatedException', we will default to a
# clean session unless explicitly configured otherwise.
# Uncomment the lines below ONLY if you are sure Chrome is closed.
# profile_dir = find_default_profile_directory()
# if profile_dir:
# options.add_argument(f"--user-data-dir={profile_dir}")
# options.add_argument("--profile-directory=Default")
# For now, we behave like the reference 'Auto_job_applier_linkedIn' and do NOT attach profile by default
pass
driver = None
print_lg("Initializing WebDriver...")
try:
if stealth_mode:
driver = uc.Chrome(options=options)
else:
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
print_lg("Resolving ChromeDriver via webdriver-manager...")
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)
except Exception as e:
print_lg(f"Error initializing Chrome: {e}")
print_lg("Ensure Chrome is installed. If using stealth_mode, try turning it off in config.")
raise e
try:
# Move to 2nd monitor or maximize, similar to reference code
driver.set_window_rect(x=0, y=0, width=1280, height=1080)
except:
driver.maximize_window()
return driver, WebDriverWait(driver, 5), ActionChains(driver)
def is_logged_in_LN(driver) -> bool:
if "linkedin.com/feed" in driver.current_url: return True
if "linkedin.com/login" in driver.current_url: return False
# If we see "Sign in" or "Join now" buttons, we are NOT logged in
if try_linkText(driver, "Sign in"): return False
if try_xp(driver, '//button[@type="submit" and contains(text(), "Sign in")]', click=False): return False
if try_linkText(driver, "Join now"): return False
# Double check for Feed
try:
driver.find_element(By.CLASS_NAME, "scaffold-layout__main") # Common feed container
return True
except:
pass
# If we are on the login page specifically
if driver.find_elements(By.ID, "username") and driver.find_elements(By.ID, "password"):
return False
print_lg("Assuming user is logged in (No login identifiers found).")
return True
def login_LN(driver, wait) -> None:
print_lg("Navigating to LinkedIn Login...")
driver.get("https://www.linkedin.com/login")
# Wait for the login form to be ready (username field is the most reliable indicator)
login_wait = WebDriverWait(driver, 15)
try:
login_wait.until(EC.presence_of_element_located((By.ID, "username")))
except:
# Retry once - LinkedIn sometimes shows interstitials
print_lg("Login page slow to load, retrying...")
time.sleep(3)
driver.get("https://www.linkedin.com/login")
try:
login_wait.until(EC.presence_of_element_located((By.ID, "username")))
except:
print_lg("Could not load login page after retry.")
try:
text_input_by_ID(driver, "username", username, 5)
except: print_lg("Couldn't find username field.")
try:
text_input_by_ID(driver, "password", password, 5)
except: print_lg("Couldn't find password field.")
try:
driver.find_element(By.XPATH, '//button[@type="submit" and contains(text(), "Sign in")]').click()
except:
try:
driver.find_element(By.XPATH, '//button[@type="submit"]').click()
except:
print_lg("Could not find Sign in button.")
try:
wait.until(EC.url_contains("linkedin.com/feed"))
print_lg("Login successful!")
except:
# Check if we hit a verification challenge
current_url = driver.current_url
if 'checkpoint' in current_url or 'challenge' in current_url:
print_lg("Verification challenge detected. Attempting auto-OTP from email...")
try:
from otp_email import fetch_otp
code = fetch_otp()
if code:
try:
input_field = driver.find_element(By.CSS_SELECTOR,
'input[name="pin"], input#input__email_verification_pin, input[type="text"]')
input_field.clear()
input_field.send_keys(code)
time.sleep(1)
submit_btn = driver.find_element(By.CSS_SELECTOR,
'button[type="submit"], button#email-pin-submit-button')
submit_btn.click()
time.sleep(5)
if is_logged_in_LN(driver):
print_lg("Login successful after OTP verification!")
return
except Exception as e:
print_lg(f"Auto-OTP submission failed: {e}")
else:
print_lg("Auto-OTP: no code found in email.")
except ImportError:
print_lg("otp_email module not available. Configure IMAP settings in .env for auto-OTP.")
except Exception as e:
print_lg(f"Auto-OTP failed: {e}")
print_lg("Login verification failed. Please log in manually.")
manual_login_retry(lambda: is_logged_in_LN(driver), 2)
# -------------------------------------------------------------------------
# METRICS & BOT LOGIC
# -------------------------------------------------------------------------
class MetricsManager:
def __init__(self, metrics_file="data/metrics.json"):
self.metrics_file = metrics_file
self.metrics = self.load_metrics()
self.today = datetime.now().strftime("%Y-%m-%d")
def load_metrics(self):
if not os.path.exists(self.metrics_file):
return {
"total_requests": 0,
"daily_requests": {},
"weekly_requests": {},
"last_run": "",
"group_runs": []
}
try:
with open(self.metrics_file, 'r') as f:
return json.load(f)
except:
return {
"total_requests": 0,
"daily_requests": {},
"weekly_requests": {},
"last_run": "",
"group_runs": []
}
def save_metrics(self):
with open(self.metrics_file, 'w') as f:
json.dump(self.metrics, f, indent=4)
def get_daily_count(self):
return self.metrics.get("daily_requests", {}).get(self.today, 0)
def get_weekly_count(self):
total = 0
today = datetime.now().date()
daily = self.metrics.get("daily_requests", {})
for date_str, count in daily.items():
try:
d = datetime.strptime(date_str, "%Y-%m-%d").date()
if (today - d).days < 7:
total += count
except ValueError:
continue
return total
def can_connect(self):
max_daily = get_config_value(config, ["limits", "max_daily_invites"], 15)
current = self.get_daily_count()
if current >= max_daily:
print_lg(f"Daily limit reached: {current}/{max_daily}")
return False
max_weekly = get_config_value(config, ["limits", "max_weekly_invites"], 80)
weekly = self.get_weekly_count()
if weekly >= max_weekly:
print_lg(f"Weekly limit reached: {weekly}/{max_weekly}")
return False
return True
def increment_requests(self, group_set_id=None):
self.metrics["total_requests"] += 1
if "daily_requests" not in self.metrics: self.metrics["daily_requests"] = {}
daily = self.metrics["daily_requests"].get(self.today, 0)
self.metrics["daily_requests"][self.today] = daily + 1
# Track per group-set daily counts
if group_set_id:
gs_key = f"daily_requests_{group_set_id}"
if gs_key not in self.metrics: self.metrics[gs_key] = {}
gs_daily = self.metrics[gs_key].get(self.today, 0)
self.metrics[gs_key][self.today] = gs_daily + 1
self.metrics["last_run"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.metrics["last_group_set"] = group_set_id or ""
self.save_metrics()
print_lg(f"Request logged. Daily Total: {self.metrics['daily_requests'][self.today]}")
def get_group_runs(self, group_set_id=None):
runs = self.metrics.get("group_runs", [])
if group_set_id:
return [r for r in runs if isinstance(r, dict) and r.get("group_set") == group_set_id]
return runs
def add_group_run(self, entry):
self.metrics.setdefault("group_runs", []).append(entry)
self.save_metrics()
class LinkedInBot:
def __init__(self, driver, wait):
self.driver = driver
self.wait = wait
self.metrics = MetricsManager(get_config_value(config, ["monitoring", "metrics_file"], 'data/metrics.json'))
# Determine run mode
self.mode = config.get('mode', 'keywords')
# Resolve groups from group_sets_config (for group_sets mode)
gsc = config.get('group_sets_config', {})
self.active_group_set_id = gsc.get('active_set', '')
group_sets = gsc.get('sets', {})
if self.active_group_set_id and self.active_group_set_id in group_sets:
active_set = group_sets[self.active_group_set_id]
self.groups = active_set.get('groups', [])
self.active_group_set_name = active_set.get('name', self.active_group_set_id)
print_lg(f"Using group set: '{self.active_group_set_name}' (id: {self.active_group_set_id})")
else:
self.groups = []
self.active_group_set_id = 'default'
self.active_group_set_name = 'Default'
if group_sets:
available = ', '.join(group_sets.keys())
print_lg(f"WARNING: active_set '{gsc.get('active_set', '')}' not found. Available: {available}")
# Load flat keywords list (for keywords mode)
self.keywords = get_config_value(config, ["keywords_config", "keywords"], [])
self.skip_keywords = [k.strip().lower() for k in get_config_value(config, ["filters", "skip_profiles_keywords"], []) if k.strip()]
self.message_templates = get_config_value(config, ["messaging", "connection_message_templates"], [])
self.search_url_base = get_config_value(
config, ["search", "base_search_url"], 'https://www.linkedin.com/search/results/people/?keywords='
)
self.search_url_params = get_config_value(config, ["search", "url_params"], {})
def run(self):
if not self.metrics.can_connect():
print_lg("Review limits in search_config.json")
return
print_lg(f"Mode: {self.mode}")
print_lg(f"Loaded {len(self.skip_keywords)} skip keywords: {self.skip_keywords}")
if self.mode == 'group_sets':
self.run_group_sets_mode()
else:
self.run_keywords_mode()
def run_keywords_mode(self):
keywords = [k.strip() for k in self.keywords if k.strip()]
if not keywords:
print_lg("No keywords configured in keywords_config.keywords. Nothing to do.")
return
print_lg(f"Keywords mode: {len(keywords)} keywords to process")
max_pages = get_config_value(config, ["pagination", "max_pages"], 10)
total_sent = 0
for keyword in keywords:
if not self.metrics.can_connect():
print_lg("Daily/weekly limit reached. Stopping.")
break
print_lg(f"Starting search for keyword: {keyword}")
sent, search_url = self.search_and_connect_keyword(keyword, max_connections=999)
total_sent += sent
print_lg(f"Keywords mode complete. Total invites sent: {total_sent}")
def run_group_sets_mode(self):
print_lg(f"Active group set: '{self.active_group_set_name}' (id: {self.active_group_set_id})")
print_lg(f"Groups in set: {len(self.groups)}")
today_name = datetime.now().strftime("%A").lower()
eligible_groups = [g for g in self.groups if self.should_run_group(g, today_name)]
if not eligible_groups:
print_lg(f"No groups scheduled for today ({today_name.title()}).")
return
for group in eligible_groups:
keywords_csv = (group.get("keywords_csv") or "").strip()
if not keywords_csv:
keywords_list = group.get("keywords") or []
if isinstance(keywords_list, list):
keywords_csv = ", ".join([k.strip() for k in keywords_list if str(k).strip()])
if not keywords_csv:
continue
print_lg(f"Starting search for group: {group.get('name', group.get('id', 'unknown'))}")
# Log group start immediately (before processing)
self.start_group_run(group)
connections_sent, search_urls = self.search_and_connect_group(group, keywords_csv)
# Update and save the group run results
self.update_group_run(search_urls, keywords_csv, connections_sent)
if not self.metrics.can_connect():
print_lg("Global daily limit reached. Stopping bot.")
break
def should_run_group(self, group, today_name):
run_day = (group.get("run_day") or "").strip().lower()
if run_day and run_day != today_name:
return False
completed_runs = self.metrics.get_group_runs(self.active_group_set_id)
if not completed_runs:
return True
days_block = get_config_value(config, ["run_policy", "do_not_repeat_group_if_logged_within_days"], 1)
today = datetime.now().date()
today_str = today.strftime("%Y-%m-%d")
invites_per_keyword = get_config_value(config, ["group_sets_config", "invites_per_keyword"], 5)
keywords_limit = get_config_value(config, ["group_sets_config", "keywords_per_group"], 4)
max_daily = get_config_value(config, ["limits", "max_daily_invites"], 15)
group_target = invites_per_keyword * keywords_limit
group_target = min(group_target, max_daily)
today_total = 0
has_today = False
for entry in completed_runs:
if not isinstance(entry, dict):
continue
if entry.get("group_id") != group.get("id"):
continue
date_str = entry.get("date")
if not date_str:
continue
try:
entry_date = datetime.strptime(date_str, "%Y-%m-%d").date()
except ValueError:
continue
if date_str == today_str:
has_today = True
today_total += int(entry.get("connections_sent", 0) or 0)
continue
if (today - entry_date).days < days_block:
return False
if has_today:
return today_total < group_target
return True
def search_and_connect_group(self, group, keywords_csv):
keywords = [k.strip() for k in keywords_csv.split(",") if k.strip()]
max_per_keyword = get_config_value(config, ["group_sets_config", "invites_per_keyword"], 5)
keywords_limit = get_config_value(config, ["group_sets_config", "keywords_per_group"], len(keywords))
keywords = keywords[:keywords_limit]
total_sent = 0
search_urls = []
for keyword in keywords:
if not self.metrics.can_connect():
break
print_lg(f"Starting search for keyword: {keyword}")
sent, search_url = self.search_and_connect_keyword(keyword, max_per_keyword)
total_sent += sent
search_urls.append(search_url)
return total_sent, search_urls
def search_and_connect_keyword(self, keyword, max_connections):
encoded_keyword = quote(keyword, safe="")
search_url = f"{self.search_url_base}{encoded_keyword}"
if self.search_url_params:
search_url = f"{search_url}&{urlencode(self.search_url_params)}"
print_lg(f"[NAVIGATE] Loading search URL: {search_url}")
self.driver.get(search_url)
time.sleep(get_config_value(config, ["delays_seconds", "page_load_delay"], 5))
print_lg(f"[PAGE] Page loaded. Current URL: {self.driver.current_url}")
print_lg(f"[PAGE] Page title: {self.driver.title}")
connections_sent = self.process_page(max_connections)
return connections_sent, search_url
def go_to_next_page(self):
"""
Click the 'Next' pagination button to go to the next page of results.
Returns True if successfully navigated, False otherwise.
"""
try:
# Look for the Next button using multiple selectors
next_btn = None
selectors = [
"//button[@data-testid='pagination-controls-next-button-visible']",
"//button[normalize-space(.)='Next']",
"//button[contains(@class, 'pagination')]//span[text()='Next']/ancestor::button"
]
for selector in selectors:
try:
next_btn = self.driver.find_element(By.XPATH, selector)
if next_btn and next_btn.is_displayed() and next_btn.is_enabled():
break
except:
continue
if not next_btn:
print_lg("[PAGINATION] Next button not found or not clickable.")
return False
# Scroll to the pagination area
scroll_to_view(self.driver, next_btn)
time.sleep(1)
# Click the Next button
print_lg("[PAGINATION] Clicking 'Next' button...")
self.driver.execute_script("arguments[0].click();", next_btn)
# Wait for page to load (10 seconds as specified)
print_lg("[PAGINATION] Waiting 10s for next page to load...")
time.sleep(10)
# Verify we're on a new page by checking URL or content change
print_lg(f"[PAGINATION] Now on: {self.driver.current_url}")
return True
except Exception as e:
print_lg(f"[PAGINATION] Error navigating to next page: {e}")
return False
def count_connect_buttons(self):
"""Count the number of Connect buttons/links on the current page."""
try:
connect_elements = self.driver.find_elements(By.XPATH,
"//a[contains(@href, 'search-custom-invite')] | "
"//button[contains(@aria-label, 'Invite')][contains(@aria-label, 'to connect')]"
)
return len(connect_elements)
except:
return 0
def process_page(self, max_connections):
connections_sent = 0
max_pages = get_config_value(config, ["pagination", "max_pages"], 10)
current_page = 1
while current_page <= max_pages and connections_sent < max_connections:
print_lg(f"[PAGE {current_page}] Processing page...")
remaining = max_connections - connections_sent
page_connections = self._process_single_page(remaining)
connections_sent += page_connections
if not self.metrics.can_connect():
print_lg("[LIMIT] Daily limit reached, stopping pagination.")
break
# Check if we should go to next page
remaining_buttons = self.count_connect_buttons()
print_lg(f"[PAGE {current_page}] Processed {page_connections} connections. {remaining_buttons} Connect buttons remaining.")
# If no more connect buttons or very few, try next page
if connections_sent >= max_connections:
break
if remaining_buttons == 0:
print_lg(f"[PAGINATION] No Connect buttons left on page {current_page}. Attempting next page...")
if self.go_to_next_page():
current_page += 1
continue
else:
print_lg("[PAGINATION] Cannot navigate to next page. Ending.")
break
else:
# Still have buttons, but we've processed what we could
# This handles the case where buttons exist but were skipped
break
return connections_sent
def _process_single_page(self, max_connections):
"""Process a single page of search results. Returns connections sent on this page."""
connections_sent = 0
# Allow page to settle
print_lg("[PROCESS] Page settling (3s)...")
time.sleep(3)
# Scroll down and up to trigger lazy loading
print_lg("[SCROLL] Scrolling to trigger lazy loading...")
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight/2);")
time.sleep(1)
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
self.driver.execute_script("window.scrollTo(0, 0);")
time.sleep(2)
print_lg("[SCROLL] Done scrolling.")
# Updated XPath to include 'a' (anchor) tags as LinkedIn frequently renders these as links
# We look for aria-labels like "Invite {Name} to connect" or inner text "Connect"
xpath_query = """
//button[contains(@aria-label, 'Invite')][contains(@aria-label, 'to connect')] |
//a[contains(@aria-label, 'Invite')][contains(@aria-label, 'to connect')] |
//button[normalize-space(.)='Connect'] |
//a[normalize-space(.)='Connect']
"""
buttons = self.driver.find_elements(By.XPATH, xpath_query)
print_lg(f"[BUTTONS] Found {len(buttons)} 'Connect' buttons/links on this page.")
# Debug: Log first few button details
for idx, btn in enumerate(buttons[:3]):
try:
aria = btn.get_attribute('aria-label') or 'N/A'
tag = btn.tag_name
txt = btn.text[:30] if btn.text else 'N/A'
print_lg(f" [BTN {idx}] <{tag}> aria='{aria}' text='{txt}'")
except: pass
processed = set()
while True:
if not self.metrics.can_connect() or connections_sent >= max_connections:
break
# Re-find items to ensure freshness as the DOM might change
current_buttons = self.driver.find_elements(By.XPATH, xpath_query)
if not current_buttons:
break
button = None
button_key = None
for candidate in current_buttons:
if not candidate.is_displayed():
continue
try:
aria = candidate.get_attribute("aria-label") or ""
href = candidate.get_attribute("href") or ""
button_key = aria or href or getattr(candidate, "id", None)
except Exception:
button_key = getattr(candidate, "id", None)
if button_key and button_key in processed:
continue
button = candidate
break
if not button:
break
# =====================================================
# STEP 1: Find and read the profile card text
# =====================================================
card_text = ""
skip_this_profile = False
try:
card = None
selectors = [
# --- Selectors verified working against live LinkedIn (April 2026) ---
"./ancestor::a[contains(@href, '/in/')]", # Profile card <a> wrapper (MOST RELIABLE)
"./ancestor::div[@data-display-contents='true'][string-length(.) > 50]", # React display-contents wrapper
"./ancestor::div[string-length(.) > 50][string-length(.) < 500]", # Text-length based fallback
# --- Legacy selectors (may work on older LinkedIn versions) ---
"./ancestor::li", # Old LinkedIn structure
"./ancestor::*[@data-view-name='people-search-result']", # LinkedIn data attribute
"./ancestor::div[contains(@class, 'entity-result__item')]", # LinkedIn class
"./ancestor::div[contains(@class, 'reusable-search__result-container')]", # Another LinkedIn class
"./ancestor::div[contains(@class, 'search-result')]", # Generic search result
"./ancestor::div[contains(@class, 'artdeco-card')]", # LinkedIn card class
"./ancestor::div[role='listitem']", # ARIA role
]
for selector in selectors:
try:
card = button.find_element(By.XPATH, selector)
if card:
break
except:
continue
if card:
# Method 1: Try specific LinkedIn subtitle selectors (most reliable)
try:
subtitle_selectors = [
".//span[contains(@class, 'entity-result__primary-subtitle')]",
".//div[contains(@class, 'entity-result__primary-subtitle')]",
".//span[contains(@class, 'entity-result__secondary-subtitle')]",
".//div[contains(@class, 'entity-result__summary')]",
".//p[contains(@class, 'entity-result')]",
]
subtitle_texts = []
for sel in subtitle_selectors:
try:
elems = card.find_elements(By.XPATH, sel)
for elem in elems:
if elem.text and elem.text.strip():
subtitle_texts.append(elem.text.strip())
except:
continue
if subtitle_texts:
card_text = " ".join(subtitle_texts).lower()
print_lg(f"[VERIFY] Got profile text via subtitle selectors: {card_text[:80]}...")
except:
pass
# Method 2: Try <p> tags
if not card_text or len(card_text.strip()) < 5:
p_texts = [p.text for p in card.find_elements(By.TAG_NAME, "p") if p.text]
if p_texts:
card_text = " ".join(p_texts).lower()
print_lg(f"[VERIFY] Got profile text via <p> tags: {card_text[:80]}...")
# Method 3: Get all text from the card
if not card_text or len(card_text.strip()) < 5:
card_text = card.text.lower()
if card_text and len(card_text.strip()) >= 5:
print_lg(f"[VERIFY] Got profile text via full card text: {card_text[:80]}...")
# Method 4: Try sibling/parent text
if not card_text or len(card_text.strip()) < 5:
try:
parent = button.find_element(By.XPATH, "./ancestor::div[1]")
siblings_text = parent.text.lower()
if siblings_text and len(siblings_text) > len(card_text or ''):
card_text = siblings_text
print_lg(f"[VERIFY] Got profile text via parent: {card_text[:80]}...")
except:
pass
else:
print_lg("[VERIFY] SKIP: Could not find profile card element. No selector matched.")
skip_this_profile = True
except Exception as e:
print_lg(f"[VERIFY] SKIP: Profile card read error: {e}")
skip_this_profile = True
# If card element wasn't found, skip for safety
if skip_this_profile:
if button_key:
processed.add(button_key)
continue
# =====================================================
# STEP 2: Verification checks (THIS CODE NOW EXECUTES)
# =====================================================
# Check 2a: Skip keywords from config
if card_text:
found_keyword = next((k for k in self.skip_keywords if k in card_text), None)
if found_keyword:
try:
name_log = card_text.split('\n')[0][:30]
except:
name_log = "Unknown"
print_lg(f"[VERIFY] SKIP '{name_log}' - matched skip keyword: '{found_keyword}'")
if button_key:
processed.add(button_key)
continue
# Check 2b: Skip non-AI roles (recruiters, HR, sales, etc.)
non_ai_roles = [
'recruiter', 'talent', 'hr ', 'human resources', 'headhunter',
'sales', 'account executive', 'account manager', 'business development',
'marketing', 'content', 'writer', 'editor', 'designer',
'operations', 'admin', 'assistant', 'coordinator',
'finance', 'accounting', 'legal', 'lawyer',
'customer service', 'support', 'success manager'
]
if card_text:
has_non_ai_role = any(role in card_text for role in non_ai_roles)
if has_non_ai_role:
print_lg(f"[VERIFY] SKIP - non-AI role detected: {card_text[:100]}...")
if button_key:
processed.add(button_key)
continue
# Check 2c: AI keyword verification (only for AI group set)
if self.active_group_set_id == 'ai':
ai_keywords = [
'ai', 'artificial intelligence', 'machine learning', 'ml',
'deep learning', 'neural network', 'llm', 'large language model',
'generative ai', 'genai', 'nlp', 'natural language processing',
'computer vision', 'data science', 'data scientist',
'mlops', 'aiops', 'prompt engineer', 'ai engineer',
'research scientist', 'ai researcher', 'ai developer',
'ai product', 'ai solutions', 'ai strategy', # AI-specific titles only
]
if card_text:
has_ai_keyword = any(keyword in card_text for keyword in ai_keywords)
if not has_ai_keyword:
print_lg(f"[VERIFY] SKIP - no AI keywords in: {card_text[:100]}...")
if button_key:
processed.add(button_key)
continue
else:
print_lg(f"[VERIFY] PASS - AI keyword found in: {card_text[:80]}...")
else:
# No card text at all - skip for safety
print_lg("[VERIFY] SKIP - no profile text available for AI verification.")
if button_key:
processed.add(button_key)
continue
# Check 2d: Profile quality - skip very sparse profiles
if card_text:
word_count = len(card_text.split())
if word_count < 3:
print_lg(f"[VERIFY] SKIP - too sparse ({word_count} words): {card_text[:100]}...")
if button_key:
processed.add(button_key)
continue
# Extract Name
first_name = "there"
try:
aria_label = button.get_attribute("aria-label")
if aria_label and "Invite" in aria_label and "to connect" in aria_label:
full_name = aria_label.split("Invite ")[1].split(" to connect")[0]
first_name = full_name.split(' ')[0]
else:
# Fallback: Use card text (first line is usually the name)
if card_text:
full_name = card_text.split('\n')[0]
first_name = full_name.split(' ')[0]
if len(first_name) < 2: first_name = "there"
except Exception as e:
print_lg(f"Name extraction warning: {e}")
print_lg(f"[CONNECT] Clicking Connect for {first_name}...")
try:
# Click Connect - use JS to avoid element interception
self.driver.execute_script("arguments[0].click();", button)
print_lg(f"[CONNECT] Click executed. Checking for modal or direct send...")
time.sleep(1.5)
# Handle Modal - pass button reference to detect direct send
success = self.handle_connection_modal(first_name, connect_element=button)