-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_docs.py
107 lines (89 loc) · 2.42 KB
/
update_docs.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import subprocess
from pathlib import Path
import requests
import urllib3
from bs4 import BeautifulSoup
from tqdm import tqdm
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
folders = ["easy", "medium", "hard"]
language_map = {
"py": "Python",
}
summary = {}
all_files = []
cache = {}
for folder in folders:
uri = f"src/{folder}"
if not Path(uri).exists():
Path(uri).mkdir(exist_ok=True)
files = list(Path(uri).glob("*"))
solutions = {}
for file in files:
if file.stem not in solutions:
solutions[file.stem] = [file.name]
else:
solutions[file.stem].append(file.name)
all_files.append(file.name)
total = len(solutions)
summary[folder] = total
try:
subprocess.call(
[
"black",
uri,
]
)
except:
pass
text = """---
hide:
- toc
---
"""
text += f"""
# Difficulty - {folder.capitalize()}
"""
prompt = f"📖 Refreshing {folder.capitalize()}"
for solution, languages in tqdm(sorted(solutions.items()), desc=prompt):
url = f"https://leetcode.com/problems/{solution}/"
if solution in cache:
name = cache[solution]
else:
html = requests.get(url, verify=False).text
soup = BeautifulSoup(html, "html.parser")
name = soup.find("title").text.split(" - ")[0]
cache[solution] = name
if len(languages) > 1:
suffix = f"s in {len(languages)} languages"
else:
ext = languages[0].split(".")[-1]
suffix = f" in {language_map[ext]}"
card = f"""
## [{name}]({url})
??? success "Solution{suffix}"
"""
for language in sorted(languages):
ext = language.split(".")[-1]
card += f"""
=== "{language_map[ext]}"
```{ext} linenums="1"
--8<-- "{uri}/{language}"
```
"""
text += card
print()
with open(f"docs/{folder}.md", "w", encoding="utf-8") as f:
f.write(text)
with open("docs/index.md", "w") as f:
text = """
<figure markdown>
{ width="128" }
</figure>
# LeetCode
## Summary by Difficulty
""".lstrip()
for folder, total in summary.items():
text += f"""
- [{folder.capitalize()} ^{total}^]({folder}.md)
"""
f.write(text)