· 6 years ago · Mar 07, 2020, 03:54 PM
1#!/usr/bin/env python3
2
3import os
4import sys
5import json
6import argparse
7import requests
8import archiveis
9
10# Modify this to add a default Perma.cc API key.
11perma_key = ""
12
13def internet_archive(url):
14 """
15 Function for pushing to The Internet Archive/Wayback Machine
16 """
17 print("[*] Pushing to the Wayback Machine...")
18
19 save_url = "https://web.archive.org/save/{}".format(url)
20 response = requests.get(save_url)
21
22 if response.status_code == 200:
23 result = response.headers["Content-Location"]
24 internet_archive_url = "https://web.archive.org{}".format(result)
25
26 print(internet_archive_url)
27 else:
28 print("[!] Connection error")
29
30def main():
31 global perma_key
32
33 parser = argparse.ArgumentParser(description="Archive a webpage on multiple online web archives")
34 parser.add_argument("--perma-key", action="store", required=False,
35 help="Specify API key required to submit URL to Perma.cc")
36 parser.add_argument("--list", action="store_true", default=False, required=False,
37 help="Enable this flag if the target is not a URL, but a file with list of URLs")
38 parser.add_argument("target", action="store",
39 help="The URL you want to archive or the path to a list of URLs")
40 args = parser.parse_args()
41
42 if args.perma_key:
43 perma_key = args.perma_key
44
45 urls = []
46
47 if args.list:
48 if not os.path.exists(args.target):
49 print("[!] ERROR: the file you specified does not exist")
50 sys.exit(-1)
51
52 with open(args.target, "r") as handle:
53 for line in handle:
54 line = line.strip()
55 if line == "":
56 continue
57 urls.append(line)
58 else:
59 urls.append(args.target)
60
61 for url in urls:
62 print("[+] Archiving {}...".format(url))
63
64 for func in [internet_archive]:
65 try:
66 func(url)
67 except Exception as e:
68 print("[!] ERROR: {}".format(e))
69
70if __name__ == "__main__":
71 main()