-
Notifications
You must be signed in to change notification settings - Fork 3
/
build_readme.py
64 lines (50 loc) · 1.83 KB
/
build_readme.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
import feedparser
import httpx
import pathlib
import re
import os
import requests
import git
root = pathlib.Path(__file__).parent.resolve()
def replace_chunk(content, marker, chunk, inline=False):
r = re.compile(
r"<!\-\- {} starts \-\->.*<!\-\- {} ends \-\->".format(marker, marker),
re.DOTALL,
)
if not inline:
chunk = "\n{}\n".format(chunk)
chunk = "<!-- {} starts -->{}<!-- {} ends -->".format(marker, chunk, marker)
return r.sub(chunk, content)
def get_tils():
til_readme = "https://raw.githubusercontent.com/milaan9/TIL/master/README.md"
r = requests.get(til_readme)
page = requests.get(til_readme)
all_text = page.text
search_re = re.findall( r'(\*+).(\[.*?\])(\(.*?\)).?-(.+)', all_text, re.M|re.I)
dt_til = sorted(search_re, key=lambda search_re: search_re[3], reverse=True)[:3]
til_md = ""
for i in dt_til:
til_md += "\n" + i[0] + ' ' + i[1] + i[2]
return til_md
def fetch_blog_entries():
entries = feedparser.parse("https://milaan9.github.io/blog/feed.xml")["entries"]
return [
{
"title": entry["title"],
"url": entry["link"].split("#")[0],
"published": entry["published"].split("T")[0],
}
for entry in entries
]
if __name__ == "__main__":
readme = root / "README.md"
readme_contents = readme.open().read()
entries = fetch_blog_entries()[:3]
entries_md = "\n".join(
# ["* [{title}]({url}) - {published}".format(**entry) for entry in entries]
["* [{title}]({url})".format(**entry) for entry in entries]
)
rewritten = replace_chunk(readme_contents, "blog", entries_md)
til_readme_contents = get_tils()
rewritten = replace_chunk(rewritten, "tils", til_readme_contents)
readme.open("w").write(rewritten)