Repository navigation
Expand file tree
/
Copy pathbuild_readme.py
More file actions
81 lines (70 loc) · 2.53 KB
/
Copy pathbuild_readme.py
File metadata and controls
81 lines (70 loc) · 2.53 KB
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
import feedparser
import httpx
import json
import pathlib
import re
import os
import datetime
import time
root=pathlib.Path(__file__).parent.resolve()
TOKEN = os.environ.get("GH_TOKEN", "")
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_douban():
entries = feedparser.parse("https://www.douban.com/feed/people/255018304/interests")["entries"]
while entries=="" or len(entries) == 0:
time.sleep(20)
entries = feedparser.parse("https://www.douban.com/feed/people/255018304/interests")["entries"]
return [
{
"title": item["title"],
"url": item["link"].split("#")[0],
"published": formatGMTime(item["published"])
}
for item in entries
]
def fetch_blog():
entries = feedparser.parse("http://blog.typoverflow.me/feed")["entries"]
res = []
for item in entries:
try:
if item["title"] == "南大印象" or item["title"] == "观影记录":
continue
summary = re.findall(r"^<blockquote><p>(.*?)</p></blockquote>", item["content"][0]["value"])
res.append(
{
"title": item["title"],
"url": item["link"],
"bio": summary[0] if len(summary) > 0 else "",
}
)
except Exception as e:
print(e)
print("continue")
continue
return res
if __name__ == "__main__":
readme = root / "README.md"
doubans = fetch_douban()[:5]
blogs = fetch_blog()[:3]
rewritten = readme.open().read()
doubans_md = "\n".join(
["* <a href='{url}' target='_blank'>{title}</a> - {published}".format(**item) for item in doubans]
)
rewritten = replace_chunk(rewritten, "douban", doubans_md)
blog_md = "\n".join(
["* <a href='{url}' target='_blank'>{title}</a><br><font color=\"gray\" size=2>{bio}</font>".format(**item) for item in blogs]
)
rewritten = replace_chunk(rewritten, "blog", blog_md)
readme.open("w").write(rewritten)