"""Lever: `lever:`, or `lever:eu/` for a board on Lever's EU instance, the whole board in one JSON answer. Lever runs two instances that share nothing but the API: `api.lever.co` and `api.eu.lever.co`, with the pages on `jobs.lever.co` and `jobs.eu.lever.co`. A site exists on one of them only (TomTom and Prima answer 404 on the other, 2026-09-29), and the same name on both would be two companies, so the region is part of the slug. It is a prefix a plain site name cannot carry, since a Lever site name has no slash, which leaves every `lever:` target as it was. The field map is ported from `openings.go` in [jobhunt-hq](https://github.com/rembrandtreyes/jobhunt-hq) (MIT). """ from __future__ import annotations 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, yearly_amount NAME = "lever" LABEL = "Lever" RANK = 20 #: Two groups on the EU hosts, `eu` and the site, which discovery joins into #: the slug `eu/`. PATTERNS = ( r"jobs\.lever\.co/([A-Za-z0-9_-]+)", r"api\.lever\.co/v0/postings/([A-Za-z0-9_-]+)", r"jobs\.(eu)\.lever\.co/([A-Za-z0-9_-]+)", r"api\.(eu)\.lever\.co/v0/postings/([A-Za-z0-9_-]+)", ) #: The region prefixes a slug may carry, to the infix of both host names. _REGIONS = {"eu": "eu."} def _split(slug: str) -> tuple[str, str]: """`(host infix, site)`: `("eu.", "tomtom")` for `eu/tomtom`, `("", "acme")` for `acme`.""" region, sep, site = slug.partition("/") return (_REGIONS.get(region, "?"), site) if sep else ("", slug) def slug_ok(slug: str) -> bool: infix, site = _split(slug) return infix != "?" and path_slug_ok(site) def allowed(slug: str) -> Callable[[str], bool]: return only(f"api.{_split(slug)[0]}lever.co") def careers_url(slug: str) -> str: infix, site = _split(slug) return f"https://jobs.{infix}lever.co/{site}" def feed_url(slug: str) -> str: infix, site = _split(slug) return f"https://api.{infix}lever.co/v0/postings/{site}?mode=json" def probe_url(slug: str) -> str: infix, site = _split(slug) return f"https://api.{infix}lever.co/v0/postings/{site}?limit=1&mode=json" def probe_slugs(guess: str) -> tuple[str, ...]: """A name is tried on both instances, the US one first, since a site that names neither leaves no other way to tell which one it is on.""" return (guess, *(f"{region}/{guess}" for region in _REGIONS)) def _salary_text(salary_range: dict[str, Any]) -> str: """One line from `salaryRange`, whatever its `interval` — including an hourly or daily one, which `yearly_amount` drops from the columns. Lever's interval is "per--" ("per-year-salary", "per-hour-wage"); the middle word is the period.""" interval = str(salary_range.get("interval") or "") parts = interval.split("-") period = parts[1] if len(parts) >= 3 else "" return pay_line(num(salary_range.get("min")), num(salary_range.get("max")), salary_range.get("currency"), period) def parse(body: bytes, slug: str) -> list[Row]: data = json.loads(body) if not isinstance(data, list): return [] rows = [] for j in data: title = (j.get("text") or "").strip() url = (j.get("hostedUrl") or "").strip() if not title or not is_web_url(url): continue categories = j.get("categories") or {} description = j.get("description") or j.get("descriptionPlain") or "" salary_range = j.get("salaryRange") or {} salary_min, salary_max, currency = yearly_amount( salary_range.get("min"), salary_range.get("max"), salary_range.get("currency"), salary_range.get("interval"), {"per-year-salary": 1, "per-month-salary": 12}) rows.append({ "source_id": str(j.get("id") or ""), "title": title, "location": (categories.get("location") or "").strip(), "url": url, "posted": parse_dt(j.get("createdAt")), "department": (categories.get("department") or categories.get("team") or "").strip(), "description": strip_html(description), "remote": (j.get("workplaceType") or "").strip() or None, "salary_text": _salary_text(salary_range), "salary_min": salary_min, "salary_max": salary_max, "currency": currency, }) return rows