def on_status(status): try: #重新组织推特数据 tweetinfo = tweet_event_deal.deal_tweet(status) #只有值得关注的推文才会推送处理,降低处理压力(能降一大截……) if tweetinfo['tweetNotable']: try: dealTweetsQueue.put(tweetinfo, timeout=15) if tweetinfo['user']['id'] != tweetinfo['Related_user']['id'] and \ tweetinfo['Related_notable'] and \ tweetinfo['Related_user']['id_str'] in push_list.spylist: #使被动推送能够不完整运行 rtweetinfo = tweetinfo.copy() rtweetinfo['trigger_user'] = tweetinfo['Related_user'][ 'id'] rtweetinfo['trigger_remote'] = True #监测重定向标识 dealTweetsQueue.put(rtweetinfo, timeout=15) except: s = traceback.format_exc(limit=5) msgSendToBot(logger, '推特监听处理队列溢出,请检查队列!') logger.error(s) if test != None: test.on_status(tweetinfo, status) except: s = traceback.format_exc(limit=5) logger.error(s)
async def group_increase_leave_me(session: NoticeSession): await asyncio.sleep(0.3) if session.event['sub_type'] == 'kick_me' or int( session.event['self_id']) == int(session.event['user_id']): push_list.delPushunitFromPushTo("group", int(session.event['group_id']), self_id=int(session.event['self_id'])) push_list.savePushList() msgSendToBot( logger, '已被移出或退出 ' + str(session.event['group_id']) + ' 群组,相关侦听已移除')
def run_tewwtlistener(): keepalive['tewwtlistener_alive'] = True new_loop = asyncio.new_event_loop() asyncio.set_event_loop(new_loop) try: tweetListener.Run() except: msgSendToBot(logger, '推特监听异常,将在10秒后尝试重启...') s = traceback.format_exc(limit=10) logger.error(s) time.sleep(10) reboot_tewwtlistener() return
def get_updata(trigger: bool = True): spylist = push_list.spylist interval = polling_interval for spy in spylist: app = ptwitterapps.getAllow('users_timeline') while app == None: logger.warning('触发轮询上限,建议增加轮询间隔') app = ptwitterapps.getAllow('users_timeline') time.sleep(1) res = app.users_timeline(user_id=int(spy)) if not res[0]: logger.error("错误,未搜索到" + str(spy) + "的时间线数据") run_info['errorlist'].join(res) if int(time.time()) - run_info['lasterror'] > 300: run_info['lasterror'] = int(time.time()) run_info['error'] = 0 run_info['error'] = run_info['error'] + 1 if run_info['error'] > 5: #短时间错误次数过高 msgSendToBot(logger, "错误,监测服务异常,请检测后手动启动") run_info['keepRun'] = False run_info['isRun'] = False break continue statuss = res[1] if not tweet_event_deal.hasUserTSInCache(spy): #初次监测不推送 logger.info("初次检测:" + spy) trigger = False for i in range(len(statuss) - 1, -1, -1): res = tweet_event_deal.tryGetTweet(statuss[i].id, user_id=spy) if not res: if trigger: on_status(statuss[i]) else: #组织推特数据 tweetinfo = tweet_event_deal.deal_tweet(statuss[i], trigger=trigger) #缓存处理 tweet_event_deal.bale_event(tweetinfo['type'], tweetinfo['trigger_user'], tweetinfo) itv = 0.5 + round(random.uniform(0, interval), 2) interval = interval - itv interval = max(0.5, interval) time.sleep(itv) time.sleep(interval)
def reboot_tewwtlistener(): keepalive['reboot_tewwtlistener'] = False if keepalive['reboot_tweetListener_cout'] > 5: msgSendToBot(logger, '重试次数过多,停止重试...') keepalive['tewwtlistener_alive'] = False return keepalive['reboot_tweetListener_cout'] = keepalive[ 'reboot_tweetListener_cout'] + 1 msgSendToBot( logger, '尝试重启推特流,' + '进行第 ' + str(keepalive['reboot_tweetListener_cout']) + ' 次尝试...') keepalive['tweetListener_threads'] = threading.Thread( group=None, target=run_tewwtlistener, name='tweetListener_threads', daemon=True) keepalive['tweetListener_threads'].start()
def on_status(self, status): try: #重新组织推特数据 tweetinfo = tweet_event_deal.deal_tweet(status) #只有值得关注的推文才会推送处理,降低处理压力(能降一大截……) if tweetinfo['tweetNotable']: try: dealTweetsQueue.put(tweetinfo, timeout=15) except: s = traceback.format_exc(limit=5) msgSendToBot(logger, '推特监听处理队列溢出,请检查队列!') logger.error(s) if test != None: test.on_status(tweetinfo, status) except: s = traceback.format_exc(limit=5) logger.error(s)
async def _(session: RequestSession): # 判断验证信息是否符合要求 #if session.event.comment == '暗号': # # 验证信息正确,同意入群 # await session.approve() # return ## 验证信息错误,拒绝入群 #await session.reject('请说暗号') self_id = session.self_id user_id = session.event.user_id nick = str(user_id) if "nickname" in session.event.sender: nick = session.event.sender['nickname'] comment = session.event.comment msg = '来自 ' + str(self_id) + ' 的入群请求:' + "\n" msg = msg + "用户:" + nick + '(' + str(user_id) + ')' + "\n" msg = msg + "验证信息:" + comment msgSendToBot(logger, msg)
def Run(): #五分钟内至多重启五次 run_info['isRun'] = True run_info['last_reboot'] = int(time.time()) run_info['error_cout'] = 0 while True: while not run_info['keepRun']: time.sleep(1) #保持运行标记为否时堵塞执行 if run_info['error_cout'] > 5: msgSendToBot(logger, '重试次数过多,停止重试...') while run_info['error_cout'] > 5: time.sleep(1) #错误次数达到上限时暂停运行 if run_info['error_cout'] > 0: msgSendToBot( logger, '尝试重启推特流,' + '进行第 ' + str(run_info['error_cout']) + ' 次尝试...') try: #创建监听流 run_info['apiStream'] = tweepy.Stream(auth=api.auth, listener=myStreamListener) run_info['apiStream'].filter(follow=push_list.spylist, is_async=False) except: s = traceback.format_exc(limit=10) logger.error(s) msgSendToBot(logger, '推特监听异常,将在10秒后尝试重启...') time.sleep(10) else: run_info['isRun'] = False msgSendToBot(logger, '推特流已停止...') logger.info('推特流正常停止') if run_info['error_cout'] > 0 and int( time.time()) - run_info['last_reboot'] > 300: run_info['last_reboot'] = int(time.time()) #两次重启间隔五分钟以上视为重启成功 run_info['error_cout'] = 0 #重置计数 run_info['error_cout'] = run_info['error_cout'] + 1
def on_disconnect(self, notice): msgSendToBot(logger, "推特流已断开链接") return False
def on_connect(self): msgSendToBot(logger, "推特流链接已就绪")
def on_error(self, status_code): #推特错误代码https://developer.twitter.com/en/docs/basics/response-codes logger.critical(status_code) msgSendToBot(logger, "推特流错误:" + str(status_code)) #返回False结束流 return False
def on_disconnect(self, notice): msgSendToBot(logger, "推特流已断开链接") self.isrun = False raise Exception
def on_connect(self): msgSendToBot(logger, "推特流链接已就绪") keepalive['reboot_tweetListener_cout'] = 0 self.isrun = True