Files

174 lines
7.0 KiB
Python

"""Greenhouse: `greenhouse:<board token>`, 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 json
import re
from collections.abc import Callable
from typing import Any
from ._contract import Ask, Fields, Place, Reads, Row
from ._shared import add_place, html_text, num, only, parse_dt, path_slug_ok, pay_line, place
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_-]+)",
)
READS = Reads(
scope="board", listing="whole", text="listing", complete="whole",
places="many",
fields=Fields(pay="figures", posted="trusted"),
robots=("read: boards-api.greenhouse.io's robots.txt disallows /embed/ only, so "
"/v1/boards/<slug>/jobs is allowed (checked 2026-09-30)"),
)
_API = "boards-api.greenhouse.io"
#: What a board writes as `location.name` when it means a way of working, not a
#: place (Cloudflare: "Hybrid", "In-Office", "Distributed; Hybrid"). The real
#: places are then in `offices`.
_PLACEHOLDER = re.compile(
r"(?:(?:hybrid|in[- ]?office|on[- ]?site|remote|distributed|flexible|office)"
r"(?:\s+or\s+|\s*[;/,&]\s*|\s+)?)+", re.I)
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, ask: Ask) -> 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 _places(j: dict[str, Any]) -> list[Place]:
"""The role's places: each of `offices` (present with `content=true`), in
the host's words, or, when there is none, `location.name` as stated. A
`location.name` that only names a way of working ("Hybrid") is not a
place: the offices replace it."""
name = ((j.get("location") or {}).get("name") or "").strip()
out: list[Place] = []
for office in j.get("offices") or []:
text = str((office or {}).get("location") or (office or {}).get("name") or "").strip()
add_place(out, place(text))
if not out or not name or _PLACEHOLDER.fullmatch(name):
return out or ([place(name)] if name else [])
# A name that says something the offices do not (a "Remote India" beside
# an office in Bangalore) is one more place; one they already contain,
# or that contains them, is the same place said twice.
low = name.lower()
if any(p["text"].lower() in low or low in p["text"].lower() for p in out):
return out
return [place(name), *out]
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()
departments = j.get("departments") or []
ranges = j.get("pay_input_ranges") or []
salary_min, salary_max, currency = _salary(ranges)
places = _places(j)
rows.append({
"source_id": str(j.get("id") or ""),
"title": title,
"places": places,
"location": "; ".join(p["text"] for p in places),
"url": url,
"posted": parse_dt(j.get("first_published") or j.get("updated_at")),
"department": (departments[0].get("name") if departments else "") or "",
# Greenhouse ships the description HTML-entity-escaped inside the
# JSON string (`&lt;p&gt;`), which `html_text` reads as markup.
"description": html_text(j.get("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