waybackpy

repository·master·Indexed 20 days ago

https://github.com/akamhy/waybackpy

A Python package and CLI tool providing a programmatic interface to the Internet Archive's Wayback Machine APIs. It includes support for the Save API (WaybackMachineSaveAPI) to archive pages, the CDX Server API (WaybackMachineCDXServerAPI) for high-performance snapshot queries, and the Availability API (WaybackMachineAvailabilityAPI) for checking archive existence.

Tokens
2K
Snippets
8
Records
11
Agent score
70%

What's inside waybackpy

  1. Install waybackpy

    master

    You can install waybackpy using pip, conda, or by pulling directly from the GitHub repository.

    Using pip (Recommended):

    pip install waybackpy -U

    Using conda (Recommended):

    conda install -c conda-forge waybackpy

    Directly from GitHub (Not recommended):

    pip install git+https://github.com/akamhy/waybackpy.git
    pip install waybackpy -U
  2. Use waybackpy CLI to interact with the Wayback Machine

    master

    The waybackpy CLI allows you to perform various operations on a given URL, such as retrieving the oldest/newest archives, saving a page to the Wayback Machine, or querying the CDX API for historical snapshots.

    Basic usage requires providing a URL with the -u or --url flag. If no specific action flag is provided, the CLI will return a NoCommandFound error.

    To see all available commands and flags, run:

    waybackpy --help
  3. Use WaybackMachineSaveAPI to save a page

    master

    The WaybackMachineSaveAPI (also known as the Save API or SavePageNow) allows you to trigger the Wayback Machine to save a specific URL.

    When initializing, you must provide the url and a user_agent.

    Key attributes:

    • save(): Triggers the save operation and returns the archive URL.
    • cached_save: A boolean indicating if the page was already cached.
    • timestamp(): Returns a datetime.datetime object of the save event.
    from waybackpy import WaybackMachineSaveAPI
    
    url = "https://github.com"
    user_agent = "Mozilla/5.0 (Windows NT 5.1; rv:40.0) Gecko/20100101 Firefox/40.0"
    
    save_api = WaybackMachineSaveAPI(url, user_agent)
    save_api.save()
    print(save_api.timestamp())
  4. Use WaybackMachineAvailabilityAPI for availability checks

    master

    The WaybackMachineAvailabilityAPI (Availability API) provides methods to check if a URL has been archived.

    Note: It is recommended to use WaybackMachineCDXServerAPI instead of this class for better performance. However, WaybackMachineAvailabilityAPI.newest() may return results more recent than the CDX server API.

    Methods:

    • oldest(): Returns the URL of the oldest archive.
    • newest(): Returns the URL of the newest archive.
    • near(year=..., month=..., day=..., hour=...): Returns the URL of the archive closest to the specified time.
    from waybackpy import WaybackMachineAvailabilityAPI
    
    url = "https://google.com"
    user_agent = "Mozilla/5.0 (Windows NT 5.1; rv:40.0) Gecko/20100101 Firefox/40.0"
    
    availability_api = WaybackMachineAvailabilityAPI(url, user_agent)
    print(availability_api.newest())
  5. Use WaybackMachineCDXServerAPI to query snapshots

    master

    The WaybackMachineCDXServerAPI (CDX API) is used to query the Wayback Machine's CDX server for historical snapshots of a URL. This is the recommended way to perform high-performance queries.

    Initialization: Pass the url, user_agent, and optionally start_timestamp and end_timestamp (as integers).

    Methods:

    • oldest(): Returns the oldest available snapshot object.
    • newest(): Returns the newest available snapshot object.
    • near(year=..., month=..., day=..., hour=..., minute=..., wayback_machine_timestamp=..., unix_timestamp=...): Returns the snapshot closest to the specified time.
    • snapshots(): A generator that yields snapshot objects for the specified range.

    Snapshot Object Attributes:

    • archive_url: The full URL to the archived page.
    • original: The original URL.
    • urlkey: The URL key used by the archive.
    • timestamp: The Wayback Machine timestamp string.
    • datetime_timestamp: A datetime.datetime object of the snapshot.
    • statuscode: The HTTP status code of the archived page.
    • mimetype: The MIME type of the archived content.
    from waybackpy import WaybackMachineCDXServerAPI
    
    url = "https://pypi.org"
    user_agent = "Mozilla/5.0 (Windows NT 5.1; rv:40.0) Gecko/20100101 Firefox/40.0"
    cdx = WaybackMachineCDXServerAPI(url, user_agent, start_timestamp=2016, end_timestamp=2017)
    
    for item in cdx.snapshots():
        print(item.archive_url)
  6. Save a URL to the Wayback Machine

    master

    Use the --save (-s) flag to trigger the SavePageNow API, which archives the current state of the URL.

    Additional options:

    • --headers (-h): If passed, the CLI will also print the headers data returned by the SavePageNow API.

    Example:

    waybackpy --url "https://example.com" --save
    waybackpy --url "https://example.com" --save
  7. Retrieve the oldest or newest archive of a URL

    master

    You can quickly find the earliest or most recent version of a URL using the following flags:

    • --oldest (-o): Retrieve the oldest archive of the URL.
    • --newest (-n): Retrieve the newest archive of the URL.

    Example to get the oldest archive:

    waybackpy --url "https://example.com" --oldest
    waybackpy --url "https://example.com" --oldest
  8. Find an archive close to a specific time

    master

    Use the --near (-N) flag along with time-based arguments to find the snapshot closest to a specific point in time. Supported time arguments are:

    • --year (-Y): 1994-9999
    • --month (-M): 1-12
    • --day (-D): 1-31
    • --hour (-H): 0-24
    • --minute (--MIN): 0-60

    Example:

    waybackpy --url "https://example.com" --near --year 2010 --month 5 --day 20
    waybackpy --url "https://example.com" --near --year 2010 --month 5 --day 20
  9. List known URLs and save to file

    master

    Use the --known-urls (-ku) flag to list all URLs known to the Wayback Machine for a given domain using the CDX API.

    Options:

    • --subdomain (-sub): Include known URLs for subdomains.
    • --file (-f): Instead of printing to stdout, save the discovered URLs into a text file in the current working directory. The filename follows the pattern {domain}-urls-{random_id}.txt.

    Example to save subdomains to a file:

    waybackpy --url "https://example.com" --known-urls --subdomain --file
    waybackpy --url "https://example.com" --known-urls --subdomain --file
  10. Query the CDX API for snapshots

    master

    The --cdx flag enables advanced querying of the Wayback Machine's CDX server. This allows you to list historical snapshots with specific filters and formatting.

    Key CDX Options:

    • --start-timestamp (-st): Start time in yyyyMMddhhmmss format.
    • --end-timestamp (-et): End time in yyyyMMddhhmmss format.
    • --closest (-C): Find archives closest to a specific timestamp.
    • --filter (-f): Filter on specific fields (can be used multiple times).
    • --match-type (-mt): Match exact URL, prefix, host, or sub-hosts.
    • --sort (-st): Sort results (default, closest, or reverse).
    • --limit (-l): Maximum records per API call (default 25000).
    • --cdx-print (-cp): Print only specific fields. Supported fields include: urlkey, timestamp, original, mimetype, statuscode, digest, length, archiveurl.
    • --collapse (-c): Filter or collapse results based on a field or substring.
    • --use-pagination (-up): Use the pagination API instead of the default.
    • --gzip (-gz): Enable/disable gzip compression (pass false to disable).

    Example to list snapshots with specific fields:

    waybackpy --url "https://example.com" --cdx --cdx-print timestamp --cdx-print archiveurl