Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dec3ae3

Browse files
committed
更新:github_bot,根据star数量展示
1 parent 595a261 commit dec3ae3

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

script/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
1. 安装依赖:`pip install -r requirements.txt`
1010
2. 配置脚本中相关参数:
1111
```
12-
API = {
13-
'events': 'https://api.github.com/users/你的github用户名/received_events/public'
14-
}
15-
1612
# github帐号
1713
ACCOUNT = {
1814
'username': '',
@@ -36,7 +32,8 @@
3632
**最后:**`python github_bot.py`
3733

3834
## 开发日志
39-
35+
#### 2016-9-24
36+
实现根据star数量,从高到低展示。
4037
#### 2016-9-5
4138
实现请求Github api获取关注的用户star的项目、过滤内容、定时发邮件
4239

script/github_bot.py

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import logging
1010
import smtplib
1111
import datetime
12+
from operator import itemgetter
1213
from email.mime.text import MIMEText
1314
from email.header import Header
1415

@@ -21,17 +22,16 @@
2122
format='%(name)s %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
2223
)
2324
logger = logging.getLogger('Bot') # 设置log名称
24-
25-
API = {
26-
'events': 'https://api.github.com/users/你的github用户名/received_events/public'
27-
}
28-
2925
# github帐号
3026
ACCOUNT = {
3127
'username': '',
3228
'password': ''
3329
}
3430

31+
API = {
32+
'events': 'https://api.github.com/users/{username}/received_events/public'.format(username=ACCOUNT['username'])
33+
}
34+
3535
# 发送邮件,邮箱的信息
3636
MAIL = {
3737
'mail': '', # 发送邮件的邮箱地址
@@ -59,7 +59,7 @@
5959
<th>starred日期</th>
6060
<th>项目star数量</th>
6161
</tr>
62-
{starred_info}
62+
{project_info_string}
6363
</table>
6464
"""
6565

@@ -111,42 +111,53 @@ def analyze(json_data):
111111
return result_data
112112

113113

114-
def make_content():
114+
def get_stars(data):
115115
"""
116-
生成发布邮件的内容
116+
获取stars数量
117117
"""
118-
json_data = get_data()
119-
data = analyze(json_data)
120-
content = []
121-
118+
project_info_list = []
122119
for fi_data in data:
123-
user = fi_data['actor']['login']
124-
user_url = 'https://github.com/' + user
125-
avatar_url = fi_data['actor']['avatar_url']
126-
repo_name = fi_data['repo']['name']
127-
repo_url = 'https://github.com/' + repo_name
128-
date_time = fi_data['date_time']
120+
project_info = dict()
121+
project_info['user'] = fi_data['actor']['login']
122+
project_info['user_url'] = 'https://github.com/' + project_info['user']
123+
project_info['avatar_url'] = fi_data['actor']['avatar_url']
124+
project_info['repo_name'] = fi_data['repo']['name']
125+
project_info['repo_url'] = 'https://github.com/' + project_info['repo_name']
126+
project_info['date_time'] = fi_data['date_time']
129127
try:
130128
repo_stars = requests.get(fi_data['repo']['url'], timeout=2).json()
131129
if repo_stars:
132-
repo_stars = repo_stars['stargazers_count']
130+
project_info['repo_stars'] = int(repo_stars['stargazers_count'])
133131
else:
134-
repo_stars = '未知数'
132+
project_info['repo_stars'] = -1
135133
except Exception as e:
136-
repo_stars = '未知数'
137-
logger.warning(u'获取:{} 项目星数失败——{}'.format(repo_name, e))
138-
starred_info = """<tr>
139-
<td><img src={avatar_url} width=32px></img></td>
140-
<td><a href={user_url}>{user}</a></td>
141-
<td><a href={repo_url}>{repo_name}</a></td>
142-
<td>{date_time}</td>
143-
<td>{repo_stars}</td>
144-
</tr>
145-
""".format(user=user, repo_name=repo_name,
146-
repo_url=repo_url, user_url=user_url,
147-
avatar_url=avatar_url, repo_stars=repo_stars,
148-
date_time=date_time)
149-
content.append(starred_info)
134+
project_info['repo_stars'] = -1
135+
logger.warning(u'获取:{} 项目星数失败——{}'.format(
136+
project_info['repo_name'], e))
137+
finally:
138+
project_info_list.append(project_info)
139+
project_info_list = sorted(project_info_list, key=itemgetter('repo_stars'), reverse=True)
140+
return project_info_list
141+
142+
143+
def make_content():
144+
"""
145+
生成发布邮件的内容
146+
"""
147+
json_data = get_data()
148+
data = analyze(json_data)
149+
content = []
150+
project_info_list = get_stars(data)
151+
for project_info in project_info_list:
152+
project_info_string = """<tr>
153+
<td><img src={avatar_url} width=32px></img></td>
154+
<td><a href={user_url}>{user}</a></td>
155+
<td><a href={repo_url}>{repo_name}</a></td>
156+
<td>{date_time}</td>
157+
<td>{repo_stars}</td>
158+
</tr>
159+
""".format(**project_info)
160+
content.append(project_info_string)
150161
return content
151162

152163

@@ -159,7 +170,7 @@ def send_email(receivers, email_content):
159170

160171
# 三个参数:第一个为文本内容,第二个 html 设置文本格式,第三个 utf-8 设置编码
161172
message = MIMEText(
162-
CONTENT_FORMAT.format(starred_info=''.join(email_content)),
173+
CONTENT_FORMAT.format(project_info_string=''.join(email_content)),
163174
'html', 'utf-8'
164175
)
165176
message['From'] = Header(u'Github机器人', 'utf-8')

0 commit comments

Comments
 (0)