Skip to content

Commit

Permalink
Version 1.2 with bug fixes and support for webpage retrieval (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
akamhy authored May 5, 2020
1 parent 929790f commit 09b4ba2
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 9 deletions.
101 changes: 98 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=================
<!--ts-->
* [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)
<!--te-->

## Installation
Using [pip](https://en.wikipedia.org/wiki/Pip_(package_manager)):

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -56,6 +72,8 @@ This returns the oldest available archive for <https://google.com>.
```diff
+ waybackpy.newest(url, UA=user_agent)
```
> url is mandatory. UA is not, but highly recommended.

```python
import waybackpy
Expand All @@ -73,6 +91,8 @@ This returns the newest available archive for <https://www.microsoft.com/en-us>,
```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
Expand All @@ -89,7 +109,82 @@ returns : <http://web.archive.org/web/20100504071154/http://www.facebook.com/>
```waybackpy.near("https://www.oracle.com/index.html", year=2019, month=1, day=5, UA ="Any-User-Agent")``` returns: <http://web.archive.org/web/20190105054437/https://www.oracle.com/index.html>
> 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)
```
<details><summary>Output of the above code</summary>
<p>

###### The source code for <https://example.com/> ! As no encoding was provided, it was auto identified.

```html
<!doctype html>
<html>
<head>
<title>Example Domain</title>

<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</style>
</head>

<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
```

</p>
</details>

## Dependency
* None, just python standard libraries. Both python 2 and 3 are supported :)


## License
Expand Down
9 changes: 8 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
4 changes: 2 additions & 2 deletions waybackpy/__init__.py
Original file line number Diff line number Diff line change
@@ -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']
26 changes: 23 additions & 3 deletions waybackpy/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import json
from datetime import datetime
from waybackpy.exceptions import *
try:
Expand Down Expand Up @@ -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'),
Expand All @@ -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&timestamp=%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)

Expand Down

0 comments on commit 09b4ba2

Please sign in to comment.