Update index.rst and remove dupes

This commit is contained in:
Akash
2020-07-20 10:07:31 +05:30
committed by GitHub
parent 1a78d88be2
commit bb94e0d1c5
3 changed files with 45 additions and 36 deletions

View File

@@ -65,7 +65,7 @@ class Url():
hdr = { 'User-Agent' : '%s' % self.user_agent } #nosec
req = Request(request_url, headers=hdr) #nosec
try:
response = urlopen(req, timeout=30) #nosec
response = urlopen(req) #nosec
except Exception:
try:
response = urlopen(req) #nosec
@@ -74,6 +74,12 @@ class Url():
header = response.headers
def archive_url_parser(header):
"""Parse out the archive from header."""
#Regex1
arch = re.search(r"rel=\"memento.*?(web\.archive\.org/web/[0-9]{14}/.*?)>", str(header))
if arch:
return arch.group(1)
#Regex2
arch = re.search(r"X-Cache-Key:\shttps(.*)[A-Z]{2}", str(header))
if arch:
return arch.group(1)
@@ -93,22 +99,24 @@ class Url():
hdr = { 'User-Agent' : '%s' % user_agent }
req = Request(url, headers=hdr) #nosec
try:
resp=urlopen(req) #nosec
except Exception:
try:
resp=urlopen(req) #nosec
except Exception as e:
raise WaybackError(e)
response = self.get_response(req)
if not encoding:
try:
encoding= resp.headers['content-type'].split('charset=')[-1]
encoding= response.headers['content-type'].split('charset=')[-1]
except AttributeError:
encoding = "UTF-8"
return response.read().decode(encoding.replace("text/html", "UTF-8", 1))
return resp.read().decode(encoding.replace("text/html", "UTF-8", 1))
def get_response(self, req):
"""Get response for the supplied request."""
try:
response = urlopen(req) #nosec
except Exception:
try:
response = urlopen(req) #nosec
except Exception as e:
raise WaybackError(e)
return response
def near(self, **kwargs):
""" Returns the archived from Wayback Machine for an URL closest to the time supplied.
@@ -124,15 +132,7 @@ class Url():
request_url = "https://archive.org/wayback/available?url=%s&timestamp=%s" % (self.clean_url(), str(timestamp))
hdr = { 'User-Agent' : '%s' % self.user_agent }
req = Request(request_url, headers=hdr) # nosec
try:
response = urlopen(req) #nosec
except Exception:
try:
response = urlopen(req) #nosec
except Exception as e:
raise WaybackError(e)
response = self.get_response(req)
data = json.loads(response.read().decode("UTF-8"))
if not data["archived_snapshots"]:
raise WaybackError("'%s' is not yet archived." % url)
@@ -154,13 +154,5 @@ class Url():
hdr = { 'User-Agent' : '%s' % self.user_agent }
request_url = "https://web.archive.org/cdx/search/cdx?url=%s&output=json&fl=statuscode" % self.clean_url()
req = Request(request_url, headers=hdr) # nosec
try:
response = urlopen(req) #nosec
except Exception:
try:
response = urlopen(req) #nosec
except Exception as e:
raise WaybackError(e)
response = self.get_response(req)
return str(response.read()).count(",") # Most efficient method to count number of archives (yet)