code / legex /scrapers /ge.py
anonymous
[code] Initial release of the code.
6f5156a
import logging
import re
import sys
import time
import urllib.request
from datetime import date
from legex.models.base import Case
from legex.scrapers.base import BaseScraper
log = logging.getLogger(__name__)
USER_AGENT = "FriendyResearcher"
DELAY_SECONDS = 0.3
URL_TEMPLATE = "https://www.supremecourt.ge/ka/fullcase/{nid}/1"
# Note: the sample is not random; it was assembled from manual searches across
# multiple supreme-court listings. See the paper for sampling caveats.
_EXISTING_29 = {75795, 76240, 76234, 76198, 76209, 76215, 76205, 76201, 76238,
76202, 76203, 76204, 76211, 76218, 76219, 76220, 76221, 76225,
76188, 76190, 76187, 76182, 76185, 76186, 76177, 76178, 76191, 76199, 76200}
_SEARCH_19 = {75596, 70414, 33102, 72758, 73910, 75109, 73341, 74047, 73589,
73919, 70283, 73640, 69240, 69940, 66739, 67977, 70199, 73785, 72004}
_PROBE_76177 = {76179, 76180, 76181, 76183, 76184, 76189, 76192, 76193, 76194,
76195, 76196, 76197, 76206, 76207, 76208, 76212, 76213, 76214,
76216, 76228, 76229, 76230, 76231, 76232, 76233, 76236, 76237, 76239}
_PROBE_75500 = {75500, 75501, 75504, 75505, 75506, 75508, 75509, 75510, 75511,
75512, 75513, 75514, 75515, 75516, 75517, 75518, 75519, 75520,
75521, 75522, 75523, 75524, 75525, 75526, 75527, 75528, 75529,
75530, 75531, 75532, 75533, 75534, 75535, 75536, 75537, 75538,
75539, 75540, 75541, 75542, 75543, 75544, 75545, 75546, 75547,
75548, 75549, 75550, 75551, 75552, 75556, 75557, 75559, 75560,
75561, 75562, 75563, 75564, 75565, 75566, 75567, 75568, 75569,
75570, 75571, 75572, 75573, 75574, 75575, 75576, 75577, 75578,
75579, 75580, 75581, 75582, 75583, 75584, 75585, 75586}
def _all_case_ids() -> list[int]:
ids = _EXISTING_29 | _SEARCH_19 | _PROBE_76177 | _PROBE_75500
# 33102 historically returns 500 — drop both string and int forms defensively.
ids.discard("33102") # type: ignore[arg-type]
ids.discard(33102)
return sorted(int(x) for x in ids)
_GEO_MONTHS = {
"იანვარი": "01", "თებერვალი": "02", "მარტი": "03", "აპრილი": "04",
"მაისი": "05", "ივნისი": "06", "ივლისი": "07", "აგვისტო": "08",
"სექტემბერი": "09", "ოქტომბერი": "10", "ნოემბერი": "11", "დეკემბერი": "12",
}
_CASE_RE_PRIMARY = re.compile(r"(?:საქმე\s*N?:?\s*|საქმე\s*№)(ას-\d+-\d+)", re.DOTALL)
_CASE_RE_FALLBACK = re.compile(r"(ას-\d+-\d+)")
_DATE_DMY = re.compile(
r"(\d{1,2})\s*(იანვარი|თებერვალი|მარტი|აპრილი|მაისი|ივნისი|ივლისი|აგვისტო|"
r"სექტემბერი|ოქტომბერი|ნოემბერი|დეკემბერი)\s*,?\s*(\d{4})"
)
_DATE_YDM = re.compile(
r"(\d{4})\s*წელი\s*[,.]?\s*(\d{1,2})\s*(იანვარი|თებერვალი|მარტი|აპრილი|მაისი|"
r"ივნისი|ივლისი|აგვისტო|სექტემბერი|ოქტომბერი|ნოემბერი|დეკემბერი)"
)
_DATE_DMY_SPACED = re.compile(
r"(\d{1,2})\s+(იანვარი|თებერვალი|მარტი|აპრილი|მაისი|ივნისი|ივლისი|აგვისტო|"
r"სექტემბერი|ოქტომბერი|ნოემბერი|დეკემბერი)\s*,?\s*(\d{4})"
)
def _fetch_case(nid: int) -> tuple[str, str] | None:
"""Return (iso_date, case_number) for a case page, or None on error."""
url = URL_TEMPLATE.format(nid=nid)
try:
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req, timeout=30) as resp:
html = resp.read().decode("utf-8", errors="replace")
except Exception as e:
sys.stderr.write(f" {nid}: ERROR - {e}\n")
return None
case_m = _CASE_RE_PRIMARY.search(html) or _CASE_RE_FALLBACK.search(html[:5000])
case_id = case_m.group(1) if case_m else f"case_{nid}"
iso_date = ""
date_m = _DATE_DMY.search(html)
if date_m:
iso_date = f"{date_m.group(3)}-{_GEO_MONTHS.get(date_m.group(2), '01')}-{date_m.group(1).zfill(2)}"
else:
date_m = _DATE_YDM.search(html)
if date_m:
year = date_m.group(1)
day = date_m.group(2).zfill(2)
month = _GEO_MONTHS.get(date_m.group(3), "01")
iso_date = f"{year}-{month}-{day}"
if not iso_date:
date_m = _DATE_DMY_SPACED.search(html)
if date_m:
iso_date = f"{date_m.group(3)}-{_GEO_MONTHS.get(date_m.group(2), '01')}-{date_m.group(1).zfill(2)}"
return iso_date, case_id
class GEScraper(BaseScraper):
country = "Georgien"
def scrape(
self,
start_date: date | None = None,
end_date: date | None = None,
) -> list[Case]:
ids = _all_case_ids()
log.info("GE total candidate case IDs: %d", len(ids))
cases: list[Case] = []
for nid in ids:
res = _fetch_case(nid)
if res is None:
time.sleep(DELAY_SECONDS)
continue
iso_date, case_id = res
decision_date: date | None = None
if iso_date:
try:
decision_date = date.fromisoformat(iso_date)
except ValueError:
decision_date = None
if start_date and decision_date and decision_date < start_date:
time.sleep(DELAY_SECONDS)
continue
if end_date and decision_date and decision_date > end_date:
time.sleep(DELAY_SECONDS)
continue
cases.append(Case(
case_id=case_id,
link=URL_TEMPLATE.format(nid=nid),
decision_date=decision_date,
jurisdiction="ge",
language="ka",
full_text=None,
metadata={"site_id": nid},
))
sys.stderr.write(f" {nid}: date={iso_date}, case={case_id}\n")
time.sleep(DELAY_SECONDS)
cases.sort(key=lambda c: c.decision_date or date.min, reverse=True)
log.info("Collected %d Georgia cases", len(cases))
return cases