| """Albanian Supreme Court (Gjykata e Lartë) via the Gatsby page-data JSON API. |
| |
| Decisions are published as periodic and thematic bulletins under |
| `/sq/lajme/buletini/`. Each bulletin's detail page exposes its decisions |
| inline as HTML strings inside `result.data.api.newsArticle.body[].content[].text_sq`, |
| so we can extract full text without touching the pre-2020 .doc archive. |
| |
| Civil filter: keep decisions tagged "Kolegji Civil" (Civil College) or |
| "Kolegjet e Bashkuara" (United Colleges, mixed civil/penal). |
| """ |
|
|
| import html |
| import logging |
| import re |
| import time |
| from datetime import date |
| from typing import Any |
|
|
| import requests |
|
|
| from legex.models.base import Case |
| from legex.scrapers.base import BaseScraper |
|
|
| log = logging.getLogger(__name__) |
|
|
| API_BASE = "https://www.gjykataelarte.gov.al/page-data" |
| PUBLIC_BASE = "https://www.gjykataelarte.gov.al" |
| USER_AGENT = "legex-research (open-data, friendly)" |
| DELAY_SECONDS = 1.0 |
| MAX_LIST_PAGES = 15 |
|
|
| |
| |
| |
| _DECISION_HEADER_RE = re.compile( |
| r"(Vendimi?\s+nr\.?\s*[\w\-\(\)\s]+?,?\s*dat[ëe]\s+\d{1,2}\.\d{1,2}\.\d{4}\s+i\s+Kolegj[a-zëÇçë ]+)", |
| re.IGNORECASE, |
| ) |
|
|
| |
| |
| |
| _OLD_DECISION_RE = re.compile( |
| r"N[ëe]\s+vendim(?:in)?\s+nr[\.\s]*([\d\(\)\s\-––]+?)\s*,?\s*dat[ëe]\s+(\d{1,2})[\.,](\d{1,2})[\.,](\d{4})", |
| re.IGNORECASE, |
| ) |
| _COLLEGE_HEADING_RE = re.compile( |
| r"Kolegj[i]?\s+(Civil|Penal|Administrativ|Administrative|Tregtar|" |
| r"Bashkuar)[a-zëçÇË]*\s+(?:i\s+)?Gjykat", |
| re.IGNORECASE, |
| ) |
| |
| _INLINE_COLLEGE_RE = re.compile(r"\bK(C|P|A)GJL\b") |
| _DOC_LINK_RE = re.compile( |
| r"https?://gjykata-media\.s3\.eu-central-1\.amazonaws\.com/[\w\-\.\/]+\.doc", |
| re.IGNORECASE, |
| ) |
|
|
| _DECISION_NUM_RE = re.compile(r"nr\.?\s*([\d\-\(\)\s]+?)\s*,", re.IGNORECASE) |
| _DATE_RE = re.compile(r"dat[ëe]\s+(\d{1,2})\.(\d{1,2})\.(\d{4})") |
| _KEYWORD_RE = re.compile( |
| r"Fjal[ëe]\s*ky[çc]e\s*[-–—]\s*(.+?)(?=P[ëe]rmbledhje|Maksima|$)", |
| re.DOTALL | re.IGNORECASE, |
| ) |
| _MAXIMA_RE = re.compile( |
| r"Maksima\s*[-–—]\s*(.+?)(?=Fjal[ëe]\s*ky[çc]e|P[ëe]rmbledhje|$)", |
| re.DOTALL | re.IGNORECASE, |
| ) |
|
|
|
|
| def _strip_html(raw: str) -> str: |
| """Remove HTML tags, decode entities, collapse whitespace.""" |
| text = re.sub(r"<[^>]+>", "\n", raw) |
| text = html.unescape(text) |
| text = re.sub(r"\n{3,}", "\n\n", text) |
| return text.strip() |
|
|
|
|
| def _detect_college(header: str) -> str: |
| h = header.lower() |
| if "civil" in h: |
| return "civil" |
| if "penal" in h: |
| return "penal" |
| if "administrat" in h: |
| return "administrative" |
| if "bashk" in h: |
| return "united" |
| return "unknown" |
|
|
|
|
| def _parse_iso_date(header: str) -> str | None: |
| m = _DATE_RE.search(header) |
| if not m: |
| return None |
| return f"{m.group(3)}-{m.group(2).zfill(2)}-{m.group(1).zfill(2)}" |
|
|
|
|
| def _parse_decision_number(header: str) -> str | None: |
| m = _DECISION_NUM_RE.search(header) |
| if not m: |
| return None |
| return re.sub(r"\s+", "", m.group(1)) |
|
|
|
|
| def _fetch_json(session: requests.Session, url: str) -> dict[str, Any] | None: |
| try: |
| resp = session.get(url, timeout=30) |
| except requests.RequestException as e: |
| log.warning("AL request error %s: %s", url, e) |
| return None |
| if resp.status_code == 404: |
| return None |
| if resp.status_code != 200: |
| log.warning("AL HTTP %d for %s", resp.status_code, url) |
| return None |
| try: |
| return resp.json() |
| except ValueError: |
| log.warning("AL non-JSON response from %s", url) |
| return None |
|
|
|
|
| def _list_bulletin_slugs(session: requests.Session) -> list[dict[str, Any]]: |
| slugs: list[dict[str, Any]] = [] |
| for page in range(1, MAX_LIST_PAGES + 1): |
| url = ( |
| f"{API_BASE}/sq/lajme/buletini/page-data.json" |
| if page == 1 |
| else f"{API_BASE}/sq/lajme/buletini/{page}/page-data.json" |
| ) |
| data = _fetch_json(session, url) |
| if not data: |
| break |
| articles = ( |
| data.get("result", {}).get("pageContext", {}).get("articles", []) |
| ) |
| if not articles: |
| break |
| for a in articles: |
| slug = a.get("slug") |
| if not slug: |
| continue |
| title_obj = a.get("title") |
| if isinstance(title_obj, dict): |
| title = title_obj.get("text_sq", "") or title_obj.get("text_en", "") |
| else: |
| title = title_obj or "" |
| slugs.append( |
| { |
| "slug": slug, |
| "publish_date": a.get("publishDate"), |
| "title": title, |
| } |
| ) |
| log.info("AL list page %d: %d bulletins (cumulative %d)", page, len(articles), len(slugs)) |
| time.sleep(DELAY_SECONDS) |
| return slugs |
|
|
|
|
| def _fetch_bulletin_html(session: requests.Session, slug: str) -> str: |
| url = f"{API_BASE}/sq/lajme/buletini/{slug}/page-data.json" |
| data = _fetch_json(session, url) |
| if not data: |
| return "" |
| try: |
| body = data["result"]["data"]["api"]["newsArticle"]["body"] |
| except (KeyError, TypeError): |
| return "" |
| chunks: list[str] = [] |
| for block in body: |
| if "Paragraph" not in block.get("__typename", ""): |
| continue |
| for content in block.get("content", []) or []: |
| txt = content.get("text_sq") or content.get("text_en") or "" |
| if txt: |
| chunks.append(txt) |
| return "".join(chunks) |
|
|
|
|
| def _parse_decisions( |
| bulletin_html: str, slug: str, bulletin_title: str |
| ) -> list[dict[str, Any]]: |
| plain = _strip_html(bulletin_html) |
| parts = _DECISION_HEADER_RE.split(plain) |
| if len(parts) < 3: |
| |
| |
| return _parse_old_format(plain, slug, bulletin_title) |
| decisions: list[dict[str, Any]] = [] |
| i = 1 |
| while i < len(parts) - 1: |
| header = parts[i].strip() |
| body = parts[i + 1].strip() |
| i += 2 |
| if len(body) < 200: |
| continue |
| decisions.append( |
| { |
| "header": header, |
| "body": body, |
| "decision_number": _parse_decision_number(header), |
| "iso_date": _parse_iso_date(header), |
| "college": _detect_college(header), |
| "maxima": (m.group(1).strip() if (m := _MAXIMA_RE.search(body)) else None), |
| "keywords": ( |
| [k.strip() for k in re.split(r"[,;]", km.group(1)) if k.strip()] |
| if (km := _KEYWORD_RE.search(body)) |
| else [] |
| ), |
| "bulletin_slug": slug, |
| "bulletin_title": bulletin_title, |
| "doc_link": None, |
| "format": "new", |
| } |
| ) |
| return decisions |
|
|
|
|
| def _section_college_at(plain: str, pos: int) -> str: |
| """Return the most recent college heading before pos in plain text.""" |
| head = plain[:pos] |
| last: str = "unknown" |
| for m in _COLLEGE_HEADING_RE.finditer(head): |
| last = _detect_college(m.group(0)) |
| return last |
|
|
|
|
| def _inline_college_at(snippet: str) -> str: |
| m = _INLINE_COLLEGE_RE.search(snippet) |
| if not m: |
| return "unknown" |
| return {"C": "civil", "P": "penal", "A": "administrative"}.get(m.group(1), "unknown") |
|
|
|
|
| def _parse_old_format( |
| plain: str, slug: str, bulletin_title: str |
| ) -> list[dict[str, Any]]: |
| matches = list(_OLD_DECISION_RE.finditer(plain)) |
| if not matches: |
| return [] |
|
|
| decisions: list[dict[str, Any]] = [] |
| for idx, m in enumerate(matches): |
| |
| body_end = matches[idx + 1].start() if idx + 1 < len(matches) else m.end() + 3000 |
| body = plain[m.start():body_end].strip() |
| if len(body) < 150: |
| continue |
|
|
| decision_num = re.sub(r"\s+", "", m.group(1)) |
| iso_date = f"{m.group(4)}-{m.group(3).zfill(2)}-{m.group(2).zfill(2)}" |
|
|
| |
| |
| |
| college = _inline_college_at(body) |
| if college == "unknown": |
| college = _section_college_at(plain, m.start()) |
|
|
| doc_link_m = _DOC_LINK_RE.search(body) |
| doc_link = doc_link_m.group(0) if doc_link_m else None |
|
|
| decisions.append( |
| { |
| "header": body[:200], |
| "body": body, |
| "decision_number": decision_num, |
| "iso_date": iso_date, |
| "college": college, |
| "maxima": None, |
| "keywords": [], |
| "bulletin_slug": slug, |
| "bulletin_title": bulletin_title, |
| "doc_link": doc_link, |
| "format": "old", |
| } |
| ) |
| return decisions |
|
|
|
|
| class ALScraper(BaseScraper): |
| country = "Albanien" |
|
|
| def scrape( |
| self, |
| start_date: date | None = None, |
| end_date: date | None = None, |
| ) -> list[Case]: |
| session = requests.Session() |
| session.headers.update( |
| {"User-Agent": USER_AGENT, "Accept": "application/json"} |
| ) |
|
|
| slugs = _list_bulletin_slugs(session) |
| log.info("AL discovered %d bulletins", len(slugs)) |
|
|
| seen: set[str] = set() |
| cases: list[Case] = [] |
| for info in slugs: |
| raw_html = _fetch_bulletin_html(session, info["slug"]) |
| time.sleep(DELAY_SECONDS) |
| if not raw_html: |
| continue |
| decisions = _parse_decisions(raw_html, info["slug"], info["title"]) |
| log.info("AL %s: %d decisions", info["slug"], len(decisions)) |
|
|
| for d in decisions: |
| decision_date: date | None = None |
| if d["iso_date"]: |
| try: |
| decision_date = date.fromisoformat(d["iso_date"]) |
| except ValueError: |
| decision_date = None |
| if start_date and decision_date and decision_date < start_date: |
| continue |
| if end_date and decision_date and decision_date > end_date: |
| continue |
|
|
| case_id = d["decision_number"] or f"AL-{info['slug']}-{len(cases)}" |
| if case_id in seen: |
| continue |
| seen.add(case_id) |
|
|
| full_text = f"{d['header']}\n\n{d['body']}" if d["format"] == "new" else d["body"] |
| |
| |
| link = d.get("doc_link") or f"{PUBLIC_BASE}/sq/lajme/buletini/{info['slug']}" |
|
|
| cases.append( |
| Case( |
| case_id=case_id, |
| link=link, |
| decision_date=decision_date, |
| jurisdiction="al", |
| language="sq", |
| full_text=full_text, |
| metadata={ |
| "college": d["college"], |
| "decision_number": d["decision_number"], |
| "bulletin_slug": d["bulletin_slug"], |
| "bulletin_title": d["bulletin_title"], |
| "maxima": d["maxima"], |
| "keywords": d["keywords"], |
| "format": d["format"], |
| "doc_link": d.get("doc_link"), |
| }, |
| ) |
| ) |
|
|
| cases.sort(key=lambda c: c.decision_date or date.min, reverse=True) |
| log.info("Collected %d Albania cases", len(cases)) |
| return cases |
|
|
| @staticmethod |
| def civil_filter(cases: list[Case]) -> list[Case]: |
| kept = [ |
| c |
| for c in cases |
| if (c.metadata or {}).get("college") in {"civil", "united"} |
| ] |
| log.info("AL civil_filter kept %d/%d", len(kept), len(cases)) |
| return kept |
|
|