From 09b4ba264947ebdc8937224a78fe0cc08299d9f7 Mon Sep 17 00:00:00 2001 From: akamhy <64683866+akamhy@users.noreply.github.com> Date: Tue, 5 May 2020 09:03:16 +0530 Subject: [PATCH] Version 1.2 with bug fixes and support for webpage retrieval (#4) --- README.md | 101 ++++++++++++++++++++++++++++++++++++++++-- setup.py | 9 +++- waybackpy/__init__.py | 4 +- waybackpy/wrapper.py | 26 +++++++++-- 4 files changed, 131 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index ca9a886..e7ebe55 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,21 @@ The waybackpy is a python wrapper for [Internet Archive](https://en.wikipedia.org/wiki/Internet_Archive) 's [Wayback Machine](https://en.wikipedia.org/wiki/Wayback_Machine). +Table of contents +================= + + * [Installation](#installation) + * [Usage](#usage) + * [Capturing/Saving an url/website. Using save().](#capturingsaving-an-urlwebsite-using-save) + * [Receiving the oldest archive for an URL. Using oldest().](#receiving-the-oldest-archive-for-an-url-using-oldest) + * [Receiving the recent most/newest archive for an URL. Using newest().](#receiving-the-recent-mostnewest-archive-for-an-url-using-newest) + * [Receiving archive close to a specified year, month, day, hour, and minute! Using near().](#receiving-archive-close-to-a-specified-year-month-day-hour-and-minute-using-near) + * [Get the content of webpage using get().](#get-the-content-of-webpage-using-get) + * [Tests](#tests) + * [Dependency](#dependency) + * [License](#license) + + ## Installation Using [pip](https://en.wikipedia.org/wiki/Pip_(package_manager)): @@ -21,10 +36,10 @@ Using [pip](https://en.wikipedia.org/wiki/Pip_(package_manager)): ```diff + waybackpy.save(url, UA=user_agent) ``` - +> url is mandatory. UA is not, but highly recommended. ```python import waybackpy -# Capturing a new archive on wayback machine. +# Capturing a new archive on Wayback machine. # Default user-agent (UA) is "waybackpy python package", if not specified in the call. archived_url = waybackpy.save("https://github.com/akamhy/waybackpy", UA = "Any-User-Agent") print(archived_url) @@ -38,6 +53,7 @@ This should print something similar to the following archived URL: ```diff + waybackpy.oldest(url, UA=user_agent) ``` +> url is mandatory. UA is not, but highly recommended. ```python @@ -56,6 +72,8 @@ This returns the oldest available archive for . ```diff + waybackpy.newest(url, UA=user_agent) ``` +> url is mandatory. UA is not, but highly recommended. + ```python import waybackpy @@ -73,6 +91,8 @@ This returns the newest available archive for , ```diff + waybackpy.near(url, year=2020, month=1, day=1, hour=1, minute=1, UA=user_agent) ``` +> url is mandotory. year,month,day,hour and minute are optional arguments. UA is not mandotory, but higly recomended. + ```python import waybackpy @@ -89,7 +109,82 @@ returns : ```waybackpy.near("https://www.oracle.com/index.html", year=2019, month=1, day=5, UA ="Any-User-Agent")``` returns: > Please note that if you only specify the year, the current month and day are default arguments for month and day respectively. Do not expect just putting the year parameter would return the archive closer to January but the current month you are using the package. If you are using it in July 2018 and let's say you use ```waybackpy.near("https://www.facebook.com/", year=2011, UA ="Any-User-Agent")``` then you would be returned the nearest archive to July 2011 and not January 2011. You need to specify the month "1" for January. -> Do not pad (use zeros in month, year, day, minute and hour arguments). +> Do not pad (don't use zeros in the month, year, day, minute, and hour arguments). e.g. For January, set month = 1 and not month = 01. + +#### Get the content of webpage using get(). + +```diff ++ waybackpy.get(url, encoding="UTF-8", UA=user_agent) +``` +> url is mandatory. UA is not, but highly recommended. encoding is detected automatically, don't specify unless necessary. + +```python +from waybackpy import get +# retriving the webpage from any url including the archived urls. Don't need to import other libraies :) +# Default user-agent (UA) is "waybackpy python package", if not specified in the call. +# supported argumnets are url, encoding and UA +webpage = get("https://example.com/", UA="User-Agent") +print(webpage) +``` +
Output of the above code +

+ +###### The source code for ! As no encoding was provided, it was auto identified. + +```html + + + + Example Domain + + + + + + + + +

+

Example Domain

+

This domain is for use in illustrative examples in documents. You may use this + domain in literature without prior coordination or asking for permission.

+

More information...

+
+ + +``` + +

+
+ +## Dependency +* None, just python standard libraries. Both python 2 and 3 are supported :) ## License diff --git a/setup.py b/setup.py index d9d91df..278473d 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,17 @@ from distutils.core import setup +import os.path + +with open(os.path.join(os.path.dirname(__file__), 'README.md')) as f: + readme = f.read() + setup( name = 'waybackpy', packages = ['waybackpy'], - version = 'v1.1', + version = 'v1.2', license='MIT', description = 'A python wrapper for Internet Archives Wayback Machine', + long_description=readme, + long_description_content_type='text/markdown', author = 'akamhy', author_email = 'akash3pro@gmail.com', url = 'https://github.com/akamhy/waybackpy', diff --git a/waybackpy/__init__.py b/waybackpy/__init__.py index 46e2ff7..5e3e1c4 100644 --- a/waybackpy/__init__.py +++ b/waybackpy/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from .wrapper import save, near, oldest, newest +from .wrapper import save, near, oldest, newest, get -__version__ = "1.1" +__version__ = "v1.2" __all__ = ['wrapper', 'exceptions'] diff --git a/waybackpy/wrapper.py b/waybackpy/wrapper.py index 3b77ba5..e136dfb 100644 --- a/waybackpy/wrapper.py +++ b/waybackpy/wrapper.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import json from datetime import datetime from waybackpy.exceptions import * try: @@ -39,6 +40,26 @@ def save(url,UA=default_UA): archived_url = "https://web.archive.org" + archive_id return archived_url +def get(url,encoding=None,UA=default_UA): + hdr = { 'User-Agent' : '%s' % UA } + request_url = clean_url(url) + req = Request(request_url, headers=hdr) + resp=urlopen(req) + if encoding is None: + try: + encoding= resp.headers['content-type'].split('charset=')[-1] + except: + encoding = "UTF-8" + return resp.read().decode(encoding) + +def wayback_timestamp(year,month,day,hour,minute): + year = str(year) + month = str(month).zfill(2) + day = str(day).zfill(2) + hour = str(hour).zfill(2) + minute = str(minute).zfill(2) + return (year+month+day+hour+minute) + def near( url, year=datetime.utcnow().strftime('%Y'), @@ -48,13 +69,12 @@ def near( minute=datetime.utcnow().strftime('%M'), UA=default_UA, ): - timestamp = str(year)+str(month)+str(day)+str(hour)+str(minute) + timestamp = wayback_timestamp(year,month,day,hour,minute) request_url = "https://archive.org/wayback/available?url=%s×tamp=%s" % (clean_url(url), str(timestamp)) hdr = { 'User-Agent' : '%s' % UA } req = Request(request_url, headers=hdr) response = urlopen(req) #nosec - import json - data = json.loads(response.read().decode('utf8')) + data = json.loads(response.read().decode("UTF-8")) if not data["archived_snapshots"]: raise ArchiveNotFound("'%s' is not yet archived." % url)