2021-04-05 13:39:51 -07:00
|
|
|
#!/usr/bin/env python3
|
2021-04-08 04:04:48 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2021-04-05 13:39:51 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
from PyPDF2 import PdfFileReader
|
|
|
|
except ImportError:
|
|
|
|
print("Please install the PyPDF2 library using `pip3 install PyPDF2`.\n")
|
|
|
|
raise
|
|
|
|
|
2021-04-08 04:04:48 -07:00
|
|
|
from urllib.request import urlopen, Request
|
|
|
|
from urllib.error import URLError
|
|
|
|
from os.path import basename
|
2021-04-05 13:39:51 -07:00
|
|
|
from collections import deque
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def get_links_and_destinations(f):
|
|
|
|
# Based on <https://stackoverflow.com/a/5978161/393146>
|
|
|
|
pdf = PdfFileReader(f)
|
|
|
|
|
2021-04-08 04:04:48 -07:00
|
|
|
links = set()
|
2021-04-05 13:39:51 -07:00
|
|
|
for pg in range(pdf.getNumPages()):
|
|
|
|
obj = pdf.getPage(pg).getObject()
|
|
|
|
|
|
|
|
for annotation in obj.get('/Annots', []):
|
|
|
|
uri = annotation.getObject().get('/A', {}).get('/URI', None)
|
|
|
|
if uri is not None and uri not in links:
|
2021-04-08 04:04:48 -07:00
|
|
|
links.add(uri)
|
2021-04-05 13:39:51 -07:00
|
|
|
|
|
|
|
dests = pdf.getNamedDestinations()
|
|
|
|
|
2021-04-08 04:04:48 -07:00
|
|
|
return (links, dests)
|
2021-04-05 13:39:51 -07:00
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
if len(args) < 2:
|
2021-04-08 04:04:48 -07:00
|
|
|
print("Usage: ./links_and_dests.py [--check] [--print-dests] <file.pdf>")
|
2021-04-05 13:39:51 -07:00
|
|
|
return 1
|
|
|
|
|
2021-04-08 04:04:48 -07:00
|
|
|
check = '--check' in args[1:]
|
|
|
|
print_dests = '--print-dests' in args[1:]
|
|
|
|
paths = [arg for arg in args[1:] if not arg.startswith('--')]
|
|
|
|
|
|
|
|
all_links = {} # url -> pdf_paths
|
|
|
|
all_dests = {} # url -> dests
|
|
|
|
|
|
|
|
for pdf_path in paths:
|
|
|
|
with open(pdf_path, 'rb') as f:
|
|
|
|
(links, dests) = get_links_and_destinations(f)
|
|
|
|
|
|
|
|
for l in links:
|
|
|
|
refs = all_links.get(l, None)
|
|
|
|
if refs is None:
|
|
|
|
all_links[l] = refs = deque()
|
|
|
|
refs.append(pdf_path)
|
|
|
|
|
|
|
|
all_dests["https://zips.z.cash/protocol/" + basename(pdf_path)] = dests
|
|
|
|
|
|
|
|
errors = deque()
|
2021-04-05 13:39:51 -07:00
|
|
|
|
|
|
|
print("Links:")
|
2021-04-08 04:04:48 -07:00
|
|
|
for (l, p) in sorted(all_links.items()):
|
|
|
|
print(l, end=" ")
|
|
|
|
sys.stdout.flush()
|
|
|
|
what = "%s (occurs in %s)" % (l, " and ".join(p)) if len(paths) > 1 else l
|
|
|
|
status = ""
|
2021-04-05 13:39:51 -07:00
|
|
|
|
2021-04-08 04:04:48 -07:00
|
|
|
if not l.startswith("https:"):
|
|
|
|
errors.append("Insecure or unrecognized protocol in link: " + what)
|
|
|
|
status = "❌"
|
|
|
|
else:
|
|
|
|
(url, _, fragment) = l.partition("#")
|
|
|
|
if url in all_dests:
|
|
|
|
if fragment and fragment not in all_dests[url]:
|
|
|
|
errors.append("Missing link target: " + what)
|
|
|
|
status = "❌"
|
|
|
|
else:
|
|
|
|
status = "✓"
|
|
|
|
elif check:
|
|
|
|
try:
|
|
|
|
headers = {"User-Agent": "Mozilla/5.0"}
|
|
|
|
res = urlopen(Request(url=l, headers=headers))
|
|
|
|
res.read()
|
|
|
|
status = "✓"
|
|
|
|
except URLError as e:
|
|
|
|
errors.append("Could not open link: %s due to %r" % (what, e))
|
|
|
|
status = "❌"
|
|
|
|
|
|
|
|
print(status)
|
|
|
|
|
|
|
|
if print_dests:
|
|
|
|
for dests in all_dests:
|
|
|
|
print("\nDestinations for %s:" % (dests,))
|
|
|
|
for d in dests:
|
|
|
|
print(d)
|
2021-04-05 13:39:51 -07:00
|
|
|
|
|
|
|
if errors:
|
|
|
|
print("\nErrors:")
|
|
|
|
for e in errors:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv))
|