"""Greenhouse: `greenhouse:`, the whole board in one JSON answer. The field map is ported from `openings.go` in [jobhunt-hq](https://github.com/rembrandtreyes/jobhunt-hq) (MIT). """ from __future__ import annotations import html import json from collections.abc import Callable from typing import Any from desk.sources import strip_html from ._shared import Row, is_web_url, num, only, parse_dt, path_slug_ok, pay_line NAME = "greenhouse" LABEL = "Greenhouse" RANK = 10 PATTERNS = ( r"(?:boards|job-boards|api)\.(?:eu\.)?greenhouse\.io/(?:v1/boards/|embed/job_board\?[^\"'\s]*for=)?([A-Za-z0-9_-]+)", r"greenhouse\.io/embed/job_board/js\?for=([A-Za-z0-9_-]+)", ) _API = "boards-api.greenhouse.io" def slug_ok(slug: str) -> bool: return path_slug_ok(slug) def allowed(slug: str) -> Callable[[str], bool]: return only(_API) def careers_url(slug: str) -> str: return f"https://job-boards.greenhouse.io/{slug}" def feed_url(slug: str) -> str: return f"https://{_API}/v1/boards/{slug}/jobs?content=true&pay_transparency=true" def fallback_url(slug: str) -> str: """The feed without `content=true`, read once when the full one exceeds the byte cap: the descriptions are lost, but the titles land rather than the whole board being skipped.""" return f"https://{_API}/v1/boards/{slug}/jobs" def probe_url(slug: str) -> str: return f"https://{_API}/v1/boards/{slug}" def _salary(ranges: list[dict[str, Any]]) -> tuple[int | None, int | None, str]: """The yearly range from `pay_input_ranges` (needs `pay_transparency=true` on the feed URL, see `feed_url`), which the list endpoint already carries in the same request — no per-role fetch. Only when there is exactly one range: a board with several ("Zone 1 Pay Range", "US Tier 2 Locations", one per office) gives no way to tell which office the role in hand is at, and picking one would misrepresent the columns. Greenhouse's own ranges are cents, and yearly by convention except an hourly or a daily one, named as such in `title` with no separate field to check — those are skipped rather than annualised into a number nobody stated.""" if len(ranges) != 1: return None, None, "" entry = ranges[0] title = str(entry.get("title") or "").lower() if any(word in title for word in ("hour", "/hr", "daily", "per day", "day rate")): return None, None, "" code = str(entry.get("currency_type") or "").strip().upper() if not code: return None, None, "" lo, hi = num(entry.get("min_cents")), num(entry.get("max_cents")) if lo is None and hi is None: return None, None, "" return (int(lo / 100) if lo is not None else None, int(hi / 100) if hi is not None else None, code) def _salary_text(ranges: list[dict[str, Any]]) -> str: """One line per `pay_input_ranges` entry, joined with "; " — every range Greenhouse states, not only the single one `_salary` accepts for the columns. With more than one, each is labelled with the board's own `title` ("Zone 1 Pay Range: ..."), since that is the only way to tell the ranges apart; with just the one, the label adds nothing and is left off. The hour/day check is the same one `_salary` uses to keep those out of the yearly columns; here it only picks the word that follows "per".""" multi = len(ranges) > 1 lines = [] for entry in ranges: title = str(entry.get("title") or "").strip() low_title = title.lower() period = ("hour" if any(w in low_title for w in ("hour", "/hr")) else "day" if "day" in low_title else "year") lo, hi = num(entry.get("min_cents")), num(entry.get("max_cents")) line = pay_line(lo / 100 if lo is not None else None, hi / 100 if hi is not None else None, entry.get("currency_type"), period) if not line: continue if multi: label = title.rstrip(":").strip() or "Range" line = f"{label}: {line}" lines.append(line) return "; ".join(lines) def parse(body: bytes, slug: str) -> list[Row]: data = json.loads(body) rows = [] for j in data.get("jobs") or []: title = (j.get("title") or "").strip() url = (j.get("absolute_url") or "").strip() if not title or not is_web_url(url): continue departments = j.get("departments") or [] # Greenhouse ships the description HTML-entity-escaped inside the # JSON string (`<p>`), so it needs unescaping before the tags # themselves can be stripped. content = html.unescape(j.get("content") or "") ranges = j.get("pay_input_ranges") or [] salary_min, salary_max, currency = _salary(ranges) rows.append({ "source_id": str(j.get("id") or ""), "title": title, "location": ((j.get("location") or {}).get("name") or "").strip(), "url": url, "posted": parse_dt(j.get("first_published") or j.get("updated_at")), "department": (departments[0].get("name") if departments else "") or "", "description": strip_html(content), "remote": None, # Greenhouse's feed does not say "salary_text": _salary_text(ranges), "salary_min": salary_min, "salary_max": salary_max, "currency": currency, }) return rows