-
Notifications
You must be signed in to change notification settings - Fork 17
/
make_bookmark.py
64 lines (53 loc) · 1.87 KB
/
make_bookmark.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 argparse
import pendulum
from main import login
YEAR = pendulum.now().year
# Bookmark issue id
BOOKMARK_ISSUE_NUMBER = 21
BOOKMARK_FILE_NAME = f"bookmark_{YEAR}.md"
BOOKMARK_FILE_HEAD = (
f"# 我的 [{YEAR}](https://github.com/yihong0618/2021/issues/21) 的书签\n\n"
)
BOOKMARK_STAT_HEAD = "| Name | Link | Add | Update | Has_file | \n | ---- | ---- | ---- | ---- | ---- |\n"
BOOKMARK_STAT_TEMPLATE = "| {name} | {link} | {add} | {update} | {has_file} |\n"
def make_bookmark_str(name, link, add, update, has_file):
# format
return BOOKMARK_STAT_TEMPLATE.format(
name=name,
link=link,
add=add,
update=update,
has_file=has_file,
)
def main(github_token, repo_name):
u = login(github_token)
repo = u.get_repo(repo_name)
bookmark_issue = repo.get_issue(BOOKMARK_ISSUE_NUMBER)
comments = bookmark_issue.get_comments()
bookmark_str = BOOKMARK_STAT_HEAD
for c in comments:
has_file = False
comment_str_list = c.body.splitlines()
# drop the empty line
comment_str_list = [c for c in comment_str_list if c]
if len(comment_str_list) < 2:
continue
name, link = comment_str_list[0], comment_str_list[1]
if link.find(f"{repo_name}/files") != -1:
has_file = True
bookmark_str += make_bookmark_str(
f"[{name}]({link})",
c.html_url,
str(c.created_at)[:10],
str(c.updated_at)[:10],
has_file,
)
with open(BOOKMARK_FILE_NAME, "w+") as f:
f.write(BOOKMARK_FILE_HEAD)
f.write(bookmark_str)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("github_token", help="github_token")
parser.add_argument("repo_name", help="repo_name")
options = parser.parse_args()
main(options.github_token, options.repo_name)