waybackpy/pywayback/wrapper.py

82 lines
2.7 KiB
Python
Raw Normal View History

2020-05-02 11:23:43 +02:00
# -*- coding: utf-8 -*-
2020-05-02 11:45:36 +02:00
2020-05-02 11:23:43 +02:00
from datetime import datetime
from urllib.request import Request, urlopen
import urllib.error
class TooManyArchivingRequestsError(Exception):
2020-05-02 11:49:52 +02:00
"""
Error when a single url reqeusted for archiving too many times in a short timespam.
2020-05-02 11:45:36 +02:00
Wayback machine doesn't supports archivng any url too many times in a short period of time.
2020-05-02 11:23:43 +02:00
"""
class ArchivingNotAllowed(Exception):
2020-05-02 11:49:52 +02:00
"""
Files like robots.txt are set to deny robot archiving.
Wayback machine respects these file, will not archive.
"""
2020-05-02 11:23:43 +02:00
2020-05-02 12:33:33 +02:00
class PageNotSavedError(Exception):
"""
Files like robots.txt are set to deny robot archiving.
Wayback machine respects these file, will not archive.
"""
class InvalidUrlError(Exception):
"""
Files like robots.txt are set to deny robot archiving.
Wayback machine respects these file, will not archive.
"""
2020-05-02 11:45:36 +02:00
2020-05-02 11:23:43 +02:00
def save(url,UA="pywayback python module"):
base_save_url = "https://web.archive.org/save/"
request_url = base_save_url + url
hdr = { 'User-Agent' : '%s' % UA }
req = Request(request_url, headers=hdr)
2020-05-02 12:33:33 +02:00
if "." not in url:
raise InvalidUrlError("'%s' is not a vaild url." % url)
2020-05-02 11:23:43 +02:00
try:
2020-05-02 12:01:33 +02:00
response = urlopen(req) #nosec
2020-05-02 11:23:43 +02:00
except urllib.error.HTTPError as e:
2020-05-02 12:42:41 +02:00
if e.code == 502:
raise PageNotSavedError(e)
elif e.code == 429:
raise TooManyArchivingRequestsError(e)
2020-05-02 11:23:43 +02:00
header = response.headers
if "exclusion.robots.policy" in str(header):
raise ArchivingNotAllowed("Can not archive %s. Disabled by site owner." % (url))
archive_id = header['Content-Location']
archived_url = "https://web.archive.org" + archive_id
return archived_url
def near(
url,
year=datetime.utcnow().strftime('%Y'),
month=datetime.utcnow().strftime('%m'),
day=datetime.utcnow().strftime('%d'),
hour=datetime.utcnow().strftime('%H'),
minute=datetime.utcnow().strftime('%M'),
2020-05-02 12:09:47 +02:00
UA="pywayback python module",
2020-05-02 11:23:43 +02:00
):
timestamp = str(year)+str(month)+str(day)+str(hour)+str(minute)
2020-05-02 12:16:02 +02:00
request_url = "https://archive.org/wayback/available?url=%s&timestamp=%s" % (str(url), str(timestamp))
hdr = { 'User-Agent' : '%s' % UA }
req = Request(request_url, headers=hdr)
response = urlopen(req) #nosec
2020-05-02 11:23:43 +02:00
encoding = response.info().get_content_charset('utf8')
import json
data = json.loads(response.read().decode(encoding))
2020-05-02 12:33:33 +02:00
print(data)
if not data["archived_snapshots"]:
raise PageNotSavedError("'%s' was not archived." % url)
2020-05-02 11:23:43 +02:00
archive_url = (data["archived_snapshots"]["closest"]["url"])
return archive_url
2020-05-02 12:09:47 +02:00
def oldest(url,UA="pywayback python module"):
return near(url,year=1995,UA=UA)
2020-05-02 11:23:43 +02:00
2020-05-02 12:09:47 +02:00
def newest(url,UA="pywayback python module"):
return near(url,UA=UA)