Skip to content

Commit 19953da

Browse files
committed
user requests and change display format
1 parent 5ea1cd9 commit 19953da

3 files changed

Lines changed: 30 additions & 18 deletions

File tree

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
with open("README.md") as readme_file:
66
readme = readme_file.read()
77

8-
install_requirements = ["click", "markdown"]
8+
install_requirements = ["click", "markdown", "requests"]
99

1010
setup_requirements = ["setuptools_scm"]
1111

@@ -17,6 +17,9 @@
1717
"Programming Language :: Python :: 3.5",
1818
"Programming Language :: Python :: 3.6",
1919
"Programming Language :: Python :: 3.7",
20+
'Intended Audience :: End Users/Desktop',
21+
'Intended Audience :: Developers',
22+
'Environment :: Console',
2023
],
2124
python_requires=">=3.5",
2225
description="Simple text/ markdown links status checker",
@@ -27,7 +30,7 @@
2730
include_package_data=True,
2831
setup_requires=setup_requirements,
2932
use_scm_version=True,
30-
keywords="linkstatus, linkchecker, link-checker, markdown, text, linklint",
33+
keywords=["linkstatus", "linkchecker", "link-checker", "markdown", "text", "linklint", "link"],
3134
name="linkstatus",
3235
packages=find_packages(include=["linkstatus"]),
3336
url="https://github.com/digitronik/linkstatus",

src/linkstatus.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import os
22
import glob
3-
from urllib import request
4-
from urllib.error import URLError
5-
from urllib.error import HTTPError
63

4+
import requests
75
import click
86

97
from src.parser import parse_file
@@ -16,15 +14,19 @@ def link_status(link):
1614
link: link to check status
1715
1816
Returns:
19-
tuple of status (bool) and status code/ reason
17+
tuple of status (bool) and status code
2018
"""
19+
2120
try:
22-
connection = request.urlopen(link)
23-
return True, connection.code
24-
except HTTPError as e:
25-
return False, e.code
26-
except URLError as e:
27-
return False, e.reason
21+
status_code = requests.get(link, timeout=3).status_code
22+
except requests.exceptions.SSLError:
23+
status_code = requests.get(link, verify=False, timeout=3).status_code
24+
except Exception: #noqa
25+
# TODO: include exception in logging
26+
status_code = None
27+
pass
28+
29+
return status_code == 200, status_code
2830

2931

3032
def all_files(source):
@@ -54,9 +56,16 @@ def main(source):
5456
for url in link.urls:
5557
status, code = link_status(url)
5658

57-
fg = "green" if status else "red"
58-
_status = "UP" if status else "DOWN"
59-
59+
if status:
60+
fg = "green"
61+
icon = "✓"
62+
else:
63+
fg = "red"
64+
icon = "✗"
6065
click.echo(
61-
"L{}: {} ==> {}".format(link.line, url, click.style(_status, fg=fg))
66+
"{icon} L{ln} : {l}".format(
67+
icon=click.style(icon, fg=fg, bold=True),
68+
ln=link.line,
69+
l=click.style(url, fg=fg)
70+
)
6271
)

src/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def parse_line(string):
2222
html_format = markdown.markdown(string, output_format='html')
2323
links = re.findall(REGULAR_EXP, html_format)
2424

25-
# TODO: Improve regex to remove this workaround for trailing </p>
26-
links = [l.replace('</p>', '') if '</p>' in l else l for l in links]
25+
# TODO: Improve regex to remove this workaround for trailing </p> or </li>
26+
links = [l.replace('</p>', '').replace('</li>', "") for l in links]
2727
return links
2828
else:
2929
return []

0 commit comments

Comments
 (0)