-
Notifications
You must be signed in to change notification settings - Fork 18
/
build_readme.py
51 lines (42 loc) · 1.55 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
import feedparser
import httpx
import json
import pathlib
import re
import os
import datetime
blog_feed_url = "https://jasonkayzk.github.io/atom.xml"
wakatime_raw_url = "https://gist.githubusercontent.com/JasonkayZK/59ead22758ee823e48b558d3cff332f1/raw/"
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 fetch_code_time():
return httpx.get(wakatime_raw_url)
def fetch_blog_entries():
entries = feedparser.parse(blog_feed_url)["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(encoding='UTF-8').read()
code_time_text = "\n```text\n"+fetch_code_time().text+"\n```\n"
rewritten = replace_chunk(readme_contents, "code_time", code_time_text)
entries = fetch_blog_entries()[:5]
entries_md = "\n".join(
["* <a href='{url}' target='_blank'>{title}</a> - {published}".format(**entry) for entry in entries]
)
rewritten = replace_chunk(rewritten, "blog", entries_md)
readme.open("w", encoding='UTF-8').write(rewritten)