Upload folder using huggingface_hub
Browse files- MANIFEST.md +21 -14
- README.md +27 -23
- data/articles.parquet +2 -2
- data/laws.parquet +2 -2
- data/manifest.json +9 -9
- scrapers/archive_fallbacks.py +188 -141
- scrapers/collect_om.py +8 -1
- scrapers/common.py +9 -10
MANIFEST.md
CHANGED
|
@@ -1,21 +1,28 @@
|
|
| 1 |
# ipfs_oman_laws manifest
|
| 2 |
|
| 3 |
-
generated_utc: 2026-09-
|
| 4 |
cc: om
|
| 5 |
-
|
| 6 |
laws_rows: 1679
|
| 7 |
-
articles_rows:
|
| 8 |
-
laws_bytes:
|
| 9 |
-
articles_bytes:
|
| 10 |
-
laws_sha256:
|
| 11 |
-
articles_sha256:
|
| 12 |
-
laws_parquet: /workspace/legal-corpora-
|
| 13 |
-
articles_parquet: /workspace/legal-corpora-
|
| 14 |
compression: zstd
|
| 15 |
collector: scrapers/collect_om.py
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
uploaded: false
|
| 20 |
-
gaps:
|
| 21 |
-
|
|
|
|
|
|
|
|
|
| 1 |
# ipfs_oman_laws manifest
|
| 2 |
|
| 3 |
+
generated_utc: 2026-09-16T21:49:25Z
|
| 4 |
cc: om
|
| 5 |
+
country: Oman
|
| 6 |
laws_rows: 1679
|
| 7 |
+
articles_rows: 1996
|
| 8 |
+
laws_bytes: 8018895
|
| 9 |
+
articles_bytes: 980053
|
| 10 |
+
laws_sha256: 97352dd73118798b45fa0a4512732b65e02dc5857ad15aeaa90d0365dbcd4202
|
| 11 |
+
articles_sha256: 82235890e69b15456b6221fc45ce6786e9ee8210cfebbfe8d61b0c0fbdb6cc26
|
| 12 |
+
laws_parquet: /workspace/legal-corpora/hf-staging/ipfs_oman_laws/data/laws.parquet
|
| 13 |
+
articles_parquet: /workspace/legal-corpora/hf-staging/ipfs_oman_laws/data/articles.parquet
|
| 14 |
compression: zstd
|
| 15 |
collector: scrapers/collect_om.py
|
| 16 |
+
license: Official MJLA texts; authentic Official Gazette prevails; not legal advice.
|
| 17 |
+
source: https://mjla.gov.om/
|
| 18 |
+
retrieval: live-official
|
| 19 |
+
live_instruments: 1679
|
| 20 |
+
archive_instruments: 0
|
| 21 |
+
wayback_instruments: 0
|
| 22 |
+
common_crawl_instruments: 0
|
| 23 |
+
constitution_present: false
|
| 24 |
uploaded: false
|
| 25 |
+
gaps: |
|
| 26 |
+
In-force laws and royal decrees from official mjla.gov.om. النظام الأساسي للدولة (decree 951) included as Constitution. qanoon.om is not the official MJLA portal and was not used. TLS verify=False only after SSLError (missing intermediate; not WAF bypass). 429/403 uses archive_fallbacks of official URLs. Started 2026-09-03T05:44:14Z.
|
| 27 |
+
|
| 28 |
+
started 2026-09-03T05:44:14Z
|
README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
---
|
| 2 |
-
pretty_name: Laws
|
| 3 |
license: other
|
| 4 |
-
license_name: oman-
|
| 5 |
language:
|
| 6 |
- ar
|
| 7 |
task_categories:
|
|
@@ -10,6 +10,8 @@ tags:
|
|
| 10 |
- legal
|
| 11 |
- law
|
| 12 |
- oman
|
|
|
|
|
|
|
| 13 |
- official-texts
|
| 14 |
size_categories:
|
| 15 |
- 1K<n<10K
|
|
@@ -21,44 +23,46 @@ configs:
|
|
| 21 |
data_files: data/articles.parquet
|
| 22 |
---
|
| 23 |
|
| 24 |
-
# Laws
|
| 25 |
|
| 26 |
-
Research snapshot of official legislation
|
| 27 |
|
| 28 |
-
**Not legal advice.**
|
| 29 |
|
| 30 |
## Snapshot
|
| 31 |
|
| 32 |
| Field | Value |
|
| 33 |
| --- | --- |
|
| 34 |
-
| Snapshot date | 2026-09-
|
| 35 |
-
| Coverage |
|
| 36 |
-
| Source |
|
| 37 |
| Collector | `scrapers/collect_om.py` |
|
| 38 |
-
| Laws / instruments |
|
| 39 |
-
| Articles |
|
| 40 |
| Language | ar |
|
| 41 |
| Jurisdiction | Oman |
|
| 42 |
-
| License | `oman-
|
| 43 |
|
| 44 |
## Contents
|
| 45 |
|
| 46 |
-
- `data/laws.parquet` — one row per instrument (
|
| 47 |
-
|
| 48 |
-
- `data/articles.parquet` — one row per article/section (`law_id`, article
|
| 49 |
-
number, title, text, source URL).
|
| 50 |
- `scrapers/collect_om.py` — research collector used to build this snapshot.
|
| 51 |
-
- `scrapers/common.py` — shared collector helpers.
|
| 52 |
|
| 53 |
## License / reuse
|
| 54 |
|
| 55 |
-
Official
|
| 56 |
-
source portal. This packaging, metadata, and collector script are provided
|
| 57 |
-
for research.
|
| 58 |
|
| 59 |
-
This dataset is **not**
|
| 60 |
-
The official source prevails.
|
| 61 |
|
| 62 |
-
##
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
pretty_name: Oman Basic Statute, Laws and Royal Decrees (MJLA)
|
| 3 |
license: other
|
| 4 |
+
license_name: oman-mjla-terms
|
| 5 |
language:
|
| 6 |
- ar
|
| 7 |
task_categories:
|
|
|
|
| 10 |
- legal
|
| 11 |
- law
|
| 12 |
- oman
|
| 13 |
+
- mjla
|
| 14 |
+
- official-gazette
|
| 15 |
- official-texts
|
| 16 |
size_categories:
|
| 17 |
- 1K<n<10K
|
|
|
|
| 23 |
data_files: data/articles.parquet
|
| 24 |
---
|
| 25 |
|
| 26 |
+
# Oman Basic Statute, Laws and Royal Decrees (MJLA)
|
| 27 |
|
| 28 |
+
Research snapshot of official national legislation from **Ministry of Justice and Legal Affairs (mjla.gov.om)**.
|
| 29 |
|
| 30 |
+
**Not legal advice.** The official gazette / authentic source prevails over this corpus.
|
| 31 |
|
| 32 |
## Snapshot
|
| 33 |
|
| 34 |
| Field | Value |
|
| 35 |
| --- | --- |
|
| 36 |
+
| Snapshot date | 2026-09-16 |
|
| 37 |
+
| Coverage | snapshot |
|
| 38 |
+
| Source | Ministry of Justice and Legal Affairs (mjla.gov.om) |
|
| 39 |
| Collector | `scrapers/collect_om.py` |
|
| 40 |
+
| Laws / instruments | 1,679 |
|
| 41 |
+
| Articles | 1,996 |
|
| 42 |
| Language | ar |
|
| 43 |
| Jurisdiction | Oman |
|
| 44 |
+
| License | `oman-mjla-terms` |
|
| 45 |
|
| 46 |
## Contents
|
| 47 |
|
| 48 |
+
- `data/laws.parquet` — one row per instrument (LAW_FLAT, zstd).
|
| 49 |
+
- `data/articles.parquet` — article/section rows for the same snapshot.
|
|
|
|
|
|
|
| 50 |
- `scrapers/collect_om.py` — research collector used to build this snapshot.
|
|
|
|
| 51 |
|
| 52 |
## License / reuse
|
| 53 |
|
| 54 |
+
Official MJLA texts; authentic Official Gazette prevails; not legal advice.
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
This dataset is **not legal advice**. The official source prevails.
|
|
|
|
| 57 |
|
| 58 |
+
## Source
|
| 59 |
|
| 60 |
+
- https://mjla.gov.om/
|
| 61 |
+
|
| 62 |
+
## Known gaps
|
| 63 |
+
|
| 64 |
+
Basic Statute + laws + royal decrees from official mjla.gov.om. qanoon.om is not the official MJLA portal and was not used.
|
| 65 |
+
|
| 66 |
+
In-force laws and royal decrees from official mjla.gov.om. النظام الأساسي للدولة (decree 951) included as Constitution. qanoon.om is not the official MJLA portal and was not used. TLS verify=False only after SSLError (missing intermediate; not WAF bypass). 429/403 uses archive_fallbacks of official URLs. Started 2026-09-03T05:44:14Z.
|
| 67 |
+
|
| 68 |
+
started 2026-09-03T05:44:14Z
|
data/articles.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:82235890e69b15456b6221fc45ce6786e9ee8210cfebbfe8d61b0c0fbdb6cc26
|
| 3 |
+
size 980053
|
data/laws.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:97352dd73118798b45fa0a4512732b65e02dc5857ad15aeaa90d0365dbcd4202
|
| 3 |
+
size 8018895
|
data/manifest.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
{
|
| 2 |
"slug": "ipfs_oman_laws",
|
| 3 |
"cc": "om",
|
| 4 |
-
"laws_rows":
|
| 5 |
-
"articles_rows":
|
| 6 |
-
"laws_bytes":
|
| 7 |
-
"articles_bytes":
|
| 8 |
-
"laws_sha256": "
|
| 9 |
-
"articles_sha256": "
|
| 10 |
"compression": "zstd",
|
| 11 |
"collector": "collect_om.py",
|
| 12 |
"license": "Official MJLA texts; authentic Official Gazette prevails; not legal advice.",
|
| 13 |
-
"live_instruments":
|
| 14 |
"archive_instruments": 0,
|
| 15 |
-
"constitution_present":
|
| 16 |
"uploaded": false,
|
| 17 |
-
"generated_utc": "2026-09-
|
| 18 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"slug": "ipfs_oman_laws",
|
| 3 |
"cc": "om",
|
| 4 |
+
"laws_rows": 1679,
|
| 5 |
+
"articles_rows": 1996,
|
| 6 |
+
"laws_bytes": 8018895,
|
| 7 |
+
"articles_bytes": 980053,
|
| 8 |
+
"laws_sha256": "97352dd73118798b45fa0a4512732b65e02dc5857ad15aeaa90d0365dbcd4202",
|
| 9 |
+
"articles_sha256": "82235890e69b15456b6221fc45ce6786e9ee8210cfebbfe8d61b0c0fbdb6cc26",
|
| 10 |
"compression": "zstd",
|
| 11 |
"collector": "collect_om.py",
|
| 12 |
"license": "Official MJLA texts; authentic Official Gazette prevails; not legal advice.",
|
| 13 |
+
"live_instruments": 1679,
|
| 14 |
"archive_instruments": 0,
|
| 15 |
+
"constitution_present": false,
|
| 16 |
"uploaded": false,
|
| 17 |
+
"generated_utc": "2026-09-16T21:49:25Z"
|
| 18 |
}
|
scrapers/archive_fallbacks.py
CHANGED
|
@@ -24,8 +24,10 @@ from requests.adapters import HTTPAdapter
|
|
| 24 |
|
| 25 |
log = logging.getLogger("archive_fallbacks")
|
| 26 |
|
| 27 |
-
WAYBACK_CDX = "
|
| 28 |
-
WAYBACK_WEB = "
|
|
|
|
|
|
|
| 29 |
CC_COLLINFO = "https://index.commoncrawl.org/collinfo.json"
|
| 30 |
CC_DATA = "https://data.commoncrawl.org"
|
| 31 |
ARCHIVE_IS_NEWEST = "https://archive.is/newest/"
|
|
@@ -55,8 +57,6 @@ _cc_indexes: Optional[list[str]] = None
|
|
| 55 |
_cc_dead: set[str] = set()
|
| 56 |
_last_wayback = 0.0
|
| 57 |
_last_cc = 0.0
|
| 58 |
-
_ssl_fail_streak = 0
|
| 59 |
-
_ssl_circuit_until = 0.0
|
| 60 |
|
| 61 |
|
| 62 |
def session() -> requests.Session:
|
|
@@ -89,26 +89,6 @@ def _sleep_cc(delay: float = 0.35) -> None:
|
|
| 89 |
_last_cc = time.time()
|
| 90 |
|
| 91 |
|
| 92 |
-
|
| 93 |
-
def _ssl_circuit_open() -> bool:
|
| 94 |
-
return time.time() < _ssl_circuit_until
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
def _note_ssl_fail() -> None:
|
| 98 |
-
global _ssl_fail_streak, _ssl_circuit_until
|
| 99 |
-
_ssl_fail_streak += 1
|
| 100 |
-
if _ssl_fail_streak >= 3:
|
| 101 |
-
cool = min(90.0, 12.0 * _ssl_fail_streak)
|
| 102 |
-
_ssl_circuit_until = time.time() + cool
|
| 103 |
-
log.info("ssl circuit-open for %.0fs (streak=%s)", cool, _ssl_fail_streak)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
def _note_ssl_ok() -> None:
|
| 107 |
-
global _ssl_fail_streak, _ssl_circuit_until
|
| 108 |
-
_ssl_fail_streak = 0
|
| 109 |
-
_ssl_circuit_until = 0.0
|
| 110 |
-
|
| 111 |
-
|
| 112 |
def has_brave_key() -> bool:
|
| 113 |
return bool(
|
| 114 |
(os.environ.get("BRAVE_API_KEY") or os.environ.get("BRAVE_SEARCH_API_KEY") or "").strip()
|
|
@@ -138,7 +118,7 @@ def is_spa_shell(html: str, text: str) -> bool:
|
|
| 138 |
|
| 139 |
# --- Wayback ---
|
| 140 |
|
| 141 |
-
CDX_PAGE =
|
| 142 |
|
| 143 |
|
| 144 |
def _parse_cdx_json(data: Any, mime_prefix: Optional[str] = None) -> tuple[list[dict], Optional[str]]:
|
|
@@ -222,37 +202,25 @@ def _wayback_cdx_page(
|
|
| 222 |
items.append(("filter", flt))
|
| 223 |
if resume_key:
|
| 224 |
items.append(("resumeKey", resume_key))
|
| 225 |
-
if _ssl_circuit_open():
|
| 226 |
-
log.info("wayback cdx skip (ssl circuit-open) %s", url)
|
| 227 |
-
return [], None
|
| 228 |
_sleep_wayback(1.0)
|
| 229 |
last_err = None
|
| 230 |
-
|
| 231 |
-
for attempt, psz in enumerate(ladder, 1):
|
| 232 |
if attempt > 1:
|
| 233 |
items = [(k, psz if k == "limit" else v) for k, v in items]
|
| 234 |
-
_sleep_wayback(
|
| 235 |
try:
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
except requests.RequestException as exc:
|
| 238 |
last_err = exc
|
| 239 |
log.warning("wayback cdx fail %s: %s", url, exc)
|
| 240 |
-
if "SSL" in type(exc).__name__ or "SSL" in str(exc) or "EOF" in str(exc):
|
| 241 |
-
_note_ssl_fail()
|
| 242 |
-
if _ssl_circuit_open():
|
| 243 |
-
break
|
| 244 |
-
time.sleep(min(8.0, 1.5 * attempt))
|
| 245 |
-
continue
|
| 246 |
-
if r.status_code == 429:
|
| 247 |
-
last_err = "http_429"
|
| 248 |
-
cool = min(60.0, 8.0 * attempt)
|
| 249 |
-
log.info("wayback cdx HTTP 429 for %s (page_size=%s) cool=%.1fs", url, psz, cool)
|
| 250 |
-
time.sleep(cool)
|
| 251 |
continue
|
| 252 |
if r.status_code in (502, 503, 504) or not r.content:
|
| 253 |
last_err = f"http_{r.status_code}"
|
| 254 |
log.info("wayback cdx HTTP %s for %s (page_size=%s)", r.status_code, url, psz)
|
| 255 |
-
time.sleep(min(20.0, 3.0 * attempt))
|
| 256 |
continue
|
| 257 |
if r.status_code != 200:
|
| 258 |
log.info("wayback cdx HTTP %s for %s", r.status_code, url)
|
|
@@ -261,7 +229,6 @@ def _wayback_cdx_page(
|
|
| 261 |
data = r.json()
|
| 262 |
except Exception:
|
| 263 |
return [], None
|
| 264 |
-
_note_ssl_ok()
|
| 265 |
return _parse_cdx_json(data, mime_prefix=mime_prefix)
|
| 266 |
log.info("wayback cdx gave up %s last=%s", url, last_err)
|
| 267 |
return [], None
|
|
@@ -321,55 +288,160 @@ def search_wayback_machine(
|
|
| 321 |
return out[:target]
|
| 322 |
|
| 323 |
|
|
|
|
| 324 |
def get_wayback_content(url: str, timestamp: Optional[str] = None) -> dict:
|
| 325 |
-
"""Fetch a snapshot; id_ modifier skips Wayback chrome. No CDX search.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
if timestamp:
|
| 327 |
-
|
| 328 |
else:
|
| 329 |
-
|
| 330 |
-
|
| 331 |
last_err = None
|
| 332 |
-
|
| 333 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
try:
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
last_err = str(exc)
|
| 338 |
log.info("wayback replay fail %s: %s", url, exc)
|
|
|
|
| 339 |
continue
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
cap_ts = timestamp or ""
|
| 347 |
-
m = re.search(r"/web/(\d{14})",
|
| 348 |
if m:
|
| 349 |
cap_ts = m.group(1)
|
| 350 |
-
ctype = r.headers.get("content-type") or ""
|
| 351 |
-
body = r.content or b""
|
| 352 |
text = ""
|
| 353 |
if "html" in ctype or "xml" in ctype or "text/" in ctype or not ctype:
|
| 354 |
-
enc = r.encoding or "utf-8"
|
| 355 |
try:
|
| 356 |
-
text = body.decode(enc, "replace")
|
| 357 |
-
except Exception:
|
| 358 |
text = body.decode("utf-8", "replace")
|
| 359 |
-
|
|
|
|
|
|
|
| 360 |
return {
|
| 361 |
"status": "error",
|
| 362 |
-
"error": f"http_{
|
| 363 |
-
"http_status":
|
| 364 |
-
"wayback_url":
|
| 365 |
"original_url": url,
|
| 366 |
}
|
| 367 |
-
if text and is_challenge(text,
|
| 368 |
return {
|
| 369 |
"status": "error",
|
| 370 |
"error": "challenge_or_shell",
|
| 371 |
-
"http_status":
|
| 372 |
-
"wayback_url":
|
| 373 |
"original_url": url,
|
| 374 |
"content": text[:2000],
|
| 375 |
}
|
|
@@ -378,58 +450,50 @@ def get_wayback_content(url: str, timestamp: Optional[str] = None) -> dict:
|
|
| 378 |
"content": body,
|
| 379 |
"text": text,
|
| 380 |
"content_type": ctype,
|
| 381 |
-
"wayback_url":
|
| 382 |
"capture_timestamp": cap_ts,
|
| 383 |
"original_url": url,
|
| 384 |
-
"http_status":
|
| 385 |
-
"method": "
|
| 386 |
}
|
| 387 |
|
| 388 |
|
| 389 |
# --- Common Crawl ---
|
| 390 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
def cc_indexes(n: int = 6) -> list[str]:
|
| 392 |
global _cc_indexes
|
| 393 |
if _cc_indexes is None:
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
except Exception as exc:
|
| 420 |
-
log.warning("cc collinfo: %s", exc)
|
| 421 |
-
_cc_indexes = [
|
| 422 |
-
"https://index.commoncrawl.org/CC-MAIN-2024-33-index",
|
| 423 |
-
"https://index.commoncrawl.org/CC-MAIN-2024-42-index",
|
| 424 |
-
"https://index.commoncrawl.org/CC-MAIN-2024-51-index",
|
| 425 |
-
"https://index.commoncrawl.org/CC-MAIN-2025-08-index",
|
| 426 |
-
"https://index.commoncrawl.org/CC-MAIN-2025-18-index",
|
| 427 |
-
"https://index.commoncrawl.org/CC-MAIN-2025-33-index",
|
| 428 |
-
"https://index.commoncrawl.org/CC-MAIN-2026-08-index",
|
| 429 |
-
"https://index.commoncrawl.org/CC-MAIN-2026-25-index",
|
| 430 |
-
"https://index.commoncrawl.org/CC-MAIN-2026-30-index",
|
| 431 |
-
"https://index.commoncrawl.org/CC-MAIN-2026-34-index",
|
| 432 |
-
]
|
| 433 |
return _cc_indexes[:n]
|
| 434 |
|
| 435 |
|
|
@@ -451,29 +515,16 @@ def search_common_crawl(url: str, *, limit: int = 80, indexes: Optional[list[str
|
|
| 451 |
"output": "json",
|
| 452 |
"limit": str(limit),
|
| 453 |
}
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
for attempt in range(1, tries + 1):
|
| 460 |
-
_sleep_cc(0.35 if attempt == 1 else min(4.0, 1.2 * attempt))
|
| 461 |
try:
|
| 462 |
-
r = session().get(
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
log.info("cc cdx %s fail attempt=%s %s", idx.split("/")[-1], attempt, exc)
|
| 467 |
-
if "SSL" in type(exc).__name__ or "SSL" in str(exc) or "EOF" in str(exc):
|
| 468 |
-
_note_ssl_fail()
|
| 469 |
-
time.sleep(min(6.0, 1.5 * attempt))
|
| 470 |
-
r = None
|
| 471 |
-
if _ssl_circuit_open():
|
| 472 |
-
break
|
| 473 |
-
if r is None:
|
| 474 |
-
if _ssl_circuit_open():
|
| 475 |
-
break
|
| 476 |
-
continue
|
| 477 |
if r.status_code == 404:
|
| 478 |
msg = (r.text or "")[:80]
|
| 479 |
log.info("cc cdx no-captures %s %s", idx.split("/")[-1], msg)
|
|
@@ -485,10 +536,6 @@ def search_common_crawl(url: str, *, limit: int = 80, indexes: Optional[list[str
|
|
| 485 |
_cc_dead.add(idx)
|
| 486 |
log.info("cc cdx dead HTTP %s %s", r.status_code, idx.split("/")[-1])
|
| 487 |
continue
|
| 488 |
-
if r.status_code in (429, 502, 503, 504):
|
| 489 |
-
log.info("cc cdx HTTP %s %s (backoff)", r.status_code, idx.split("/")[-1])
|
| 490 |
-
time.sleep(8.0)
|
| 491 |
-
continue
|
| 492 |
if r.status_code != 200 or not r.content:
|
| 493 |
log.info("cc cdx HTTP %s %s", r.status_code, idx.split("/")[-1])
|
| 494 |
continue
|
|
@@ -514,7 +561,7 @@ def search_common_crawl(url: str, *, limit: int = 80, indexes: Optional[list[str
|
|
| 514 |
out.append(rec)
|
| 515 |
if out:
|
| 516 |
log.info("cc cdx %s hits=%s", idx.split("/")[-1], len(out))
|
| 517 |
-
if len(out) >= limit:
|
| 518 |
break
|
| 519 |
return out[:limit]
|
| 520 |
|
|
|
|
| 24 |
|
| 25 |
log = logging.getLogger("archive_fallbacks")
|
| 26 |
|
| 27 |
+
WAYBACK_CDX = "http://web.archive.org/cdx/search/cdx" # https SSLEOF from collector host
|
| 28 |
+
WAYBACK_WEB = "http://web.archive.org/web" # https SSLEOF from collector host
|
| 29 |
+
WAYBACK_CDX_HTTP = "http://web.archive.org/cdx/search/cdx"
|
| 30 |
+
WAYBACK_WEB_HTTP = "http://web.archive.org/web"
|
| 31 |
CC_COLLINFO = "https://index.commoncrawl.org/collinfo.json"
|
| 32 |
CC_DATA = "https://data.commoncrawl.org"
|
| 33 |
ARCHIVE_IS_NEWEST = "https://archive.is/newest/"
|
|
|
|
| 57 |
_cc_dead: set[str] = set()
|
| 58 |
_last_wayback = 0.0
|
| 59 |
_last_cc = 0.0
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
def session() -> requests.Session:
|
|
|
|
| 89 |
_last_cc = time.time()
|
| 90 |
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
def has_brave_key() -> bool:
|
| 93 |
return bool(
|
| 94 |
(os.environ.get("BRAVE_API_KEY") or os.environ.get("BRAVE_SEARCH_API_KEY") or "").strip()
|
|
|
|
| 118 |
|
| 119 |
# --- Wayback ---
|
| 120 |
|
| 121 |
+
CDX_PAGE = 200 # large single-shot limits (2000+) 504 on web.archive.org
|
| 122 |
|
| 123 |
|
| 124 |
def _parse_cdx_json(data: Any, mime_prefix: Optional[str] = None) -> tuple[list[dict], Optional[str]]:
|
|
|
|
| 202 |
items.append(("filter", flt))
|
| 203 |
if resume_key:
|
| 204 |
items.append(("resumeKey", resume_key))
|
|
|
|
|
|
|
|
|
|
| 205 |
_sleep_wayback(1.0)
|
| 206 |
last_err = None
|
| 207 |
+
for attempt, psz in enumerate((page_size, 120, 80), 1):
|
|
|
|
| 208 |
if attempt > 1:
|
| 209 |
items = [(k, psz if k == "limit" else v) for k, v in items]
|
| 210 |
+
_sleep_wayback(1.5)
|
| 211 |
try:
|
| 212 |
+
try:
|
| 213 |
+
# Prefer HTTP CDX — HTTPS to archive.org often SSLEOF from this host
|
| 214 |
+
r = session().get(WAYBACK_CDX_HTTP, params=items, timeout=(20, 90))
|
| 215 |
+
except requests.RequestException:
|
| 216 |
+
r = session().get(WAYBACK_CDX, params=items, timeout=(20, 90))
|
| 217 |
except requests.RequestException as exc:
|
| 218 |
last_err = exc
|
| 219 |
log.warning("wayback cdx fail %s: %s", url, exc)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
continue
|
| 221 |
if r.status_code in (502, 503, 504) or not r.content:
|
| 222 |
last_err = f"http_{r.status_code}"
|
| 223 |
log.info("wayback cdx HTTP %s for %s (page_size=%s)", r.status_code, url, psz)
|
|
|
|
| 224 |
continue
|
| 225 |
if r.status_code != 200:
|
| 226 |
log.info("wayback cdx HTTP %s for %s", r.status_code, url)
|
|
|
|
| 229 |
data = r.json()
|
| 230 |
except Exception:
|
| 231 |
return [], None
|
|
|
|
| 232 |
return _parse_cdx_json(data, mime_prefix=mime_prefix)
|
| 233 |
log.info("wayback cdx gave up %s last=%s", url, last_err)
|
| 234 |
return [], None
|
|
|
|
| 288 |
return out[:target]
|
| 289 |
|
| 290 |
|
| 291 |
+
|
| 292 |
def get_wayback_content(url: str, timestamp: Optional[str] = None) -> dict:
|
| 293 |
+
"""Fetch a snapshot; id_ modifier skips Wayback chrome. No CDX search.
|
| 294 |
+
|
| 295 |
+
Prefer HTTP Wayback via curl (requests follows http→https and SSL-EOFs here).
|
| 296 |
+
Follow 3xx Location only after rewriting https://web.archive.org → http://.
|
| 297 |
+
Redirect hops do not consume the HTTP retry budget.
|
| 298 |
+
"""
|
| 299 |
+
import subprocess
|
| 300 |
+
|
| 301 |
if timestamp:
|
| 302 |
+
path = f"/{timestamp}id_/{url}"
|
| 303 |
else:
|
| 304 |
+
path = f"/2id_/{url}"
|
| 305 |
+
target = WAYBACK_WEB_HTTP + path
|
| 306 |
last_err = None
|
| 307 |
+
body = b""
|
| 308 |
+
ctype = ""
|
| 309 |
+
final_url = target
|
| 310 |
+
status = 0
|
| 311 |
+
redirects_left = 8
|
| 312 |
+
attempts_left = 5
|
| 313 |
+
first = True
|
| 314 |
+
while attempts_left > 0:
|
| 315 |
+
_sleep_wayback(0.35 if not first and redirects_left < 8 else (0.8 if first else min(8.0, 1.8 * (6 - attempts_left))))
|
| 316 |
+
first = False
|
| 317 |
try:
|
| 318 |
+
# Stay on HTTP: no -L (redirects flip to https and SSLEOF on this host)
|
| 319 |
+
proc = subprocess.run(
|
| 320 |
+
[
|
| 321 |
+
"curl", "-sS", "--max-time", "90",
|
| 322 |
+
"-A", UA,
|
| 323 |
+
"-w", "\n__HTTP_STATUS__%{http_code}\n__CONTENT_TYPE__%{content_type}\n__URL_EFFECTIVE__%{url_effective}\n__REDIRECT_URL__%{redirect_url}",
|
| 324 |
+
"--output", "-",
|
| 325 |
+
target,
|
| 326 |
+
],
|
| 327 |
+
capture_output=True,
|
| 328 |
+
timeout=100,
|
| 329 |
+
)
|
| 330 |
+
raw = proc.stdout or b""
|
| 331 |
+
marker = b"\n__HTTP_STATUS__"
|
| 332 |
+
redirect_url = ""
|
| 333 |
+
if marker in raw:
|
| 334 |
+
body, rest = raw.rsplit(marker, 1)
|
| 335 |
+
trailer = (b"__HTTP_STATUS__" + rest).decode("utf-8", "replace")
|
| 336 |
+
mstat = re.search(r"__HTTP_STATUS__(\d+)", trailer)
|
| 337 |
+
mctype = re.search(r"__CONTENT_TYPE__([^\n]*)", trailer)
|
| 338 |
+
murl = re.search(r"__URL_EFFECTIVE__([^\n]*)", trailer)
|
| 339 |
+
mredir = re.search(r"__REDIRECT_URL__([^\n]*)", trailer)
|
| 340 |
+
status = int(mstat.group(1)) if mstat else 0
|
| 341 |
+
ctype = (mctype.group(1) if mctype else "") or ""
|
| 342 |
+
final_url = (murl.group(1) if murl else target) or target
|
| 343 |
+
redirect_url = (mredir.group(1) if mredir else "") or ""
|
| 344 |
+
else:
|
| 345 |
+
body = raw
|
| 346 |
+
status = 200 if proc.returncode == 0 and raw else 0
|
| 347 |
+
if status in (301, 302, 303, 307, 308):
|
| 348 |
+
loc = (redirect_url or "").strip()
|
| 349 |
+
if loc.startswith("/"):
|
| 350 |
+
loc = "http://web.archive.org" + loc
|
| 351 |
+
if loc.startswith("https://web.archive.org/"):
|
| 352 |
+
loc = "http://" + loc[len("https://"):]
|
| 353 |
+
if loc.startswith("http://web.archive.org/") and redirects_left > 0:
|
| 354 |
+
log.info("wayback replay follow HTTP redirect %s -> %s", status, loc[:120])
|
| 355 |
+
target = loc
|
| 356 |
+
final_url = loc
|
| 357 |
+
redirects_left -= 1
|
| 358 |
+
continue # redirect hop: do not consume attempts_left
|
| 359 |
+
last_err = f"http_{status}_bad_redirect"
|
| 360 |
+
log.info("wayback replay HTTP %s bad redirect %s", status, (loc or "")[:160])
|
| 361 |
+
attempts_left -= 1
|
| 362 |
+
continue
|
| 363 |
+
if proc.returncode != 0 and not body:
|
| 364 |
+
last_err = (proc.stderr or b"").decode("utf-8", "replace")[:300] or f"curl_rc_{proc.returncode}"
|
| 365 |
+
log.info("wayback replay curl fail %s: %s", url, last_err)
|
| 366 |
+
attempts_left -= 1
|
| 367 |
+
continue
|
| 368 |
+
if status in (403, 429, 503, 502, 504) and attempts_left > 1:
|
| 369 |
+
log.info("wayback replay HTTP %s %s attempts_left=%s", status, url, attempts_left)
|
| 370 |
+
attempts_left -= 1
|
| 371 |
+
continue
|
| 372 |
+
break
|
| 373 |
+
except Exception as exc:
|
| 374 |
last_err = str(exc)
|
| 375 |
log.info("wayback replay fail %s: %s", url, exc)
|
| 376 |
+
attempts_left -= 1
|
| 377 |
continue
|
| 378 |
+
else:
|
| 379 |
+
# fallback: requests without following redirects to https
|
| 380 |
+
try:
|
| 381 |
+
hops = 0
|
| 382 |
+
while hops < 8:
|
| 383 |
+
r = session().get(target, timeout=(20, 90), allow_redirects=False)
|
| 384 |
+
status = r.status_code
|
| 385 |
+
body = r.content or b""
|
| 386 |
+
ctype = r.headers.get("content-type") or ""
|
| 387 |
+
final_url = r.url or target
|
| 388 |
+
if status not in (301, 302, 303, 307, 308):
|
| 389 |
+
break
|
| 390 |
+
loc = (r.headers.get("Location") or "").strip()
|
| 391 |
+
if loc.startswith("/"):
|
| 392 |
+
loc = "http://web.archive.org" + loc
|
| 393 |
+
if loc.startswith("https://web.archive.org/"):
|
| 394 |
+
loc = "http://" + loc[len("https://"):]
|
| 395 |
+
if not loc.startswith("http://web.archive.org/"):
|
| 396 |
+
last_err = f"http_{status}_bad_redirect"
|
| 397 |
+
break
|
| 398 |
+
target = loc
|
| 399 |
+
final_url = loc
|
| 400 |
+
hops += 1
|
| 401 |
+
except requests.RequestException as exc:
|
| 402 |
+
return {"status": "error", "error": last_err or str(exc), "url": url}
|
| 403 |
+
|
| 404 |
+
if (not body and status != 200) or status == 404:
|
| 405 |
+
# /2id_/ sometimes lands on a missing capture; pick an explicit CDX 200 snapshot.
|
| 406 |
+
if not timestamp:
|
| 407 |
+
try:
|
| 408 |
+
hits = search_wayback_machine(url, limit=5, filter_status="200", collapse="digest")
|
| 409 |
+
for hit in hits:
|
| 410 |
+
ts = hit.get("timestamp") or ""
|
| 411 |
+
orig = hit.get("original") or url
|
| 412 |
+
if not ts:
|
| 413 |
+
continue
|
| 414 |
+
alt = get_wayback_content(orig, timestamp=ts)
|
| 415 |
+
if alt.get("status") == "success" and (alt.get("content") or b""):
|
| 416 |
+
return alt
|
| 417 |
+
# also try original host/path casing from CDX
|
| 418 |
+
except Exception as exc:
|
| 419 |
+
log.info("wayback cdx retry after http_%s: %s", status, exc)
|
| 420 |
+
return {"status": "error", "error": last_err or f"http_{status}", "url": url}
|
| 421 |
cap_ts = timestamp or ""
|
| 422 |
+
m = re.search(r"/web/(\d{14})", final_url or "")
|
| 423 |
if m:
|
| 424 |
cap_ts = m.group(1)
|
|
|
|
|
|
|
| 425 |
text = ""
|
| 426 |
if "html" in ctype or "xml" in ctype or "text/" in ctype or not ctype:
|
|
|
|
| 427 |
try:
|
|
|
|
|
|
|
| 428 |
text = body.decode("utf-8", "replace")
|
| 429 |
+
except Exception:
|
| 430 |
+
text = ""
|
| 431 |
+
if status != 200 or not body:
|
| 432 |
return {
|
| 433 |
"status": "error",
|
| 434 |
+
"error": f"http_{status}",
|
| 435 |
+
"http_status": status,
|
| 436 |
+
"wayback_url": final_url,
|
| 437 |
"original_url": url,
|
| 438 |
}
|
| 439 |
+
if text and is_challenge(text, status):
|
| 440 |
return {
|
| 441 |
"status": "error",
|
| 442 |
"error": "challenge_or_shell",
|
| 443 |
+
"http_status": status,
|
| 444 |
+
"wayback_url": final_url,
|
| 445 |
"original_url": url,
|
| 446 |
"content": text[:2000],
|
| 447 |
}
|
|
|
|
| 450 |
"content": body,
|
| 451 |
"text": text,
|
| 452 |
"content_type": ctype,
|
| 453 |
+
"wayback_url": final_url,
|
| 454 |
"capture_timestamp": cap_ts,
|
| 455 |
"original_url": url,
|
| 456 |
+
"http_status": status,
|
| 457 |
+
"method": "wayback_curl_http",
|
| 458 |
}
|
| 459 |
|
| 460 |
|
| 461 |
# --- Common Crawl ---
|
| 462 |
|
| 463 |
+
def _http_cc_index(url: str) -> str:
|
| 464 |
+
if url.startswith("https://index.commoncrawl.org"):
|
| 465 |
+
return "http://" + url[len("https://"):]
|
| 466 |
+
return url
|
| 467 |
+
|
| 468 |
+
|
| 469 |
def cc_indexes(n: int = 6) -> list[str]:
|
| 470 |
global _cc_indexes
|
| 471 |
if _cc_indexes is None:
|
| 472 |
+
cols = []
|
| 473 |
+
last_exc = None
|
| 474 |
+
for coll in (CC_COLLINFO, CC_COLLINFO.replace("https://", "http://", 1)):
|
| 475 |
+
try:
|
| 476 |
+
r = session().get(coll, timeout=30)
|
| 477 |
+
if r.status_code == 200:
|
| 478 |
+
cols = r.json() or []
|
| 479 |
+
break
|
| 480 |
+
except Exception as exc:
|
| 481 |
+
last_exc = exc
|
| 482 |
+
log.warning("cc collinfo %s: %s", coll, exc)
|
| 483 |
+
ids = []
|
| 484 |
+
for c in cols:
|
| 485 |
+
cid = c.get("id") or ""
|
| 486 |
+
cdx = c.get("cdx-api") or c.get("cdx_api") or ""
|
| 487 |
+
if cid:
|
| 488 |
+
ids.append(_http_cc_index(cdx or f"http://index.commoncrawl.org/{cid}-index"))
|
| 489 |
+
# collinfo is newest-first. Do not drop crawls that 404 a
|
| 490 |
+
# specific URL (that means "no captures", not a dead index).
|
| 491 |
+
_cc_indexes = ids or [
|
| 492 |
+
"http://index.commoncrawl.org/CC-MAIN-2026-34-index",
|
| 493 |
+
"http://index.commoncrawl.org/CC-MAIN-2024-33-index",
|
| 494 |
+
]
|
| 495 |
+
if last_exc and not ids:
|
| 496 |
+
log.warning("cc collinfo fallback indexes after %s", last_exc)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
return _cc_indexes[:n]
|
| 498 |
|
| 499 |
|
|
|
|
| 515 |
"output": "json",
|
| 516 |
"limit": str(limit),
|
| 517 |
}
|
| 518 |
+
_sleep_cc(0.3)
|
| 519 |
+
try:
|
| 520 |
+
r = session().get(idx, params=params, timeout=(12, 25))
|
| 521 |
+
except requests.RequestException as exc:
|
| 522 |
+
alt = _http_cc_index(idx) if idx.startswith("https://") else idx.replace("http://", "https://", 1)
|
|
|
|
|
|
|
| 523 |
try:
|
| 524 |
+
r = session().get(alt, params=params, timeout=(12, 25))
|
| 525 |
+
except requests.RequestException as exc2:
|
| 526 |
+
log.info("cc cdx %s fail %s / %s", idx, exc, exc2)
|
| 527 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
if r.status_code == 404:
|
| 529 |
msg = (r.text or "")[:80]
|
| 530 |
log.info("cc cdx no-captures %s %s", idx.split("/")[-1], msg)
|
|
|
|
| 536 |
_cc_dead.add(idx)
|
| 537 |
log.info("cc cdx dead HTTP %s %s", r.status_code, idx.split("/")[-1])
|
| 538 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
if r.status_code != 200 or not r.content:
|
| 540 |
log.info("cc cdx HTTP %s %s", r.status_code, idx.split("/")[-1])
|
| 541 |
continue
|
|
|
|
| 561 |
out.append(rec)
|
| 562 |
if out:
|
| 563 |
log.info("cc cdx %s hits=%s", idx.split("/")[-1], len(out))
|
| 564 |
+
if len(out) >= limit or out:
|
| 565 |
break
|
| 566 |
return out[:limit]
|
| 567 |
|
scrapers/collect_om.py
CHANGED
|
@@ -225,6 +225,11 @@ def add_item(items, seen, row: dict):
|
|
| 225 |
|
| 226 |
def page_title(html: str, fallback: str) -> str:
|
| 227 |
# item heading often in h3.card-title or near تحميل
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
for pat in (
|
| 229 |
r"<h[1-3][^>]*class=\"[^\"]*card-title[^\"]*\"[^>]*>([\s\S]{3,240})</h[1-3]>",
|
| 230 |
r"<h1[^>]*>([\s\S]{3,240})</h1>",
|
|
@@ -234,7 +239,7 @@ def page_title(html: str, fallback: str) -> str:
|
|
| 234 |
if m:
|
| 235 |
t = re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", m.group(1))).strip()
|
| 236 |
t = re.sub(r"^\s*(التشريعات|مكتبة الإصدارات|وزارة العدل)[^|]*\|\s*", "", t)
|
| 237 |
-
if t and t not in ("ا
|
| 238 |
return t[:300]
|
| 239 |
return fallback
|
| 240 |
|
|
@@ -329,6 +334,8 @@ def fetch_one(it: dict, done: set[str]) -> str:
|
|
| 329 |
page = official_get(page_url)
|
| 330 |
html = page.get("text") or ""
|
| 331 |
title = page_title(html, it.get("title") or f"{kind} {lid}")
|
|
|
|
|
|
|
| 332 |
text = ""
|
| 333 |
backend = page.get("method") or "live"
|
| 334 |
used = page.get("final_url") or page_url
|
|
|
|
| 225 |
|
| 226 |
def page_title(html: str, fallback: str) -> str:
|
| 227 |
# item heading often in h3.card-title or near تحميل
|
| 228 |
+
chrome = (
|
| 229 |
+
"التشريعات", "مكتبة الإصدارات",
|
| 230 |
+
"وزارة العدل و الشؤون القانونية", "وزارة العدل والشؤون القانونية",
|
| 231 |
+
"وزارة العدل و الشؤون القانونية",
|
| 232 |
+
)
|
| 233 |
for pat in (
|
| 234 |
r"<h[1-3][^>]*class=\"[^\"]*card-title[^\"]*\"[^>]*>([\s\S]{3,240})</h[1-3]>",
|
| 235 |
r"<h1[^>]*>([\s\S]{3,240})</h1>",
|
|
|
|
| 239 |
if m:
|
| 240 |
t = re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", m.group(1))).strip()
|
| 241 |
t = re.sub(r"^\s*(التشريعات|مكتبة الإصدارات|وزارة العدل)[^|]*\|\s*", "", t)
|
| 242 |
+
if t and t not in chrome and not t.startswith("وزارة العدل"):
|
| 243 |
return t[:300]
|
| 244 |
return fallback
|
| 245 |
|
|
|
|
| 334 |
page = official_get(page_url)
|
| 335 |
html = page.get("text") or ""
|
| 336 |
title = page_title(html, it.get("title") or f"{kind} {lid}")
|
| 337 |
+
if kind == "constitution" or lid == CONST_DECREE:
|
| 338 |
+
title = it.get("title") or "النظام الأساسي للدولة"
|
| 339 |
text = ""
|
| 340 |
backend = page.get("method") or "live"
|
| 341 |
used = page.get("final_url") or page_url
|
scrapers/common.py
CHANGED
|
@@ -233,15 +233,9 @@ def xml_to_text(raw: str, skip_tags: Optional[set[str]] = None) -> str:
|
|
| 233 |
|
| 234 |
|
| 235 |
ARTICLE_SPLIT = re.compile(
|
| 236 |
-
r"(?im)^\s*((?:"
|
| 237 |
-
r"art(?:
|
| 238 |
-
r"
|
| 239 |
-
r"المادة|مادة"
|
| 240 |
-
r")"
|
| 241 |
-
r"[ \t]*[\(\[\{\u202a\u202b\u202c]*[ \t]*"
|
| 242 |
-
r"[\dIVXLCDM\u0660-\u0669]+[a-zA-Z]?"
|
| 243 |
-
r"[ \t]*[\)\]\}\u202a\u202b\u202c]*"
|
| 244 |
-
r")"
|
| 245 |
)
|
| 246 |
|
| 247 |
|
|
@@ -515,8 +509,13 @@ def iso_date(val: Optional[str]) -> Optional[str]:
|
|
| 515 |
def pdf_bytes_to_text(raw: bytes, timeout: int = 180) -> str:
|
| 516 |
"""Extract text from PDF bytes via pdftotext. Empty on failure."""
|
| 517 |
import subprocess
|
| 518 |
-
if not raw
|
| 519 |
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
try:
|
| 521 |
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=True) as tmp:
|
| 522 |
tmp.write(raw)
|
|
|
|
| 233 |
|
| 234 |
|
| 235 |
ARTICLE_SPLIT = re.compile(
|
| 236 |
+
r"(?im)^\s*((?:art(?:icle|ikel|\.)|§|článek|článok|član|artikkel|articolo|"
|
| 237 |
+
r"artigo|artículo|artykuł|art\.|str\.|para(?:graph)?\.?|szakasz)\s+"
|
| 238 |
+
r"[\dIVXLCDM]+[a-zA-Z]?)\b"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
)
|
| 240 |
|
| 241 |
|
|
|
|
| 509 |
def pdf_bytes_to_text(raw: bytes, timeout: int = 180) -> str:
|
| 510 |
"""Extract text from PDF bytes via pdftotext. Empty on failure."""
|
| 511 |
import subprocess
|
| 512 |
+
if not raw:
|
| 513 |
return ""
|
| 514 |
+
if raw[:4] != b"%PDF":
|
| 515 |
+
idx = raw.find(b"%PDF", 0, 8192)
|
| 516 |
+
if idx < 0:
|
| 517 |
+
return ""
|
| 518 |
+
raw = raw[idx:]
|
| 519 |
try:
|
| 520 |
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=True) as tmp:
|
| 521 |
tmp.write(raw)
|