Spaces:
Sleeping
Sleeping
Create linkedin_job_search.py
Browse files- tools/linkedin_job_search.py +52 -0
tools/linkedin_job_search.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents.tools import Tool
|
| 2 |
+
import requests
|
| 3 |
+
from typing import List, Dict
|
| 4 |
+
|
| 5 |
+
class LinkedInJobSearchTool(Tool):
|
| 6 |
+
name = "linkedin_job_search"
|
| 7 |
+
description = "Searches for job postings on LinkedIn based on job title, location, and work mode (remote, hybrid, in-office)."
|
| 8 |
+
|
| 9 |
+
inputs = {
|
| 10 |
+
"position": {"type": "string", "description": "Job title (e.g., Data Scientist)"},
|
| 11 |
+
"location": {"type": "string", "description": "City or country (e.g., Germany)"},
|
| 12 |
+
"work_mode": {"type": "string", "description": "remote, hybrid, in-office"}
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
output_type = "array"
|
| 16 |
+
|
| 17 |
+
def forward(self, position: str, location: str, work_mode: str) -> List[Dict]:
|
| 18 |
+
"""
|
| 19 |
+
Fetches job listings from LinkedIn using SerpAPI and returns structured JSON.
|
| 20 |
+
"""
|
| 21 |
+
SERPAPI_KEY = "YOUR-API-KEY" # Replace with actual key
|
| 22 |
+
base_url = "https://serpapi.com/search"
|
| 23 |
+
|
| 24 |
+
params = {
|
| 25 |
+
"engine": "google_jobs",
|
| 26 |
+
"q": f"{position} {work_mode} jobs",
|
| 27 |
+
"location": location,
|
| 28 |
+
"hl": "en",
|
| 29 |
+
"api_key": SERPAPI_KEY
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
response = requests.get(base_url, params=params)
|
| 34 |
+
response.raise_for_status()
|
| 35 |
+
|
| 36 |
+
data = response.json()
|
| 37 |
+
job_results = data.get("jobs_results", [])
|
| 38 |
+
|
| 39 |
+
# ✅ Properly format job URLs
|
| 40 |
+
return [
|
| 41 |
+
{
|
| 42 |
+
"Title": job["title"],
|
| 43 |
+
"Company": job.get("company_name", "N/A"),
|
| 44 |
+
"Location": job.get("location", "N/A"),
|
| 45 |
+
"Posted": job.get("detected_extensions", {}).get("posted_at", "N/A"),
|
| 46 |
+
"Link": f"https://www.linkedin.com/jobs/view/{job['job_id']}" if "job_id" in job else "N/A"
|
| 47 |
+
}
|
| 48 |
+
for job in job_results
|
| 49 |
+
] if job_results else [{"Error": "No jobs found. Try different keywords."}]
|
| 50 |
+
|
| 51 |
+
except requests.exceptions.RequestException as e:
|
| 52 |
+
return [{"Error": f"Error fetching job listings: {str(e)}"}]
|