| from __future__ import annotations |
|
|
| import logging |
| import re |
| import time |
| from datetime import date |
| from html import unescape |
| from urllib.parse import urljoin |
|
|
| import requests |
| from dateutil import parser as dateutil_parser |
|
|
| from legex.models.base import Case |
| from legex.scrapers.base import BaseScraper |
|
|
| |
| |
| |
|
|
| log = logging.getLogger(__name__) |
|
|
| BASE_URL = "https://juportal.be" |
| SEARCH_FORM_URL = f"{BASE_URL}/zoekmachine/zoekformulier" |
| SEARCH_RESULTS_URL = f"{BASE_URL}/zoekmachine/zoekresultaten" |
| DELAY_SECONDS = 3 |
| RETRY_DELAY_SECONDS = 30 |
| MAX_PAGES_PER_QUERY = 300 |
|
|
| _RE_FORM_TOKEN = re.compile(r'name=["\']TOKEN["\'][^>]*value=["\']([^"\']*)', re.IGNORECASE) |
| _RE_ACTION_VALUE = re.compile( |
| r'<button[^>]*name=["\']action["\'][^>]*value=["\']([^"\']*)', |
| re.IGNORECASE, |
| ) |
| _RE_NEXT_VALUE = re.compile( |
| r'<button[^>]*name=["\']next_page["\'][^>]*value=["\']([^"\']*)', |
| re.IGNORECASE, |
| ) |
| _RE_CONTENT_LINK = re.compile(r'href=["\'](/content/ECLI:BE:CASS:[^"\']+)["\']', re.IGNORECASE) |
| _RE_ROLE = re.compile(r"Rolnummer:\s*([A-Z]\.[0-9]{2}\.[0-9]{4}\.[A-Z])", re.IGNORECASE) |
| _RE_DATE = re.compile(r"Vonnis/arrest van\s+([0-9]{1,2}\s+[A-Za-z]+\s+[0-9]{4})", re.IGNORECASE) |
| _RE_ECLI = re.compile(r"(ECLI:BE:CASS:[A-Z0-9:.]+)") |
| _RE_RESULT_ROW_META = re.compile( |
| r"Hof van Cassatie\s*-\s*([0-9]{1,2}\s+[A-Za-z]+\s+[0-9]{4})\s*-\s*([A-Z]\.[0-9]{2}\.[0-9]{4}\.[A-Z])", |
| re.IGNORECASE, |
| ) |
|
|
|
|
| def _parse_date(text: str | None) -> date | None: |
| if not text: |
| return None |
| cleaned = text.strip() |
| try: |
| return date.fromisoformat(cleaned) |
| except ValueError: |
| pass |
| try: |
| return dateutil_parser.parse(cleaned, dayfirst=True).date() |
| except (ValueError, OverflowError, dateutil_parser.ParserError): |
| return None |
|
|
|
|
| def _extract_token(html: str) -> str | None: |
| match = _RE_FORM_TOKEN.search(html) |
| return match.group(1) if match else None |
|
|
|
|
| def _extract_action_value(html: str) -> str | None: |
| match = _RE_ACTION_VALUE.search(html) |
| return match.group(1) if match else None |
|
|
|
|
| def _extract_next_value(html: str) -> str | None: |
| match = _RE_NEXT_VALUE.search(html) |
| return match.group(1) if match else None |
|
|
|
|
| def _extract_result_links(html: str) -> list[str]: |
| links = [] |
| seen: set[str] = set() |
| for raw_link in _RE_CONTENT_LINK.findall(html): |
| clean = raw_link.split("#", 1)[0] |
| if ":ARR." not in clean: |
| continue |
| if clean in seen: |
| continue |
| seen.add(clean) |
| links.append(urljoin(BASE_URL, clean)) |
| return links |
|
|
|
|
| def _extract_result_cases(html: str) -> list[Case]: |
| cases: list[Case] = [] |
| seen: set[str] = set() |
| chunks = html.split("<tr>") |
| for chunk in chunks: |
| link_match = _RE_CONTENT_LINK.search(chunk) |
| if not link_match: |
| continue |
| rel = link_match.group(1).split("#", 1)[0] |
| if ":ARR." not in rel: |
| continue |
| link = urljoin(BASE_URL, rel) |
| if link in seen: |
| continue |
| seen.add(link) |
|
|
| ecli_match = _RE_ECLI.search(chunk) |
| meta_match = _RE_RESULT_ROW_META.search(unescape(re.sub(r"<[^>]+>", " ", chunk))) |
| if not meta_match: |
| continue |
| decision_date = _parse_date(meta_match.group(1)) |
| case_id = meta_match.group(2).upper() |
| if not case_id.startswith("C."): |
| continue |
|
|
| cases.append( |
| Case( |
| case_id=case_id, |
| link=link, |
| decision_date=decision_date, |
| jurisdiction="be", |
| language="nl", |
| full_text=None, |
| metadata={ |
| "ecli": (ecli_match.group(1).upper() if ecli_match else None), |
| "source": "juportal.be", |
| }, |
| ) |
| ) |
| return cases |
|
|
|
|
| def _extract_case_id(page_text: str) -> str | None: |
| match = _RE_ROLE.search(page_text) |
| return match.group(1).upper() if match else None |
|
|
|
|
| def _extract_ecli(page_text: str) -> str | None: |
| match = _RE_ECLI.search(page_text) |
| return match.group(1).upper() if match else None |
|
|
|
|
| def _extract_decision_date(page_text: str) -> date | None: |
| match = _RE_DATE.search(page_text) |
| return _parse_date(match.group(1) if match else None) |
|
|
|
|
| def _extract_text(page_html: str) -> str | None: |
| text = unescape(re.sub(r"<[^>]+>", "\n", page_html)) |
| lines = [line.strip() for line in text.splitlines()] |
| cleaned = [line for line in lines if line] |
| return "\n".join(cleaned) if cleaned else None |
|
|
|
|
| class BEScraper(BaseScraper): |
| country = "Belgium" |
|
|
| def scrape( |
| self, |
| start_date: date | None = None, |
| end_date: date | None = None, |
| ) -> list[Case]: |
| start = start_date or date(2015, 1, 1) |
| end = end_date or date.today() |
| cases: list[Case] = [] |
| seen: set[str] = set() |
| session = requests.Session() |
|
|
| for year in range(start.year, end.year + 1): |
| year_start = max(start, date(year, 1, 1)) |
| year_end = min(end, date(year, 12, 31)) |
| if year_start > year_end: |
| continue |
| year_cases = self._scrape_year(session, year_start, year_end) |
| for case in year_cases: |
| if not case.link or case.link in seen: |
| continue |
| seen.add(case.link) |
| cases.append(case) |
|
|
| return cases |
|
|
| def _scrape_year( |
| self, |
| session: requests.Session, |
| start_date: date, |
| end_date: date, |
| ) -> list[Case]: |
| log.info("Belgium %s to %s", start_date.isoformat(), end_date.isoformat()) |
| form_html = self._fetch_text(session, SEARCH_FORM_URL) |
| if form_html is None: |
| return [] |
| token = _extract_token(form_html) |
| action_value = _extract_action_value(form_html) |
| if not token or not action_value: |
| return [] |
|
|
| payload = { |
| "TOKEN": token, |
| "action": action_value, |
| "TRECHNOROLE": "C.", |
| "TRECHDECISIONDE": start_date.isoformat(), |
| "TRECHDECISIONA": end_date.isoformat(), |
| "TRECHNPPAGE": "50", |
| "TRECHORDER": "DATEDEC", |
| "TRECHDESCASC": "DESC", |
| "TRECHOPER": "AND", |
| "TRECHLIMIT": "25000", |
| "TRECHMODE": "SIMPLE", |
| "TRECHSCORE": "1", |
| "TRECHSHOWFICHES": "fiches", |
| } |
| page_html = self._post_text(session, SEARCH_FORM_URL, payload) |
| if page_html is None: |
| return [] |
|
|
| results: list[Case] = [] |
| seen_links: set[str] = set() |
| for page_idx in range(1, MAX_PAGES_PER_QUERY + 1): |
| links = _extract_result_links(page_html) |
| if not links: |
| break |
| log.info("Belgium page %d: %d decision links", page_idx, len(links)) |
| page_cases = _extract_result_cases(page_html) |
| for case in page_cases: |
| if not case.link or case.link in seen_links: |
| continue |
| seen_links.add(case.link) |
| results.append(case) |
|
|
| next_value = _extract_next_value(page_html) |
| next_token = _extract_token(page_html) |
| if not next_value or not next_token: |
| break |
| time.sleep(DELAY_SECONDS) |
| page_html = self._post_text( |
| session, |
| SEARCH_RESULTS_URL, |
| {"TOKEN": next_token, "next_page": next_value}, |
| ) |
| if page_html is None: |
| break |
|
|
| return [ |
| c for c in results |
| if c.decision_date is not None |
| and start_date <= c.decision_date <= end_date |
| ] |
|
|
| def _fetch_case(self, link: str) -> Case | None: |
| html = self._fetch_url_text(link) |
| if html is None: |
| return None |
| text = _extract_text(html) |
| if text is None: |
| return None |
|
|
| case_id = _extract_case_id(text) |
| if not case_id or not case_id.startswith("C."): |
| return None |
|
|
| decision_date = _extract_decision_date(text) |
| ecli = _extract_ecli(text) |
| return Case( |
| case_id=case_id, |
| link=link, |
| decision_date=decision_date, |
| jurisdiction="be", |
| language="nl", |
| full_text=text, |
| metadata={"ecli": ecli, "source": "juportal.be"} if ecli else {"source": "juportal.be"}, |
| ) |
|
|
| @staticmethod |
| def civil_filter(cases: list[Case]) -> list[Case]: |
| return [c for c in cases if (c.case_id or "").startswith("C.")] |
|
|
| def _fetch_text(self, session: requests.Session, url: str) -> str | None: |
| try: |
| response = session.get(url, timeout=30) |
| if response.status_code == 429: |
| time.sleep(RETRY_DELAY_SECONDS) |
| response = session.get(url, timeout=30) |
| if not response.ok: |
| return None |
| return response.text |
| except requests.RequestException: |
| return None |
|
|
| def _post_text(self, session: requests.Session, url: str, data: dict[str, str]) -> str | None: |
| try: |
| response = session.post(url, data=data, timeout=30) |
| if response.status_code == 429: |
| time.sleep(RETRY_DELAY_SECONDS) |
| response = session.post(url, data=data, timeout=30) |
| if not response.ok: |
| return None |
| return response.text |
| except requests.RequestException: |
| return None |
|
|
|
|