-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrun.py
executable file
·163 lines (141 loc) · 5.04 KB
/
run.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import random
from weibo import Client
import cPickle as pickle
import config
reload(sys)
sys.setdefaultencoding( "utf-8" )
class WeiboBot():
def __init__(self, config):
self.app_key = config.app_key
self.app_secret = config.app_secret
self.redirect_uri = config.redirect_uri
self.username = config.username
self.password = config.password
self.keywords = config.keywords.split(',')
self.client = self.create_api_client()
self.state_file = self.get_state_file()
self.init_state()
self.app = {
'repost_news': '小易读新闻',
'auto_reply': '小易陪聊'
}
def get_state_file(self):
return os.path.join(os.getcwd(), 'state.pkl')
def create_api_client(self):
return Client(
self.app_key,
self.app_secret,
self.redirect_uri,
username = self.username,
password = self.password
)
def init_state(self):
if not os.path.exists(self.state_file):
# set initial state
self.state = {
'replied_comments': []
}
self.dump_state()
else:
self.state = self.load_state()
def dump_state(self):
f = file(self.state_file, 'wb')
pickle.dump(self.state, f, True)
def load_state(self):
f = file(self.state_file, 'rb')
return pickle.load(f)
def read_weibo(self, id):
'''
根据微博ID获取单条微博内容
'''
return self.client.get('statuses/show', id=id)
def read_comments_with_mentions(self):
'''
获取提到当前用户的评论
'''
return self.client.get('comments/mentions')
def post_weibo(self, text):
'''
使用当前账号发送微博
'''
self.client.post('statuses/update', status=text)
def reply_comment(self, cid, id, reply):
'''
回复一条评论
'''
self.client.post('comments/reply', cid=cid, id=id, comment=reply, comment_ori=0)
def repost_weibo(self, id, status):
'''
转发一条微博
'''
print '>>> 转发微博...'
self.client.post('statuses/repost', id=id, status=status+'[哈欠]')
def get_followers(self, screen_name):
'''
返回当前账号的关注用户列表
'''
return self.client.get('friendships/friends/ids', screen_name=screen_name, count=500)
def get_friends_timeline_weibo(self):
'''
获取当前登录用户及其所关注用户的最新微博的ID
'''
return self.client.get('statuses/friends_timeline/ids', feature=1)
def repost_by_keyword_from_public_timeline(self):
'''
返回含有关键字的最新的公开微博
'''
print '>> 筛选最新的公开微博...'
weibos = self.client.get('statuses/public_timeline', count=200)
for weibo in weibos['statuses']:
for keyword in self.keywords:
if keyword.decode('utf-8') in weibo['text']:
self.repost_weibo(weibo['id'], '#%s##%s#' % (self.app['repost_news'], keyword))
return
def get_reply(self, comment):
answer_pool = [
'恩,然后呢?',
'哦,好吧...',
'人生得意须尽欢啊呦喂!',
'剽悍的人生不需要解释',
'大家都是出来混的,都不容易!',
'人生总有几次踩到大便的时候',
'令人愉悦的忧伤...',
'Don\'t panic!',
'我走来走去,为中国的命运苦苦思索...'
]
reply = random.choice(answer_pool)
reply += ' #%s#' % self.app['auto_reply']
return reply
def reply_comments_with_mentions(self):
'''
回复最新的@我的评论
'''
print '>> 回复最新的@我的评论...'
comments = bot.read_comments_with_mentions()['comments']
for comment in comments:
cid = comment['id']
id = comment['status']['id']
reply = self.get_reply(comment['text'])
# 如果没有回复过,则进行回复并更新已回复列表
if cid not in self.state['replied_comments']:
try:
print '原评论:%s' % comment['text']
bot.reply_comment(cid, id, reply)
self.state['replied_comments'].append(cid)
self.dump_state()
except Exception as e:
print e
if __name__ == '__main__':
bot = WeiboBot(config)
# 转发最新关注的人的最新的一条微博
#res = bot.get_friends_timeline_weibo()
#latest_weibo = res['statuses'][0]
#bot.repost_weibo(latest_weibo)
# 转发最新的含有预设关键词的公开微博
bot.repost_by_keyword_from_public_timeline()
# 回复@我的评论
bot.reply_comments_with_mentions()