def _taipy_wx_login(self, account): partner = CiPartner.query.filter_by(account_id=account.id).first() api = TaipyWxClient( conf=OSLO_CONF, username=account.username, password=encryption.decrypt( crypt=account.password, key=current_app.config['ENCRYPTION_SECRET_KEY']), unitcode=partner.partner_code ) login_data = api.login() result = { "account_id": account.id, "agent": "", "channel": "", "cookies": "" } logger.debug(login_data) if login_data.get('status') and login_data.get('data'): cookies = base64.b64encode( json.dumps(login_data['data'])) CiAccount.query.filter_by(id=account.id).update( {'cookies': cookies}) mysql_db.session.commit() result['agent'] = login_data['data'].get('agent', '') result['channel'] = login_data['data'].get('channel', '') result['cookies'] = base64.b64encode( json.dumps(login_data['data']['cookies'])) return result
def _taipy_login(self, account): api = TaipyClient( conf=OSLO_CONF, username=account.username, password=encryption.decrypt( crypt=account.password, key=current_app.config['ENCRYPTION_SECRET_KEY']) ) status = api.login() cookies = base64.b64encode( json.dumps( requests.utils.dict_from_cookiejar( api.session.cookies))) logger.debug(api.cookies) cookies = base64.b64encode( json.dumps(api.cookies)) logger.debug(status) if status: CiAccount.query.filter_by(id=account.id).update({'cookies': cookies}) mysql_db.session.commit() result = { "account_id": account.id, "agent": "", "channel": "", "cookies": cookies } return result
def fields_decrypt(fields): for f, v in fields.items(): if isinstance(v, dict): # 递归解密 fields[f] = fields_decrypt(v) continue if f in current_app.config['ENCRYPTION_SECRET_FIELDS']: fields[f] = encryption.decrypt( crypt=v, key=current_app.config['ENCRYPTION_SECRET_KEY']) return fields
def run(self): while True: try: rows = mysql_db.session.query(CiAccount).filter(CiAccount.cookies != '') for account in rows: api = None if account.ins_company == 'taipy': cookies = json.loads(base64.b64decode(account.cookies)) api = TaipyClient( conf=OSLO_CONF, username=account.username, password=encryption.decrypt( crypt=account.password, key=current_app.config['ENCRYPTION_SECRET_KEY']), cookies=cookies ) if account.ins_company == 'taipy_wx': partner = CiPartner.query.filter_by(account_id=account.id).first() cookies = json.loads(base64.b64decode(account.cookies)).get('cookies', None) api = TaipyWxClient( conf=OSLO_CONF, username=account.username, password=encryption.decrypt( crypt=account.password, key=current_app.config['ENCRYPTION_SECRET_KEY']), cookies=cookies, unitcode=partner.partner_code ) if api and not api.is_login(): logger.info('clear cookies') mysql_db.session.query(CiAccount).filter_by(id=account.id).update( {'cookies': ''}) else: logger.info('keep success') except Exception as e: logger.exception(e) finally: mysql_db.session.commit() time.sleep(3)