| import logging |
| import re |
| import sys |
| import time |
| import urllib.request |
| from datetime import date, datetime |
|
|
| from legex.models.base import Case |
| from legex.scrapers.base import BaseScraper |
|
|
| log = logging.getLogger(__name__) |
|
|
| BASE = "https://www.cassationcourt.am/en/decisions/?chamber=2&page=" |
| USER_AGENT = "FriendlyResearcher" |
| MAX_PAGES = 30 |
| DELAY_SECONDS = 0.5 |
| CIVIL_CHAMBER_LABEL = "Քաղաքացիական" |
| EMPTY_PAGE_MARKER = "Գրառումներ չկան" |
|
|
| _DATE_FMT_RE = re.compile(r"^\d{2}\.\d{2}\.\d{4}$") |
| _DIV_RE = re.compile(r"<div>(.*?)</div>", re.DOTALL) |
|
|
|
|
| def _fetch_page(page: int) -> str | None: |
| url = BASE + str(page) |
| req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT}) |
| try: |
| with urllib.request.urlopen(req, timeout=30) as resp: |
| return resp.read().decode("utf-8") |
| except Exception as e: |
| print(f" ERROR page {page}: {e}", file=sys.stderr) |
| return None |
|
|
|
|
| def _parse_decisions(html: str) -> list[tuple[str, str, str]]: |
| """Extract (iso_date, link, case_number) triples from a listing page.""" |
| results: list[tuple[str, str, str]] = [] |
| for block in html.split("resultListBody")[1:]: |
| divs = _DIV_RE.findall(block) |
| if len(divs) >= 3 and divs[2].strip() == CIVIL_CHAMBER_LABEL: |
| date_str = divs[0].strip() |
| case_num = divs[1].strip() |
| if not _DATE_FMT_RE.match(date_str): |
| continue |
| try: |
| iso_date = datetime.strptime(date_str, "%d.%m.%Y").strftime("%Y-%m-%d") |
| except ValueError: |
| iso_date = date_str |
| case_anchor = case_num.replace("/", "-") |
| link = f"https://www.cassationcourt.am/en/decisions/?chamber=2&page=1#case-{case_anchor}" |
| results.append((iso_date, link, case_num)) |
| return results |
|
|
|
|
| class AMScraper(BaseScraper): |
| country = "Armenien" |
|
|
| def scrape( |
| self, |
| start_date: date | None = None, |
| end_date: date | None = None, |
| ) -> list[Case]: |
| all_decisions: list[tuple[str, str, str]] = [] |
| for page in range(1, MAX_PAGES + 1): |
| log.info("AM fetching page %d", page) |
| html = _fetch_page(page) |
| if html is None: |
| log.warning("AM page %d failed; stopping", page) |
| break |
| decisions = _parse_decisions(html) |
| if not decisions: |
| if EMPTY_PAGE_MARKER in html: |
| log.info("AM no records on page %d; stopping", page) |
| break |
| log.info("AM no decisions parsed on page %d; continuing", page) |
| continue |
| log.info("AM page %d: %d decisions", page, len(decisions)) |
| all_decisions.extend(decisions) |
| time.sleep(DELAY_SECONDS) |
|
|
| |
| seen: set[str] = set() |
| unique: list[tuple[str, str, str]] = [] |
| for entry in all_decisions: |
| _, _, case_num = entry |
| if case_num in seen: |
| continue |
| seen.add(case_num) |
| unique.append(entry) |
| log.info("AM total unique civil decisions: %d", len(unique)) |
|
|
| cases: list[Case] = [] |
| for iso_date, link, case_num in unique: |
| decision_date: date | None = None |
| try: |
| decision_date = date.fromisoformat(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 |
|
|
| cases.append(Case( |
| case_id=case_num, |
| link=link, |
| decision_date=decision_date, |
| jurisdiction="am", |
| language="hy", |
| full_text=None, |
| metadata={"chamber": "civil"}, |
| )) |
|
|
| cases.sort(key=lambda c: c.decision_date or date.min, reverse=True) |
| log.info("Collected %d Armenia cases", len(cases)) |
| return cases |
|
|