-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
977 lines (837 loc) · 38.8 KB
/
bot.py
File metadata and controls
977 lines (837 loc) · 38.8 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
"""
LinkedIn Auto-Apply Bot
=======================
Applies to all saved jobs on LinkedIn at a scheduled time.
- Handles Easy Apply (multi-step modal) + External Apply (company site)
- Auto-fills forms using your profile config
- Pauses & asks for unknown fields, remembers answers for future runs
- Removes job from Saved after applying
- Logs every action to application_log.json
Requirements:
pip install playwright schedule colorama
playwright install chromium
"""
import asyncio
import json
import os
import re
import sys
import time
import logging
from datetime import datetime
from pathlib import Path
import schedule
from colorama import Fore, Style, init as colorama_init
from playwright.async_api import async_playwright, TimeoutError as PWTimeout
import config
# ── Logging Setup ──────────────────────────────────────────────────────────────
colorama_init(autoreset=True)
class ColorFormatter(logging.Formatter):
COLORS = {
logging.DEBUG: Fore.CYAN,
logging.INFO: Fore.GREEN,
logging.WARNING: Fore.YELLOW,
logging.ERROR: Fore.RED,
logging.CRITICAL: Fore.MAGENTA,
}
def format(self, record):
color = self.COLORS.get(record.levelno, "")
msg = super().format(record)
return f"{color}{msg}{Style.RESET_ALL}"
logger = logging.getLogger("LinkedInBot")
logger.setLevel(logging.DEBUG)
_ch = logging.StreamHandler()
_ch.setFormatter(ColorFormatter("[%(asctime)s] %(levelname)s %(message)s", "%H:%M:%S"))
_fh = logging.FileHandler("bot_debug.log", encoding="utf-8")
_fh.setFormatter(logging.Formatter("[%(asctime)s] %(levelname)s %(message)s"))
logger.addHandler(_ch)
logger.addHandler(_fh)
# ── JSON Helpers ───────────────────────────────────────────────────────────────
def load_json(path: str, default):
if Path(path).exists():
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
return default
def save_json(path: str, data):
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
# ── Application Logger ─────────────────────────────────────────────────────────
def log_application(job: dict, status: str, note: str = ""):
logs = load_json(config.LOG_FILE, [])
logs.append({
"timestamp": datetime.now().isoformat(),
"job_id": job.get("id", "unknown"),
"title": job.get("title", "unknown"),
"company": job.get("company", "unknown"),
"apply_type": job.get("apply_type", "unknown"),
"status": status,
"note": note,
"url": job.get("url", ""),
})
save_json(config.LOG_FILE, logs)
icons = {"applied": "✅", "skipped": "⏭️", "error": "❌", "manual_review": "👀"}
icon = icons.get(status, "•")
logger.info(
f"{icon} [ID:{job.get('id','?')}] {job.get('title','?')} "
f"@ {job.get('company','?')} → {status.upper()} {note}"
)
# ── Answers Memory ─────────────────────────────────────────────────────────────
class AnswersMemory:
"""Persist user-provided answers keyed by normalised question text."""
def __init__(self):
self.data: dict = load_json(config.ANSWERS_MEMORY_FILE, {})
def _key(self, q: str) -> str:
return re.sub(r"\s+", " ", q.lower().strip())
def get(self, question: str):
return self.data.get(self._key(question))
def remember(self, question: str, answer: str):
self.data[self._key(question)] = answer
save_json(config.ANSWERS_MEMORY_FILE, self.data)
logger.debug(f"💾 Remembered: '{question}' → '{answer}'")
memory = AnswersMemory()
# ── Known Answers Map ──────────────────────────────────────────────────────────
KNOWN = {
"phone": config.PROFILE["phone"],
"mobile": config.PROFILE["phone"],
"experience": config.PROFILE["total_experience"],
"years of exp": config.PROFILE["total_experience"],
"total exp": config.PROFILE["total_experience"],
"current ctc": config.PROFILE["current_ctc"],
"current salary": config.PROFILE["current_ctc"],
"expected ctc": config.PROFILE["expected_ctc"],
"expected salary": config.PROFILE["expected_ctc"],
"notice": config.PROFILE["notice_period"],
"authorized": "Yes",
"work authoriz": config.PROFILE["work_authorization"],
"visa": "No sponsorship required",
"location": config.PROFILE["location"],
"city": config.PROFILE["location"],
"name": config.PROFILE["name"],
"email": config.PROFILE["email"],
"linkedin": config.PROFILE["linkedin"],
"sponsorship": "No",
"gender": "Prefer not to say",
"disability": "No",
"veteran": "No",
"salary": config.PROFILE["expected_ctc"],
"compensation": config.PROFILE["expected_ctc"],
"relocate": "Yes",
"willing to relocat": "Yes",
"remote": "Yes",
"cover letter": (
f"I am Jayesh Jain, a Senior Software Engineer with 9+ years of experience "
f"in Java/Spring Boot, Microservices, and Fintech (UBS, Barclays). "
f"I am excited about this opportunity and confident I can add immediate value."
),
}
def match_known(label: str) -> str | None:
lower = label.lower()
for keyword, answer in KNOWN.items():
if keyword in lower:
return answer
return None
# ── Terminal Prompt (pauses bot) ───────────────────────────────────────────────
def ask_user_sync(question: str) -> str:
cached = memory.get(question)
if cached:
logger.info(f"🧠 Reusing remembered answer for '{question}' → '{cached}'")
return cached
print(f"\n{Fore.YELLOW}{'━'*55}")
print(f"{Fore.YELLOW}❓ UNKNOWN FIELD — Please answer:")
print(f"{Fore.WHITE} {question}")
print(f"{Fore.YELLOW}{'━'*55}")
answer = input(f"{Fore.CYAN}Your answer: {Style.RESET_ALL}").strip()
memory.remember(question, answer)
return answer
# ── Playwright Form Handler ────────────────────────────────────────────────────
async def fill_form_fields(page, job: dict):
"""
Detect all visible form inputs in the current modal/page and fill them.
Returns False if an unresolvable field blocks progress.
"""
# Text inputs / textareas
inputs = await page.query_selector_all(
"input:not([type=hidden]):not([type=submit]):not([type=button]), textarea"
)
for inp in inputs:
try:
visible = await inp.is_visible()
if not visible:
continue
# Get label
label = ""
inp_id = await inp.get_attribute("id") or ""
if inp_id:
lbl_el = await page.query_selector(f"label[for='{inp_id}']")
if lbl_el:
label = (await lbl_el.inner_text()).strip()
if not label:
placeholder = await inp.get_attribute("placeholder") or ""
aria_label = await inp.get_attribute("aria-label") or ""
label = placeholder or aria_label or inp_id
inp_type = (await inp.get_attribute("type") or "text").lower()
# Skip already-filled fields
current_val = await inp.input_value()
if current_val.strip():
continue
# File upload
if inp_type == "file":
if Path(config.RESUME_PATH).exists():
await inp.set_input_files(config.RESUME_PATH)
logger.debug(f"📎 Uploaded resume for field '{label}'")
else:
logger.warning(f"⚠️ Resume not found at {config.RESUME_PATH}")
continue
# Radio / checkbox — skip, handled separately
if inp_type in ("radio", "checkbox"):
continue
# Resolve answer
answer = match_known(label)
if not answer:
answer = ask_user_sync(label or f"field:{inp_id}")
await inp.fill(str(answer))
logger.debug(f" ✏️ Filled '{label}' → '{answer}'")
except Exception as e:
logger.debug(f" ⚠️ Skipped input: {e}")
# Select dropdowns
selects = await page.query_selector_all("select")
for sel in selects:
try:
visible = await sel.is_visible()
if not visible:
continue
sel_id = await sel.get_attribute("id") or ""
label = ""
if sel_id:
lbl_el = await page.query_selector(f"label[for='{sel_id}']")
if lbl_el:
label = (await lbl_el.inner_text()).strip()
if not label:
label = await sel.get_attribute("aria-label") or sel_id
# Get options
options = await sel.query_selector_all("option")
opt_texts = [await o.inner_text() for o in options if await o.get_attribute("value")]
answer = match_known(label)
if answer:
# Try to find best matching option
best = next(
(t for t in opt_texts if answer.lower() in t.lower()), None
) or (opt_texts[1] if len(opt_texts) > 1 else None)
if best:
await sel.select_option(label=best)
logger.debug(f" 🔽 Selected '{best}' for '{label}'")
else:
print(f"\n{Fore.YELLOW}❓ Dropdown — '{label}'")
print(f" Options: {', '.join(opt_texts)}")
chosen = ask_user_sync(f"Choose option for: {label}")
await sel.select_option(label=chosen)
except Exception as e:
logger.debug(f" ⚠️ Skipped select: {e}")
# Radio groups — pick Yes where applicable
radio_groups = {}
radios = await page.query_selector_all("input[type=radio]")
for r in radios:
name = await r.get_attribute("name")
if name:
radio_groups.setdefault(name, []).append(r)
for name, radios_list in radio_groups.items():
try:
checked = any([await r.is_checked() for r in radios_list])
if checked:
continue
# Get group label
first = radios_list[0]
group_label = await first.get_attribute("aria-label") or name
answer = match_known(group_label)
if not answer:
labels = []
for r in radios_list:
rid = await r.get_attribute("id") or ""
lbl = await page.query_selector(f"label[for='{rid}']")
labels.append((await lbl.inner_text()).strip() if lbl else rid)
print(f"\n{Fore.YELLOW}❓ Radio group — '{group_label}'")
print(f" Options: {', '.join(labels)}")
answer = ask_user_sync(f"Radio choice for: {group_label}")
for r in radios_list:
rid = await r.get_attribute("id") or ""
lbl_el = await page.query_selector(f"label[for='{rid}']")
lbl_text = (await lbl_el.inner_text()).strip() if lbl_el else ""
if answer.lower() in lbl_text.lower():
await r.click()
logger.debug(f" 🔘 Radio '{lbl_text}' selected for '{group_label}'")
break
except Exception as e:
logger.debug(f" ⚠️ Skipped radio group {name}: {e}")
# ── Easy Apply Handler ─────────────────────────────────────────────────────────
async def handle_easy_apply(page, job: dict) -> bool:
"""
Walk through LinkedIn's Easy Apply multi-step modal.
Returns True if successfully submitted.
"""
logger.info(f" 🖱️ Starting Easy Apply for [{job['id']}] {job['title']}")
step = 0
max_steps = 15
while step < max_steps:
step += 1
await page.wait_for_timeout(1000)
# Fill all visible fields on this step
await fill_form_fields(page, job)
# Upload resume if file input visible
file_inputs = await page.query_selector_all("input[type=file]")
for fi in file_inputs:
if await fi.is_visible():
if Path(config.RESUME_PATH).exists():
await fi.set_input_files(config.RESUME_PATH)
logger.debug(" 📎 Resume uploaded")
# Check for Submit button
submit = await page.query_selector(
"button[aria-label*='Submit application'], "
"button:has-text('Submit application')"
)
if submit and await submit.is_visible():
await submit.click()
logger.debug(" 🚀 Clicked Submit Application")
await page.wait_for_timeout(2000)
return True
# Next / Continue / Review
next_btn = await page.query_selector(
"button[aria-label*='Continue'], "
"button[aria-label*='Next'], "
"button[aria-label*='Review'], "
"button:has-text('Next'), "
"button:has-text('Continue'), "
"button:has-text('Review')"
)
if next_btn and await next_btn.is_visible():
await next_btn.click()
logger.debug(f" ➡️ Clicked Next (step {step})")
await page.wait_for_timeout(1500)
continue
# Dismiss / close if done
close_btn = await page.query_selector(
"button[aria-label='Dismiss'], "
"button[aria-label='Close']"
)
if close_btn and await close_btn.is_visible():
await close_btn.click()
return True
logger.warning(f" ⚠️ Could not find Next/Submit on step {step}")
break
return False
# ── External Apply Handler ─────────────────────────────────────────────────────
async def handle_external_apply(page, context, job: dict) -> bool:
"""
Handles 'Apply' that opens an external company website.
Opens in new tab, attempts to fill standard fields and submit.
"""
logger.info(f" 🌐 External Apply for [{job['id']}] {job['title']}")
async with context.expect_page() as new_page_info:
apply_btn = await page.query_selector(
"button:has-text('Apply'), "
"a:has-text('Apply'), "
".jobs-apply-button"
)
if apply_btn:
await apply_btn.click()
else:
return False
ext_page = await new_page_info.value
await ext_page.wait_for_load_state("domcontentloaded", timeout=config.PAGE_TIMEOUT * 1000)
logger.debug(f" 🌐 External page: {ext_page.url}")
# Basic form fill on external page
await fill_form_fields(ext_page, job)
# Look for file upload
file_inputs = await ext_page.query_selector_all("input[type=file]")
for fi in file_inputs:
if await fi.is_visible() and Path(config.RESUME_PATH).exists():
await fi.set_input_files(config.RESUME_PATH)
logger.debug(" 📎 Resume uploaded on external page")
# Try to submit
submit = await ext_page.query_selector(
"button[type=submit], "
"input[type=submit], "
"button:has-text('Submit'), "
"button:has-text('Apply'), "
"button:has-text('Send application')"
)
if submit and await submit.is_visible():
await submit.click()
await ext_page.wait_for_timeout(3000)
logger.debug(" 🚀 Clicked Submit on external page")
await ext_page.close()
return True
# Can't auto-submit — log for manual review
log_application(job, "manual_review", f"External URL: {ext_page.url}")
await ext_page.close()
return False
# ── Save/Unsave Job ────────────────────────────────────────────────────────────
async def unsave_job(page, job: dict):
"""Remove job from saved list after applying (works for both old & new UI)."""
try:
# New Job Tracker UI — go back to tracker and remove from there
# after apply we try the unsave button on the job detail page first
unsave_btn = await page.query_selector(
"button[aria-label*='Unsave'], "
"button[aria-label*='Remove from saved'], "
"button[aria-label*='Save job']:not([aria-pressed='false']), "
"button:has-text('Saved')"
)
if unsave_btn and await unsave_btn.is_visible():
await unsave_btn.click()
await page.wait_for_timeout(800)
logger.debug(f" 🗑️ Unsaved [{job['id']}] {job['title']}")
return
# Fallback: go to tracker, find the job row, click ··· menu → Remove
logger.debug(f" 🗑️ Trying tracker page to unsave [{job['id']}]")
await page.goto("https://www.linkedin.com/jobs/tracker/",
wait_until="domcontentloaded",
timeout=config.PAGE_TIMEOUT * 1000)
await page.wait_for_timeout(2000)
# Find the job row by job ID in any link href
job_link = await page.query_selector(f"a[href*='/jobs/view/{job['id']}']")
if job_link:
# Walk up to find the row, then find the ··· (overflow) button
overflow_btn = await page.evaluate("""(el) => {
let node = el;
for (let i = 0; i < 8; i++) {
node = node.parentElement;
if (!node) break;
const btn = node.querySelector(
'button[aria-label*="more"], button[aria-label*="options"], '
+ 'button[aria-label*="More actions"], button.artdeco-dropdown__trigger'
);
if (btn) { btn.click(); return true; }
}
return false;
}""", job_link)
if overflow_btn:
await page.wait_for_timeout(600)
remove_btn = await page.query_selector(
"div[role='option']:has-text('Remove'), "
"li:has-text('Remove'), "
"button:has-text('Remove')"
)
if remove_btn:
await remove_btn.click()
await page.wait_for_timeout(800)
logger.debug(f" 🗑️ Removed via tracker menu [{job['id']}] {job['title']}")
except Exception as e:
logger.debug(f" ⚠️ Could not unsave job: {e}")
# ── Scrape Saved Jobs ──────────────────────────────────────────────────────────
async def _get_row_text_lines(page, link_el):
"""
Pure-Playwright row text extraction — NO page.evaluate, NO JS strings.
Walks up the DOM using Playwright's element handles to find the job row,
then reads its inner_text and splits by newline.
"""
CONTAINER_TAGS = {"TR", "LI", "ARTICLE", "SECTION"}
CONTAINER_KEYWORDS = ("card", "row", "item", "job-tracker", "result", "entity")
node = link_el
for _ in range(10):
try:
parent = await node.evaluate_handle("el => el.parentElement")
if not parent:
break
tag = (await parent.evaluate("el => el.tagName") or "").upper()
cls = (await parent.evaluate("el => el.className || ''") or "").lower()
is_container = (
tag in CONTAINER_TAGS
or any(k in cls for k in CONTAINER_KEYWORDS)
)
if is_container:
raw = (await parent.inner_text() or "").strip()
lines = [l.strip() for l in raw.split("\n") if l.strip()]
return lines
node = parent
except Exception:
break
return []
async def get_saved_jobs(page) -> list[dict]:
"""
Navigate to LinkedIn Job Tracker, click Saved tab, then paginate
dynamically through ALL pages until the Next button is absent or disabled.
Uses ZERO page.evaluate calls — pure Playwright API only.
"""
logger.info("Fetching saved jobs from LinkedIn Job Tracker...")
await page.goto(
"https://www.linkedin.com/jobs/tracker/",
wait_until="domcontentloaded",
timeout=config.PAGE_TIMEOUT * 1000
)
await page.wait_for_timeout(3000)
# Click the Saved tab
try:
saved_tab = await page.query_selector(
"button:has-text('Saved'), "
"[aria-label*='Saved'], "
".job-tracker-tab:has-text('Saved')"
)
if saved_tab:
await saved_tab.click()
await page.wait_for_timeout(2500)
logger.debug(" Clicked Saved tab")
except Exception as e:
logger.debug(f" Could not click Saved tab: {e}")
NOISE_EXACT = {
"add note", "apply", "connections", "notes", "verified",
"jobs", "previous", "next", "date posted", "archived",
"in progress", "applied", "interview", "saved"
}
NOISE_PREFIX = ("posted", "reposted", "ago", "+")
NOISE_TYPES = (
"on-site", "hybrid", "remote", "full-time",
"part-time", "contract", "internship", "temporary"
)
def clean_lines(raw_lines):
result = []
for line in raw_lines:
low = line.lower().strip()
if not low or len(low) < 2:
continue
if low in NOISE_EXACT:
continue
if any(low.startswith(p) for p in NOISE_PREFIX):
continue
if low in NOISE_TYPES:
continue
if re.match(r"^\d+$", low): # pure number e.g. "10"
continue
if re.match(r"^\+\d+$", low): # "+10"
continue
if re.match(r"^page \d+$", low): # "Page 9"
continue
result.append(line.strip())
return result
jobs = []
seen_ids = set()
page_num = 0
while True:
page_num += 1
await page.wait_for_timeout(1500)
job_links = await page.query_selector_all("a[href*='/jobs/view/']")
logger.info(f" Page {page_num}: {len(job_links)} job link(s) found")
for link_el in job_links:
try:
# ── Job ID from href ──
href = await link_el.get_attribute("href") or ""
marker = "/jobs/view/"
m_idx = href.find(marker)
if m_idx == -1:
continue
rest = href[m_idx + len(marker):]
m = re.match(r"^(\d+)", rest)
job_id = m.group(1) if m else ""
if not job_id or job_id in seen_ids:
continue
seen_ids.add(job_id)
# ── Title from link text or children ──
title = (await link_el.inner_text()).strip()
if not title or len(title) < 3:
title = (await link_el.get_attribute("aria-label") or "").strip()
if not title or len(title) < 3:
for sel in ["strong", "span", "h3", "h2", "h4"]:
child = await link_el.query_selector(sel)
if child:
t = (await child.inner_text()).strip()
if len(t) > 2:
title = t
break
# ── Row text via pure Playwright (no page.evaluate) ──
raw_lines = await _get_row_text_lines(page, link_el)
lines = clean_lines(raw_lines)
logger.debug(f" Row lines for {job_id}: {lines[:6]}")
# Title fallback
if (not title or len(title) < 3) and lines:
title = lines[0]
# ── Company: line with "·" separator, or second clean line ──
company = "Unknown Company"
for line in lines:
if line == title:
continue
# LinkedIn format: "JPMorgan · Pune Division (On-site)"
if "·" in line:
company = line.split("·")[0].strip()
break
# Fallback: first non-title line
if len(line) > 1:
company = line
break
# Strip verified badge from company
company = re.sub(
r"\s*(Verified|\u2713|\u2611)\s*", "",
company, flags=re.IGNORECASE
).strip()
title = re.sub(r"\s+", " ", title).strip()
company = re.sub(r"\s+", " ", company).strip()
if not title or len(title) < 3:
title = "Unknown Title"
if not company or len(company) < 2:
company = "Unknown Company"
jobs.append({
"id": job_id,
"title": title,
"company": company,
"url": f"https://www.linkedin.com/jobs/view/{job_id}/",
"apply_type": "unknown",
})
logger.info(f" Found: [{job_id}] {title} @ {company}")
except Exception as e:
logger.debug(f" Row error for link: {e}")
logger.info(f" Page {page_num} done — {len(jobs)} total jobs so far")
# ── Stop condition: Next button absent or disabled ──
# Works for ANY number of pages — purely dynamic
try:
next_btn = None
# Try aria-label first (most reliable)
for aria in ("Next", "Go to next page", "next"):
next_btn = await page.query_selector(f"button[aria-label='{aria}']")
if next_btn:
break
# Fallback: scan all buttons for text "Next"
if not next_btn:
all_btns = await page.query_selector_all("button")
for btn in all_btns:
txt = (await btn.inner_text() or "").strip().lower()
if txt in ("next", "next ›", "›", ">"):
next_btn = btn
break
if not next_btn:
logger.info(f" No Next button — scraped all {page_num} page(s)")
break
if await next_btn.is_disabled() or not await next_btn.is_visible():
logger.info(f" Next button disabled — last page was {page_num}")
break
# Active Next button — go to next page
await next_btn.click()
await page.wait_for_timeout(2500)
logger.debug(f" Moving to page {page_num + 1}")
except Exception as e:
logger.warning(f" Pagination error: {e} — stopping")
break
logger.info(f"Total saved jobs found: {len(jobs)} across {page_num} page(s)")
return jobs
# ── Process Single Job ─────────────────────────────────────────────────────────
async def process_job(page, context, job: dict):
"""Navigate to job page and apply."""
logger.info(f"🔍 Processing [{job['id']}] {job['title']} @ {job['company']}")
try:
await page.goto(job["url"], wait_until="domcontentloaded",
timeout=config.PAGE_TIMEOUT * 1000)
await page.wait_for_timeout(2000)
# ── Detect apply button type ──
# Log all buttons on the page for debugging
all_btns = await page.query_selector_all("button")
btn_labels = []
for b in all_btns:
try:
lbl = (await b.get_attribute("aria-label") or "") + " | " + (await b.inner_text() or "")
lbl = lbl.strip()
if lbl and len(lbl) > 1:
btn_labels.append(lbl[:80])
except Exception:
pass
logger.debug(f" 🔎 Buttons found on page: {btn_labels[:10]}")
# Easy Apply: LinkedIn's own apply modal
easy_apply_btn = None
for sel in [
"button[aria-label*='Easy Apply']",
"button.jobs-apply-button[aria-label*='Easy Apply']",
"button:has-text('Easy Apply')",
]:
easy_apply_btn = await page.query_selector(sel)
if easy_apply_btn and await easy_apply_btn.is_visible():
break
else:
easy_apply_btn = None
# External Apply: any Apply button that is NOT Easy Apply
# From screenshot: button with text "Apply" + external link icon
external_btn = None
if not easy_apply_btn:
for sel in [
"button[aria-label*='Apply']:not([aria-label*='Easy Apply'])",
"button.jobs-apply-button:not([aria-label*='Easy Apply'])",
"a.jobs-apply-button",
# Broad fallback: any visible button whose text starts with "Apply"
"button:has-text('Apply')",
"a:has-text('Apply')",
]:
candidate = await page.query_selector(sel)
if candidate and await candidate.is_visible():
btn_text = (await candidate.inner_text() or "").strip()
# Make sure it's not Easy Apply
if "easy" not in btn_text.lower():
external_btn = candidate
logger.debug(f" 🔎 External apply candidate: '{btn_text}' via selector '{sel}'")
break
if easy_apply_btn and await easy_apply_btn.is_visible():
job["apply_type"] = "easy_apply"
await easy_apply_btn.click()
await page.wait_for_timeout(1500)
success = await handle_easy_apply(page, job)
if success:
log_application(job, "applied", "Easy Apply")
await unsave_job(page, job)
else:
log_application(job, "error", "Easy Apply modal did not complete")
elif external_btn and await external_btn.is_visible():
job["apply_type"] = "external"
success = await handle_external_apply(page, context, job)
if success:
log_application(job, "applied", "External Apply")
await unsave_job(page, job)
# else already logged inside handler
else:
# Last resort: dump page title for debug
page_title = await page.title()
logger.warning(f" ⚠️ No apply button found. Page title: '{page_title}' | URL: {page.url}")
log_application(job, "skipped", f"No apply button found | {page_title}")
except PWTimeout:
log_application(job, "error", "Page timeout")
except Exception as e:
log_application(job, "error", str(e))
logger.error(f" ❌ Exception on [{job['id']}]: {e}")
# ── Session: Login ─────────────────────────────────────────────────────────────
COOKIES_FILE = "linkedin_cookies.json"
async def login_or_load_session(page):
"""Load saved cookies or prompt manual login and save cookies."""
if Path(COOKIES_FILE).exists():
cookies = load_json(COOKIES_FILE, [])
await page.context.add_cookies(cookies)
await page.goto("https://www.linkedin.com/feed/",
wait_until="domcontentloaded",
timeout=config.PAGE_TIMEOUT * 1000)
await page.wait_for_timeout(2000)
# Verify still logged in
if "feed" in page.url or "mynetwork" in page.url:
logger.info("🔑 Session restored from cookies")
return True
else:
logger.warning("⚠️ Cookies expired — need to log in again")
# Manual login
print(f"\n{Fore.CYAN}{'═'*55}")
print(f"{Fore.CYAN} 🔐 MANUAL LOGIN REQUIRED")
print(f"{Fore.WHITE} A browser window will open.")
print(f"{Fore.WHITE} Please log into LinkedIn manually.")
print(f"{Fore.WHITE} Press ENTER here once you are logged in.")
print(f"{Fore.CYAN}{'═'*55}\n")
await page.goto("https://www.linkedin.com/login",
wait_until="domcontentloaded",
timeout=config.PAGE_TIMEOUT * 1000)
input(f"{Fore.YELLOW} ↳ Press ENTER after logging in: {Style.RESET_ALL}")
# Save cookies
cookies = await page.context.cookies()
save_json(COOKIES_FILE, cookies)
logger.info(f"🍪 Session cookies saved to {COOKIES_FILE}")
return True
# ── Main Runner ────────────────────────────────────────────────────────────────
async def run_bot():
logger.info("=" * 55)
logger.info(f"🚀 LinkedIn Auto-Apply Bot started at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
logger.info("=" * 55)
# Always create log file immediately on startup
if not Path(config.LOG_FILE).exists():
save_json(config.LOG_FILE, [])
logger.info(f"📄 Created {config.LOG_FILE}")
logs = load_json(config.LOG_FILE, [])
logs.append({
"timestamp": datetime.now().isoformat(),
"job_id": "SESSION",
"title": "Bot session started",
"company": "",
"apply_type": "",
"status": "session_start",
"note": f"Bot launched at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
"url": "",
})
save_json(config.LOG_FILE, logs)
async with async_playwright() as pw:
browser = await pw.chromium.launch(
headless=config.HEADLESS,
slow_mo=config.SLOW_MO,
args=["--start-maximized"]
)
context = await browser.new_context(
viewport={"width": 1440, "height": 900},
user_agent=(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/124.0.0.0 Safari/537.36"
)
)
page = await context.new_page()
# Login / restore session
await login_or_load_session(page)
# Fetch saved jobs
jobs = await get_saved_jobs(page)
if not jobs:
logger.warning("📭 No saved jobs found.")
logger.warning(" Saving debug snapshot -> debug_snapshot.html + debug_screenshot.png")
# Save full page HTML for debugging
try:
html_content = await page.content()
with open("debug_snapshot.html", "w", encoding="utf-8") as dbf:
dbf.write(html_content)
logger.info(" Saved debug_snapshot.html — open in browser to inspect page structure")
except Exception as e:
logger.debug(f" Could not save HTML snapshot: {e}")
# Save screenshot
try:
await page.screenshot(path="debug_screenshot.png", full_page=True)
logger.info(" Saved debug_screenshot.png — shows what the bot actually sees")
except Exception as e:
logger.debug(f" Could not save screenshot: {e}")
logger.info(f" Current page URL: {page.url}")
# Count job links on page
all_links = await page.query_selector_all("a[href]")
job_links = []
for lnk in all_links:
href = await lnk.get_attribute("href") or ""
if "/jobs/" in href:
job_links.append(href)
logger.info(f" Total job-related links on page: {len(job_links)}")
if job_links:
logger.info(f" Sample hrefs: {job_links[:5]}")
# Write to log file
logs = load_json(config.LOG_FILE, [])
logs.append({
"timestamp": datetime.now().isoformat(),
"job_id": "SCAN",
"title": "No saved jobs found",
"company": "",
"apply_type": "",
"status": "no_jobs",
"note": f"Page: {page.url} | Job links: {len(job_links)}",
"url": page.url,
})
save_json(config.LOG_FILE, logs)
await browser.close()
logger.warning(" Check debug_snapshot.html & debug_screenshot.png in your project folder")
return
logger.info(f"\n{'─'*55}")
logger.info(f" Processing {len(jobs)} saved job(s)...")
logger.info(f"{'─'*55}")
for i, job in enumerate(jobs, 1):
logger.info(f"\n[{i}/{len(jobs)}] ────────────────────────────────")
await process_job(page, context, job)
await page.wait_for_timeout(2000)
logger.info(f"\n{'=' * 55}")
logger.info(f"Run complete. Check {config.LOG_FILE} for full report.")
logger.info(f"{'=' * 55}\n")
await browser.close()
# ── Scheduler ─────────────────────────────────────────────────────────────────
def run_bot_sync():
asyncio.run(run_bot())
def start_scheduler():
ist_time = config.RUN_TIME
schedule.every().day.at(ist_time).do(run_bot_sync)
logger.info(f"⏰ Scheduler set — bot will run daily at {ist_time} IST")
logger.info(f" (Running immediately on first start too...)\n")
run_bot_sync() # also run immediately on launch
while True:
schedule.run_pending()
time.sleep(30)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "--now":
asyncio.run(run_bot())
else:
start_scheduler()