tools / web / archive-snapshot.py

web/archive-snapshot.py

webinternetdocuments● networkpython3

Look up whether archive.ph already has a snapshot of a URL -- GET /newest/<url> and report its Location header on a redirect. Lookup only: never submits a new snapshot, so it can't be used to force-archive something that isn't already there. Prints nothing (not an error) when no snapshot exists -- the caller falls back to the original link.

Use when a link may be paywalled or gone and the user wants an existing archive.ph snapshot of it, without creating one.

Run it

1. Once per machine: install fragletc (Docker must already be running).

curl -fsSL https://raw.githubusercontent.com/ofthemachine/fraglet/main/install.sh | sh

2. Fetch the tool and print its contract.

curl -fsSLO https://tools.ofthemachine.com/web/archive-snapshot.py && chmod +x archive-snapshot.py && ./archive-snapshot.py --fraglet-help

3. Run it.

./archive-snapshot.py -p url="<url>"

Parameters

nametyperequirementdescription
urlstringrequiredURL to look up on archive.ph

Outputs

stdout

Provenance

image
ofthemachine/python3@sha256:a015b4f9f0e7c1648f8fcd4164f6a296dd3c9189404f88f333a9764d08d13522
reach
network=required — output depends on live external state; do not cache indefinitely.
procedure_hash
sha256:48f71510689a887cd27b9ab7b4b51755bc511a225dedac23fe76f05d854ed10d
receipt
add --receipt run.json to any run; receipt/attest.py verifies it against this hash without re-running.

Source

archive-snapshot.py · 29 lines · sha256 48f71510689a…
#!/usr/bin/env -S fragletc --image ofthemachine/python3@sha256:a015b4f9f0e7c1648f8fcd4164f6a296dd3c9189404f88f333a9764d08d13522
#: d=Look up whether archive.ph already has a snapshot of a URL -- GET /newest/<url> and report its Location header on a redirect. Lookup only: never submits a new snapshot, so it can't be used to force-archive something that isn't already there. Prints nothing (not an error) when no snapshot exists -- the caller falls back to the original link.
#: when=Use when a link may be paywalled or gone and the user wants an existing archive.ph snapshot of it, without creating one.
#: network=required
#: stdin=none
#: param=url:required:d=URL to look up on archive.ph
import http.client
import os
import sys

target = os.environ.get("URL", "").strip()
if not target:
    print("archive-snapshot: missing url", file=sys.stderr)
    sys.exit(1)

conn = http.client.HTTPSConnection("archive.ph", timeout=20)
try:
    conn.request("GET", "/newest/" + target, headers={"User-Agent": "Mozilla/5.0"})
    resp = conn.getresponse()
    resp.read()
    if resp.status in (301, 302, 303, 307, 308):
        loc = resp.getheader("Location")
        if loc:
            print(loc)
except OSError as e:
    print(f"archive-snapshot: request failed: {e}", file=sys.stderr)
    sys.exit(1)
finally:
    conn.close()