Spaces:
Sleeping
Sleeping
Create backend/agents/remoteok_agent.py
Browse files
backend/agents/remoteok_agent.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from .normalizer import normalize_job_data
|
| 3 |
+
|
| 4 |
+
def fetch_remoteok_jobs(limit=None):
|
| 5 |
+
"""
|
| 6 |
+
Fetches job listings from the RemoteOK API and normalizes them.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
limit (int, optional): The maximum number of jobs to return. Defaults to None.
|
| 10 |
+
|
| 11 |
+
Returns:
|
| 12 |
+
list: A list of dictionaries, where each dictionary represents a normalized job posting.
|
| 13 |
+
Returns an empty list if the API call fails.
|
| 14 |
+
"""
|
| 15 |
+
url = "https://remoteok.com/api"
|
| 16 |
+
try:
|
| 17 |
+
response = requests.get(url)
|
| 18 |
+
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
|
| 19 |
+
jobs = response.json()
|
| 20 |
+
|
| 21 |
+
# The first element is often a header/legal notice, so we skip it.
|
| 22 |
+
if isinstance(jobs, list) and len(jobs) > 1:
|
| 23 |
+
valid_jobs = jobs[1:]
|
| 24 |
+
normalized_jobs = [normalize_job_data(job, "RemoteOK") for job in valid_jobs]
|
| 25 |
+
|
| 26 |
+
if limit:
|
| 27 |
+
return normalized_jobs[:limit]
|
| 28 |
+
return normalized_jobs
|
| 29 |
+
else:
|
| 30 |
+
print("RemoteOK API returned an unexpected format.")
|
| 31 |
+
return []
|
| 32 |
+
|
| 33 |
+
except requests.exceptions.RequestException as e:
|
| 34 |
+
print(f"Error fetching jobs from RemoteOK: {e}")
|
| 35 |
+
return []
|
| 36 |
+
except ValueError as e:
|
| 37 |
+
print(f"Error parsing JSON from RemoteOK: {e}")
|
| 38 |
+
return []
|
| 39 |
+
|
| 40 |
+
if __name__ == '__main__':
|
| 41 |
+
# Example usage for testing
|
| 42 |
+
remote_jobs = fetch_remoteok_jobs(limit=5)
|
| 43 |
+
if remote_jobs:
|
| 44 |
+
print(f"Successfully fetched {len(remote_jobs)} jobs from RemoteOK.")
|
| 45 |
+
for job in remote_jobs:
|
| 46 |
+
print(f"- {job['title']} at {job['company']}")
|
| 47 |
+
else:
|
| 48 |
+
print("Failed to fetch jobs from RemoteOK.")
|