-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
176 lines (115 loc) · 3.95 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from github import Github
from github.Issue import Issue
from github.Repository import Repository
import os
import time
import urllib.parse
import codecs
user: Github
blog: Repository
cur_time: str
def format_issue(issue: Issue):
return '- [%s](%s) \t\t\t %s \n\n' % (
issue.title, issue.html_url, sub(issue.created_at))
def sup(text: str):
return '<sup>%s</sup>' % text
def sub(text: str):
return text
def update_readme_md_file(contents):
print(contents)
with codecs.open('README.md', 'w', encoding='utf-8') as f:
f.writelines(contents)
f.flush()
f.close()
def login():
global user
user = Github(os.getenv('GITHUB_ACCESS_TOKEN'))
def get_blog():
global blog
blog = user.get_repo(os.getenv('GITHUB_USER') + '/LiuJiang-Blog')
def bundle_summary_section():
global blog
global cur_time
global user
total_label_count = blog.get_labels().totalCount
total_issue_count = blog.get_issues().totalCount
labels_html_url = 'https://github.com/%s/blog/labels' % user.get_user().login
issues_html_url = 'https://github.com/%s/blog/issues' % user.get_user().login
summary_section = '''
# GitHub Issues Blog
> 上次更新: %s
共 [%s](%s) 个标签, [%s](%s) 篇博文
''' % (cur_time, total_label_count, labels_html_url, total_issue_count, issues_html_url)
return summary_section
def bundle_pinned_issues_section():
global blog
pinned_label = blog.get_label(':+1:置顶')
pinned_issues = blog.get_issues(labels=(pinned_label,))
pinned_issues_section = '\n## 置顶 :thumbsup: \n'
for issue in pinned_issues:
pinned_issues_section += format_issue(issue)
return pinned_issues_section
def format_issue_with_labels(issue: Issue):
global user
labels = issue.get_labels()
labels_str = ''
if labels:
labels_str = '\n :label: \t' + sub('|')
for label in labels:
labels_str += sub('[%s](https://github.com/%s/blog/labels/%s)\t|\t' % (
label.name, user.get_user().login, urllib.parse.quote(label.name)))
return '- [%s](%s) \t\t\t %s \n\n' % (
issue.title, issue.html_url, sub(issue.created_at))
def bundle_new_created_section():
# global blog
# new_5_created_issues = blog.get_issues()[:5]
# new_created_section = '## 最新 \n'
# for issue in new_5_created_issues:
# new_created_section += format_issue_with_labels(issue)
# return new_created_section
global blog
all_created_issues = blog.get_issues()
all_created_section = '## 所有 \n'
for issue in all_created_issues:
all_created_section += format_issue_with_labels(issue)
return all_created_section
def bundle_list_by_labels_section():
global blog
global user
list_by_labels_section = '## 所有 \n'
all_labels = blog.get_labels()
for label in all_labels:
temp = ''
count = 0
issues_in_label = blog.get_issues(labels=(label,))
for issue in issues_in_label:
temp += format_issue(issue)
count += 1
list_by_labels_section += '%s' % (temp)
return list_by_labels_section
def execute():
global cur_time
# common
cur_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 1. login
login()
# 2. get blog
get_blog()
# 3. summary section
summary_section = bundle_summary_section()
print(summary_section)
# 4. new created section
new_created_section = bundle_new_created_section()
print(new_created_section)
# 5. list by labels section
# list_by_labels_section = bundle_list_by_labels_section()
# print(list_by_labels_section)
# 6. test
# contents = [summary_section, new_created_section, list_by_labels_section]
contents = [summary_section, new_created_section]
update_readme_md_file(contents)
print('README.md updated successfully!!!')
if __name__ == '__main__':
execute()