#!/bin/bash # # fetch-sts-deir.sh # # Retrieves the 2011 Draft Environmental Impact Report and the Final EIR # Response to Comments for the Charles M. Schulz - Sonoma County Airport # Master Plan Update Implementation Project. # # Sonoma County lists every part of both documents on its Environmental # Reports 2011 page, but the files sit under a path that no longer resolves. # This script retrieves archived captures from the Internet Archive instead. # # Method: # 1. Query the Wayback Machine's CDX index for every archived capture of # every file under sonomacountyairport.org/pdf/, filtered to captures # that returned HTTP 200, and record the stored size of each. # 2. For each file, try its captures largest first, using the id_ modifier, # which returns the original file rather than the Wayback viewer's HTML # wrapper. # 3. Verify that what arrived is a complete PDF, and move to the next # capture if it isn't. # # Step 1 and step 2 matter. Some Internet Archive crawls stop saving a large # file partway through, so a capture can be a genuinely incomplete copy # rather than a failed download. Several files here were captured at exactly # one mebibyte in 2020 and captured in full in 2022. Taking the earliest # capture of each file therefore produces a set with silently truncated # documents in it. Taking the largest, and checking the result, does not. # # A truncated PDF keeps its header and loses its end-of-file marker. Some # readers will partially open one, so a file opening is not evidence that it # is complete. # # Filenames are preserved exactly as the county published them, so each file # can be matched against the county's own index page. # # The script writes a manifest recording, for every file, the original county # URL and the capture it came from. # # Requires: curl. Tested on macOS and Linux. # Usage: bash fetch-sts-deir.sh # Output: PDFs and manifest.csv in the current directory. # # The Internet Archive rate-limits requests, so this pauses between files and # backs off when a connection is refused. A complete run takes fifteen to # twenty minutes. Re-running is safe: complete files are skipped and # truncated ones are retrieved again, so an interrupted run resumes where it # stopped. set -u CDX="http://web.archive.org/cdx/search/cdx" TARGET="sonomacountyairport.org/pdf*" MANIFEST="manifest.csv" WORK=".cdx-captures" # The county published this path for years and it holds unrelated material # alongside the environmental record: pre-2011 hangar leases and fee # schedules, airport newsletters, and a set of figure plates that are # illustrations rather than documents. Those are excluded here. Everything # forming part of the Draft EIR or the Final EIR Response to Comments is # retrieved. EXCLUDE='red_baron|redbaron|guidebooklet|hangar|tiedown|tie_down|lease|agmt|agreement|movement-area|minstdsfinal|sts_flyer|sts_report|press_release|airportfees|airport_parking|driving|air%20service|complaintsummary|gate%20device|termination|portable|shade|runway-approach-diagrams|biological_resource_figure|appendix_h_apxafigure|figure_3_4-9' is_complete_pdf() { local f="$1" [ -s "$f" ] || return 1 head -c 5 "$f" | grep -q '%PDF' || return 1 tail -c 2048 "$f" | grep -q '%%EOF' || return 1 return 0 } echo "Querying the Wayback CDX index for all captures ..." raw=$(curl -fsS --connect-timeout 60 \ "$CDX?url=$TARGET&output=text&fl=original,timestamp,length&filter=statuscode:200&limit=20000") if [ -z "$raw" ]; then echo "The index returned nothing." echo "Either the Internet Archive is refusing connections, in which case" echo "wait an hour and run this again, or there is a network problem." exit 1 fi # Keep only the 2011 environmental record. An older uppercase /PDF/ path # holds 2006-era airport paperwork. # # Sort by URL, then by stored size descending, so each file's captures are # grouped together with the largest first. printf '%s\n' "$raw" \ | grep '/pdf/' \ | grep -viE "$EXCLUDE" \ | awk 'NF>=3 {print $1, $2, $3+0}' \ | sort -k1,1 -k3,3nr > "$WORK" files=$(awk '{print $1}' "$WORK" | sort -u) count=$(printf '%s\n' "$files" | grep -c . ) echo "Found $count files." echo if [ ! -f "$MANIFEST" ]; then echo "filename,original_url,wayback_capture,stored_bytes" > "$MANIFEST" fi saved=0 failed=() for original in $files; do filename="${original##*/}" if [ -f "$filename" ]; then if is_complete_pdf "$filename"; then echo "have $filename" saved=$((saved + 1)) continue fi echo "redo $filename (incomplete copy on disk)" rm -f "$filename" fi printf 'fetch %-52s ' "$filename" got=0 used="" usedlen="" # Captures for this file, largest stored size first. while read -r ts len; do [ -z "${ts:-}" ] && continue url="https://web.archive.org/web/${ts}id_/${original}" attempt=1 while [ $attempt -le 3 ]; do if curl -fsSL --connect-timeout 30 --max-time 600 "$url" -o "$filename"; then break fi rm -f "$filename" pause=$((attempt * 30)) printf 'refused, waiting %ss ... ' "$pause" sleep $pause attempt=$((attempt + 1)) done if [ -f "$filename" ] && is_complete_pdf "$filename"; then got=1 used="$ts" usedlen="$len" break fi rm -f "$filename" printf 'capture %s incomplete, trying older ... ' "$ts" sleep 3 done <<< "$(awk -v u="$original" '$1==u {print $2, $3}' "$WORK")" if [ $got -eq 1 ]; then echo "ok ($(du -h "$filename" | cut -f1), capture $used)" printf '%s,%s,%s,%s\n' "$filename" "$original" "$used" "$usedlen" >> "$MANIFEST" saved=$((saved + 1)) else echo "FAILED" failed+=("$original") fi sleep 5 done rm -f "$WORK" echo echo "Retrieved $saved of $count files into $(pwd)" echo "Manifest written to $MANIFEST" if [ ${#failed[@]} -gt 0 ]; then echo echo "No complete capture (${#failed[@]}):" for f in "${failed[@]}"; do echo " $f" done echo echo "Run this again to retry; complete files are skipped." echo "Files that fail repeatedly have no capture in the archive that" echo "holds a complete copy." fi