-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
195 lines (133 loc) · 4.9 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
#!/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
my_notes: Repository
cur_time: str
def format_issue(issue: Issue):
return '- [%s](%s) %s \t :alarm_clock:%s \n' % (
issue.title, issue.html_url, sup('%s :speech_balloon:' % issue.comments), sub(issue.created_at))
def sup(text: str):
return '<sup>%s</sup>' % text
def sub(text: str):
return '<sub>%s</sub>' % text
def update_readme_md_file(contents):
with codecs.open('README.md', 'w', encoding='utf-8') as f:
f.writelines(contents)
f.flush()
f.close()
def login():
global user
username = os.environ.get('GITHUB_LOGIN')
password = os.environ.get('GITHUB_PASSWORD')
user = Github(username, password)
def get_my_notes():
global my_notes
my_notes = user.get_repo('%s/my-notes' % user.get_user().login)
def bundle_summary_section():
global my_notes
global cur_time
global user
total_label_count = my_notes.get_labels().totalCount
total_issue_count = my_notes.get_issues().totalCount
labels_html_url = 'https://github.com/%s/my-notes/labels' % user.get_user().login
issues_html_url = 'https://github.com/%s/my-notes/issues' % user.get_user().login
summary_section = '''
# GitHub Issues Blog :tada::tada::tada:
> :alarm_clock: 上次更新: %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 my_notes
pinned_label = my_notes.get_label(':+1:置顶')
pinned_issues = my_notes.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/my-notes/labels/%s)\t|\t' % (
label.name, user.get_user().login, urllib.parse.quote(label.name)))
return '- [%s](%s) %s \t\t\t :alarm_clock:%s %s\n\n' % (
issue.title, issue.html_url, sup('%s :speech_balloon:' % issue.comments), sub(issue.created_at), labels_str)
def bundle_new_created_section():
global my_notes
new_5_created_issues = my_notes.get_issues()[:5]
new_created_section = '## 最新 :new: \n'
for issue in new_5_created_issues:
new_created_section += format_issue_with_labels(issue)
return new_created_section
def bundle_list_by_labels_section():
global my_notes
global user
list_by_labels_section = '## 分类 :card_file_box: \n'
all_labels = my_notes.get_labels()
for label in all_labels:
temp = ''
# 这里的count是用来计算该label下有多少issue的, 按理说应该是取issues_in_label的totalCount, 但是不知道为什么取出来的一直都是
# 所有的issue数量, 之后再优化.
count = 0
issues_in_label = my_notes.get_issues(labels=(label,))
for issue in issues_in_label:
temp += format_issue(issue)
count += 1
list_by_labels_section += '''
<details>
<summary>%s\t<sup>%s:newspaper:</sup></summary>
%s
</details>
''' % (label.name, count, temp)
return list_by_labels_section
def bundle_about_me_section():
global user
about_me_section = '''
## 关于:boy:
[<img alt="%s" src="%s" width="233"/>](%s)
**%s**
:round_pushpin: %s
:black_flag: %s
''' % (user.get_user().name, user.get_user().avatar_url, user.get_user().html_url, user.get_user().name,
user.get_user().location,
user.get_user().bio)
return about_me_section
def execute():
global cur_time
# common
cur_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 1. login
login()
# 2. get my_notes
get_my_notes()
# 3. summary section
summary_section = bundle_summary_section()
print(summary_section)
# 4. pinned issues section
pinned_issues_section = bundle_pinned_issues_section()
print(pinned_issues_section)
# 5. new created section
new_created_section = bundle_new_created_section()
print(new_created_section)
# 6. list by labels section
list_by_labels_section = bundle_list_by_labels_section()
print(list_by_labels_section)
# 7. about me section
about_me_section = bundle_about_me_section()
print(about_me_section)
contents = [summary_section, pinned_issues_section, new_created_section, list_by_labels_section, about_me_section]
update_readme_md_file(contents)
print('README.md updated successfully!!!')
if __name__ == '__main__':
execute()