Esempio n. 1
0
 def login(self, answer):
     """
     登录
     :param answer: 验证图片答案
     :return:
     """
     login_url = self.base_url + '/passport/web/login'
     data = {
         'username': self.username,
         'password': self.password,
         'appid': 'otn',
         'answer': answer
     }
     login_result = post(self.session, url=login_url, data=data)
     if login_result.status_code != 200:
         logger.error(f'很遗憾,登录失败了({login_result.status_code})...')
     try:
         res_json = login_result.json()
         if res_json.get('result_message') == '登录成功':
             self.session.cookies.set('uamtk', res_json.get('uamtk'))
             logger.info('*' * 10 + '恭喜你,登录成功啦!' + '*' * 10)
         else:
             logger.error('登录失败,请检查账号密码是否正确!')
     except json.decoder.JSONDecodeError as e:
         logger.error(
             f'登录接口没有返回json文件,检查cookies设置是否正确:{ self.session.cookies }')
         is_retry = input('是否重试(Y/N)?')
         if is_retry.lower() == 'y':
             self.login(answer)
         else:
             sys.exit(0)
Esempio n. 2
0
    def get_repeat_submit_token_key(self, res):
        """获取下单时需要的token、key

        :param res: 发起下单申请的结果(用于判断当前是否可以下单)
        :return: {'repeat_submit_token': '***', 'key_check_is_change': '***'}
        """
        try:
            res_json = res.json()
            if res_json['httpstatus'] == 200 and res_json['status'] == True:
                data = {'_json_att': ''}
                response = post(self.session,
                                url=self.base_url + '/confirmPassenger/initDc', data=data).text
                pattern = re.compile(
                    "globalRepeatSubmitToken = '(.*?)'.*?'key_check_isChange':'(.*?)'", re.DOTALL)
                result = pattern.findall(response)
                repeat_submit_token = result[0][0] if len(result) else ''
                key_check_is_change = result[0][1] if len(
                    result) and len(result[0]) > 1 else ''
                return {
                    'repeat_submit_token': repeat_submit_token,
                    'key_check_is_change': key_check_is_change
                }
            else:
                logger.error(','.join(res_json.get(
                    'messages', ['无法获取下单token,原因不明'])))
                # sys.exit(0)
        except json.JSONDecodeError as e:
            logger.error('提交申请下单请求失败!')
Esempio n. 3
0
    def check_order_info(self, passenger, repeat_submit_token):
        """检查订单信息,确认无误才可以下单

        :param passenger: 乘车人信息
        :param repeat_submit_token: token
        :return: bool值,True信息正确
        """
        data = {
            'cancel_flag': '2',
            'bed_level_order_num': '000000000000000000000000000000',
            'passengerTicketStr': self.generate_passenger_ticket(passenger),
            'oldPassengerStr': self.generate_old_passenger(passenger),
            'tour_flag': 'dc',
            'randCode': '',
            'whatsSelect': 1,
            '_json_att': '',
            'REPEAT_SUBMIT_TOKEN': repeat_submit_token
        }
        check_order_info_url = self.base_url + '/confirmPassenger/checkOrderInfo'
        check_order_info_res = post(
            self.session, url=check_order_info_url, data=data)
        try:
            res_json = check_order_info_res.json()
            if res_json['httpstatus'] == 200 \
                    and res_json['status'] == True \
                    and res_json['data'].get('submitStatus') == True:
                return True
            else:
                return False
        except json.decoder.JSONDecodeError as e:
            logger.error('当前不能检查订单信息,请检查是否有余票(ticket)以及令牌(token)。')
            return False
Esempio n. 4
0
    def save_image64(self):
        """保存验证图片"""
        img_url = self.base_url + \
            '/passport/captcha/captcha-image64?login_site=E&module=login&rand=sjrand'
        res = post(self.session, url=img_url).json()
        img = base64.b64decode(res.get('image'))

        with open(self.img_path, 'wb') as f:
            f.write(img)
Esempio n. 5
0
 def get_app_tk(self):
     """获取app的token"""
     data = {'appid': 'otn'}
     res = post(self.session,
                self.base_url + '/passport/web/auth/uamtk',
                data=data)
     if res.status_code == 200:
         res_json = res.json()
         if res_json.get('result_code') == 0:
             app_tk = res_json.get('newapptk')
             return app_tk
Esempio n. 6
0
 def get_passenger_info(self, repeat_submit_token):
     """获取乘车人信息"""
     get_passenger_url = self.base_url + '/confirmPassenger/getPassengerDTOs '
     data = {
         '_json_att': '',
         'REPEAT_SUBMIT_TOKEN': repeat_submit_token
     }
     res = post(self.session, url=get_passenger_url, data=data)
     try:
         passenger = res.json()
         normal_passengers = passenger['data']['normal_passengers'][0]
         return normal_passengers
     except BaseException:
         logger.error('获取乘车人信息失败!')
Esempio n. 7
0
 def auth_client(self, app_tk):
     """
     客户端验证token是否有效
     :param app_tk:
     :return:
     """
     data = {'tk': app_tk}
     res = post(self.session,
                self.base_url + '/otn/uamauthclient',
                data=data)
     if res.status_code == 200:
         res_json = res.json()
         if res_json.get('result_code') == 0:
             logger.info('*' * 10 + '恭喜你,客户端认证成功啦!' + '*' * 10)
             return True
     return False
Esempio n. 8
0
 def submit_order_request(self):
     """发起下单申请"""
     data = {
         'secretStr': unquote(self.ticket['secret_str']) if self.ticket else '',
         'train_date': datetime.datetime.strptime(self.ticket['train_date'], '%Y%m%d').strftime('%Y-%m-%d') \
             if self.ticket else '',
         'back_train_date': datetime.datetime.now().strftime('%Y-%m-%d'),
         'tour_flag': 'dc',
         'purpose_codes': 'ADULT',
         'query_from_station_name': self.ticket['from_station'] if self.ticket else '',
         'query_to_station_name': self.ticket['to_station'] if self.ticket else '',
         'undefined': ''
     }
     res = post(self.session, url=self.base_url +
                '/leftTicket/submitOrderRequest', data=data)
     return res
Esempio n. 9
0
    def confirm_single_for_queue(
            self, passenger, repeat_submit_token, key_check_is_change):
        """下单

        :param passenger: 乘车人信息
        :param repeat_submit_token: token
        :param key_check_is_change: key
        :return: bool值,True代表下单成功
        """
        data = {
            'passengerTicketStr': self.generate_passenger_ticket(passenger),
            'oldPassengerStr': self.generate_old_passenger(passenger),
            'randCode': '',
            'purpose_codes': '00',
            'key_check_isChange': key_check_is_change,
            'leftTicketStr': self.ticket['left_ticket'] if self.ticket else '',
            'train_location': self.ticket['train_location'] if self.ticket else '',
            'choose_seats': '',
            'seatDetailType': '000',
            'whatsSelect': '1',
            'roomType': '00',
            'dwAll': 'N',
            '_json_att': '',
            'REPEAT_SUBMIT_TOKEN': repeat_submit_token
        }
        confirm_single_for_queue_url = self.base_url + \
            '/confirmPassenger/confirmSingleForQueue'

        confirm_single_for_queue_res = post(self.session,
                                            url=confirm_single_for_queue_url, data=data)
        try:
            res_json = confirm_single_for_queue_res.json()
            if res_json['httpstatus'] == 200 \
                    and res_json['status'] == True \
                    and res_json['data'].get('submitStatus') == True:
                return True
            else:
                return False
        except json.decoder.JSONDecodeError as e:
            logger.error('当前不能下单,请检查是否有余票(ticket)、令牌(token)以及关键值(key)。')
            return False