def __init__(self): self.msgDefault = u'暂时没有新的消息' self.friendNameListDefault = ['wxrobottest'] self.msgQueue = [] self.crcTool = tool.CRCTool() # load conf.json with open("conf.json", 'r') as conf_file: conf = json.load(conf_file) self.ipaddr = conf['ipaddr'] self.port = conf['port'] self.dbno = conf['dbno'] # print('ipaddr = ', self.ipaddr) # print('port = ', self.port) # print('dbno = ', self.dbno) redis_tool = tool.RedisTool(self.ipaddr, self.port, self.dbno) self.conn = redis_tool.get_conn() # print('Is connected to Redis: ', self.conn.ping() ) if not self.conn.ping(): print('Is connected to Redis: ', self.conn.ping()) return self.index = self.conn.get('index') if not self.index: self.index = 0 self.index = int(self.index)
def reload(): # load conf.json with open("conf.json", 'r') as conf_file: conf = json.load(conf_file) ipaddr = conf['ipaddr'] port = conf['port'] dbno = conf['dbno'] redis_tool = tool.RedisTool(ipaddr, port, dbno) conn = redis_tool.get_conn() # print('Is connected to Redis: ', conn.ping() ) if not conn.ping(): print('Is connected to Redis: ', conn.ping()) return # clear conn.delete('friend_name_list') # load friends with open("friends.txt", 'r') as friends_file: for line in friends_file.readlines(): if not len(line) or line.startswith('#'): continue line = line.replace('\n', '') print('line = ', line) conn.rpush('friend_name_list', line) friend_name_list = conn.lrange('friend_name_list', 0, -1) # print('friend_name_list = ', friend_name_list) for friend_name in friend_name_list: # friend_name = unicode(friend_name, 'utf-8') # python 2 friend_name = friend_name.decode('utf-8').replace('\n', '') # python 3 print('friend_name = ', friend_name)
def info(): # load conf.json with open("conf.json",'r') as conf_file: conf = json.load(conf_file) ipaddr = conf['ipaddr'] port = conf['port'] dbno = conf['dbno'] redis_tool = tool.RedisTool(ipaddr, port, dbno) conn = redis_tool.get_conn() # print('Is connected to Redis: ', conn.ping() ) if not conn.ping(): print('Is connected to Redis: ', conn.ping() ) return # infos friend_name_list = conn.lrange('friend_name_list', 0, -1) # print('friend_name_list = ', friend_name_list) for friend_name in friend_name_list: # friend_name = unicode(friend_name, 'utf-8') # python 2 friend_name = friend_name.decode('utf-8').replace('\n', '') # python 3 print('friend_name = ', friend_name ) print('') index = conn.get('index') index = int(index) print('index = ', index) print('') message_pool_length = conn.llen('message_pool') print('message_pool_length = ', message_pool_length) message_pool = conn.lrange('message_pool', 0, -1) # print('message_pool = ', message_pool) for i, msg in enumerate(message_pool): if i >= 5: break # msg = unicode(msg, 'utf-8') # python 2 msg = msg.decode('utf-8') # python 3 print('i = %d, msg = %s' % (i, msg) ) print('') message_pool_crc_length = conn.scard('message_pool_crc') print('message_pool_crc_length = ', message_pool_crc_length) message_pool_crc = conn.smembers('message_pool_crc') # print('message_pool_crc = ', message_pool_crc) for i, msg_crc in enumerate(message_pool_crc): if i >= 5: break # msg_crc = unicode(msg_crc, 'utf-8') # python 2 msg_crc = msg_crc.decode('utf-8') # python 3 print('i = %d, msg_crc = %s' % (i, msg_crc) ) print('') last_hot_msg_update_timestamp = conn.get('last_hot_msg_update_timestamp') last_hot_msg_update_timestamp = int(last_hot_msg_update_timestamp) print('last_hot_msg_update_timestamp = ', last_hot_msg_update_timestamp) print('')
def reset(): # load conf.json with open("conf.json",'r') as conf_file: conf = json.load(conf_file) ipaddr = conf['ipaddr'] port = conf['port'] dbno = conf['dbno'] redis_tool = tool.RedisTool(ipaddr, port, dbno) conn = redis_tool.get_conn() # print('Is connected to Redis: ', conn.ping() ) if not conn.ping(): print('Is connected to Redis: ', conn.ping() ) return # clear conn.delete('friend_name_list') conn.delete('index') conn.delete('message_pool') conn.delete('message_pool_crc') conn.delete('last_hot_msg_update_timestamp') # load friends with open("friends.txt",'r') as friends_file: for line in friends_file.readlines(): if not len(line) or line.startswith('#'): continue print('line = ', line) conn.rpush('friend_name_list', line) friend_name_list = conn.lrange('friend_name_list', 0, -1) # print('friend_name_list = ', friend_name_list) for friend_name in friend_name_list: # friend_name = unicode(friend_name, 'utf-8') # python 2 friend_name = friend_name.decode('utf-8').replace('\n', '') # python 3 print('friend_name = ', friend_name ) # initial message index conn.set('index', 0) index = conn.get('index') index = int(index) print('index = ', index)
def __init__(self): self.crc_tool = tool.CRCTool() # load conf.json with open("conf.json", 'r') as conf_file: conf = json.load(conf_file) self.ipaddr = conf['ipaddr'] self.port = conf['port'] self.dbno = conf['dbno'] # print('ipaddr = ', self.ipaddr) # print('port = ', self.port) # print('dbno = ', self.dbno) redis_tool = tool.RedisTool(self.ipaddr, self.port, self.dbno) self.conn = redis_tool.get_conn() # print('Is connected to Redis: ', self.conn.ping() ) if not self.conn.ping(): print('Is connected to Redis: ', self.conn.ping()) return self.last_hot_msg_update_timestamp = self.conn.get( 'last_hot_msg_update_timestamp') if not self.last_hot_msg_update_timestamp: self.last_hot_msg_update_timestamp = 0 self.interval = 5 * 60 # 5 minutes print('self.last_hot_msg_update_timestamp = ', self.last_hot_msg_update_timestamp) print('self.interval = ', self.interval) self.length = self.conn.llen('message_pool') print('self.length = ', self.length) ua = UserAgent() self.headers = {'UserAgent': 'us.random'} self.page = 1 self.domain = 'http://www.qiushibaike.com/hot/page/' self.domain_text = 'https://www.qiushibaike.com/text/page/'
def setup(): # load conf.json with open("conf.json",'r') as conf_file: conf = json.load(conf_file) ipaddr = conf['ipaddr'] port = conf['port'] dbno = conf['dbno'] redis_tool = tool.RedisTool(ipaddr, port, dbno) conn = redis_tool.get_conn() # print('Is connected to Redis: ', conn.ping() ) if not conn.ping(): print('Is connected to Redis: ', conn.ping() ) return # clear conn.delete('friend_name_list') conn.delete('index') conn.delete('message_pool') conn.delete('message_pool_crc') # load friends with open("friends.txt",'r') as friends_file: for line in friends_file.readlines(): if not len(line) or line.startswith('#'): continue print('line = ', line) conn.rpush('friend_name_list', line) friend_name_list = conn.lrange('friend_name_list', 0, -1) # print('friend_name_list = ', friend_name_list) for friend_name in friend_name_list: # friend_name = unicode(friend_name, 'utf-8') # python 2 friend_name = friend_name.decode('utf-8').replace('\n', '') # python 3 print('friend_name = ', friend_name ) # initial message index conn.set('index', 0) index = conn.get('index') print('index = ', index) # initial message pool conn.rpush('message_pool', '你好') conn.rpush('message_pool', '我好') conn.rpush('message_pool', '他好') conn.rpush('message_pool', '大家好') conn.rpush('message_pool', '风暴英雄') conn.rpush('message_pool', '炉石传说') conn.rpush('message_pool', '魔兽世界') conn.rpush('message_pool', '暗黑破坏神') conn.rpush('message_pool', '星际争霸') conn.rpush('message_pool', '守望先锋') conn.rpush('message_pool', '英雄联盟') conn.rpush('message_pool', '绝地求生') conn.rpush('message_pool', '虎口脱险') conn.rpush('message_pool', '毕业生') conn.rpush('message_pool', '实习生') conn.rpush('message_pool', '歌舞青春') conn.rpush('message_pool', '放牛班的春天') message_pool = conn.lrange('message_pool', index, 20) print('message_pool = ', message_pool) for msg in message_pool: msg = msg.decode('utf-8') print('msg = ', msg)