Skip to content

Commit

Permalink
refactor: migrate links check to py3
Browse files Browse the repository at this point in the history
  • Loading branch information
Nytelife26 committed Jun 6, 2021
1 parent a8d43d8 commit 0b992d5
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions proselint/checks/links/broken.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,15 @@
"""
from proselint.tools import memoize
from future import standard_library
import re
try:
import urllib.request as urllib_request # for Python 3
except ImportError:
import urllib2 as urllib_request # for Python 2
from urllib import error, request
from socket import error as SocketError
standard_library.install_aliases()


@memoize
def check(text):
"""Check the text."""
err = "links.valid"
err = "links.broken"
msg = u"Broken link: {}"

regex = re.compile(
Expand All @@ -50,15 +45,12 @@ def check(text):
return errors


@memoize
def is_broken_link(url):
"""Determine whether the link returns a 404 error."""
try:
request = urllib_request.Request(
req = request.Request(
url, headers={'User-Agent': 'Mozilla/5.0'})
urllib_request.urlopen(request).read()
request.urlopen(req).read()
return False
except urllib_request.URLError:
return True
except SocketError:
except (error.URLError, SocketError):
return True

0 comments on commit 0b992d5

Please sign in to comment.