diff --git a/plugins/atshosts/phenom.py b/plugins/atshosts/phenom.py index 7b0512a..260a6d4 100644 --- a/plugins/atshosts/phenom.py +++ b/plugins/atshosts/phenom.py @@ -169,15 +169,14 @@ def rows(search: dict[str, Any], slug: str) -> list[Row]: _JSON_LD = re.compile(r"]+application/ld\+json[^>]*>(.*?)", re.S | re.I) -def detail(get: Get, slug: str, row: Row) -> dict[str, Any]: - """The full description from the role's page: the `JobPosting` in its - JSON-LD, whose `description` is HTML with its entities escaped (`<p>` - and `&` inside), so it is unescaped, stripped and unescaped again. - `datePosted` is taken as `posted`: it can be earlier than the list's - `postedDate` (Philips's list said the day of the read for a role whose page - said 2026-09-08), and a role keeps the earliest date it ever had.""" - body = get(row["url"], headers={"Accept": "text/html"}).decode("utf-8", "replace") - posting: dict[str, Any] = {} +#: Where a page assigns its state, and the words its `checkRemote` says that +#: `store._remote` reads (King's pages say "On-site"; no other was seen). +_STATE = "phApp.ddo = " +_REMOTE = {"remote": "remote", "hybrid": "hybrid", "on-site": "onsite", "onsite": "onsite"} + + +def _job_posting(body: str) -> dict[str, Any]: + """The `JobPosting` among a page's JSON-LD blocks, or {}.""" for block in _JSON_LD.findall(body): try: found = json.loads(block) @@ -185,13 +184,46 @@ def detail(get: Get, slug: str, row: Row) -> dict[str, Any]: continue for candidate in found if isinstance(found, list) else [found]: if isinstance(candidate, dict) and candidate.get("@type") == "JobPosting": - posting = candidate - break - if posting: - break + return candidate + return {} + + +def _page_job(body: str) -> dict[str, Any]: + """The role as the page's own state holds it, `phApp.ddo.jobDetail.data.job`, + or {}. King's pages of roles posted the same day had no `JobPosting`, and + this had the full description. Only that one object is read: the state + around it holds the site's session values, which nothing here uses.""" + start = body.find(_STATE) + if start < 0: + return {} + try: + state, _end = json.JSONDecoder().raw_decode(body, start + len(_STATE)) + job = state["jobDetail"]["data"]["job"] + except (ValueError, KeyError, TypeError): + return {} + return job if isinstance(job, dict) else {} + + +def detail(get: Get, slug: str, row: Row) -> dict[str, Any]: + """The full description from the role's page: the `JobPosting` in its + JSON-LD, whose `description` is HTML with its entities escaped (`<p>` + and `&` inside), so it is unescaped, stripped and unescaped again; or, + where the page has none, the page state's own `description`, which is + plain HTML. A page with neither raises, so the role is asked again on a + later pull instead of counting as read with no text. `datePosted` is taken + as `posted`: it can be earlier than the list's `postedDate` (Philips's list + said the day of the read for a role whose page said 2026-09-08), and a role + keeps the earliest date it ever had. King's page state says `checkRemote`.""" + body = get(row["url"], headers={"Accept": "text/html"}).decode("utf-8", "replace") + posting, job = _job_posting(body), _page_job(body) out: dict[str, Any] = {} - if description := _plain(html.unescape(_text(posting.get("description")))): + if description := (_plain(html.unescape(_text(posting.get("description")))) + or _plain(job.get("description"))): out["description"] = description + else: + raise ValueError("no description on the role's page") if posted := parse_dt(posting.get("datePosted")): out["posted"] = posted + if remote := _REMOTE.get(_text(job.get("checkRemote")).lower()): + out["remote"] = remote return out diff --git a/tests/fixtures/ats/phenom-detail-3.html b/tests/fixtures/ats/phenom-detail-3.html new file mode 100644 index 0000000..991245c --- /dev/null +++ b/tests/fixtures/ats/phenom-detail-3.html @@ -0,0 +1,6 @@ + + +Principal Economy Designer | New Games in Barcelona, Spain | Games Design & Production at King + + + diff --git a/tests/test_ats_phenom.py b/tests/test_ats_phenom.py index 437fc50..a5ce596 100644 --- a/tests/test_ats_phenom.py +++ b/tests/test_ats_phenom.py @@ -198,11 +198,33 @@ def test_detail_finds_the_job_posting_among_other_blocks_and_takes_its_date() -> assert got["posted"].isoformat() == "2026-09-08T00:00:00+00:00" -def test_detail_of_a_page_without_a_posting_has_nothing_to_read() -> None: +def test_detail_falls_back_to_the_page_state_where_there_is_no_job_posting() -> None: + """King's pages of roles posted that day had no `JobPosting` block, only a + `WebPage` one, and the role in the state the page assigns to `phApp.ddo`.""" + row = _rows()[0] + got = phenom.detail(_get_of("phenom-detail-3.html", []), SLUG, row) + assert got["description"].startswith("Craft: Games Design & Production Job Description:") + assert "<" not in got["description"] and "&" not in got["description"] + assert got["remote"] == "onsite" + assert "posted" not in got and set(got) <= set(ats.DETAIL_FIELDS) + + +def test_detail_of_a_page_with_no_description_fails_so_that_it_is_asked_again() -> None: def get(url: str, **kwargs: Any) -> bytes: return (b'' - b'') - assert phenom.detail(get, SLUG, {"url": "https://careers.king.com/us/en/job/1"}) == {} + b'' + b'') + with pytest.raises(ValueError, match="no description"): + phenom.detail(get, SLUG, {"url": "https://careers.king.com/us/en/job/1"}) + + +@pytest.mark.parametrize("state", ["", "phApp.ddo = {broken", 'phApp.ddo = {"jobDetail": []};', + 'phApp.ddo = {"siteConfig": {}};']) +def test_a_page_state_that_is_not_a_role_is_no_description(state: str) -> None: + def get(url: str, **kwargs: Any) -> bytes: + return f"".encode() + with pytest.raises(ValueError, match="no description"): + phenom.detail(get, SLUG, {"url": "https://careers.king.com/us/en/job/1"}) # ----------------------------------------------------------- slug and URLs