def _submitOrderRequest(self, tourFlag='dc'): formData = { 'secretStr': TrainUtils.undecodeSecretStr(self.__ticket.secretStr), # 'secretStr': self.__ticket.secretStr, # 'secretStr': urllib.parse.unquote(self.__ticket.secretStr), 'train_date': Utils.formatDate(self.__ticket.startDate), # 2018-01-04 'back_train_date': time.strftime("%Y-%m-%d", time.localtime()), # query date:2017-12-31 'tour_flag': tourFlag, 'purpose_codes': self.__ticket.passengerType, 'query_from_station_name': self.__ticket.fromStation, 'query_to_station_name': self.__ticket.toStation, 'undefined': '', } order_request = copy.deepcopy(self._urlInfo['submitOrderRequest']) order_request['headers'][ 'Referer'] = self._urlInfo['submitOrderRequest']['headers'][ 'Referer'] + '?linktypeid=' + tourFlag # jsonRet = EasyHttp.send(self._urlInfo['submitOrderRequest'], data=formData) response = EasyHttp.post_custom(order_request, data=formData) # print('submitOrderRequest %s' % jsonRet) if response and response.status_code == 302: self._urlInfo['submitOrderRequest']['url'] = response.headers[ 'Location'] response = EasyHttp.post_custom( self._urlInfo['submitOrderRequest'], data=formData) if response and response.status_code == requests.codes.ok: return True, 'ok' return False, 'failed'
def verifyCodeAuto(self): try: response = EasyHttp.send(autoVerifyUrls['12305']) if response['result_code'] != '0': return None, False img_base64 = response['image'] # result = eval(response.split("(")[1].split(")")[0]).get("image") # img_base64 = result body = {'base64': img_base64} response = EasyHttp.post_custom(autoVerifyUrls['api'],data=json.dumps(body)).json() # response = requests.post(autoVerifyUrls['api']['url'],json=body,headers ={ # 'Content-Type': 'application/json', # }).json() if response['success'] != True: return None, False body = { 'check': response['data']['check'], 'img_buf': img_base64, 'logon': 1, 'type': 'D', '=':'' } response = EasyHttp.post_custom(autoVerifyUrls['img_url'],data=json.dumps(body)).json() content = str(response['res']) results = content.replace('(','').replace(')','') Log.d('识别坐标:%s'%results) except Exception as e: Log.w(e) return None, False return results, self._captchaAutoCheck(results)
def _getExtraInfo(self): def getRepeatSubmitToken(html): repeatSubmitToken = re.findall( r"var globalRepeatSubmitToken = '(.*)'", html)[0] # print('RepeatSubmitToken = %s' % repeatSubmitToken) return repeatSubmitToken formData = {'_json_att': ''} extra_info = copy.deepcopy(self._urlInfo['getExtraInfo']) # extra_info['headers']['Referer'] = self._urlInfo['getExtraInfo']['headers']['Referer']+ '?linktypeid='+self.__ticket.tourFlag response = EasyHttp.post_custom(extra_info, data=formData) if response and response.status_code == requests.codes.ok: html = response.text if html.find('系统忙,请稍后重试') != -1: Log.e('系统忙,请稍后重试...') return False else: html = None # html = EasyHttp.send(self._urlInfo['getExtraInfo']) if not Utils.check( html, 'failed to visit %s' % self._urlInfo['getExtraInfo']['url']): return False self.__ticket.repeatSubmitToken = getRepeatSubmitToken(html) def decodeTicketInfoForPassengerForm(html): ticketInfoForPassengerForm = re.findall( r'var ticketInfoForPassengerForm=(.*);', html)[0] return json.loads(ticketInfoForPassengerForm.replace("'", "\"")) # print('=======================') self.__ticket.ticketInfoForPassengerForm = decodeTicketInfoForPassengerForm( html) return True
def _loginNormal(self, userName, userPwd, autoCheck=2): if autoCheck == CAPTCHA_CHECK_METHOD_THREE: results, verify = Captcha().verifyCodeAuto() elif autoCheck == CAPTCHA_CHECK_METHOD_HAND: results, verify = Captcha().verifyCaptchaByHand() else: results, verify = Captcha().verifyCodeAutoByMyself() if not verify: return False, '验证码识别错误!' Log.v('验证码识别成功') payload = OrderedDict() payload['username'] = userName payload['password'] = userPwd payload['appid'] = 'otn' payload['answer'] = results response = EasyHttp.post_custom(self._urlInfo['login'], data=payload) def isLoginSuccess(responseJson): return 0 == responseJson['result_code'] if responseJson and 'result_code' in responseJson else False, \ responseJson[ 'result_message'] if responseJson and 'result_message' in responseJson else '登录失败' if response.status_code != requests.codes.ok: return False, "登录请求被强制重定向,准备重试..." result, msg = isLoginSuccess(response.json()) if not result: return False, msg self._userLogin() self._passportRedirect() result, msg, apptk = self._uamtk() if not Utils.check(result, msg): return False, 'uamtk failed' return self._uamauthclient(apptk)
def _check_user(self, tourFlag='dc'): formData = {'_json_att': ''} check_user = copy.deepcopy(self._urlInfo['checkUser']) check_user['headers']['Referer'] = self._urlInfo['checkUser'][ 'headers']['Referer'] + '?linktypeid=' + tourFlag jsonRet = EasyHttp.post_custom(check_user, data=formData) return True, jsonRet.text
def check_re_login(): try: response = EasyHttp.post_custom(loginUrls['normal']['conf']) resp = response.json() login_status = resp.get('data').get('is_login') if 'Y' != login_status: return False, '登录状态已过期' except Exception as e: return False, '查询登录返回状态异常' return True, 'ok'
def check_re_login(): response = EasyHttp.post_custom(loginUrls['normal']['conf']) if not response or not response.json(): return False resp = response.json() login_status = resp.get('data').get('is_login') Log.d('登录状态:%s' % login_status) if 'Y' != login_status: Log.d('登录状态已过期') return False return True
def _loginNormal(self, userName, userPwd, autoCheck=2, type=TYPE_LOGIN_NORMAL_WAY): status, msg = self._login_init() if not status: return status, msg self._uamtk_static() if autoCheck == CAPTCHA_CHECK_METHOD_THREE: results, verify = Captcha().verifyCodeAuto() elif autoCheck == CAPTCHA_CHECK_METHOD_HAND: results, verify = Captcha().verifyCaptchaByHand(type=type) elif autoCheck == CAPTCHA_CHECK_METHOD_AUTO: results, verify = Captcha().verifyCaptchaByAuto(type=type) else: results, verify = Captcha().verifyCodeAutoByMyself(type=type) if not verify: return False, '验证码识别错误!' Log.v('验证码识别成功') payload = OrderedDict() payload['username'] = userName payload['password'] = userPwd payload['appid'] = 'otn' payload['answer'] = results response = EasyHttp.post_custom(self._urlInfo['login'], data=payload) def isLoginSuccess(responseJson): return 0 == responseJson['result_code'] if responseJson and 'result_code' in responseJson else False, \ responseJson[ 'result_message'] if responseJson and 'result_message' in responseJson else '登录失败' result, msg = isLoginSuccess(response.json()) if not result: return False, msg self._userLogin() self._passportRedirect() result, msg, apptk = self._uamtk() if not Utils.check(result, msg): return False, 'uamtk failed' return self._uamauthclient(apptk)
def main(): #免费代理ip访问 GetFreeProxy.getAllProxy(THREAD_POOL_SIZE, THREAD_OR_PROCESS, IS_REFASH_IP_POOL) init_ip_pool() EasyHttp.load_cookies(COOKIE_SAVE_ADDRESS) cookies = {c.name: c.value for c in EasyHttp.get_session().cookies} RAIL_EXPIRATION = cookies.get('RAIL_EXPIRATION') #(int(RAIL_EXPIRATION)-172800000) < int(time.time()*1000) if RAIL_EXPIRATION and int(RAIL_EXPIRATION) < int(time.time() * 1000): Log.d('cookie登录已过期,重新请求') status, login = do_login() if not status: return else: if not ('uamtk' in cookies and 'RAIL_DEVICEID' in cookies): status, login = do_login() if not status: return else: response = EasyHttp.post_custom(loginUrls['normal']['conf']) if not response or not response.json(): Log.d('登录状态检查失败,重新请求') status, login = do_login() if not status: return resp = response.json() login_status = resp.get('data').get('is_login') Log.d('登录状态:%s' % login_status) if 'Y' != login_status: Log.d('登录状态已过期,重新请求') status, login = do_login() if not status: return login = Login() login._urlInfo = loginUrls['normal'] Log.v('已登录状态,开始寻找小票票') seatTypesCode = SEAT_TYPE_CODE if SEAT_TYPE_CODE else [ SEAT_TYPE[key] for key in SEAT_TYPE.keys() ] passengerTypeCode = PASSENGER_TYPE_CODE if PASSENGER_TYPE_CODE else '1' count = 0 while True: # 死循环一直查票,直到下单成功 try: nowTime, status = deadline.do_fix_time() if status: Log.v('当前时间:%s 处于23点到6点之间,12306处于维护状态,暂不处理下单业务' % nowTime) continue count += 1 Log.v('第%d次访问12306网站' % count) print('-' * 40) ticketDetails = Query.loopQuery( TRAIN_DATE, FROM_STATION, TO_STATION, TrainUtils.passengerType2Desc(passengerTypeCode), TRAINS_NO, seatTypesCode, PASSENGERS_ID, POLICY_BILL, QUERY_TICKET_REFERSH_INTERVAL) Log.v('已为您查询到可用余票:%s' % ticketDetails) ticketDetails.passengersId = PASSENGERS_ID ticketDetails.ticketTypeCodes = passengerTypeCode ticketDetails.tourFlag = TOUR_FLAG if TOUR_FLAG else 'dc' submit = Submit(ticketDetails) if submit.submit(CHOOSE_SEATS): status, contents = submit.showSubmitInfoPretty() if status: flag = send_mail(mail_user, mailto_list, '12306订票结果通知', mail_host, mail_user, mail_pass, contents) if flag: Log.v("邮件发送成功!") else: Log.v("邮件发送失败!") sms_id = send_sms(ACCOUNT_SID, AUTO_TOKEN, FROM_NUM, TO_NUM, '小机机已经成功拿到小票票,请主人记得在30分钟内完成支付!!!') if sms_id: Log.v("短信提醒发送成功!") else: Log.v("短信提醒发送失败!") break time.sleep(1) except Exception as e: Log.w(e) login.loginOut() Log.d('注销登录成功')