Skip to content

Commit c852001

Browse files
committed
add options of recurcive and timeout of requrest
1 parent 9a50cf2 commit c852001

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

linkstatus/linkstatus.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from linkstatus.parser import parse_file
88

99

10-
def link_status(link):
10+
def link_status(link, timeout=5):
1111
"""Check link status
1212
1313
Args:
@@ -18,9 +18,9 @@ def link_status(link):
1818
"""
1919

2020
try:
21-
status_code = requests.get(link, timeout=5).status_code
21+
status_code = requests.get(link, timeout=timeout).status_code
2222
except requests.exceptions.SSLError:
23-
status_code = requests.get(link, verify=False, timeout=5).status_code
23+
status_code = requests.get(link, verify=False, timeout=timeout).status_code
2424
except Exception: # noqa
2525
# TODO: include exception in logging
2626
status_code = None
@@ -29,11 +29,11 @@ def link_status(link):
2929
return status_code == 200, status_code
3030

3131

32-
def all_files(source):
32+
def all_files(source, recursive=False):
3333
files = []
3434
for src in source:
3535
if os.path.isdir(src):
36-
files.extend([f for f in glob.glob(src + "**/**", recursive=True) if os.path.isfile(f)])
36+
files.extend([f for f in glob.glob(src + "**/**", recursive=recursive) if os.path.isfile(f)])
3737
elif os.path.isfile(src):
3838
files.append(src)
3939
else:
@@ -43,9 +43,13 @@ def all_files(source):
4343

4444
@click.command(help="Check Link Status")
4545
@click.argument("source", nargs=-1, type=click.Path())
46-
def main(source):
46+
@click.option(
47+
"-r", "--recursive", is_flag=True, help="Include all files from directories recursively"
48+
)
49+
@click.option("-t", "--timeout", default=5, help="Request timeout (default 4 second)")
50+
def main(source, recursive, timeout):
4751
exit_code = 0
48-
files = all_files(source)
52+
files = all_files(source, recursive=recursive)
4953

5054
for f in files:
5155
links = parse_file(f)
@@ -57,7 +61,7 @@ def main(source):
5761
for url in link.urls:
5862
# try two time at least
5963
for _ in range(2):
60-
status, code = link_status(url)
64+
status, code = link_status(url, timeout)
6165
if status:
6266
break
6367

linkstatus/parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
LINKS = namedtuple("LINKS", ["line", "urls"])
1010

1111

12-
def parse_line(string):
12+
def parse_line(line):
1313
"""Parse links from line/string
1414
1515
Args:
1616
string: data string
1717
Returns:
1818
list of links
1919
"""
20+
string = line.strip()
21+
2022
# if `noqa` (no quality assurance) marked in line then just skip that string
2123
if "noqa" not in string:
2224
html_format = markdown.markdown(string, output_format="html")
File renamed without changes.

0 commit comments

Comments
 (0)