Files
jobboard/plugins/atshosts/ashby.py
T

123 lines
4.5 KiB
Python

"""Ashby: `ashby:<job board name>`, the whole board in one JSON answer. A
board name may carry a dot (`jimdo.com`) or be a uuid.
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 = "ashby"
LABEL = "Ashby"
RANK = 30
PATTERNS = (
r"jobs\.ashbyhq\.com/([A-Za-z0-9_.-]+)",
r"api\.ashbyhq\.com/posting-api/job-board/([A-Za-z0-9_.-]+)",
)
_API = "api.ashbyhq.com"
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://jobs.ashbyhq.com/{slug}"
def feed_url(slug: str) -> str:
return f"https://{_API}/posting-api/job-board/{slug}?includeCompensation=true"
def probe_url(slug: str) -> str:
return f"https://{_API}/posting-api/job-board/{slug}"
def probe_ok(body: bytes) -> bool:
"""Ashby answers an empty listing for an unknown name, so a listing
without jobs counts as no board."""
return b'"jobs":[' in body and b'"jobs":[]' not in body
def _salary(components: list[dict[str, Any]]) -> tuple[int | None, int | None, str]:
"""The yearly range from `compensation.summaryComponents` (needs
`?includeCompensation=true` on the feed URL, see `feed_url`): the first
`Salary` component, yearly or monthly (a monthly one multiplied by 12);
an hourly one is left out rather than annualised."""
for component in components:
if component.get("compensationType") != "Salary":
continue
return yearly_amount(component.get("minValue"), component.get("maxValue"),
component.get("currencyCode"), component.get("interval"),
{"1 YEAR": 1, "1 MONTH": 12})
return None, None, ""
def _salary_text(compensation: dict[str, Any]) -> str:
"""One line per `summaryComponents` entry that states a bound, labelled
by the board's own `compensationType` when more than one does (a salary
range beside a bonus range, say); with just the one, the label is left
off, the same as Greenhouse's. `interval` is "<n> <PERIOD>" ("1 YEAR",
"1 HOUR"); the last word, lowered, is the period. With nothing to build
a line from, Ashby's own prose summary (`compensationTierSummary`, else
`scrapeableCompensationSalarySummary`) is what is left to show."""
components = compensation.get("summaryComponents") or []
stated = [c for c in components if num(c.get("minValue")) is not None
or num(c.get("maxValue")) is not None]
lines = []
for component in stated:
period = str(component.get("interval") or "").split(" ")[-1].lower()
line = pay_line(num(component.get("minValue")), num(component.get("maxValue")),
component.get("currencyCode"), period)
if not line:
continue
if len(stated) > 1:
label = str(component.get("compensationType") or "").strip()
line = f"{label}: {line}" if label else line
lines.append(line)
if lines:
return "; ".join(lines)
return str(compensation.get("compensationTierSummary")
or compensation.get("scrapeableCompensationSalarySummary") or "")
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("jobUrl") or j.get("applyUrl") or "").strip()
if not title or not is_web_url(url):
continue
description = j.get("descriptionHtml") or j.get("descriptionPlain") or ""
compensation = j.get("compensation") or {}
salary_min, salary_max, currency = _salary(compensation.get("summaryComponents") or [])
rows.append({
"source_id": str(j.get("id") or ""),
"title": title,
"location": (j.get("location") or "").strip(),
"url": url,
"posted": parse_dt(j.get("publishedAt")),
"department": (j.get("department") or j.get("team") or "").strip(),
"description": strip_html(description),
"remote": j.get("isRemote"),
"salary_text": _salary_text(compensation),
"salary_min": salary_min,
"salary_max": salary_max,
"currency": currency,
})
return rows