def send_msg(self, message_type:str, send_id:int, message:str,bindCQID:int = config.default_bot_QQ): bot = nonebot.get_bot() try: if message_type == 'private': bot.sync.send_msg_rate_limited(self_id=bindCQID,user_id=send_id,message=message) #requests.post('http://127.0.0.1:5700/send_msg_rate_limited',data={'user_id': send_id_str, 'message': str}) elif message_type == 'group': bot.sync.send_msg_rate_limited(self_id=bindCQID,group_id=send_id,message=message) #requests.post('http://127.0.0.1:5700/send_msg_rate_limited',data={'group_id': send_id_str, 'message': str}) except: s = traceback.format_exc(limit=5) log_print(2,s) pass
def run_nonebot(new_loop): asyncio.set_event_loop(new_loop) try: nonebot.init(config) nonebot.load_plugins( path.join(path.dirname(__file__), 'plugins'), 'plugins' ) nonebot.run(host='127.0.0.1', port = 8190) except: log_print(1,'BOT状态异常') s = traceback.format_exc(limit=10) log_print(2,s)
def run_tewwtlistener(new_loop): asyncio.set_event_loop(new_loop) try: tweetListener.Run() except (ConnectionError,TimeoutError): log_print(0,"推特监听流连接失败!\n"+errorMsg) except: log_print(0,'推特监听异常') s = traceback.format_exc(limit=10) log_print(2,s)
def on_status(self, status): try: #重新组织推特数据 tweetinfo = self.deal_tweet(status) #酷Q输出 eventunit = self.bale_event(tweetinfo['type'],tweetinfo['user']['id'],tweetinfo) self.deal_event(eventunit) #控制台输出 if tweetinfo['type'] == 'none': str = '推特ID:' +tweetinfo['id_str']+','+tweetinfo['user']['screen_name']+"发送了推文:\n"+tweetinfo['text'] log_print(5,'(!)'+ str) elif tweetinfo['type'] == 'retweet': pass else: str = '标识 ' + self.type_to_str(tweetinfo['type']) +',推特ID:' +tweetinfo['id_str']+','+ tweetinfo['user']['screen_name']+'与'+tweetinfo['Related_user']['screen_name']+"的互动:\n"+tweetinfo['text'] if tweetinfo['user']['id_str'] in push_list.spylist: log_print(5,'(!)'+ str) else: log_print(4,str) except: str = traceback.format_exc(limit=5) log_print(2,str)
def seve_image(self, name, url, file_path='img'): #保存图片到磁盘文件夹 cache/file_path中,默认为当前脚本运行目录下的 cache/img 文件夹 base_path = 'cache/' #基准路径 try: if not os.path.exists(base_path + file_path): log_print(4,'文件夹' + base_path + file_path + '不存在,重新建立') #os.mkdir(file_path) os.makedirs(base_path + file_path) #获得图片后缀 file_suffix = os.path.splitext(url)[1] #拼接图片名(包含路径) filename = '{}{}{}{}'.format(base_path + file_path,os.sep,name,file_suffix) #下载图片,并保存到文件夹中 if not os.path.isfile(filename): urllib.request.urlretrieve(url,filename=filename) except IOError as e: s = traceback.format_exc(limit=5) log_print(2,'文件操作失败'+s) except Exception as e: s = traceback.format_exc(limit=5) log_print(2,s)
asyncio.set_event_loop(new_loop) try: nonebot.init(config) nonebot.load_plugins( path.join(path.dirname(__file__), 'plugins'), 'plugins' ) nonebot.run(host='127.0.0.1', port = 8190) except: log_print(1,'BOT状态异常') s = traceback.format_exc(limit=10) log_print(2,s) if __name__ == "__main__": #启动线程 log_print(4,'启动nonebot...') new_loop = asyncio.new_event_loop() Future = threads.submit(run_nonebot,new_loop) time.sleep(2) log_print(4,'启动推特流...') new_loop = asyncio.new_event_loop() Future = threads.submit(run_tewwtlistener,new_loop) loop = asyncio.get_event_loop() log_print(4,'维持主线程运行...') loop.run_forever()
def on_disconnect(self, notice): log_print(4,"推送流已断开链接") self.isrun = False
def on_connect(self): log_print(4,"推送流链接已就绪") self.isrun = True
def on_error(self, status_code): #推特错误代码https://developer.twitter.com/en/docs/basics/response-codes log_print(1,status_code) #返回False结束流 return False