Create wayback.py

This commit is contained in:
Namhyeon Go 2024-03-13 16:11:29 +09:00 committed by GitHub
parent aefe835f69
commit 8f8814a5d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

56
plugins/wayback.py Normal file
View File

@ -0,0 +1,56 @@
// https://github.com/gnh1201/caterpillar
import requests
from server import Extension
try:
client_encoding = config('CLIENT_ENCODING')
except Exception as e:
print ("[*] Invaild configration: %s" % (str(e)))
def get_previous_page_content(url):
# Wayback Machine API URL
wayback_api_url = "http://archive.org/wayback/available?url=" + url
# Send a GET request to Wayback Machine API
response = requests.get(wayback_api_url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
try:
# Parse JSON response
data = response.json()
archived_snapshots = data.get("archived_snapshots", {})
closest_snapshot = archived_snapshots.get("closest", {})
# Check if the URL is available in the archive
if closest_snapshot:
archived_url = closest_snapshot.get("url", "")
# If URL is available, fetch the content of the archived page
if archived_url:
archived_page_response = requests.get(archived_url)
if archived_page_response.status_code == 200:
return archived_page_response.text
else:
return "Error fetching archived page content. Status code: " + str(archived_page_response.status_code)
else:
return "No archived URL found."
else:
return "URL is not available in the archive."
except Exception as e:
return "Error processing response: " + str(e)
else:
return "Error accessing Wayback Machine API. Status code: " + str(response.status_code)
class Wayback(Extension):
def __init__(self):
self.type = "connector" # this is a connctor
self.connection_type = "wayback"
def connect(self, conn, data, webserver, port, scheme, method, url):
previous_page_content = get_previous_page_content(url)
conn.send(previous_page_content.encode(client_encoding)