Add a local searchable-PDF orientation reader, a stale OCR example, and verified results on 343 USGS and 553 Bengali archive pages
88248d0 verified Download text-layer/study/fetch_pdfs.py from foliokestrel/folio-orient: direct link, hf CLI and curl.
- Browser
- Download file 1.59 kB
-
https://huggingface.co/foliokestrel/folio-orient/resolve/main/text-layer/study/fetch_pdfs.py
- Command line
-
hf download hf://foliokestrel/folio-orient/text-layer/study/fetch_pdfs.py
-
curl -L -o fetch_pdfs.py https://huggingface.co/foliokestrel/folio-orient/resolve/main/text-layer/study/fetch_pdfs.py
1.59 kB
| """Download the four source revisions, verifying hashes before installing each PDF.""" | |
| import argparse | |
| from hashlib import sha256 | |
| import os | |
| from pathlib import Path | |
| from tempfile import NamedTemporaryFile | |
| from urllib.request import Request, urlopen | |
| from unblind import rows | |
| def fetch(directory): | |
| directory.mkdir(parents=True, exist_ok=True) | |
| for book in rows("sources.jsonl"): | |
| if book["issue"] is not None: | |
| continue | |
| target = directory / f'wsp{book["series"]}.pdf' | |
| if target.exists(): | |
| if sha256(target.read_bytes()).hexdigest() != book["sha256"]: | |
| raise ValueError(f"Existing file differs from the source revision: {target}") | |
| continue | |
| request = Request(book["url"], headers={"User-Agent":"Folio-Kestrel PDF orientation evaluation"}) | |
| with urlopen(request, timeout=90) as response: | |
| data = response.read(book["bytes"]+1) | |
| if len(data) != book["bytes"] or sha256(data).hexdigest() != book["sha256"]: | |
| raise ValueError(f"Source revision differs: {book['url']}") | |
| tmp = None | |
| try: | |
| with NamedTemporaryFile(dir=directory, delete=False) as f: | |
| tmp = Path(f.name) | |
| f.write(data) | |
| os.link(tmp,target) | |
| finally: | |
| if tmp is not None: | |
| tmp.unlink(missing_ok=True) | |
| print(target,flush=True) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("directory", type=Path) | |
| fetch(parser.parse_args().directory) | |