This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from Delegation-numerique-en-sante/link-checks
Tech: ajout d’un vérificateur de liens basique
- Loading branch information
Showing
4 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
httpx==0.13.3 | ||
Jinja2==2.11.2 | ||
livereload==2.6.1 | ||
MarkupSafe==1.1.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python3 | ||
import fnmatch | ||
import os | ||
from html.parser import HTMLParser | ||
from pathlib import Path | ||
from time import perf_counter | ||
from http import HTTPStatus | ||
|
||
import httpx | ||
from minicli import cli, run, wrap | ||
|
||
HERE = Path(__file__).parent | ||
|
||
|
||
class LinkExtractor(HTMLParser): | ||
def reset(self): | ||
HTMLParser.reset(self) | ||
self.links = set() | ||
|
||
def handle_starttag(self, tag, attrs): | ||
if tag == "a": | ||
attrs = dict(attrs) | ||
if attrs["href"].startswith("http"): | ||
self.links.update([attrs["href"]]) | ||
|
||
|
||
@cli | ||
def links(): | ||
parser = LinkExtractor() | ||
content = open(HERE / "src" / "index.html").read() | ||
parser.feed(content) | ||
for link in parser.links: | ||
response = httpx.get(link) | ||
if response.status_code != HTTPStatus.OK: | ||
raise Exception(f"{link} is broken! ({response.status_code})") | ||
|
||
|
||
@wrap | ||
def perf_wrapper(): | ||
start = perf_counter() | ||
yield | ||
elapsed = perf_counter() - start | ||
print(f"Done in {elapsed:.5f} seconds.") | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |