Datasets:
Deepen cy: 261 laws / 3619 arts (honest counts)
Browse filesFiles changed (6)
- MANIFEST.md+3-7
- README.md+6-18
- data/articles.parquet+2-2
- data/laws.parquet+2-2
- scrapers/collect_cy.py+9-7
- scrapers/world_lib.pyAdded+302
123456789# MANIFEST- generated_at: 2026-09-04T17:50:59Z- laws: 198- articles: 2690- files:- data/laws.parquet: 9537903 bytes- data/articles.parquet: 6783079 bytes- README.md: 1670 bytes12345# MANIFEST- generated_at: 2026-09-04T19:25:23Z- laws: 261- articles: 3619
34 unmodified lines35363738394041424344454647484950515253545556575859606162636434 unmodified lines| Coverage | catalog-backed incomplete || Source | GPO mof.gov.cy || Collector | `scrapers/collect_cy.py` || Laws / instruments | 198 || Articles | 2690 || Language | el || Jurisdiction | Cyprus || License | `cyprus-gpo-gazette` |## Contents- `data/laws.parquet` — one row per instrument (id, title, full text, ELI,dates, status, identifiers, article count, source URL).- `data/articles.parquet` — one row per article/section (`law_id`, articlenumber, title, text, source URL).- `scrapers/collect_cy.py` — research collector used to build this snapshot.- `scrapers/common.py` — shared collector helpers.## License / reuseOfficial government / gazette texts; reuse per public-sector terms of thesource portal. This packaging, metadata, and collector script are providedfor research.This dataset is **not** an official consolidation and is **not legal advice**.The official source prevails.## NotesRound 4: +71 GPO PDFs (Wayback-prefer + skip known fails); honest article split → 2690 arts across 87/198 laws. Not legal advice.34 unmodified lines35363738394041424344454647484950515234 unmodified lines| Coverage | catalog-backed incomplete || Source | GPO mof.gov.cy || Collector | `scrapers/collect_cy.py` || Laws / instruments | 261 || Articles | 3619 || Language | el || Jurisdiction | Cyprus || License | `cyprus-gpo-gazette` |## Contents- `data/laws.parquet` — one row per instrument- `data/articles.parquet` — one row per article/section- `scrapers/collect_cy.py` — research collector## NotesDeepen: official GPO gazette PDFs (no cylaw.org). Honest Άρθρο/Αριθμός/ΜΕΡΟΣ heading splits (>=2). README matches parquet. Not legal advice.
123version https://git-lfs.github.com/spec/v1oid sha256:d20e7f4cf526643ee657d822713ae2f1243203dd75454f150d7fd8f9166bd92esize 6783079123version https://git-lfs.github.com/spec/v1oid sha256:8c386514b14360d36674fe1e1a52b1f043a30ddafaa1f3444cad5f1415b2eaa8size 8349005
123version https://git-lfs.github.com/spec/v1oid sha256:180661ed8a35bfc2bbeb6d9ef7d048d8b0e65753267b32b50ad2a1baadb966fesize 9537903123version https://git-lfs.github.com/spec/v1oid sha256:1dbcfbebc5796a555aadd04cc8c7e79d44ddb3b776a27eabb34c82a5c65eb802size 11900105
49 unmodified lines5051525354555686 unmodified lines14314414514614714814915015115215315449 unmodified linesfor verify in order:try:time.sleep(0.35)r = get_session(UA).get(url, timeout=(10, 45), verify=verify,headers={"User-Agent": UA, "Accept": "text/html, application/pdf, */*"})return r, verifyexcept Exception as exc:86 unmodified lineslog.info("wayback pdf %s: %s", url[-50:], exc)return Noneorder = (_wb, _live) if (prefer_wayback and ts) else (_live, _wb)for fn in order:if fn is _wb:got = _wb(ts)else:got = _live()if got:return gotreturn "", url, "none"49 unmodified lines5051525354555686 unmodified lines14314414514614714814915015115215315415515649 unmodified linesfor verify in order:try:time.sleep(0.35)r = get_session(UA).get(url, timeout=(5, 15), verify=verify,headers={"User-Agent": UA, "Accept": "text/html, application/pdf, */*"})return r, verifyexcept Exception as exc:86 unmodified lineslog.info("wayback pdf %s: %s", url[-50:], exc)return None# Live mof.gov.cy often hangs; when CDX ts is known, Wayback-only.if prefer_wayback and ts:got = _wb(ts)if got:return gotreturn "", url, "none"for fn in (_live, _wb):got = fn(ts) if fn is _wb else _live()if got:return gotreturn "", url, "none"
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302#!/usr/bin/env python3"""Shared helpers for new-country official gazette collectors."""from __future__ import annotations
import loggingimport osimport reimport timeimport subprocessimport sysimport tempfilefrom pathlib import Pathfrom typing import Optionalfrom urllib.parse import urljoin, urlparse
sys.path.insert(0, str(Path(__file__).resolve().parent))from common import ( ROOT, DEFAULT_UA, base_record, ensure_dirs, existing_ids, html_to_text, http_get, iso_date, log_failure, slug_id, split_articles, utcnow, write_instrument, write_summary,)import archive_fallbacks as af
log = logging.getLogger("world_lib")
MIN_TEXT = 80MAX_PDF = 35 * 1024 * 1024
def setup_log(cc: str, name: str | None = None) -> None: ensure_dirs(cc) logging.basicConfig( level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s", handlers=[ logging.FileHandler(ROOT / cc / "logs" / "collector.log", encoding="utf-8"), logging.StreamHandler(sys.stdout), ], force=True, )
def pdf_to_text(raw: bytes) -> str: if not raw or raw[:4] != b"%PDF": return "" try: with tempfile.NamedTemporaryFile(suffix=".pdf", delete=True) as tmp: tmp.write(raw) tmp.flush() proc = subprocess.run( ["pdftotext", "-layout", "-enc", "UTF-8", tmp.name, "-"], check=False, capture_output=True, timeout=180, ) if proc.returncode == 0 and proc.stdout: return proc.stdout.decode("utf-8", "replace").strip() except Exception as exc: log.warning("pdftotext: %s", exc) return ""
def split_custom(text: str, law_id: str, source_url: str, date: Optional[str], pattern: Optional[re.Pattern]) -> list[dict]: docs = split_articles(text, law_id, source_url, date) if not pattern: return docs matches = list(pattern.finditer(text or "")) if len(matches) < 2: return docs if len(docs) >= 2 else [] out = [] for i, m in enumerate(matches): start = m.start() end = matches[i + 1].start() if i + 1 < len(matches) else len(text) chunk = text[start:end].strip() if len(chunk) < 20: continue num = re.sub(r"\s+", " ", m.group(1)).strip() aid = re.sub(r"[^a-zA-Z0-9\u0400-\u04FF\u0530-\u058F-]+", "-", num.lower()).strip("-")[:80] heading = chunk.split("\n", 1)[0][:200] out.append({ "id": f"{law_id}-{aid}"[:180], "title": heading, "text": chunk, "date_filed": date, "document_number": num, "source_url": source_url, "record_type": "article", "article_number": num, "law_identifier": law_id, "metadata": {"text_extraction": {"source": "official", "backend": "collector"}}, }) if len(out) >= 4000: break return out if len(out) >= 2 else docs
def live_get(url: str, *, ua: str = DEFAULT_UA, timeout: tuple = (20, 90), verify: bool = True, retries: int = 3): import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) last = None headers = {"User-Agent": ua, "Accept": "application/pdf, text/html, application/xml, */*"} for attempt in range(1, retries + 1): try: time.sleep(0.2) resp = requests.get(url, timeout=timeout, headers=headers, verify=verify, allow_redirects=True) return resp except Exception as exc: last = exc if verify and ("SSL" in type(exc).__name__ or "SSL" in str(exc)): try: resp = requests.get(url, timeout=timeout, headers=headers, verify=False, allow_redirects=True) return resp except Exception as exc2: last = exc2 time.sleep(min(8, 1.5 * attempt)) raise RuntimeError(f"live_get failed {url}: {last}")
def fetch_official( url: str, *, ua: str = DEFAULT_UA, verify: bool = True, wayback: bool = True, wayback_ts: Optional[str] = None, min_text: int = MIN_TEXT,) -> dict: """Live official URL first; Wayback of the same official URL on failure. No WAF bypass.""" out = { "status": "error", "text": "", "content": b"", "source_url": url, "method": "", "content_type": "", "error": "", } body = b"" ctype = "" try: r = live_get(url, ua=ua, verify=verify) body = r.content or b"" ctype = r.headers.get("content-type") or "" if r.status_code == 200 and body: if body[:4] == b"%PDF" or "pdf" in ctype.lower(): text = pdf_to_text(body) if len(text) >= min_text: out.update(status="success", text=text, content=body, method="http_pdf", content_type=ctype) return out text = "" if "html" in ctype.lower() or "xml" in ctype.lower() or "json" in ctype.lower() or body[:1] in (b"<", b"{", b"["): raw = body.decode(r.encoding or "utf-8", "replace") if af.is_challenge(raw, r.status_code): out["error"] = "challenge_or_shell" else: text = html_to_text(raw) if "<" in raw[:500] else raw if len(text) >= min_text and not af.is_spa_shell(raw, text): out.update(status="success", text=text, content=body, method="http_html", content_type=ctype) return out if len(text) < min_text: out["error"] = "short_or_spa" elif body[:4] == b"%PDF": out["error"] = "pdf_extract_failed" elif len(body) > min_text: try: text = body.decode("utf-8", "replace") if text.lstrip().startswith("%PDF") or "endobj" in text[:200]: out["error"] = "raw_pdf_bytes" elif len(text) >= min_text: out.update(status="success", text=text, content=body, method="http_text", content_type=ctype) return out except Exception: pass else: out["error"] = f"http_{getattr(r, 'status_code', '?')}" except Exception as exc: out["error"] = f"live:{exc}"
if wayback: try: w = af.get_wayback_content(url, timestamp=wayback_ts) if w.get("status") == "success": raw_b = w.get("content") or b"" raw_t = w.get("text") or "" if (isinstance(raw_b, bytes) and raw_b[:4] == b"%PDF") or (raw_t[:4] == "%PDF"): pdf_bytes = raw_b if isinstance(raw_b, bytes) and raw_b[:4] == b"%PDF" else raw_t.encode("latin-1", "replace") text = pdf_to_text(pdf_bytes) if len(text) >= min_text and not text.lstrip().startswith("%PDF"): out.update( status="success", text=text, content=pdf_bytes, method="wayback_pdf", source_url=w.get("url") or url, content_type="application/pdf", ) return out out["error"] = (out.get("error") or "") + ";pdf_extract_failed" return out if not raw_t and isinstance(raw_b, bytes): raw_t = raw_b.decode("utf-8", "replace") if raw_t.lstrip().startswith("%PDF") or "endobj" in raw_t[:200]: out["error"] = (out.get("error") or "") + ";raw_pdf_bytes" return out text = html_to_text(raw_t) if "<" in (raw_t[:400] or "") else raw_t if len(text) >= min_text and not af.is_challenge(raw_t, 200): out.update( status="success", text=text, content=raw_b if isinstance(raw_b, bytes) else raw_t.encode("utf-8", "replace"), method="wayback_html", source_url=w.get("url") or url, content_type=w.get("content_type") or "text/html", ) return out out["error"] = (out.get("error") or "") + ";wayback_short" else: out["error"] = (out.get("error") or "") + f";wayback:{w.get('error')}" except Exception as exc: out["error"] = (out.get("error") or "") + f";wayback:{exc}" return out
def first_title(text: str, fallback: str) -> str: for line in (text or "").splitlines(): s = line.strip() if len(s) >= 12 and not s.lower().startswith("just a moment"): return s[:240] return fallback[:240]
def save_instrument( *, cc: str, country: str, language: str, ident: str, title: str, text: str, source_url: str, source_type: str, license_text: str, collector: str, date: Optional[str] = None, article_re: Optional[re.Pattern] = None, extra_meta: Optional[dict] = None,) -> bool: if not text or len(text) < MIN_TEXT: return False rid = slug_id(cc, ident) docs = split_custom(text, rid, source_url, date, article_re) rec = base_record( cc=cc, country=country, language=language, ident=ident, title=title or first_title(text, ident), text=text, source_url=source_url, source_type=source_type, license_text=license_text, collector=collector, date=iso_date(date) or date, documents=docs, extra_meta=extra_meta, ) write_instrument(cc, rec) return True
def cdx_urls(host_or_url: str, *, limit: int = 200, match_type: str = "prefix", extra_filters=None) -> list[dict]: try: hits = af.search_wayback_machine( host_or_url, limit=limit, match_type=match_type, collapse="urlkey", extra_filters=extra_filters, ) except Exception as exc: log.info("cdx err %s: %s", host_or_url, exc) return [] return hits or []
def env_int(name: str, default: int) -> int: try: return int(os.environ.get(name, str(default)) or default) except Exception: return defaultMANIFEST.md+3-7
README.md+6-18
data/articles.parquet+2-2
data/laws.parquet+2-2
scrapers/collect_cy.py+9-7
scrapers/world_lib.pyAdded+302