-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
553 lines (471 loc) · 15.4 KB
/
scraper.py
File metadata and controls
553 lines (471 loc) · 15.4 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
import os
import json
import time
import threading
import random
import sys
import io
import csv
from datetime import datetime, timezone
from urllib.parse import urlparse
from fastapi import FastAPI, Response
from pydantic import BaseModel
from duckduckgo_search import DDGS
import requests
from bs4 import BeautifulSoup
from playwright.sync_api import sync_playwright
import db
from ai_service import AIService
USE_PLAYWRIGHT = os.environ.get("USE_PLAYWRIGHT", "true").lower() == "true"
REQUEST_DELAY_SECONDS = float(os.environ.get("REQUEST_DELAY_SECONDS", "2.0"))
RETRY_MAX_ATTEMPTS = int(os.environ.get("RETRY_MAX_ATTEMPTS", "5"))
RETRY_BASE_SECONDS = float(os.environ.get("RETRY_BASE_SECONDS", "2.0"))
RETRY_MAX_SECONDS = float(os.environ.get("RETRY_MAX_SECONDS", "16"))
_pause_requested = False
_stop_requested = False
def data_dir() -> str:
return os.environ.get("DATA_DIR", "./data")
def config_dir() -> str:
return os.environ.get("CONFIG_DIR", "./config")
def status_path() -> str:
return os.path.join(data_dir(), "status.json")
def log_path() -> str:
return os.path.join(data_dir(), "scraper.log")
def lock_path() -> str:
return os.path.join(data_dir(), "run.lock")
api = FastAPI()
class RunRequest(BaseModel):
provider: str | None = "groq"
api_key: str | None = ""
groq_api_key: str | None = ""
anthropic_api_key: str | None = ""
gemini_api_key: str | None = ""
lite_mode: bool = False
sites: list[str] | None = None
keywords: list[str] | None = None
cv_text: str | None = None
def read_lines(path: str):
try:
with open(path, "r", encoding="utf-8") as f:
return [x.strip() for x in f.read().splitlines() if x.strip()]
except Exception:
return []
def read_text(path: str):
try:
with open(path, "r", encoding="utf-8") as f:
return f.read()
except Exception:
return ""
def normalize_site(site: str) -> str:
s = (site or "").strip()
if not s:
return ""
if "://" in s:
try:
p = urlparse(s)
s = p.netloc or p.path or s
except Exception:
pass
s = s.strip().strip("/")
if "/" in s:
s = s.split("/", 1)[0]
return s
def write_status(message: str, progress: int, meta: dict | None = None):
os.makedirs(data_dir(), exist_ok=True)
payload = {
"message": message,
"progress": int(progress),
"timestamp": time.time(),
}
if meta:
payload.update(meta)
try:
with open(status_path(), "w", encoding="utf-8") as f:
json.dump(payload, f)
except Exception:
pass
def append_log(line: str):
os.makedirs(data_dir(), exist_ok=True)
try:
with open(log_path(), "a", encoding="utf-8") as f:
f.write(f"{datetime.now(timezone.utc).isoformat()} {line}\n")
except Exception:
pass
def log_event(event: str, **fields):
payload = {
"ts": datetime.now(timezone.utc).isoformat(),
"event": event,
**fields,
}
try:
sys.stdout.write(json.dumps(payload, ensure_ascii=False) + "\n")
sys.stdout.flush()
except Exception:
pass
def is_running():
return os.path.exists(lock_path())
def is_paused():
return _pause_requested
def pause_requested():
return _pause_requested
def stop_requested():
return _stop_requested
def request_pause():
global _pause_requested
_pause_requested = True
def request_resume():
global _pause_requested
_pause_requested = False
def request_stop():
global _stop_requested
_stop_requested = True
def lock():
os.makedirs(data_dir(), exist_ok=True)
with open(lock_path(), "w", encoding="utf-8") as f:
f.write(str(time.time()))
def unlock():
try:
if os.path.exists(lock_path()):
os.remove(lock_path())
except Exception:
pass
def sleep_delay():
if REQUEST_DELAY_SECONDS > 0:
time.sleep(REQUEST_DELAY_SECONDS)
USER_AGENTS = [
"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 (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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0",
]
def get_random_user_agent():
return random.choice(USER_AGENTS)
def make_ddgs():
headers = {"User-Agent": get_random_user_agent()}
try:
return DDGS(headers=headers)
except TypeError:
return DDGS()
def ddg_text(query: str, max_results: int = 10):
mock = (os.environ.get("MOCK_SEARCH") or "").strip().lower() in (
"1",
"true",
"yes",
)
if mock:
return [
{
"title": "Mock Job",
"href": "https://example.com/job",
"body": f"mock snippet for {query}",
}
]
last = None
for backend in ("lite", "html", None):
try:
with make_ddgs() as ddgs:
if backend is None:
return list(ddgs.text(query, max_results=max_results))
try:
return list(
ddgs.text(query, max_results=max_results, backend=backend)
)
except TypeError:
return list(ddgs.text(query, max_results=max_results))
except Exception as e:
last = e
continue
if last:
raise last
return []
def backoff_sleep(attempt: int):
base = RETRY_BASE_SECONDS * (2**max(0, attempt))
delay = min(RETRY_MAX_SECONDS, base)
jitter = random.random() * 0.25 * delay
time.sleep(delay + jitter)
def with_retry(fn):
last = None
for attempt in range(RETRY_MAX_ATTEMPTS):
try:
return fn()
except Exception as e:
last = e
if attempt < RETRY_MAX_ATTEMPTS - 1:
log_event(
"retry",
attempt=attempt + 1,
max_attempts=RETRY_MAX_ATTEMPTS,
error=str(e),
)
backoff_sleep(attempt)
if last:
raise last
def html_extract(url: str):
if USE_PLAYWRIGHT:
try:
content = ""
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
try:
page = browser.new_page()
page.set_default_timeout(25000)
page.goto(url, wait_until="domcontentloaded")
content = page.content()
finally:
browser.close()
soup = BeautifulSoup(content, "html.parser")
text = soup.get_text(" ", strip=True)
if text:
return text[:8000]
except Exception:
log_event("fetch_playwright_failed", url=url)
pass
try:
def fetch():
sleep_delay()
return requests.get(
url,
timeout=20,
headers={"User-Agent": get_random_user_agent()},
)
r = with_retry(fetch)
soup = BeautifulSoup(r.text, "html.parser")
text = soup.get_text(" ", strip=True)
return text[:8000]
except Exception:
log_event("fetch_requests_failed", url=url)
return ""
def run_scrape(
api_key: str,
lite_mode: bool,
provider: str | None = "groq",
api_keys: dict[str, str] | None = None,
sites: list[str] | None = None,
keywords: list[str] | None = None,
cv_text: str | None = None,
):
global _pause_requested, _stop_requested
_pause_requested = False
_stop_requested = False
if is_running():
log_event("run_rejected", reason="already_running")
return
lock()
try:
db.init()
if sites is None:
sites = read_lines(os.path.join(config_dir(), "sites.txt"))
if keywords is None:
keywords = read_lines(os.path.join(config_dir(), "keywords.txt"))
if cv_text is None:
cv_text = read_text(os.path.join(config_dir(), "cv.txt"))
if not keywords:
write_status(
"no_keywords",
0,
{"running": False, "added": 0, "paused": False},
)
log_event("run_invalid", reason="no_keywords")
return
selected_provider = (provider or "groq").strip().lower()
keys = {
"groq": (os.environ.get("GROQ_API_KEY") or "").strip(),
"anthropic": (os.environ.get("ANTHROPIC_API_KEY") or "").strip(),
"gemini": (os.environ.get("GEMINI_API_KEY") or "").strip(),
}
for k, v in (api_keys or {}).items():
keys[str(k).strip().lower()] = (v or "").strip()
if api_key and not keys.get("groq"):
keys["groq"] = api_key.strip()
use_ai = bool(keys.get(selected_provider, "")) and not lite_mode
ai = AIService(selected_provider, keys)
log_event(
"run_started",
provider=selected_provider,
use_ai=use_ai,
lite_mode=bool(lite_mode),
sites=len(sites),
keywords=len(keywords),
)
total = max(1, len(sites) * max(1, len(keywords)))
done = 0
added = 0
write_status("starting", 0, {"running": True, "added": 0, "paused": False})
for site in sites or [""]:
site_norm = normalize_site(site)
if stop_requested():
log_event("run_stopped")
break
while pause_requested():
write_status(
"paused",
int((done / total) * 100),
{"running": True, "added": added, "paused": True},
)
time.sleep(1)
if stop_requested():
log_event("run_stopped")
break
if stop_requested():
break
for kw in keywords or [""]:
if stop_requested():
log_event("run_stopped")
break
while pause_requested():
write_status(
"paused",
int((done / total) * 100),
{"running": True, "added": added, "paused": True},
)
time.sleep(1)
if stop_requested():
log_event("run_stopped")
break
if stop_requested():
break
query_site = f"site:{site_norm}" if site_norm else ""
query = " ".join([x for x in [query_site, kw] if x])
append_log(f"query={query}")
log_event("query", query=query)
try:
def search():
sleep_delay()
return ddg_text(query, max_results=10)
results = with_retry(search)
except Exception:
log_event("search_failed", query=query)
results = []
for r in results:
title = r.get("title") or ""
link = r.get("href") or ""
snippet = r.get("body") or ""
company = site_norm or site
text = snippet
if use_ai and link:
fetched = html_extract(link)
if fetched:
text = fetched
score, reasoning = ai.analyze(text, cv_text, keywords)
h = db.upsert_job(
title=title,
company=company,
link=link,
site=site_norm or site,
snippet=snippet,
score=score,
reasoning=reasoning,
)
if h:
added += 1
done += 1
pct = int((done / total) * 100)
write_status(
"running",
pct,
{"running": True, "added": added, "paused": False},
)
if stop_requested():
write_status(
"stopped",
int((done / total) * 100),
{"running": False, "added": added, "paused": False},
)
log_event("run_stopped", added=added)
else:
write_status(
"complete",
100,
{"running": False, "added": added, "paused": False},
)
log_event("run_complete", added=added)
try:
from notifications import check_and_notify
n = check_and_notify()
if n > 0:
log_event("notifications_sent", count=n)
except Exception:
pass
finally:
unlock()
@api.get("/health")
def health():
return {"ok": True}
@api.get("/status")
def status():
try:
with open(status_path(), "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return {
"message": "idle",
"progress": 0,
"running": is_running(),
"added": 0,
"paused": False,
}
@api.get("/jobs")
def jobs(limit: int = 200):
db.init()
return {"jobs": db.list_jobs(limit=limit)}
@api.get("/export/json")
def export_json(limit: int = 200):
db.init()
return db.list_jobs(limit=limit)
@api.get("/export/csv")
def export_csv(limit: int = 200):
db.init()
rows = db.list_jobs(limit=limit)
fieldnames = []
for r in rows:
for k in r.keys():
if k not in fieldnames:
fieldnames.append(k)
buf = io.StringIO()
w = csv.DictWriter(buf, fieldnames=fieldnames)
w.writeheader()
for r in rows:
w.writerow(r)
return Response(content=buf.getvalue(), media_type="text/csv")
@api.post("/run")
def run(req: RunRequest):
keys = {
"groq": (req.groq_api_key or "").strip(),
"anthropic": (req.anthropic_api_key or "").strip(),
"gemini": (req.gemini_api_key or "").strip(),
}
t = threading.Thread(
target=run_scrape,
args=(
req.api_key or "",
bool(req.lite_mode),
req.provider or "groq",
keys,
req.sites,
req.keywords,
req.cv_text,
),
daemon=True,
)
t.start()
return {"started": True}
@api.post("/pause")
def pause():
if not is_running():
return {"paused": False, "running": False}
request_pause()
return {"paused": True, "running": True}
@api.post("/resume")
def resume():
if not is_running():
return {"paused": False, "running": False}
request_resume()
return {"paused": False, "running": True}
@api.post("/stop")
def stop():
if not is_running():
return {"stopping": False, "running": False}
request_stop()
request_resume()
return {"stopping": True, "running": True}