-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
73 lines (64 loc) · 2.44 KB
/
Copy pathscraper.py
File metadata and controls
73 lines (64 loc) · 2.44 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
import json
import os
import requests
from typing import List, Dict, Any
from config import JOB_API_KEY, SEEN_JOBS_FILE
def load_companies(filepath: str = "companies.json") -> List[str]:
try:
with open(filepath, 'r') as f:
return json.load(f)
except FileNotFoundError:
return []
def get_seen_jobs(filepath: str = SEEN_JOBS_FILE) -> set:
if not os.path.exists(filepath):
return set()
try:
with open(filepath, 'r') as f:
return set(line.strip() for line in f if line.strip())
except Exception as e:
return set()
def save_seen_job(job_id: str, filepath: str = SEEN_JOBS_FILE):
with open(filepath, 'a') as f:
f.write(f"{job_id}\n")
def fetch_jobs(query: str, company: str) -> List[Dict[str, Any]]:
print(f"Fetching jobs for query: '{query}'")
if not JOB_API_KEY:
print("Error: JOB_API_KEY is not set.")
return []
try:
url = "https://serpapi.com/search.json"
params = {
"engine": "google_jobs",
"q": query,
"hl": "en",
"location": "Bengaluru, Karnataka, India",
"api_key": JOB_API_KEY
}
response = requests.get(url, params=params, timeout=15)
response.raise_for_status()
data = response.json()
jobs_results = data.get("jobs_results", [])
parsed_jobs = []
for job in jobs_results:
job_id = job.get("job_id")
if not job_id:
continue
apply_options = job.get("apply_options", [])
url_link = apply_options[0].get("link", "#") if apply_options else job.get("share_link", "#")
description = job.get("description", "")
extensions = job.get("extensions", [])
parsed_jobs.append({
"job_id": job_id,
"title": job.get("title", "Job Role"),
"company_name": job.get("company_name", company),
"location": job.get("location", "Unknown"),
"url": url_link,
"job_type": extensions[0] if extensions else "N/A",
"via": job.get("via", "Direct"),
"full_description": description,
"snippet": description[:200] + "..." if len(description) > 200 else description
})
return parsed_jobs
except Exception as e:
print(f"Job fetch failed for {company}: {e}")
return []