-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdead_links.py
62 lines (51 loc) · 1.66 KB
/
dead_links.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
51
52
53
54
55
56
57
58
59
60
61
62
import json
from pathlib import Path
import urllib.request
import concurrent.futures
import os
import datetime as dt
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
REPO = "Tobi-De/htmx_extensions"
def check_link(url):
try:
urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
print(f"HTTPError: {url} {e.code}")
return url
except urllib.error.URLError as e:
print(f"URLError: {url} {e.reason}")
return url
else:
print(f"OK: {url}")
def check_dead_links():
extensions = Path("data/extensions.json")
extensions = extensions.read_text()
extensions = json.loads(extensions)
with concurrent.futures.ThreadPoolExecutor() as executor:
future_to_url = {
executor.submit(check_link, url): url
for extension in extensions.values()
for url in (extension["doc_url"], extension["download_url"])
}
dead_links = [
future.result()
for future in concurrent.futures.as_completed(future_to_url)
if future.result() is not None
]
return dead_links
def create_github_issue(dead_links):
url = f"https://api.github.com/repos/{REPO}/issues"
headers = {
"Authorization": f"token {GITHUB_TOKEN}",
"Accept": "application/vnd.github.v3+json",
}
data = {
"title": f"Dead links - {dt.datetime.now().strftime('%Y-%m-%d %H:%M')}",
"body": "\n".join(dead_links),
}
req = urllib.request.Request(url, json.dumps(data).encode(), headers)
urllib.request.urlopen(req)
if __name__ == "__main__":
dead_links = check_dead_links()
if dead_links:
create_github_issue(dead_links)