Files
jobboard/plugins/readers/platsbanken.py
T
WolfandClaude Opus 5.5 c497fe965d Keep a description's headings, bullets and paragraphs, and draw them in the reader
The readers flattened a posting's HTML into one line with strip_html, so
sections and bullets ran together. _shared.html_text gives a line per
paragraph, '• ' before a list item and '## ' before a heading; every
reader's description goes through it, sections read per role get their
own headings, and the reader pane draws the lines as h3, ul and p.
Excerpts leave the marks out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 21:40:12 +02:00

130 lines
5.5 KiB
Python

"""Platsbanken: Sweden's public employment service, through the JobTech search
API at jobsearch.api.jobtechdev.se; no key.
Checked live on 2026-09-29 and 2026-09-30, moved here from `plugins/search.py`.
`region` is the id of a län in the JobTech taxonomy (Skåne län is
`CaRE_1nn_cSU`), empty for all of Sweden. 100 results a page, and an `offset`
above 2000 is refused, so a search reads 2,100 results at most. It states
`total`. A hit has the whole ad's text, so the list has the full text. An ad
may name several workplaces: `workplace_addresses` holds one entry each, and
every one is a place of the role. `workplace_model` is mostly empty; its
label is passed on as `remote`, in the host's words.
"""
from __future__ import annotations
import html
import json
from collections.abc import Callable
from typing import Any
from urllib.parse import urlencode
from desk.sources import strip_html
from ._contract import Ask, Fields, Filters, Place, Reads
from ._shared import Get, Page, Row, html_text, is_web_url, only, parse_dt
NAME = "platsbanken"
LABEL = "Platsbanken"
RANK = 30
READS = Reads(
scope="search", listing="paged", page_size=100, ceiling=2000,
text="listing", complete="stated",
filters=Filters(query="one", place="one"),
places="many",
fields=Fields(posted="trusted", remote="stated", country="stated"),
robots="read: the host publishes no robots.txt, /robots.txt answers 404 "
"(checked 2026-09-30)")
#: Saved param -> the filter dimension it fills.
PARAMS = {"q": "query", "region": "place"}
SEARCH = "https://jobsearch.api.jobtechdev.se/search"
#: The 21 Swedish län as concept ids of the JobTech taxonomy, from its `region`
#: concepts (which also list regions outside Sweden). "" is all of Sweden and
#: is not a key here.
REGIONS: dict[str, str] = {
"DQZd_uYs_oKb": "Blekinge län", "oDpK_oZ2_WYt": "Dalarnas län",
"K8iD_VQv_2BA": "Gotlands län", "zupA_8Nt_xcD": "Gävleborgs län",
"wjee_qH2_yb6": "Hallands län", "65Ms_7r1_RTG": "Jämtlands län",
"MtbE_xWT_eMi": "Jönköpings län", "9QUH_2bb_6Np": "Kalmar län",
"tF3y_MF9_h5G": "Kronobergs län", "9hXe_F4g_eTG": "Norrbottens län",
"CaRE_1nn_cSU": "Skåne län", "CifL_Rzy_Mku": "Stockholms län",
"s93u_BEb_sx2": "Södermanlands län", "zBon_eET_fFU": "Uppsala län",
"EVVp_h6U_GSZ": "Värmlands län", "g5Tt_CAV_zBd": "Västerbottens län",
"NvUF_SP1_1zo": "Västernorrlands län", "G6DV_fKE_Viz": "Västmanlands län",
"zdoY_6u5_Krt": "Västra Götalands län", "xTCk_nT5_Zjm": "Örebro län",
"oLT3_Q9p_3nn": "Östergötlands län",
}
def allowed(slug: str) -> Callable[[str], bool]:
return only("jobsearch.api.jobtechdev.se")
def clean_params(params: dict[str, Any]) -> dict[str, Any]:
"""The saved params of a search: `q` and `region`, the id of a län or
empty for all of Sweden."""
region = str(params.get("region") or "").strip()
if region and region not in REGIONS:
raise ValueError("region is empty or the id of a Swedish län")
return {"q": params["q"], "region": region}
def _text(fragment: str) -> str:
return html.unescape(strip_html(fragment)).strip()
def page(get: Get, slug: str, cursor: Any, ask: Ask) -> Page:
"""One page of a search, newest first; the cursor is the offset. The next
page follows a full page while the offset is within the ceiling."""
region = str(ask.places[0] if ask.places else ask.params.get("region") or "")
if region and region not in REGIONS:
raise ValueError(f"platsbanken region {region!r} is not one of the ids in REGIONS")
size, ceiling = READS.page_size or 100, READS.ceiling or 2000
offset = int(cursor or 0)
query: dict[str, Any] = {"q": ask.query or str(ask.params.get("q") or ""),
"sort": "pubdate-desc", "limit": size, "offset": offset}
if region:
query["region"] = region
data = json.loads(get(SEARCH + "?" + urlencode(query)))
hits = [hit for hit in data.get("hits") or [] if isinstance(hit, dict)]
total = (data.get("total") or {}).get("value")
more = len(hits) >= size and offset + size <= ceiling
return (data, [{"id": str(hit.get("id") or "")} for hit in hits],
total if isinstance(total, int) else None, offset + size if more else None)
def _place(address: dict[str, Any]) -> Place | None:
text = (address.get("municipality") or address.get("region")
or address.get("country") or "").strip()
return {"text": text, "remote": None} if text else None
def rows(decoded: Any, slug: str) -> list[Row]:
out: list[Row] = []
for hit in decoded.get("hits") or []:
title = _text(hit.get("headline") or "")
url = (hit.get("webpage_url") or "").strip()
if not title or not is_web_url(url):
continue
addresses = hit.get("workplace_addresses") or [hit.get("workplace_address") or {}]
places = [place for address in addresses if isinstance(address, dict)
if (place := _place(address))]
model = str((hit.get("workplace_model") or {}).get("label") or "").strip()
out.append({
"source_id": str(hit.get("id") or ""),
"title": title,
"company": ((hit.get("employer") or {}).get("name") or "").strip(),
"url": url,
"places": places,
"location": places[0]["text"] if places else "",
"country": "SE",
"remote": model or None,
"posted": parse_dt(hit.get("publication_date")),
"description": html_text((hit.get("description") or {}).get("text")),
})
return out