def waitForAuth(self, conf): qrcodeManager = QrcodeManager(conf) try: qrcodeManager.Show(self.getQrcode()) x, y = 1, 1 while True: time.sleep(3) authStatus = self.getAuthStatus() if '二维码未失效' in authStatus: if x: INFO('等待二维码扫描及授权...') x = 0 elif '二维码认证中' in authStatus: if y: INFO('二维码已扫描,等待授权...') y = 0 elif '二维码已失效' in authStatus: WARN('二维码已失效, 重新获取二维码') qrcodeManager.Show(self.getQrcode()) x, y = 1, 1 elif '登录成功' in authStatus: INFO('已获授权') items = authStatus.split(',') self.nick = str(items[-1].split("'")[1]) self.qq = str(int(self.session.cookies['superuin'][1:])) self.urlPtwebqq = items[2].strip().strip("'") conf.qq = self.qq break else: CRITICAL('获取二维码扫描状态时出错, html="%s"', authStatus) sys.exit(1) finally: qrcodeManager.Destroy()
def runBot(argv): # argv -1, sub program call if sys.argv[-1] == '--subprocessCall': # Remove the last item sys.argv.pop() try: # Bot login, run # QQBot is defined below. bot = QQBot._bot bot.Login(argv) bot.Run() finally: # Finally, always run, no matter what # If bot has conf, bot conf storeQQ if hasattr(bot, 'conf'): bot.conf.StoreQQ() else: # Not sub program called conf = QConf() # QQ run in background if conf.daemon: conf.Daemonize() if sys.argv[0].endswith('py') or sys.argv[0].endswith('pyc'): args = [sys.executable] + sys.argv else: args = sys.argv args = args + ['--mailAuthCode', conf.mailAuthCode] args = args + ['--qq', conf.qq] args = args + ['--subprocessCall'] while True: p = subprocess.Popen(args) code = p.wait() if code == 0: INFO('QQBot 正常停止') sys.exit(code) elif code == RESTART: args[-2] = conf.LoadQQ() INFO('5 秒后重新启动 QQBot (自动登陆,qq=%s)', args[-2]) time.sleep(5) elif code == FRESH_RESTART: args[-2] = '' INFO('5 秒后重新启动 QQBot (手工登陆)') time.sleep(5) else: CRITICAL('QQBOT 异常停止(code=%s)', code) if conf.restartOnOffline: args[-2] = conf.LoadQQ() INFO('15秒后重新启动 QQBot (自动登陆,qq=%s)', args[-2]) time.sleep(15) else: sys.exit(code)
def pollForever(self): while True: try: result = self.poll() except RequestError: Put(sys.exit, POLL_ERROR) break except: CRITICAL('qsession.Poll 方法出错', exc_info=True) Put(sys.exit, POLL_ERROR) break else: Put(self.onPollComplete, *result)
def runBot(botCls, qq, user): if sys.argv[-1] == '--subprocessCall': isSubprocessCall = True sys.argv.pop() else: isSubprocessCall = False if isSubprocessCall: bot = botCls() try: bot.Login(qq, user) bot.Run() finally: if hasattr(bot, 'conf'): bot.conf.StoreQQ() else: conf = QConf(qq, user) if sys.argv[0].endswith('py') or sys.argv[0].endswith('pyc'): args = [sys.executable] + sys.argv else: args = sys.argv args = args + ['--mailAuthCode', conf.mailAuthCode] args = args + ['--qq', conf.qq] args = args + ['--subprocessCall'] while True: p = subprocess.Popen(args) pid = p.pid code = p.wait() qq = conf.LoadQQ(pid) if code == 0: INFO('QQBot 正常停止') sys.exit(code) elif code == RESTART: args[-2] = qq INFO('5 秒后重新启动 QQBot (自动登陆,qq=%s)', args[-2]) time.sleep(5) elif code == FRESH_RESTART: args[-2] = '' INFO('5 秒后重新启动 QQBot (手工登陆)') time.sleep(5) else: CRITICAL('QQBOT 异常停止(code=%s)', code) if conf.restartOnOffline: args[-2] = qq INFO('15秒后重新启动 QQBot (自动登陆,qq=%s)', args[-2]) time.sleep(15) else: sys.exit(code)
def runBot(botCls, qq, user): if sys.argv[-1] == '--subprocessCall': isSubprocessCall = True sys.argv.pop() else: isSubprocessCall = False if isSubprocessCall: bot = botCls() bot.Login(qq, user) bot.Run() else: conf = QConf(qq, user) if sys.argv[0].endswith('py') or sys.argv[0].endswith('pyc'): args = [sys.executable] + sys.argv else: args = sys.argv args = args + ['--mailAuthCode', conf.mailAuthCode] args = args + ['--qq', conf.qq] args = args + ['--subprocessCall'] while True: code = subprocess.call(args) if code == 0: INFO('QQBot 正常停止') sys.exit(code) elif code == RESTART: args[-2] = conf.LoadQQ() INFO('5 秒后重新启动 QQBot (自动登陆)') time.sleep(5) elif code == FRESH_RESTART: args[-2] = '' INFO('5 秒后重新启动 QQBot (手工登陆)') time.sleep(5) else: CRITICAL('QQBOT 异常停止(code=%s)', code) if conf.restartOnOffline: args[-2] = conf.LoadQQ() INFO('30秒后重新启动 QQBot (自动登陆)') time.sleep(30) else: sys.exit(code)
def waitForAuth(self, conf): qrcodeManager = QrcodeManager(conf) try: qrcodeManager.Show(self.getQrcode()) x, y = 1, 1 while True: time.sleep(3) authStatus = self.getAuthStatus() if '二维码未失效' in authStatus: if x: INFO('等待二维码扫描及授权...') x = 0 elif '二维码认证中' in authStatus: if y: INFO('二维码已扫描,等待授权...') y = 0 elif '二维码已失效' in authStatus: WARN('二维码已失效, 重新获取二维码') qrcodeManager.Show(self.getQrcode()) x, y = 1, 1 elif '登录成功' in authStatus: INFO('已获授权') items = authStatus.split(',') self.nick = str(items[-1].split("'")[1]) self.qq = str(int(self.session.cookies['superuin'][1:])) self.urlPtwebqq = items[2].strip().strip("'") t = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) self.dbbasename = '%s-%s-contact.db' % (t, self.qq) self.dbname = conf.absPath(self.dbbasename) conf.SetQQ(self.qq) break else: CRITICAL('获取二维码扫描状态时出错, html="%s"', authStatus) sys.exit(1) finally: qrcodeManager.Destroy()
def smartRequest(self, url, data=None, timeoutRetVal=None, resultExtractor=None, repeateOnDeny=2, **kw): nCE, nTO, nUE, nDE = 0, 0, 0, 0 while True: url = url.format(rand=repr(random.random())) html = '' errorInfo = '' try: resp = self.urlGet(url, data, **kw) except requests.ConnectionError as e: nCE += 1 errorInfo = '网络错误 %s' % e else: html = resp.content if not PY3 else resp.content.decode('utf8') if resp.status_code in (502, 504, 404): self.session.get( ('http://pinghot.qq.com/pingd?dm=w.qq.com.hot&' 'url=/&hottag=smartqq.im.polltimeout&hotx=9999&' 'hoty=9999&rand=%s') % random.randint(10000, 99999) ) if url == 'https://d1.web2.qq.com/channel/poll2': return {'errmsg': ''} nTO += 1 errorInfo = '超时' else: try: result = JsonLoads(html) except ValueError: nUE += 1 errorInfo = ' URL 地址错误' else: if resultExtractor: try: result = resultExtractor(result) except: DEBUG('', exc_info=True) result = None if result: return result else: if 'retcode' in result: retcode = result['retcode'] elif 'errCode' in result: retcode = result('errCode') elif 'ec' in result: retcode = result['ec'] else: retcode = -1 if retcode in (0, 100003, 100100): return result.get('result', result) nDE += 1 errorInfo = '请求被拒绝错误' n = nCE + nTO + nUE+ nDE # 出现网络错误、超时、 URL 地址错误可以多试几次 # 若网络没有问题但 retcode 有误,一般连续 3 次都出错就没必要再试了 if nCE < 5 and nTO < 20 and nUE < 5 and nDE <= repeateOnDeny: DEBUG('第%d次请求“%s”时出现 %s,html=%s', n, url.split('?', 1)[0], errorInfo, repr(html)) time.sleep(0.5) elif nTO == 20 and timeoutRetVal: # by @killerhack return timeoutRetVal else: CRITICAL('第%d次请求“%s”时出现 %s,html=%s', n, url.split('?', 1)[0], errorInfo, repr(html)) raise RequestError