-
Notifications
You must be signed in to change notification settings - Fork 42
/
ia_handler.py
44 lines (41 loc) · 1.83 KB
/
ia_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import requests
class IA_handler(object):
def __init__(self):
self.enabled = True
self.name = 'The Internet Archive'
self.api_required = False
def push(self, uri_org, p_args=[]):
msg = ''
try:
uri = 'https://web.archive.org/save/' + uri_org
# push into the archive
r = requests.get(uri, timeout=120, allow_redirects=True)
r.raise_for_status()
# extract the link to the archived copy
if (r != None):
if "Location" in r.headers:
return r.headers["Location"]
elif "Content-Location" in r.headers:
if (r.headers["Content-Location"]).startswith("/web/"):
return "https://web.archive.org"+r.headers["Content-Location"]
else:
try:
uri_from_content = "https://web.archive.org" + r.text.split('var redirUrl = "',1)[1].split('"',1)[0]
except:
uri_from_content = r.headers["Content-Location"]
#pass;
return uri_from_content
else:
for r2 in r.history:
if 'Location' in r2.headers:
return r.url
#return r2.headers['Location']
if 'Content-Location' in r2.headers:
return r.url
#return r2.headers['Content-Location']
msg = "("+self.name+ "): No HTTP Location/Content-Location header is returned in the response"
except Exception as e:
if msg == '':
msg = "Error (" + self.name+ "): " + str(e)
pass;
return msg