- indeed refactor

- wanted_results init
pull/12/head
zacharyhampton 2023-07-10 17:43:45 -05:00 committed by Cullen Watson
parent 6f447bcd2c
commit eba455115c
3 changed files with 43 additions and 18 deletions

View File

@ -58,8 +58,12 @@ class JobPost(BaseModel):
class JobResponse(BaseModel): class JobResponse(BaseModel):
job_count: int success: bool
page: int = 1 error: str = None
total_pages: int
total_pages: int = None
job_count: int = None
page: int = None
jobs: list[JobPost] = []
jobs: list[JobPost]

View File

@ -1,6 +1,6 @@
from pydantic import BaseModel from pydantic import BaseModel
from enum import Enum from enum import Enum
from ..jobs import JobResponse from ..jobs import JobResponse, JobPost
class Site(Enum): class Site(Enum):
@ -13,13 +13,11 @@ class ScraperInput(BaseModel):
location: str location: str
search_term: str search_term: str
distance: int = 25 distance: int = 25
results_wanted: int = 15 #: TODO: implement
page: int = 1
class Scraper: #: to be used as a child class class Scraper: #: to be used as a child class
def __init__(self, site: Site): def __init__(self, site: Site):
self.site = site self.site = site
def scrape(self, scraper_input: ScraperInput) -> JobResponse: def scrape(self, scraper_input: ScraperInput) -> JobResponse: ...
...

View File

@ -10,6 +10,10 @@ from api.core.jobs import *
from api.core.scrapers import Scraper, ScraperInput, Site from api.core.scrapers import Scraper, ScraperInput, Site
class ParsingException(Exception):
pass
class IndeedScraper(Scraper): class IndeedScraper(Scraper):
def __init__(self): def __init__(self):
site = Site(Site.INDEED) site = Site(Site.INDEED)
@ -25,7 +29,7 @@ class IndeedScraper(Scraper):
"q": scraper_input.search_term, "q": scraper_input.search_term,
"l": scraper_input.location, "l": scraper_input.location,
"filter": 0, "filter": 0,
"start": 0 if scraper_input.page is None else (scraper_input.page - 1) * 10, "start": 0,
"radius": scraper_input.distance, "radius": scraper_input.distance,
} }
@ -38,12 +42,25 @@ class IndeedScraper(Scraper):
soup = BeautifulSoup(response.content, "html.parser") soup = BeautifulSoup(response.content, "html.parser")
try:
jobs = IndeedScraper.parse_jobs(soup) jobs = IndeedScraper.parse_jobs(soup)
except ParsingException:
return JobResponse(
success=False,
error="Failed to parse jobs.",
)
total_num_jobs = IndeedScraper.total_jobs(soup) total_num_jobs = IndeedScraper.total_jobs(soup)
total_pages = ceil(total_num_jobs / 15) total_pages = ceil(total_num_jobs / 15)
job_list: list[JobPost] = [] job_list: list[JobPost] = []
# page_number = jobs["metaData"]["mosaicProviderJobCardsModel"]["pageNumber"] if not jobs.get('metaData', {}).get("mosaicProviderJobCardsModel", {}).get("results"):
return JobResponse(
success=False,
error="No jobs found",
)
page_number = jobs["metaData"]["mosaicProviderJobCardsModel"]["pageNumber"]
for job in jobs["metaData"]["mosaicProviderJobCardsModel"]["results"]: for job in jobs["metaData"]["mosaicProviderJobCardsModel"]["results"]:
snippet_html = BeautifulSoup(job["snippet"], "html.parser") snippet_html = BeautifulSoup(job["snippet"], "html.parser")
@ -94,9 +111,10 @@ class IndeedScraper(Scraper):
job_list.append(job_post) job_list.append(job_post)
job_response = JobResponse( job_response = JobResponse(
success=True,
jobs=job_list, jobs=job_list,
job_count=total_num_jobs, job_count=total_num_jobs,
page=scraper_input.page, page=page_number,
total_pages=total_pages, total_pages=total_pages,
) )
return job_response return job_response
@ -116,7 +134,14 @@ class IndeedScraper(Scraper):
return None return None
@staticmethod @staticmethod
def parse_jobs(soup): def parse_jobs(soup: BeautifulSoup) -> dict:
"""
Parses the jobs from the soup object
:param soup:
:return: jobs
"""
script_tag = IndeedScraper.find_mosaic_script(soup) script_tag = IndeedScraper.find_mosaic_script(soup)
if script_tag: if script_tag:
@ -130,11 +155,9 @@ class IndeedScraper(Scraper):
jobs = json.loads(m.group(1).strip()) jobs = json.loads(m.group(1).strip())
return jobs return jobs
else: else:
return {"message": f"Could not find mosaic provider job cards data"} raise ParsingException("Could not find mosaic provider job cards data")
else: else:
return { raise ParsingException("Could not find a script tag containing mosaic provider data")
"message": f"Could not find a script tag containing mosaic provider data"
}
@staticmethod @staticmethod
def total_jobs(soup): def total_jobs(soup):