1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 16:49:42 +00:00

refactor link checker

This commit is contained in:
deadc0de6
2023-01-28 16:14:27 +01:00
committed by deadc0de
parent b9015528bb
commit 7e7bb0bd73

View File

@@ -12,6 +12,13 @@ from urllib.parse import urlparse
import requests import requests
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
MAGENTA = '\033[95m'
RESET = '\033[0m'
TIMEOUT = 3 TIMEOUT = 3
VALID_RET = [ VALID_RET = [
200, 200,
@@ -51,9 +58,10 @@ def check_links(urls):
cnt += 1 cnt += 1
hostname = urlparse(url).hostname hostname = urlparse(url).hostname
if hostname in IGNORES: if hostname in IGNORES:
print(f' [IGN] {url}') print(f' {YELLOW}[IGN]{RESET} {url}')
ign += 1 ign += 1
continue continue
print(f' checking {MAGENTA}{url}{RESET}')
verb = 'head' verb = 'head'
ret = requests.head(url, ret = requests.head(url,
@@ -62,7 +70,7 @@ def check_links(urls):
headers=HEADERS).status_code headers=HEADERS).status_code
if ret not in VALID_RET: if ret not in VALID_RET:
msg = ( msg = (
f' [WARN] HEAD {url} returned {ret}' f' {YELLOW}[WARN]{RESET} HEAD {url} returned {ret}'
f' ... checking with GET' f' ... checking with GET'
) )
print(msg) print(msg)
@@ -72,10 +80,10 @@ def check_links(urls):
allow_redirects=True, allow_redirects=True,
headers=HEADERS).status_code headers=HEADERS).status_code
if ret not in VALID_RET: if ret not in VALID_RET:
print(f' [ERROR] {url} returned {ret}') print(f' {RED}[ERROR]{RESET} {url} returned {ret}')
return False return False
print(f' [OK-{verb}-{ret}] {url}') print(f' [{GREEN}OK{RESET}-{verb}-{ret}] {MAGENTA}{url}{RESET}')
print(f'OK - total {cnt} links checked ({ign} ignored)') print(f' {GREEN}OK{RESET} - total {cnt} links checked ({ign} ignored)')
return True return True
@@ -84,19 +92,14 @@ if __name__ == '__main__':
print(f'usage: {sys.argv[0]} <path>') print(f'usage: {sys.argv[0]} <path>')
sys.exit(1) sys.exit(1)
print(f'checking {sys.argv[1]} for links...') print(f'checking {BLUE}{sys.argv[1]}{RESET} for links...')
links = get_links(sys.argv[1]) links = get_links(sys.argv[1])
print(f' found {len(links)} links') print(f' found {len(links)} links')
try: try:
if not check_links(links): if not check_links(links):
sys.exit(1) sys.exit(1)
except ValueError as exc: # pylint: disable=W0703
except Exception as exc:
print(f'error {exc}') print(f'error {exc}')
sys.exit(1) sys.exit(1)
except urlparse.URLError as exc:
print(f'urlparse error {exc}')
sys.exit(1)
except requests.exceptions.RequestException as exc:
print(f'requests error {exc}')
sys.exit(1)
sys.exit(0) sys.exit(0)