Datasets:
Upload scripts/bulk_download.py with huggingface_hub
Browse files- scripts/bulk_download.py +65 -0
scripts/bulk_download.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Bulk-download committee zapis PDFs with cache and manifest."""
|
| 2 |
+
import json, sys, pathlib, urllib.request, urllib.error, concurrent.futures as cf
|
| 3 |
+
|
| 4 |
+
CACHE = pathlib.Path("sejm_cache")
|
| 5 |
+
PDFDIR = CACHE / "pdf"; PDFDIR.mkdir(parents=True, exist_ok=True)
|
| 6 |
+
APIDIR = CACHE / "api"
|
| 7 |
+
BASE = "https://api.sejm.gov.pl/sejm/term{t}/committees/{c}/sittings/{n}/pdf"
|
| 8 |
+
|
| 9 |
+
term = int(sys.argv[1])
|
| 10 |
+
tasks = []
|
| 11 |
+
for f in sorted(APIDIR.glob(f"sittings_{term}_*.json")):
|
| 12 |
+
code = f.stem.split("_", 2)[2]
|
| 13 |
+
for s in json.loads(f.read_text(encoding="utf-8")):
|
| 14 |
+
if s.get("num"):
|
| 15 |
+
tasks.append((code, s["num"]))
|
| 16 |
+
|
| 17 |
+
def fetch(t):
|
| 18 |
+
code, num = t
|
| 19 |
+
dest = PDFDIR / f"t{term}_{code}_{num}.pdf"
|
| 20 |
+
if dest.exists() and dest.stat().st_size > 100:
|
| 21 |
+
return {"code": code, "num": num, "status": "cached", "bytes": dest.stat().st_size}
|
| 22 |
+
url = BASE.format(t=term, c=code, n=num)
|
| 23 |
+
for a in range(2):
|
| 24 |
+
try:
|
| 25 |
+
req = urllib.request.Request(url, headers={"User-Agent": "committee-corpus/0.1"})
|
| 26 |
+
with urllib.request.urlopen(req, timeout=60) as r:
|
| 27 |
+
data = r.read()
|
| 28 |
+
if data[:5] == b"%PDF-":
|
| 29 |
+
dest.write_bytes(data)
|
| 30 |
+
return {"code": code, "num": num, "status": "ok", "bytes": len(data)}
|
| 31 |
+
return {"code": code, "num": num, "status": "notpdf", "bytes": len(data)}
|
| 32 |
+
except urllib.error.HTTPError as e:
|
| 33 |
+
return {"code": code, "num": num, "status": f"http{e.code}", "bytes": 0}
|
| 34 |
+
except Exception as e:
|
| 35 |
+
if a == 1:
|
| 36 |
+
return {"code": code, "num": num, "status": "error", "bytes": 0, "err": repr(e)[:80]}
|
| 37 |
+
|
| 38 |
+
manifest = APIDIR / f"dl_manifest_{term}.jsonl"
|
| 39 |
+
done = set()
|
| 40 |
+
if manifest.exists():
|
| 41 |
+
for line in open(manifest, encoding="utf-8"):
|
| 42 |
+
try:
|
| 43 |
+
r = json.loads(line)
|
| 44 |
+
if r["status"] not in ("error",):
|
| 45 |
+
done.add((r["code"], r["num"]))
|
| 46 |
+
except Exception:
|
| 47 |
+
pass
|
| 48 |
+
tasks = [t for t in tasks if t not in done]
|
| 49 |
+
print(f"term {term}: downloading {len(tasks)} ({len(done)} already done)", flush=True)
|
| 50 |
+
|
| 51 |
+
with open(manifest, "a", encoding="utf-8") as fh, cf.ThreadPoolExecutor(48) as ex:
|
| 52 |
+
futs = {ex.submit(fetch, t): t for t in tasks}
|
| 53 |
+
for i, fut in enumerate(cf.as_completed(futs)):
|
| 54 |
+
r = fut.result()
|
| 55 |
+
fh.write(json.dumps(r) + "\n")
|
| 56 |
+
if (i + 1) % 250 == 0:
|
| 57 |
+
print(f"{i+1}/{len(tasks)}", flush=True)
|
| 58 |
+
|
| 59 |
+
rows = [json.loads(l) for l in open(manifest, encoding="utf-8")]
|
| 60 |
+
ok = [r for r in rows if r["status"] in ("ok", "cached")]
|
| 61 |
+
print(json.dumps({
|
| 62 |
+
"term": term, "total": len(rows), "pdf": len(ok),
|
| 63 |
+
"bytes_GB": round(sum(r["bytes"] for r in ok) / 1e9, 2),
|
| 64 |
+
"hist": {s: sum(1 for r in rows if r["status"] == s) for s in sorted({r["status"] for r in rows})},
|
| 65 |
+
}, ensure_ascii=False))
|