-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathlink_validator.py
50 lines (35 loc) · 1.59 KB
/
link_validator.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
45
46
47
48
49
50
# Simple link validator for the README.md file
import asyncio
import re
import httpx
async def validate_link(client: httpx.AsyncClient, url: str) -> tuple[str, bool]:
url = url.rstrip(")")
try:
response = await client.head(url, follow_redirects=True, timeout=5.0)
is_valid = 200 <= response.status_code < 400
print(f"URL: {url}, Status Code: {response.status_code}")
return url, is_valid
except Exception as e:
print(f"URL: {url}, Exception: {e}")
return url, False
def extract_markdown_links(content: str) -> list[str]:
# Match markdown links [text](url) and direct URLs
markdown_pattern = r"\[(?:[^\]]*)\]\((https?://[^\s\)]+)\)"
direct_url_pattern = r"(?<!\])\b(https?://[^\s\)]+)"
# Find all markdown format links
markdown_links = re.findall(markdown_pattern, content)
# Find all direct URLs (not part of markdown syntax)
direct_urls = re.findall(direct_url_pattern, content)
return markdown_links + direct_urls
async def check_readme_links(readme_path: str, only_invalid: bool = False) -> None:
with open(readme_path, "r") as file:
content = file.read()
links = extract_markdown_links(content)
async with httpx.AsyncClient() as client:
tasks = [validate_link(client, link) for link in links]
results = await asyncio.gather(*tasks)
print("Results:")
for url, is_valid in results:
if not only_invalid or not is_valid:
print(f"{url}: {'Valid' if is_valid else 'Invalid'}")
asyncio.run(check_readme_links("README.md", only_invalid=True))