├── .gitignore ├── README.md ├── config.py └── run.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | *.pyc 3 | config.py 4 | state.pkl 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 环境 2 | 1. Python 2.7 3 | 2. Weibo SDK 4 | ``` 5 | pip install weibo 6 | ``` 7 | 8 | # 运行方法 9 | ``` 10 | python run.py 11 | ``` 12 | 13 | # 已实现功能 14 | 1. 获取最新的200条公开微博,从中挑选出含有指定关键字的微博进行转发 15 | 2. 自动回复@机器人的评论(目前仅支持回复指定内容) 16 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # weibo app name 4 | app_name = '' 5 | 6 | # weibo app config (required) 7 | app_key = '' 8 | app_secret = '' 9 | redirect_uri = '' 10 | 11 | # weibo account config (required) 12 | username = '' 13 | password = '' 14 | 15 | # keywords (required) 16 | keywords = '机器学习,大数据,深度学习,神经网络,云计算,区块链,数学模型,Hadoop,数理统计,人工智能,语音识别,图像识别,Python,数据科学,机器视觉' 17 | -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import sys 6 | import random 7 | from weibo import Client 8 | import cPickle as pickle 9 | import config 10 | 11 | reload(sys) 12 | sys.setdefaultencoding( "utf-8" ) 13 | 14 | class WeiboBot(): 15 | def __init__(self, config): 16 | self.app_key = config.app_key 17 | self.app_secret = config.app_secret 18 | self.redirect_uri = config.redirect_uri 19 | self.username = config.username 20 | self.password = config.password 21 | self.keywords = config.keywords.split(',') 22 | self.client = self.create_api_client() 23 | self.state_file = self.get_state_file() 24 | self.init_state() 25 | self.app = { 26 | 'repost_news': '小易读新闻', 27 | 'auto_reply': '小易陪聊' 28 | } 29 | 30 | def get_state_file(self): 31 | return os.path.join(os.getcwd(), 'state.pkl') 32 | 33 | def create_api_client(self): 34 | return Client( 35 | self.app_key, 36 | self.app_secret, 37 | self.redirect_uri, 38 | username = self.username, 39 | password = self.password 40 | ) 41 | 42 | def init_state(self): 43 | if not os.path.exists(self.state_file): 44 | # set initial state 45 | self.state = { 46 | 'replied_comments': [] 47 | } 48 | self.dump_state() 49 | else: 50 | self.state = self.load_state() 51 | 52 | def dump_state(self): 53 | f = file(self.state_file, 'wb') 54 | pickle.dump(self.state, f, True) 55 | 56 | def load_state(self): 57 | f = file(self.state_file, 'rb') 58 | return pickle.load(f) 59 | 60 | def read_weibo(self, id): 61 | ''' 62 | 根据微博ID获取单条微博内容 63 | ''' 64 | return self.client.get('statuses/show', id=id) 65 | 66 | def read_comments_with_mentions(self): 67 | ''' 68 | 获取提到当前用户的评论 69 | ''' 70 | return self.client.get('comments/mentions') 71 | 72 | def post_weibo(self, text): 73 | ''' 74 | 使用当前账号发送微博 75 | ''' 76 | self.client.post('statuses/update', status=text) 77 | 78 | def reply_comment(self, cid, id, reply): 79 | ''' 80 | 回复一条评论 81 | ''' 82 | self.client.post('comments/reply', cid=cid, id=id, comment=reply, comment_ori=0) 83 | 84 | def repost_weibo(self, id, status): 85 | ''' 86 | 转发一条微博 87 | ''' 88 | print '>>> 转发微博...' 89 | self.client.post('statuses/repost', id=id, status=status+'[哈欠]') 90 | 91 | def get_followers(self, screen_name): 92 | ''' 93 | 返回当前账号的关注用户列表 94 | ''' 95 | return self.client.get('friendships/friends/ids', screen_name=screen_name, count=500) 96 | 97 | def get_friends_timeline_weibo(self): 98 | ''' 99 | 获取当前登录用户及其所关注用户的最新微博的ID 100 | ''' 101 | return self.client.get('statuses/friends_timeline/ids', feature=1) 102 | 103 | def repost_by_keyword_from_public_timeline(self): 104 | ''' 105 | 返回含有关键字的最新的公开微博 106 | ''' 107 | print '>> 筛选最新的公开微博...' 108 | weibos = self.client.get('statuses/public_timeline', count=200) 109 | for weibo in weibos['statuses']: 110 | for keyword in self.keywords: 111 | if keyword.decode('utf-8') in weibo['text']: 112 | self.repost_weibo(weibo['id'], '#%s##%s#' % (self.app['repost_news'], keyword)) 113 | return 114 | 115 | def get_reply(self, comment): 116 | answer_pool = [ 117 | '恩,然后呢?', 118 | '哦,好吧...', 119 | '人生得意须尽欢啊呦喂!', 120 | '剽悍的人生不需要解释', 121 | '大家都是出来混的,都不容易!', 122 | '人生总有几次踩到大便的时候', 123 | '令人愉悦的忧伤...', 124 | 'Don\'t panic!', 125 | '我走来走去,为中国的命运苦苦思索...' 126 | ] 127 | reply = random.choice(answer_pool) 128 | reply += ' #%s#' % self.app['auto_reply'] 129 | return reply 130 | 131 | def reply_comments_with_mentions(self): 132 | ''' 133 | 回复最新的@我的评论 134 | ''' 135 | print '>> 回复最新的@我的评论...' 136 | comments = bot.read_comments_with_mentions()['comments'] 137 | for comment in comments: 138 | cid = comment['id'] 139 | id = comment['status']['id'] 140 | reply = self.get_reply(comment['text']) 141 | # 如果没有回复过,则进行回复并更新已回复列表 142 | if cid not in self.state['replied_comments']: 143 | try: 144 | print '原评论:%s' % comment['text'] 145 | bot.reply_comment(cid, id, reply) 146 | self.state['replied_comments'].append(cid) 147 | self.dump_state() 148 | except Exception as e: 149 | print e 150 | 151 | if __name__ == '__main__': 152 | bot = WeiboBot(config) 153 | 154 | # 转发最新关注的人的最新的一条微博 155 | #res = bot.get_friends_timeline_weibo() 156 | #latest_weibo = res['statuses'][0] 157 | #bot.repost_weibo(latest_weibo) 158 | 159 | # 转发最新的含有预设关键词的公开微博 160 | bot.repost_by_keyword_from_public_timeline() 161 | 162 | # 回复@我的评论 163 | bot.reply_comments_with_mentions() 164 | --------------------------------------------------------------------------------