-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (59 loc) · 2.1 KB
/
main.py
File metadata and controls
68 lines (59 loc) · 2.1 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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import pandas as pd
from supabase import create_client, Client
import os
import dotenv
from scraper_registry import get_all_scrapers
import uvicorn
app = FastAPI()
# Load environment variables
dotenv.load_dotenv()
# Supabase credentials
url = os.getenv("SUPER_BASE_URL")
key = os.getenv("SUPER_BASE_KEY")
supabase: Client = create_client(url, key)
# Model for input data
class JobSearch(BaseModel):
job_title: str
location: str
@app.post("/search_jobs")
def search_jobs(job: JobSearch):
all_results = []
found_job_links = False
found_job_data = False
for scraper in get_all_scrapers():
try:
job_links = scraper.get_job_links(job.job_title, job.location)
if not job_links or len(job_links) == 0:
continue
found_job_links = True
file_path = f"output_{scraper.__class__.__name__}.csv"
scraper.extract_job_details(job_links, file_path)
# Read and upload results
df = pd.read_csv(file_path)
if df.empty:
continue
found_job_data = True
df.columns = ['POSITION', 'COMPANY NAME', 'LOCATION', 'SALARY', 'JOB LINK', 'BENEFITS', 'DESCRIPTION', 'EMPLOYMENT TYPE']
df = df.astype(str).replace('nan', '')
records = df.to_dict(orient='records')
response = supabase.table('Job_listing').insert(records).execute()
all_results.append({
'scraper': scraper.__class__.__name__,
'uploaded_data': response.data
})
except Exception as e:
all_results.append({
'scraper': scraper.__class__.__name__,
'error': str(e)
})
if not found_job_links:
return {"detail": "No job links found."}
if not found_job_data:
return {"detail": "No job data could be extracted."}
return {'results': all_results}
if __name__ == "__main__":
import os
port = int(os.getenv("PORT", 8000))
uvicorn.run(app, host="0.0.0.0", port=port)