2020-05-04 21:06:00 +05:30
# waybackpy
2020-07-18 00:40:21 +05:30
[](https://travis-ci.org/akamhy/waybackpy)
2020-05-05 17:54:38 +05:30
[](https://pypistats.org/packages/waybackpy)
2020-05-05 19:08:26 +05:30
[](https://github.com/akamhy/waybackpy/releases)
2020-05-05 09:37:53 +05:30
[](https://www.codacy.com/manual/akamhy/waybackpy?utm_source=github.com& utm_medium=referral& utm_content=akamhy/waybackpy& utm_campaign=Badge_Grade)
2020-05-05 09:31:43 +05:30
[](https://github.com/akamhy/waybackpy/blob/master/LICENSE)
2020-05-06 09:18:02 +05:30
[](https://codeclimate.com/github/akamhy/waybackpy/maintainability)
2020-05-07 16:51:08 +05:30
[](https://www.codefactor.io/repository/github/akamhy/waybackpy)
2020-05-05 09:31:43 +05:30
[](https://www.python.org/)
2020-05-07 20:02:47 +05:30

2020-05-07 08:23:31 +05:30

2020-05-05 09:31:43 +05:30
[](https://github.com/akamhy/waybackpy/graphs/commit-activity)
2020-07-16 20:39:12 +05:30
[](https://codecov.io/gh/akamhy/waybackpy)
2020-07-18 00:32:00 +05:30

2020-07-18 16:22:09 +05:30

2020-05-05 09:31:43 +05:30
2020-05-04 21:06:00 +05:30


2020-07-18 16:46:40 +05:30
Waybackpy is a Python library that interfaces with the [Internet Archive ](https://en.wikipedia.org/wiki/Internet_Archive )'s [Wayback Machine ](https://en.wikipedia.org/wiki/Wayback_Machine ) API. Archive pages and retrieve archived pages easily.
2020-05-04 21:06:00 +05:30
2020-05-05 09:03:16 +05:30
Table of contents
=================
<!-- ts -->
2020-05-05 09:27:02 +05:30
2020-07-18 00:20:58 +05:30
* [Installation ](#installation )
2020-05-05 09:27:02 +05:30
2020-07-18 00:20:17 +05:30
* [Usage ](#usage )
2020-07-22 21:35:02 +05:30
* [As a python package ](#as-a-python-package )
* [Saving an url using save() ](#capturing-aka-saving-an-url-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-newest-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 )
* [Count total archives for an URL using total_archives() ](#count-total-archives-for-an-url-using-total_archives )
* [With CLI ](#with-the-cli )
* [Save ](#save )
* [Oldest archive ](#oldest-archive )
* [Newest archive ](#newest-archive )
* [Total archives ](#total-number-of-archives )
* [Archive near a time ](#archive-near-time )
* [Get the source code ](#get-the-source-code )
2020-05-05 09:27:02 +05:30
2020-07-18 00:20:17 +05:30
* [Tests ](#tests )
2020-05-05 09:27:02 +05:30
2020-07-18 00:20:17 +05:30
* [Dependency ](#dependency )
2020-05-05 09:27:02 +05:30
2020-07-18 16:30:20 +05:30
* [License ](#license )
2020-05-05 09:27:02 +05:30
2020-05-05 09:03:16 +05:30
<!-- te -->
2020-05-04 21:06:00 +05:30
## Installation
Using [pip ](https://en.wikipedia.org/wiki/Pip_(package_manager )):
2020-07-18 08:16:59 +05:30
```bash
pip install waybackpy
```
2020-07-22 21:35:02 +05:30
or direct from this repository using git.
```bash
pip install git+https://github.com/akamhy/waybackpy.git
```
2020-05-04 21:06:00 +05:30
## Usage
2020-07-22 21:35:02 +05:30
### As a python package
2020-07-19 16:39:39 +05:30
#### Capturing aka Saving an url using save()
2020-05-04 21:06:00 +05:30
```python
import waybackpy
2020-07-19 16:39:39 +05:30
new_archive_url = waybackpy.Url(
url = "https://en.wikipedia.org/wiki/Multivariable_calculus",
user_agent = "Mozilla/5.0 (Windows NT 5.1; rv:40.0) Gecko/20100101 Firefox/40.0"
).save()
print(new_archive_url)
```
```bash
https://web.archive.org/web/20200504141153/https://github.com/akamhy/waybackpy
2020-05-04 21:06:00 +05:30
```
2020-07-19 22:28:08 +05:30
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPySaveExample ></ sub >
2020-05-04 21:06:00 +05:30
2020-07-19 16:39:39 +05:30
#### Receiving the oldest archive for an URL using oldest()
2020-05-04 21:06:00 +05:30
```python
import waybackpy
2020-07-19 16:39:39 +05:30
oldest_archive_url = waybackpy.Url(
"https://www.google.com/",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:40.0) Gecko/20100101 Firefox/40.0"
).oldest()
print(oldest_archive_url)
2020-05-04 21:06:00 +05:30
```
2020-07-19 16:39:39 +05:30
```bash
http://web.archive.org/web/19981111184551/http://google.com:80/
```
2020-07-19 22:28:08 +05:30
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyOldestExample ></ sub >
2020-05-04 21:06:00 +05:30
2020-05-05 09:03:16 +05:30
2020-05-04 21:06:00 +05:30
2020-07-18 16:22:09 +05:30
#### Receiving the newest archive for an URL using newest()
2020-05-04 21:06:00 +05:30
```python
import waybackpy
2020-07-19 16:39:39 +05:30
newest_archive_url = waybackpy.Url(
"https://www.facebook.com/",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0"
).newest()
print(newest_archive_url)
```
```bash
https://web.archive.org/web/20200714013225/https://www.facebook.com/
2020-05-04 21:06:00 +05:30
```
2020-07-19 22:28:08 +05:30
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyNewestExample ></ sub >
2020-05-04 21:06:00 +05:30
2020-05-05 09:03:16 +05:30
2020-05-04 21:06:00 +05:30
2020-07-18 16:22:09 +05:30
#### Receiving archive close to a specified year, month, day, hour, and minute using near()
2020-05-04 21:06:00 +05:30
```python
2020-07-19 16:39:39 +05:30
from waybackpy import Url
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0"
github_url = "https://github.com/"
github_wayback_obj = Url(github_url, user_agent)
# 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.
```
```python
github_archive_near_2010 = github_wayback_obj.near(year=2010)
print(github_archive_near_2010)
```
```bash
https://web.archive.org/web/20100719134402/http://github.com/
```
```python
github_archive_near_2011_may = github_wayback_obj.near(year=2011, month=5)
print(github_archive_near_2011_may)
```
```bash
https://web.archive.org/web/20110519185447/https://github.com/
2020-05-04 21:06:00 +05:30
```
2020-07-19 16:39:39 +05:30
```python
github_archive_near_2015_january_26 = github_wayback_obj.near(
year=2015, month=1, day=26
)
print(github_archive_near_2015_january_26)
```
```bash
https://web.archive.org/web/20150127031159/https://github.com
```
```python
github_archive_near_2018_4_july_9_2_am = github_wayback_obj.near(
year=2018, month=7, day=4, hour = 9, minute = 2
)
print(github_archive_near_2018_4_july_9_2_am)
```
```bash
https://web.archive.org/web/20180704090245/https://github.com/
2020-05-04 21:06:00 +05:30
2020-07-19 16:39:39 +05:30
```
< sub > The library doesn't supports seconds yet. You are encourged to create a PR ;)< / sub >
2020-07-19 22:28:08 +05:30
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyNearExample ></ sub >
2020-05-04 21:06:00 +05:30
2020-05-05 09:03:16 +05:30
2020-07-18 16:22:09 +05:30
#### Get the content of webpage using get()
2020-05-05 09:03:16 +05:30
```python
2020-07-18 16:22:09 +05:30
import waybackpy
2020-07-19 16:39:39 +05:30
google_url = "https://www.google.com/"
User_Agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36"
waybackpy_url_object = waybackpy.Url(google_url, User_Agent)
# If no argument is passed in get(), it gets the source of the Url used to create the object.
current_google_url_source = waybackpy_url_object.get()
print(current_google_url_source)
# The following chunk of code will force a new archive of google.com and get the source of the archived page.
# waybackpy_url_object.save() type is string.
google_newest_archive_source = waybackpy_url_object.get(
waybackpy_url_object.save()
)
print(google_newest_archive_source)
# waybackpy_url_object.oldest() type is str, it's oldest archive of google.com
google_oldest_archive_source = waybackpy_url_object.get(
waybackpy_url_object.oldest()
)
print(google_oldest_archive_source)
2020-05-05 09:03:16 +05:30
```
2020-07-19 22:28:08 +05:30
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyGetExample#main . py ></ sub >
2020-07-19 16:39:39 +05:30
2020-05-05 09:03:16 +05:30
2020-05-07 19:02:12 +05:30
#### Count total archives for an URL using total_archives()
2020-05-07 15:12:37 +05:30
```python
2020-07-19 16:39:39 +05:30
import waybackpy
URL = "https://en.wikipedia.org/wiki/Python (programming language)"
UA = "Mozilla/5.0 (iPad; CPU OS 8_1_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B435 Safari/600.1.4"
archive_count = waybackpy.Url(
url=URL,
user_agent=UA
).total_archives()
2020-05-07 15:12:37 +05:30
2020-07-19 16:39:39 +05:30
print(archive_count) # total_archives() returns an int
```
```bash
2440
```
2020-07-19 22:28:08 +05:30
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyTotalArchivesExample ></ sub >
2020-07-18 16:22:09 +05:30
2020-07-22 21:35:02 +05:30
### With the CLI
#### Save
```bash
$ waybackpy --url "https://en.wikipedia.org/wiki/Social_media" --user_agent "my-unique-user-agent" --save
https://web.archive.org/web/20200719062108/https://en.wikipedia.org/wiki/Social_media
```
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyBashSave ></ sub >
#### Oldest archive
```bash
$ waybackpy --url "https://en.wikipedia.org/wiki/SpaceX" --user_agent "my-unique-user-agent" --oldest
https://web.archive.org/web/20040803000845/http://en.wikipedia.org:80/wiki/SpaceX
```
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyBashOldest ></ sub >
#### Newest archive
```bash
$ waybackpy --url "https://en.wikipedia.org/wiki/YouTube" --user_agent "my-unique-user-agent" --newest
https://web.archive.org/web/20200606044708/https://en.wikipedia.org/wiki/YouTube
```
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyBashNewest ></ sub >
#### Total number of archives
```bash
$ waybackpy --url "https://en.wikipedia.org/wiki/Linux_kernel" --user_agent "my-unique-user-agent" --total
853
```
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyBashTotal ></ sub >
#### Archive near time
```bash
$ waybackpy --url facebook.com --user_agent "my-unique-user-agent" --near --year 2012 --month 5 --day 12
https://web.archive.org/web/20120512142515/https://www.facebook.com/
```
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyBashNear ></ sub >
#### Get the source code
```bash
$ waybackpy --url google.com --user_agent "my-unique-user-agent" --get url # Prints the source code of the url
$ waybackpy --url google.com --user_agent "my-unique-user-agent" --get oldest # Prints the source code of the oldest archive
$ waybackpy --url google.com --user_agent "my-unique-user-agent" --get newest # Prints the source code of the newest archive
$ waybackpy --url google.com --user_agent "my-unique-user-agent" --get save # Save a new archive on wayback machine then print the source code of this archive.
```
< sub > Try this out in your browser @ < https: // repl . it / @akamhy/WaybackPyBashGet ></ sub >
2020-05-05 17:47:00 +05:30
## Tests
* [Here ](https://github.com/akamhy/waybackpy/tree/master/tests )
2020-07-18 16:22:09 +05:30
2020-05-05 09:03:16 +05:30
## Dependency
2020-07-22 21:35:02 +05:30
* None, just python standard libraries (re, json, urllib, argparse and datetime). Both python 2 and 3 are supported :)
2020-05-04 21:06:00 +05:30
## License
2020-05-05 17:44:12 +05:30
[MIT License ](https://github.com/akamhy/waybackpy/blob/master/LICENSE )