| 1 | """Jibe (an iCIMS front end): the careers site's own ``/api/jobs`` search. |
| 2 | |
| 3 | One call returns up to 100 roles, newest first, each with its full text and |
| 4 | the day it was posted, and the board's total. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | from typing import Any, Callable |
| 10 | from urllib.parse import quote |
| 11 | |
| 12 | from .. import http, jobs |
| 13 | from . import detect |
| 14 | from .detect import PROVIDER_JIBE |
| 15 | |
| 16 | PAGE_SIZE = 100 |
| 17 | |
| 18 | |
| 19 | def listing_url(entry: dict[str, Any], page: int = 1, keywords: str = "") -> str: |
| 20 | """One listing call; ``keywords`` makes it a search of the board.""" |
| 21 | return ( |
| 22 | f"https://{entry['board']}/api/jobs?page={page}&limit={PAGE_SIZE}" |
| 23 | "&sortBy=posted_date&descending=true&internal=false" |
| 24 | + (f"&keywords={quote(keywords)}" if keywords else "") |
| 25 | ) |
| 26 | |
| 27 | |
| 28 | def parse_listing(payload: Any, entry: dict[str, Any]) -> tuple[list[dict[str, Any]], int]: |
| 29 | rows = payload.get("jobs") if isinstance(payload, dict) else None |
| 30 | if not isinstance(rows, list): |
| 31 | raise jobs.BoardShapeError("no jobs list") |
| 32 | board = entry["board"] |
| 33 | raw_jobs_path = str(entry.get("jobs_path") or "/jobs").strip() |
| 34 | if ( |
| 35 | not raw_jobs_path.startswith("/") |
| 36 | or raw_jobs_path.startswith("//") |
| 37 | or "\\" in raw_jobs_path |
| 38 | or any(ch.isspace() or ord(ch) < 0x20 or ord(ch) == 0x7F for ch in raw_jobs_path) |
| 39 | ): |
| 40 | raw_jobs_path = "/jobs" |
| 41 | jobs_path = raw_jobs_path.rstrip("/") |
| 42 | items: list[dict[str, Any]] = [] |
| 43 | for row in rows: |
| 44 | data = row.get("data") if isinstance(row, dict) else None |
| 45 | if not isinstance(data, dict) or not data.get("title"): |
| 46 | continue |
| 47 | ident = str(data.get("req_id") or data.get("slug") or "").strip() |
| 48 | if not ident: |
| 49 | continue |
| 50 | html = "<br/>".join( |
| 51 | str(data.get(key) or "") |
| 52 | for key in ("description", "responsibilities", "qualifications") if data.get(key) |
| 53 | ) |
| 54 | posted = jobs._date_part(data.get("posted_date")) |
| 55 | categories = [ |
| 56 | str(category.get("name") or "").strip() |
| 57 | for category in data.get("categories") or [] if isinstance(category, dict) |
| 58 | ] |
| 59 | role_slug = quote(str(data.get("slug") or ident).strip(), safe="") |
| 60 | items.append(jobs._ats_item( |
| 61 | provider=PROVIDER_JIBE, |
| 62 | slug=board, |
| 63 | ident=ident, |
| 64 | title=str(data["title"]).strip(), |
| 65 | url=f"https://{board}{jobs_path}/{role_slug}", |
| 66 | description=jobs._clean_html(html), |
| 67 | description_blocks=jobs._block_text(html), |
| 68 | date=posted, |
| 69 | posted_date_true=bool(posted), |
| 70 | department=str(data.get("department") or "").strip() or next( |
| 71 | (category for category in categories if category), "", |
| 72 | ), |
| 73 | location=str(data.get("full_location") or data.get("location_name") or "").strip(), |
| 74 | company_name=str(data.get("hiring_organization") or "").strip(), |
| 75 | whole_posting=True, |
| 76 | )) |
| 77 | return items, jobs._reported_total(payload, "totalCount") |
| 78 | |
| 79 | |
| 80 | def read_board( |
| 81 | entry: dict[str, Any], *, deadline: float, date_range: tuple[str, str], |
| 82 | ) -> tuple[list[dict[str, Any]], dict[str, Any]]: |
| 83 | """The board's roles, newest first, with the total it reports.""" |
| 84 | |
| 85 | def page(offset: int) -> Callable[[float], Any]: |
| 86 | url = listing_url(entry, offset // PAGE_SIZE + 1) |
| 87 | |
| 88 | def fetch(http_deadline: float) -> Any: |
| 89 | if not detect.is_safe_public_url(url): |
| 90 | raise http.HTTPError("Refused non-public URL") |
| 91 | return http.get( |
| 92 | url, headers={"Accept": "application/json"}, |
| 93 | timeout=jobs.API_TIMEOUT_SECONDS, retries=2, deadline_monotonic=http_deadline, |
| 94 | public_only=True, redirect_guard=detect.same_site_redirect_guard(url), |
| 95 | ) |
| 96 | |
| 97 | return fetch |
| 98 | |
| 99 | return jobs._read_newest_first( |
| 100 | page, lambda payload: parse_listing(payload, entry), page_size=PAGE_SIZE, |
| 101 | row_ceiling=jobs.COMPANY_BOARD_ROW_CEILING, window_start=date_range[0], |
| 102 | deadline=deadline, |
| 103 | ) |
| 104 | |
| 105 | |
| 106 | def search_story( |
| 107 | entry: dict[str, Any], term: str, *, deadline_monotonic: float, |
| 108 | ) -> list[dict[str, Any]]: |
| 109 | """The board's roles the site's own search finds for a story term.""" |
| 110 | url = listing_url(entry, 1, term) |
| 111 | if not detect.is_safe_public_url(url): |
| 112 | raise http.HTTPError("Refused non-public URL") |
| 113 | payload = http.get( |
| 114 | url, headers={"Accept": "application/json"}, |
| 115 | timeout=jobs.STORY_TIMEOUT_SECONDS, retries=1, deadline_monotonic=deadline_monotonic, |
| 116 | public_only=True, redirect_guard=detect.same_site_redirect_guard(url), |
| 117 | ) |
| 118 | return parse_listing(payload, entry)[0] |
| 119 |