| # download_pg_pre1950.sh | |
| # Dependencies: wget, bzip2, tar, python3, rsync | |
| set -Eeuo pipefail | |
| ROOT="$HOME/projects/pre-1950s-text/data" | |
| META_DIR="$ROOT/meta" | |
| LIST="$ROOT/pre1950_ids.txt" | |
| TXT_DIR="$ROOT/text" | |
| MIRROR="rsync://rsync.gutenberg.org/gutenberg" # any listed mirror is acceptable | |
| mkdir -p "$META_DIR" "$TXT_DIR" | |
| # 1. obtain full metadata archive (≈ 400 MB compressed, ~2 GB uncompressed) | |
| # wget -N -P "$META_DIR" http://www.gutenberg.org/cache/epub/feeds/rdf-files.tar.bz2 # :contentReference[oaicite:0]{index=0} | |
| # bzip2 -dk "$META_DIR/rdf-files.tar.bz2" | |
| # tar -xf "$META_DIR/rdf-files.tar" -C "$META_DIR" | |
| # 2. build list of eBook IDs satisfying: | |
| # – dcterms:issued ≤ 1949 (best available proxy; original print date is absent) :contentReference[oaicite:1]{index=1} | |
| python3 - <<'PY' | |
| import pathlib, re, xml.etree.ElementTree as ET, sys, datetime, itertools | |
| META_ROOT = pathlib.Path("/Users/brk/projects/pre-1950s-text/data") | |
| YEAR_LIMIT = 1949 | |
| ids = [] | |
| for rdf in META_ROOT.rglob("*.rdf"): | |
| # try: | |
| tree = ET.parse(rdf) | |
| issued = tree.find(".//{http://purl.org/dc/terms/}issued") | |
| if issued is None: | |
| print("issued is none") | |
| continue | |
| year = int(issued.text[:4]) | |
| print(f"year is {year}") | |
| if year <= YEAR_LIMIT: | |
| match = re.search(r'pg(\d+)\.rdf$', rdf.name) | |
| if match: | |
| ids.append(int(match.group(1))) | |
| print(f"Adding id {ids[-1]} to list") | |
| # except Exception: | |
| # print('Failed wtih exception') | |
| # continue | |
| ids.sort() | |
| with open("/Users/brk/projects/pre-1950s-text/data/pre1950_ids.txt", "w") as f: | |
| for i in ids: | |
| # construct rsync path fragment e.g. id 1342 ➜ 1/3/4/1342/1342-0.txt | |
| path = "/".join(list(str(i)[:-1])) # '', '1', '3', '4' for 1342 | |
| if path: | |
| path += "/" | |
| path += f"{i}/{i}-*.txt" | |
| f.write(f"{path}\n") | |
| print(f"IDs written: {len(ids)}", file=sys.stderr) | |
| PY | |
| # 3. download only the plain‑text files matching those IDs | |
| rsync -av --delete --include="*/" --include-from="$LIST" --exclude="*" "$MIRROR/" "$TXT_DIR/" # :contentReference[oaicite:2]{index=2} | |