"""Eightfold: `eightfold:/`, for example `eightfold:infineon.eightfold.ai/infineon.com` or, for a career site on the company's own host, `eightfold:jobs.ericsson.com/ericsson.com`. The public PCSX search, paged by offset at ten roles a page with the total stated, and a role's text on its own request. Written from Ericsson's site on 2026-09-29 (637 roles), with Infineon's and Microsoft's read the same day in the findings. Unverified on a tenant other than those three: that the search takes the same parameters and answers with the same fields. The `domain` is the tenant's own name for the company, usually the company's domain, and it is not the host: the site's page carries it as `domain=` in its links. `/api/apply/v2/jobs` answers 403 "Not authorized for PCSX" on the sites tried, so it is not used. The search ignores `num` and answers ten roles a page, so a board costs one request per ten roles, and `ats.MAX_PAGES` (100) stops a read at 1,000 roles: a larger board (Infineon's 1,292, Microsoft's 2,319) is read incomplete every pull, and an incomplete read never closes the roles it did not list. The rows are sorted newest first (`sort_by=timestamp`) so that what the cap keeps is the newest thousand and the pages do not shuffle between two requests, unlike the default order, which puts hot roles first. The search does take a `location` free text (Ericsson's "Sweden" found 157 of 637), which could bring a read under the cap, but a target names no filter and none is built here. Discovery names Eightfold from an `eightfold.ai` link, and the host and the domain from one that carries `domain=`. """ from __future__ import annotations import html import json import re from collections.abc import Callable from typing import Any from desk.sources import strip_html from ._contract import Ask, Fields, Place, Reads, Row from ._shared import Get, Page, is_web_url, only, parse_dt READS = Reads( scope="board", listing="paged", page_size=10, text="detail", complete="stated", places="many", fields=Fields(posted="listed", remote="stated"), robots="read: robots.txt allows /careers (checked 2026-09-29)", ) NAME = "eightfold" LABEL = "Eightfold" PATTERNS = ( # Both parts, from a link to a tenant's site that says its domain. r"https?://([a-z0-9-]+\.eightfold\.ai)/[^\s\"'<>]*?[?&]domain=([a-z0-9.-]+\.[a-z]{2,})", # No slug: a link to a tenant's site that does not. r"[a-z0-9-]+\.eightfold\.ai", ) #: A whole host name: two labels or more, the last one letters, so neither an #: address, a bare word nor a port names a site. `ats._get` refuses every host #: but the tenant's, redirects included. _NAME = re.compile(r"[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}") #: Names a private network uses, never a company's career site. _LOCAL = (".localhost", ".local", ".internal", ".lan", ".home.arpa") #: `workLocationOption` -> `store._remote`'s words. Only "onsite" and "hybrid" #: were seen; the other spellings are the obvious ones. _REMOTE = {"onsite": "onsite", "on_site": "onsite", "hybrid": "hybrid", "remote": "remote"} def parts(slug: str) -> tuple[str, str]: """`(host, domain)` of a slug, lowercased.""" host, _, domain = slug.lower().partition("/") return host, domain def _name_ok(name: str) -> bool: return bool(_NAME.fullmatch(name)) and not name.endswith(_LOCAL) def slug_ok(slug: str) -> bool: host, sep, domain = slug.lower().partition("/") return bool(sep) and _name_ok(host) and _name_ok(domain) def allowed(slug: str) -> Callable[[str], bool]: return only(parts(slug)[0]) def careers_url(slug: str) -> str: return f"https://{parts(slug)[0]}/careers" def page(get: Get, slug: str, cursor: Any, ask: Ask) -> Page: """One page of `/api/pcsx/search` -> `{"status": 200, "data": {"positions": [...], "count": n}}`; the next starts at the offset after this page's positions, and an empty page ends the read.""" host, domain = parts(slug) start = int(cursor or 0) answer = json.loads(get(f"https://{host}/api/pcsx/search?domain={domain}" f"&sort_by=timestamp&start={start}")) data = answer.get("data") if isinstance(answer, dict) else None positions = data.get("positions") if isinstance(data, dict) else None if not isinstance(positions, list): raise ValueError("not an Eightfold search answer: no data.positions" + (f" (status {answer.get('status')})" if isinstance(answer, dict) else "")) count = data.get("count") results = [{"id": str(p.get("id") or "")} for p in positions] return (positions, results, count if isinstance(count, int) else None, start + len(positions) if positions else None) def _places(locations: Any) -> list[Place]: """The role's places. Eightfold writes each as "city,region,country" with no space and repeats the name when a city is its own region ("Mexico City,Mexico City,Mexico"), so the repeats go.""" out: list[Place] = [] for loc in locations if isinstance(locations, list) else []: names: list[str] = [] for name in str(loc).split(","): name = name.strip() if name and name not in names: names.append(name) text = ", ".join(names) if text and text not in (p["text"] for p in out): out.append({"text": text, "remote": True if "remote" in text.lower() else None}) return out def rows(positions: list[dict[str, Any]], slug: str) -> list[Row]: """A position has `id` (a number, kept as a string), `displayJobId`, `name`, `locations`, `creationTs` (epoch seconds; `postedTs` is refreshed in bulk), `department`, `workLocationOption` and `positionUrl`, the path of the role's page on the tenant's host. The list has no text, and no pay is stated anywhere.""" host = parts(slug)[0] out = [] for p in positions: title = (p.get("name") or "").strip() source_id = str(p.get("id") or "") path = str(p.get("positionUrl") or "").strip() url = f"https://{host}{path}" if path.startswith("/") and path[1:2] != "/" else "" if not title or not source_id or not is_web_url(url): continue places = _places(p.get("locations") or p.get("standardizedLocations")) out.append({ "source_id": source_id, "title": title, "places": places, "location": "; ".join(pl["text"] for pl in places), "url": url, # `postedTs` is refreshed in bulk (a whole page says today); # `creationTs` is the posting date. "posted": parse_dt(p.get("creationTs") or p.get("postedTs")), "department": str(p.get("department") or "").strip(), "description": "", "remote": _REMOTE.get(str(p.get("workLocationOption") or "").strip().lower()), "salary_text": "", }) return out def detail(get: Get, slug: str, row: Row) -> dict[str, Any]: """`/api/pcsx/position_details` of the role -> `data.jobDescription`, the HTML text of the ad. The same answer repeats the listing's fields, and `publicUrl`, none of which is needed.""" host, domain = parts(slug) role = str(row["source_id"]) if not role.isdigit(): raise ValueError(f"not an Eightfold position id: {role!r}") answer = json.loads(get(f"https://{host}/api/pcsx/position_details" f"?position_id={role}&domain={domain}&hl=en")) data = answer.get("data") if isinstance(answer, dict) else None if not isinstance(data, dict) or "jobDescription" not in data: raise ValueError("not an Eightfold position: no data.jobDescription") text = html.unescape(strip_html(data["jobDescription"] or "")) return {"description": " ".join(text.split())}