-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
233 lines (159 loc) · 5.88 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/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
from nasa_client import NasaClient
user: Github
ghiblog: Repository
cur_time: str
def format_issue(issue: Issue):
return '- [%s](%s) %s \t \n' % (
issue.title, issue.html_url, sup('%s :speech_balloon:' % issue.comments))
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_TOKEN')
user = Github(username, password)
def get_ghiblog():
global ghiblog
ghiblog = user.get_repo('%s/bobjoy.github.io' % user.get_user().login)
def bundle_summary_section():
global ghiblog
global cur_time
global user
total_label_count = ghiblog.get_labels().totalCount
total_issue_count = ghiblog.get_issues().totalCount
user_login = user.get_user().login
pic_of_the_day = NasaClient().get_picture_of_the_day()
summary_section = '''
# GitHub Issues Blog :tada::tada::tada:
<p align='center'>
<img src="https://badgen.net/circleci/github/Bobjoy/bobjoy.github.io"/>
<img src="https://badgen.net/badge/labels/%s"/>
<img src="https://badgen.net/badge/issues/%s"/>
<img src="https://badgen.net/badge/last-commit/%s"/>
<img src="https://badgen.net/github/forks/%s/bobjoy.github.io"/>
<img src="https://badgen.net/github/stars/%s/bobjoy.github.io"/>
<img src="https://badgen.net/github/watchers/%s/bobjoy.github.io"/>
<img src="https://badgen.net/github/release/%s/bobjoy.github.io"/>
</p>
<p align='center'>
<a href="https://github.com/Bobjoy/visitor-count-badge">
<img src="https://visitor-count-badge.herokuapp.com/total.svg?repo_id=Bobjoy.bobjoy.github.io"/>
</a>
<a href="https://github.com/Bobjoy/visitor-count-badge">
<img src="https://visitor-count-badge.herokuapp.com/today.svg?repo_id=Bobjoy.bobjoy.github.io"/>
</a>
</p>
## :artificial_satellite:今日图片
**%s**
> %s
<center>
<img src="%s" title="%s" alt="%s"/>
</center>
''' % (
total_label_count, total_issue_count, cur_time, user_login, user_login, user_login, user_login,
pic_of_the_day.title, pic_of_the_day.explanation, pic_of_the_day.url,
pic_of_the_day.title, pic_of_the_day.explanation)
return summary_section
def bundle_pinned_issues_section():
global ghiblog
pinned_label = ghiblog.get_label(':+1:置顶')
pinned_issues = ghiblog.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 \t' + sub('|')
for label in labels:
labels_str += sub('[%s](https://github.com/%s/bobjoy.github.io/labels/%s)\t|\t' % (
label.name, user.get_user().login, urllib.parse.quote(label.name)))
return '- [%s](%s) %s \t\t\t %s\n%s\n\n' % (
issue.title, issue.html_url, sup('%s :speech_balloon:' % issue.comments), issue.created_at, labels_str)
def bundle_new_created_section():
global ghiblog
new_5_created_issues = ghiblog.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 ghiblog
global user
list_by_labels_section = '## 分类 :card_file_box: \n'
all_labels = ghiblog.get_labels()
for label in all_labels:
temp = ''
# 这里的count是用来计算该label下有多少issue的, 按理说应该是取issues_in_label的totalCount, 但是不知道为什么取出来的一直都是
# 所有的issue数量, 之后再优化.
count = 0
issues_in_label = ghiblog.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 ghiblog
get_ghiblog()
# 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()