-
Notifications
You must be signed in to change notification settings - Fork 250
/
main.py
206 lines (157 loc) · 6.16 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*- coding: utf-8 -*-
import argparse
import os
import shutil
from github import Github
MD_HEAD = """## ssh 的博客
大家好,我是 ssh,现在在字节跳动的 Web Infra 担任前端工程师,微信:**[sshsunlight](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/017d568dc1d14cd883cc3238350a39ec~tplv-k3u1fbpfcp-watermark.image)**,欢迎找我交个朋友。
我会在公众号「前端从进阶到入院」每日更新精心挑选的技术文章(标准就是我自己看了也会有收获),欢迎大家一起成长。
![qrcode_for_gh_d2b31290dd8b_258](https://user-images.githubusercontent.com/23615778/134800856-9a44fa9a-4f1b-4884-a0b6-b58c5f3331df.jpg)
"""
BACKUP_DIR = "src/pages"
ANCHOR_NUMBER = 5
TOP_ISSUES_LABELS = ["Top"]
TODO_ISSUES_LABELS = ["TODO"]
def get_me(user):
return user.get_user().login
def isMe(issue, me):
return issue.user.login == me
def format_time(time):
return str(time)[:10]
def login(token):
return Github(token)
def get_repo(user: Github, repo: str):
return user.get_repo(repo)
def parse_TODO(issue):
body = issue.body.splitlines()
todo_undone = [l for l in body if l.startswith("- [ ] ")]
todo_done = [l for l in body if l.startswith("- [x] ")]
# just add info all done
if not todo_undone:
return f"[{issue.title}]({issue.html_url}) all done", []
return (
f"[{issue.title}]({issue.html_url})--{len(todo_undone)} jobs to do--{len(todo_done)} jobs done",
todo_done + todo_undone,
)
def get_top_issues(repo):
return repo.get_issues(labels=TOP_ISSUES_LABELS)
def get_todo_issues(repo):
return repo.get_issues(labels=TODO_ISSUES_LABELS)
def get_repo_labels(repo):
return [l for l in repo.get_labels()]
def get_issues_from_label(repo, label):
return repo.get_issues(labels=(label,))
def add_issue_info(issue, md):
time = format_time(issue.created_at)
md.write(f"- [{issue.title}]({issue.html_url})--{time}\n")
def add_md_todo(repo, md, me):
todo_issues = list(get_todo_issues(repo))
if not TODO_ISSUES_LABELS or not todo_issues:
return
with open(md, "a+", encoding="utf-8") as md:
md.write("## TODO\n")
for issue in todo_issues:
if isMe(issue, me):
todo_title, todo_list = parse_TODO(issue)
md.write("TODO list from " + todo_title + "\n")
for t in todo_list:
md.write(t + "\n")
# new line
md.write("\n")
def add_md_top(repo, md, me):
top_issues = list(get_top_issues(repo))
if not TOP_ISSUES_LABELS or not top_issues:
return
with open(md, "a+", encoding="utf-8") as md:
md.write("## 置顶文章\n")
for issue in top_issues:
if isMe(issue, me):
add_issue_info(issue, md)
def add_md_recent(repo, md, me):
new_last_issues = repo.get_issues()[:10]
with open(md, "a+", encoding="utf-8") as md:
# one the issue that only one issue and delete (pyGitHub raise an exception)
try:
md.write("## 最近更新\n")
for issue in new_last_issues:
if isMe(issue, me):
add_issue_info(issue, md)
except:
return
def add_md_header(md):
with open(md, "w", encoding="utf-8") as md:
md.write(MD_HEAD)
def add_md_label(repo, md, me):
labels = get_repo_labels(repo)
with open(md, "a+", encoding="utf-8") as md:
for label in labels:
# we don't need add top label again
if label.name in TOP_ISSUES_LABELS:
continue
# we don't need add todo label again
if label.name in TODO_ISSUES_LABELS:
continue
issues = get_issues_from_label(repo, label)
if issues.totalCount:
md.write("## " + label.name + "\n")
issues = sorted(
issues, key=lambda x: x.created_at, reverse=True)
i = 0
for issue in issues:
if not issue:
continue
if isMe(issue, me):
if i == ANCHOR_NUMBER:
md.write("<details><summary>显示更多</summary>\n")
md.write("\n")
add_issue_info(issue, md)
i += 1
if i > ANCHOR_NUMBER:
md.write("</details>\n")
md.write("\n")
def get_to_generate_issues(repo, dir_name, me, issue_number=None):
to_generate_issues = [
i
for i in list(repo.get_issues())
if isMe(i, me)
]
if issue_number:
to_generate_issues.append(repo.get_issue(int(issue_number)))
return to_generate_issues
def main(token, repo_name="blogs", issue_number=None, dir_name=BACKUP_DIR):
user = login(token)
me = get_me(user)
repo = get_repo(user, repo_name)
add_md_header("README.md")
# add to readme one by one, change order here
for func in [add_md_top, add_md_recent, add_md_label, add_md_todo]:
func(repo, "README.md", me)
to_generate_issues = get_to_generate_issues(
repo, dir_name, me, issue_number)
# save md files to backup folder
for issue in to_generate_issues:
save_issue(issue, me, dir_name)
def save_issue(issue, me, dir_name=BACKUP_DIR):
md_dir = os.path.join(
dir_name, str(issue.id)
)
if not os.path.exists(md_dir):
os.makedirs(md_dir)
md_name = os.path.join(md_dir, "index.md")
with open(md_name, "w") as f:
f.write('---\n')
f.write(f"title: '{issue.title}'\n")
f.write(f"date: '{issue.created_at.strftime('%Y-%m-%d')}'\n")
f.write("spoiler: ''\n")
f.write('---\n\n')
f.write(issue.body)
print(os.path.abspath(BACKUP_DIR))
shutil.rmtree(os.path.abspath(BACKUP_DIR))
os.makedirs(BACKUP_DIR)
parser = argparse.ArgumentParser()
parser.add_argument("github_token", help="github_token")
parser.add_argument("repo_name", help="repo_name")
parser.add_argument("--issue_number", help="issue_number",
default=None, required=False)
options = parser.parse_args()
main(options.github_token, options.repo_name, options.issue_number)