Repository navigation
Expand file tree
/
Copy pathchecks.py
More file actions
55 lines (44 loc) · 1.41 KB
/
Copy pathchecks.py
File metadata and controls
55 lines (44 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import argparse
import re
import sys
from pathlib import Path
import requests
BASEDIR = Path(".")
def check_link(link, bookmark_file, only_error=False):
invalid = False
msg = f"{bookmark_file}: {link} => OK"
try:
response = requests.get(link)
status = response.status_code
if status >= 400:
msg = f"{bookmark_file}: {link} => KO ({status})"
invalid = True
except requests.exceptions.ConnectionError:
msg = f"{bookmark_file}: {link} => KO (connection error)"
invalid = True
if only_error and not invalid:
return invalid
print(msg)
return invalid
def check_file(bookmark_file, only_error=False):
invalid = True
with open(bookmark_file) as content:
for line in content.readlines():
if "http" not in line:
continue
match = re.search("(?P<url>https?://[^\s]+)", line).group("url") # noqa
link = match.replace(')', '')
check_link(link, bookmark_file, only_error)
return invalid
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--only-error', action='store_true')
args = parser.parse_args()
invalid = False
for bookmark_file in list(BASEDIR.glob('**/*.md')):
invalid = check_file(
bookmark_file,
args.only_error)
sys.exit(int(invalid))
if __name__ == "__main__":
main()