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
|
|
|
|
2021-09-09 06:57:51 -07:00
|
|
|
from urllib.request import build_opener, HTTPCookieProcessor, HTTPSHandler, Request
|
|
|
|
from urllib.error import URLError, HTTPError
|
|
|
|
from os.path import relpath
|
|
|
|
from collections import deque
|
|
|
|
import sys
|
|
|
|
from time import sleep
|
|
|
|
import ssl
|
|
|
|
from io import BytesIO
|
|
|
|
|
2021-04-05 13:39:51 -07:00
|
|
|
try:
|
2021-09-09 06:57:51 -07:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
import html5lib
|
|
|
|
import certifi
|
2021-04-05 13:39:51 -07:00
|
|
|
except ImportError:
|
2021-09-09 06:57:51 -07:00
|
|
|
print("Please install the BeautifulSoup, html5lib, and certifi libraries using `pip install bs4 html5lib certifi`.\n")
|
2021-04-05 13:39:51 -07:00
|
|
|
raise
|
|
|
|
|
2021-09-09 06:57:51 -07:00
|
|
|
if [int(v) for v in certifi.__version__.split('.')] < [2021, 5, 30]:
|
|
|
|
print("Please upgrade certifi using `pip install --upgrade certifi`.\n")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def get_links_and_destinations_from_pdf(f):
|
|
|
|
try:
|
|
|
|
from PyPDF2 import PdfFileReader
|
|
|
|
except ImportError:
|
|
|
|
print("Please install the PyPDF2 library using `pip install PyPDF2`.\n")
|
|
|
|
raise
|
2021-04-05 13:39:51 -07:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2021-09-09 06:57:51 -07:00
|
|
|
dests = pdf.getNamedDestinations().keys()
|
|
|
|
|
|
|
|
return (links, dests)
|
|
|
|
|
2021-04-05 13:39:51 -07:00
|
|
|
|
2021-09-09 06:57:51 -07:00
|
|
|
def get_links_and_destinations_from_html(f):
|
|
|
|
links = set()
|
|
|
|
internal = set()
|
|
|
|
dests = set()
|
|
|
|
|
|
|
|
soup = BeautifulSoup(f.read(), "html5lib")
|
|
|
|
for link in soup.find_all('a'):
|
|
|
|
if link.has_attr('href'):
|
|
|
|
url = link['href']
|
|
|
|
(internal if url.startswith('#') else links).add(url)
|
|
|
|
|
|
|
|
if link.has_attr('name'):
|
|
|
|
dests.add(link['name'])
|
|
|
|
|
|
|
|
for link in soup.find_all(id=True):
|
|
|
|
dests.add(link['id'])
|
2021-12-03 06:54:22 -08:00
|
|
|
# GitHub's rendering of .mediawiki files puts 'id="user-content-<ANCHOR>"' in the source
|
|
|
|
# and dynamically creates a corresponding link #<ANCHOR>.
|
|
|
|
if link['id'].startswith("user-content-"):
|
|
|
|
dests.add(link['id'][13:])
|
2021-09-09 06:57:51 -07:00
|
|
|
|
|
|
|
internal.difference_update(['#' + d for d in dests]) # ignore internal links satisfied by a dest
|
|
|
|
links.update(internal)
|
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-09-09 06:57:51 -07:00
|
|
|
print("Usage: ./links_and_dests.py [--check] [--print-dests] <file.pdf|html|xhtml>")
|
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
|
|
|
|
|
2021-09-09 06:57:51 -07:00
|
|
|
errors = deque()
|
|
|
|
|
|
|
|
print("Reading files...")
|
|
|
|
for path in paths:
|
|
|
|
print(path, end=" ")
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
with open(path, 'rb') as f:
|
|
|
|
if path.endswith(".html") or path.endswith(".xhtml"):
|
|
|
|
(links, dests) = get_links_and_destinations_from_html(f)
|
|
|
|
elif path.endswith(".pdf"):
|
|
|
|
(links, dests) = get_links_and_destinations_from_pdf(f)
|
|
|
|
else:
|
|
|
|
errors.append("Unrecognized file type: " + path)
|
|
|
|
continue
|
2021-04-08 04:04:48 -07:00
|
|
|
|
2021-09-09 06:57:51 -07:00
|
|
|
path = relpath(path)
|
2021-04-08 04:04:48 -07:00
|
|
|
for l in links:
|
|
|
|
refs = all_links.get(l, None)
|
|
|
|
if refs is None:
|
|
|
|
all_links[l] = refs = deque()
|
2021-09-09 06:57:51 -07:00
|
|
|
refs.append(path)
|
2021-04-08 04:04:48 -07:00
|
|
|
|
2021-09-09 06:57:51 -07:00
|
|
|
all_dests["https://zips.z.cash/" + path] = dests
|
|
|
|
if path.endswith(".html"):
|
|
|
|
all_dests["https://zips.z.cash/" + path[:-5]] = dests
|
2021-04-05 13:39:51 -07:00
|
|
|
|
2021-09-09 06:57:51 -07:00
|
|
|
print("\n")
|
2021-04-05 13:39:51 -07:00
|
|
|
print("Links:")
|
2021-09-09 06:57:51 -07:00
|
|
|
|
|
|
|
last_url = None
|
|
|
|
content = None
|
|
|
|
content_type = None
|
|
|
|
dests = None
|
|
|
|
|
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-09-09 06:57:51 -07:00
|
|
|
if ":" not in l:
|
|
|
|
l = "https://zips.z.cash/" + l
|
|
|
|
|
|
|
|
if l.startswith("mailto:"):
|
|
|
|
status = "(not checked)"
|
|
|
|
elif l.startswith("https:") or l.startswith("HTTP:"): # use uppercase HTTP: for links with no https: equivalent
|
2021-04-08 04:04:48 -07:00
|
|
|
(url, _, fragment) = l.partition("#")
|
2021-09-09 06:57:51 -07:00
|
|
|
|
2021-04-08 04:04:48 -07:00
|
|
|
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:
|
2021-09-09 06:57:51 -07:00
|
|
|
# If url == last_url, there is no need to refetch content. This is an optimization when
|
|
|
|
# checking URLs with the same site but different fragments (which will be sorted together).
|
|
|
|
if url != last_url:
|
2021-04-08 04:04:48 -07:00
|
|
|
headers = {"User-Agent": "Mozilla/5.0"}
|
2021-09-09 06:57:51 -07:00
|
|
|
https_handler = HTTPSHandler(context=ssl.create_default_context(cafile=certifi.where()))
|
|
|
|
|
2021-09-01 05:11:10 -07:00
|
|
|
# Some DOI links (i.e. to https://doi.org/) redirect to link.springer.com
|
|
|
|
# in a way that requires cookies (booo!). We allow this for DOI links,
|
|
|
|
# but for all other links we simulate a client that never sets cookies.
|
|
|
|
if l.startswith("https://doi.org/"):
|
2021-09-09 06:57:51 -07:00
|
|
|
opener = build_opener(HTTPCookieProcessor(), https_handler)
|
2021-09-01 05:11:10 -07:00
|
|
|
else:
|
2021-09-09 06:57:51 -07:00
|
|
|
opener = build_opener(https_handler)
|
|
|
|
|
|
|
|
for retry in range(2):
|
|
|
|
try:
|
|
|
|
response = opener.open(Request(url=l, headers=headers))
|
|
|
|
content_type = response.info().get_content_type()
|
|
|
|
content = response.read()
|
|
|
|
last_url = url
|
|
|
|
except URLError as e:
|
|
|
|
if retry == 0 and isinstance(e, HTTPError) and e.code == 429:
|
|
|
|
try:
|
|
|
|
delay = int(e.headers['Retry-After'], 10) + 1
|
|
|
|
except Exception:
|
|
|
|
delay = 60
|
|
|
|
|
|
|
|
print("(waiting %ds due to rate limiting)" % (delay,), end=" ")
|
|
|
|
sys.stdout.flush()
|
|
|
|
sleep(delay)
|
|
|
|
continue
|
|
|
|
|
|
|
|
errors.append("Could not open link: %s due to %r" % (what, e))
|
|
|
|
status = "❌"
|
|
|
|
content_type = None
|
|
|
|
content = None
|
|
|
|
last_url = None
|
|
|
|
|
|
|
|
dests = None
|
|
|
|
break
|
|
|
|
|
|
|
|
if content is not None:
|
|
|
|
if fragment:
|
|
|
|
if dests is None:
|
2021-12-03 06:54:22 -08:00
|
|
|
if content_type in ('text/html', 'application/xhtml+xml'):
|
2021-09-09 06:57:51 -07:00
|
|
|
(_, dests) = get_links_and_destinations_from_html(BytesIO(content))
|
|
|
|
elif content_type == 'application/pdf':
|
|
|
|
(_, dests) = get_links_and_destinations_from_pdf(BytesIO(content))
|
|
|
|
|
|
|
|
if dests is None:
|
|
|
|
print("(link target not checked)", end=" ")
|
|
|
|
status = "✓"
|
|
|
|
elif fragment not in dests:
|
|
|
|
errors.append("Missing link target: " + what)
|
|
|
|
status = "❌"
|
|
|
|
else:
|
|
|
|
status = "✓"
|
|
|
|
else:
|
|
|
|
status = "✓"
|
|
|
|
else:
|
|
|
|
errors.append("Insecure or unrecognized protocol in link: " + what)
|
|
|
|
status = "❌"
|
2021-04-08 04:04:48 -07:00
|
|
|
|
|
|
|
print(status)
|
|
|
|
|
|
|
|
if print_dests:
|
2021-09-09 06:57:51 -07:00
|
|
|
for (path, dests) in all_dests.items():
|
|
|
|
if path + ".html" not in all_dests: # avoid duplication
|
|
|
|
print("\nDestinations for %s:" % (path,))
|
|
|
|
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
|
|
|
|
|
2021-09-09 06:57:51 -07:00
|
|
|
|
2021-04-05 13:39:51 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv))
|