Find Wayback Machine snapshots easily - Use the Wayback Machine API to quickly find the most recent snapshot for a url. https://www.damiencarbery.com/2019/09/find-wayback-machine-snapshots-easily/
<!DOCTYPE html> | |
<!-- | |
Find Wayback Machine snapshots easily | |
Use the Wayback Machine API to quickly find the most recent snapshot for a url. | |
https://www.damiencarbery.com/2019/09/find-wayback-machine-snapshots-easily/ | |
--> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Wayback Availability</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<style> | |
/* Some styles from inspired by the old http://www.blueprintcss.org/ framework. */ | |
table { border-collapse: collapse; border-spacing: 0; } | |
table, th, td {vertical-align: middle;} | |
thead th { background: #c3d9ff; font-weight: bold; } | |
th, td { border: 1px solid #444; padding: 4px 10px 4px 5px; text-align: left; } | |
tbody tr:nth-child(even) td, tbody tr.even td { background-color: #e5ecf9; } | |
</style> | |
</head> | |
<body> | |
<h1>Wayback Availability</h1> | |
<table><thead><tr><th>URL</th><th>Snapshot</th><th>Snapshot Date</th></tr></thead> | |
<tbody> | |
<?php | |
define('WP_USE_THEMES', false); | |
/** Loads the WordPress Environment and Template */ | |
require( dirname( __FILE__ ) . '/wp-blog-header.php' ); | |
$urls_to_check = array( | |
'https://www.damiencarbery.com', | |
'https://www.damiencarbery.com/blah', | |
); | |
$site_url = 'https://www.damiencarbery.com'; | |
$wayback_availability_url = 'https://archive.org/wayback/available?url='; | |
foreach ( $urls_to_check as $site_url ) { | |
$response = wp_remote_get( $wayback_availability_url . $site_url ); | |
$http_code = wp_remote_retrieve_response_code( $response ); | |
// First we check that the Wayback Availability API responsed successfully. | |
if ( 200 == $http_code ) { | |
$body = wp_remote_retrieve_body( $response ); | |
$snapshot_data = json_decode( $body, true ); | |
// Then check that we got valid JSON in the response. | |
if ( $snapshot_data ) { | |
// Check that there is a snapshot available. | |
if ( count( $snapshot_data[ 'archived_snapshots' ] ) ) { | |
// This check that the snapshot is available and had a successful http response code | |
// is probably over the top but doing this just to be sure. | |
if ( ( 200 == $snapshot_data[ 'archived_snapshots' ][ 'closest' ][ 'status' ] ) && | |
( true == $snapshot_data[ 'archived_snapshots' ][ 'closest' ][ 'available' ] ) ) { | |
$date = DateTime::createFromFormat('Ymdhis', $snapshot_data[ 'archived_snapshots' ][ 'closest' ][ 'timestamp' ] ); | |
$snapshot_url = $snapshot_data[ 'archived_snapshots' ][ 'closest' ][ 'url' ]; | |
// Display the checked url, a link to the snapshot and the snapshot date. | |
printf( '<tr><td>%s</td><td><a href="%s">%s</a></td><td>%s</td></tr>', | |
$site_url, $snapshot_url, $snapshot_url, $date->format('Y-m-d H:i:s') ); | |
} | |
else { | |
printf( '<tr><td>%s</td><td>Unknown issue with snapshot.</td><td></td></tr>', $site_url ); | |
} | |
} | |
else { | |
printf( '<tr><td>%s</td><td>No snapshot available</td><td></td></tr>', $site_url ); | |
} | |
} | |
} | |
else { | |
// There was a problem contacting the Wayback Availability API. | |
printf( '<tr><td>%s</td><td>Error - Response code: %s</td><td></td></tr>', $site_url, $http_code ); | |
} | |
} | |
?> | |
</tbody> | |
</table> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment