-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_urls.py
executable file
·44 lines (37 loc) · 1.12 KB
/
check_urls.py
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
#!/usr/bin/env python3
### Validate URLs found in markdown documents using HEAD requests
import sys, markdown, requests, bs4 as BeautifulSoup
def check_url(url):
try:
return bool(requests.head(url, allow_redirects=True))
except Exception as e:
print ('Error checking URL %s: %s' % (url, e))
return False
def retrieve_urls(filename):
with open(filename) as fd:
mdtext = fd.read()
html_text = markdown.markdown(mdtext)
soup = BeautifulSoup.BeautifulSoup(html_text)
return [a['href'] for a in soup.findAll('a')]
def check_urls(filename):
print ('checking URLs for %s' % (filename,))
ok = True
for url in retrieve_urls(filename):
msg = 'Checking %s => ' % (url,)
if check_url(url):
print (msg + 'OK')
else:
print (msg + 'FAILED')
ok = False
return ok
def main():
ok = True
for filename in sys.argv[1:]:
try:
ok &= check_urls(filename)
except IOError as e:
print (e)
ok = False
exit (0 if ok else 1)
if __name__ == '__main__':
main()