-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
50 lines (41 loc) · 1.34 KB
/
main.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
import feedparser
import pathlib
import datetime
import re
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 formatGMTime(timestamp):
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
dateStr = datetime.datetime.strptime(
timestamp, GMT_FORMAT) + datetime.timedelta(hours=8)
return dateStr.date()
def fetch_blog():
'''解析博客'''
items = feedparser.parse('https://blog.loongphy.com/rss.xml')['entries']
return [
{
"title": item.title,
"url": item.link,
"published": formatGMTime(item.published)
}
for item in items
]
if __name__ == '__main__':
root = pathlib.Path(__file__).parent.resolve()
readme = root / "README.md"
readme_contents = readme.open().read()
entries = fetch_blog()[:5]
entries_md = "\n".join(
["* <a href={url} target='_blank'>{title}</a> - {published}"
.format(**entry) for entry in entries]
)
rewritten = replace_chunk(readme_contents, "blog", entries_md)
readme.open("w").write(rewritten)