"""Workday: `workday:./`, for example `workday:lego.wd103/LEGO_External` (the career site https://lego.wd103.myworkdayjobs.com/LEGO_External), paged by offset with a total that only the first page states. The tenant is what the company was named when Workday was set up and is not always the company (`unitytech`, `novonesis`); the data centre is not derivable either, and a tenant has several sites, so all three are stored as they are found. The site is case-insensitive for the API (Red Hat's `jobs` and `Jobs` both work) but its robots.txt paths are not, so the slug keeps the case the URL wrote (`KEEP_CASE`). Written from live career sites on 2026-09-29 (`docs/plan-hosts/workday.md`). The listing carries no description, so `detail` reads each role's own JSON. Countries and remote are filtered on the server by the tenant's own facets (`Filters(country="ids", remote="ids")`): `appliedFacets` takes `{: [, ...]}`, ids within one facet are a union, and two facets are an intersection, which is why the runner asks for remote in a series of its own. Each tenant names its facets its own way (LEGO's `Location` lists countries, others nest `locationCountry` under `locationMainGroup`), so `facets` reads them off the first page and picks the one whose values are countries. A host id is `=`. LEGO answered 97 of 455 for Denmark on 2026-09-30; a role at several places is kept when one of them is in the country. """ from __future__ import annotations import html import json import logging import re from collections.abc import Callable from datetime import UTC, datetime, timedelta from typing import Any from desk.sources import strip_html from ._contract import Ask, Fields, Filters, Place, Reads from ._countries import code_of from ._shared import Get, Page, Row, only, parse_dt log = logging.getLogger(__name__) NAME = "workday" LABEL = "Workday" KEEP_CASE = True READS = Reads( scope="board", listing="paged", page_size=20, ceiling=2000, text="detail", complete="stated", places="many", filters=Filters(country="ids", remote="ids"), fields=Fields(posted="trusted", remote="stated", country="stated", employment="stated"), robots="read: robots.txt allows // (checked 2026-09-29)", ) #: One group for `.` and one for the site, which discovery joins #: into the slug with "/". A locale segment (`/en-US/`, `/de-DE/`) may come #: between; it is matched case-sensitively, and a bare `/en-US` that is no #: site at all is refused, so a site is never mistaken for a locale or the #: other way round. `wday/` is the API path, read by the second pattern. The #: tenant is lower case only: a slug with capitals in it is no slug. _TENANT = r"(?-i:[a-z0-9-]+)" _LOCALE = r"(?-i:[a-z]{2}-[A-Z]{2})" PATTERNS = ( rf"(? tuple[str, str, str] | None: """`(tenant, data centre, site)`, or None for a slug that is none.""" match = _SLUG.fullmatch(slug) return match.groups() if match else None # type: ignore[return-value] def _host(slug: str) -> str: tenant, dc, _site = _parts(slug) or ("", "", "") return f"{tenant}.{dc}.myworkdayjobs.com" def slug_ok(slug: str) -> bool: return _parts(slug) is not None def allowed(slug: str) -> Callable[[str], bool]: """Only the tenant's own host. A slug that is none admits nothing, rather than a host the company does not own.""" return only(_host(slug)) if _parts(slug) else (lambda host: False) def careers_url(slug: str) -> str: return f"https://{_host(slug)}/{_parts(slug)[2]}" # type: ignore[index] def page(get: Get, slug: str, cursor: Any, ask: Ask = Ask()) -> Page: """`cursor` is `(offset, total)`. `total` is read on the first page only: on the later ones it is 0 on most tenants and the real figure on a few, so it is carried. An offset at or past the end does not answer an empty list but the first page again, so the read stops on `offset >= total` rather than on an empty page.""" tenant, _dc, site = _parts(slug) # type: ignore[misc] offset, total = cursor or (0, None) data = json.loads(get( f"https://{_host(slug)}/wday/cxs/{tenant}/{site}/jobs", method="POST", payload={"appliedFacets": _applied(ask), "limit": PAGE_SIZE, "offset": offset, "searchText": ""})) if cursor is None: stated = data.get("total") total = stated if isinstance(stated, int) and not isinstance(stated, bool) else None postings = data.get("jobPostings") or [] # Every posting is counted, a path-less one (LEGO lists one that has only # `bulletFields`) by its position, so the parser dropping it does not # leave the board short of its total. results = [{"id": _posting_id(p, offset + i)} for i, p in enumerate(postings)] following = offset + len(postings) more = bool(postings) and (total is None or following < total) return data, results, total, (following, total) if more else None def _applied(ask: Ask) -> dict[str, list[str]]: """`appliedFacets` for the host ids `ask` carries, `=` each, grouped by facet.""" out: dict[str, list[str]] = {} for value in (*ask.countries, *ask.remote_ids): facet, sep, value_id = value.partition("=") if sep and facet and value_id: out.setdefault(facet, []).append(value_id) return out #: A facet whose values are countries: at least this share of them name one. _COUNTRY_SHARE = 0.8 def _facet_lists(facets: Any) -> list[tuple[str, list[dict[str, Any]]]]: """`(facetParameter, values)` of every facet of an answer, the facets nested in a group (`locationMainGroup` holds `locationCountry` on some tenants) included.""" out: list[tuple[str, list[dict[str, Any]]]] = [] for facet in facets if isinstance(facets, list) else []: if not isinstance(facet, dict): continue values = [v for v in facet.get("values") or [] if isinstance(v, dict)] nested = [v for v in values if "values" in v and "facetParameter" in v] out += _facet_lists(nested) plain = [v for v in values if v not in nested] if plain and str(facet.get("facetParameter") or ""): out.append((str(facet["facetParameter"]), plain)) return out def facets(get: Get, slug: str) -> dict[str, dict[str, str]]: """The tenant's host ids, read off one page of one role: `country` maps an ISO code to `=` in the facet whose values are countries (most of them, `_COUNTRY_SHARE`; the one with most countries when two are), and `remote` maps each facet value that `_REMOTE` reads as remote ("Remote", "Fully Flexible"), in whatever facet, to its id. A tenant with neither states no such dimension, and the runner then does not filter on it.""" tenant, _dc, site = _parts(slug) # type: ignore[misc] data = json.loads(get( f"https://{_host(slug)}/wday/cxs/{tenant}/{site}/jobs", method="POST", payload={"appliedFacets": {}, "limit": 1, "offset": 0, "searchText": ""})) country: dict[str, str] = {} remote: dict[str, str] = {} for name, values in _facet_lists(data.get("facets")): found = {code: f"{name}={v['id']}" for v in values if v.get("id") and (code := code_of(str(v.get("descriptor") or "")))} if len(found) >= _COUNTRY_SHARE * len(values) and len(found) > len(country): country = found for v in values: word = " ".join(str(v.get("descriptor") or "").split()) if v.get("id") and _remote(word) == "remote": remote[word.lower()] = f"{name}={v['id']}" out: dict[str, dict[str, str]] = {} if country: out["country"] = country if remote: out["remote"] = remote return out def _posting_id(posting: dict[str, Any], position: int) -> str: return str(posting.get("externalPath") or "".join(posting.get("bulletFields") or []) or f"#{position}") def _posted(text: str) -> datetime | None: """The day of "Posted Today", "Posted Yesterday" and "Posted 5 Days Ago" at midnight UTC; None for "Posted 30+ Days Ago", which says only that it is older, and for a text in another language. Workday counts the days in its own time zone, so the day can be one off; `detail` states the date itself and replaces this one.""" match = re.fullmatch(r"Posted (?:(Today)|(Yesterday)|(\d+) Days? Ago)", (text or "").strip()) if not match: return None days = 0 if match.group(1) else 1 if match.group(2) else int(match.group(3)) return _today() - timedelta(days=days) def _today() -> datetime: return datetime.now(UTC).replace(hour=0, minute=0, second=0, microsecond=0) #: `remoteType` -> `store._remote`'s words. Only some tenants send it, and #: each words it its own way (Zendesk's "Fully Flexible", Axis' "On-Site"); a #: word not listed, a bare "No" among them, says nothing and stays unknown. _REMOTE = {"remote": "remote", "fully flexible": "remote", "hybrid": "hybrid", "in office": "onsite", "on-site": "onsite", "onsite": "onsite"} def _requisition(posting: dict[str, Any], path: str) -> str: """The requisition id, which `bulletFields` lead with, if the path ends in it (`..._R-059566`, and `..._R-059566-2` for a repost of it). Where a tenant puts something else first, a place, the path is the id.""" first = next(iter(posting.get("bulletFields") or []), None) name = path.rsplit("/", 1)[-1] return first if isinstance(first, str) and re.fullmatch( rf".*_{re.escape(first)}(?:-\d+)?", name) else "" def _remote(value: Any) -> str | None: return _REMOTE.get(str(value or "").strip().lower()) #: `timeType` -> the app's employment words. _EMPLOYMENT = {"full time": "full_time", "part time": "part_time"} #: What `locationsText` says of a role at several places: "2 Locations". The #: places arrive with the role's own read. _COUNT = re.compile(r"\d+ locations?", re.I) def _place(text: Any, remote: str | None) -> Place | None: """A place as Workday writes it, or None for none. Some tenants write "Country - City" ("Denmark - Copenhagen", "USA - Texas - Austin"), which is turned round to the order the rest of the app reads, "Copenhagen, Denmark"; a place that starts with a work-mode word ("Remote - Germany") is left as it is. A place is remote or an office as far as the role's `remoteType` says.""" text = " ".join(str(text or "").split()) if not text or _COUNT.fullmatch(text): return None parts = [part.strip() for part in text.split(" - ")] if len(parts) > 1 and "," not in text and all(parts) \ and not parts[0].lower().startswith(("remote", "hybrid", "on-site", "onsite")): text = ", ".join(reversed(parts)) return {"text": text, "remote": {"remote": True, "hybrid": False, "onsite": False}.get( remote or "")} def rows(decoded: dict[str, Any], slug: str) -> list[Row]: """One page of `.../wday/cxs///jobs` -> `{"total", "jobPostings": [...], "facets": [...]}`. A posting has `title`, `externalPath` (`/job//_<requisition id>`), `locationsText` (a place, or "2 Locations"), `postedOn` (relative text) and `bulletFields`, which lead with the requisition id, and on some tenants `remoteType`. No description, no date and no country: `detail` has those, and the places of a role that "2 Locations" hides. The id is the requisition id where the path carries it, and the path where not; a posting with no title or no path is skipped.""" out = [] for p in decoded.get("jobPostings") or []: title = (p.get("title") or "").strip() path = (p.get("externalPath") or "").strip() if not title or not _ROLE_PATH.fullmatch(path): continue remote = _remote(p.get("remoteType")) place = _place(p.get("locationsText"), remote) out.append({ "source_id": _requisition(p, path) or path, "title": title, "places": [place] if place else [], "location": place["text"] if place else "", "url": f"https://{_host(slug)}/{_parts(slug)[2]}{path}", # type: ignore[index] "posted": _posted(p.get("postedOn") or ""), "department": "", "description": "", "remote": remote, "salary_text": "", # no field for pay, only the description's text }) return out def detail(get: Get, slug: str, row: Row) -> dict[str, Any]: """The role's own JSON, `.../wday/cxs/<tenant>/<Site><externalPath>`. The path is taken back off the row's URL, which was built from it. Pay is in the description text when it is anywhere: there is no field for it on any tenant. `startDate` is the posting date (it matched the "Posted N Days Ago" text in every sample), not a start of employment.""" tenant, _dc, site = _parts(slug) # type: ignore[misc] prefix = f"https://{_host(slug)}/{site}" path = str(row.get("url") or "").removeprefix(prefix) if path == row.get("url") or not _ROLE_PATH.fullmatch(path) or ".." in path.split("/"): raise ValueError(f"{row.get('url')!r} is not a role of {slug}") info = json.loads(get(f"https://{_host(slug)}/wday/cxs/{tenant}/{site}{path}")) posting = info.get("jobPostingInfo") if not isinstance(posting, dict): raise ValueError("not a Workday role: no jobPostingInfo") out: dict[str, Any] = {} if description := " ".join(html.unescape(strip_html(posting.get("jobDescription") or "")) .split()): out["description"] = description remote = _remote(posting.get("remoteType")) if places := _places(posting, remote): out["places"] = places out["location"] = "; ".join(p["text"] for p in places) if len(places) == 1 and (code := _country_code(posting)): out["country"] = code if employment := _EMPLOYMENT.get(str(posting.get("timeType") or "").strip().lower()): out["employment"] = employment if posted := parse_dt(posting.get("startDate")): out["posted"] = posted if remote: out["remote"] = remote return out def _country_code(posting: dict[str, Any]) -> str | None: """The role's country as an ISO code, from the requisition's location. The code belongs to the main location only, so it is used for a role with one place.""" country = (posting.get("jobRequisitionLocation") or {}).get("country") or {} code = str(country.get("alpha2Code") or "").strip().upper() return code if len(code) == 2 else None def _places(posting: dict[str, Any], remote: str | None) -> list[Place]: """The main location and the additional ones, each once, which replace the listing's "2 Locations". A single location that does not name its country (LEGO's "Billund") gets it from the role's `country`.""" found: list[Place] = [] for name in [posting.get("location"), *(posting.get("additionalLocations") or [])]: place = _place(name, remote) if place and place["text"] not in (p["text"] for p in found): found.append(place) country = str((posting.get("country") or {}).get("descriptor") or "").strip() if len(found) == 1 and country and country.lower() not in found[0]["text"].lower(): found[0]["text"] += f", {country}" return found