Exemplo n.º 1
0
def task_img(param_dict):

    r = RedisUtils()

    username = param_dict['username']

    s = requests.session()
    header = headers

    # 获取验证图片
    url = "https://kyfw.12306.cn/otn/login/init"
    s.get(url, headers=header, verify=False, timeout=TIME_OUT)
    url = "https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&%.16f" % random.random(
    )
    response = s.get(url, headers=header, verify=False, timeout=TIME_OUT)

    # 把验证码图片保存到本地
    # img_file_path_str = "./static/" + username + ".jpeg"
    # img_file_path_str = "./static/" + "test" + ".jpeg"
    # with open(img_file_path_str, "wb") as f: f.write(response.content)

    # 获取验证图片的base64编码
    img_base64_str = base64.b64encode(response.content)
    # 获取session.cookies
    """
        json.dumps(session.cookies.get_dict())) # 保存
        session.cookies.update(json.loads(f.read())) # 读取
    """
    cookies = json.dumps(s.cookies.get_dict())

    # 构造并返回redis
    result_dict = {
        "status": '1',
        "desc": "获取验证图片成功",
        "result": {
            "img_base64_str": img_base64_str
        },
        "cookies": cookies,
        "username": username,
        "headers": header,
    }
    r.setSessionDict(username, result_dict)  # True
    # print 111
    return
Exemplo n.º 2
0
def task_price(param_dict):
    seat_types = '123469PMOF'
    r = RedisUtils()
    s = requests.session()

    username = param_dict['username']
    train_date = param_dict['train_date']
    train_no = param_dict['train_no']
    from_station_no = param_dict['from_station_no']
    to_station_no = param_dict['to_station_no']

    # 判断登陆过期
    redis_dict = r.getSessionDict(username)
    if redis_dict == {} or redis_dict.get('cookies', '') == '':
        result_dict = {'status': '3', 'desc': '登陆过期', 'result': '请重新登陆'}
        r.setSessionDict(username, result_dict)
        return

    headers = json.loads(redis_dict['headers'].replace("'", '"'))
    cookies = redis_dict['cookies']
    s.cookies.update(json.loads(cookies))  # 更新

    url = 'https://kyfw.12306.cn/otn/leftTicket/queryTicketPriceFL?train_no=%s&from_station_no=%s&to_station_no=%s&seat_types=%s&train_date=%s' % (train_no, from_station_no, to_station_no, seat_types, train_date)
    _ = s.get(url, headers=headers, verify=False, timeout=TIME_OUT)

    url = 'https://kyfw.12306.cn/otn/leftTicket/queryTicketPrice?train_no=%s&from_station_no=%s&to_station_no=%s&seat_types=%s&train_date=%s' % (train_no, from_station_no, to_station_no, seat_types, train_date)
    response = s.get(url, headers=headers, verify=False, timeout=TIME_OUT)
    # print '='*30
    # print response.content
    if json.loads(response.content)['status'] != True:
        r.setSessionDict(username, {'status': '2', 'desc': '票价查询失败', 'result': response.content})
        return
    # 解析列车票价信息
    price_dict = parsePrice(json.loads(response.content)['data'])
    # print price_dict
    cookies = json.dumps(s.cookies.get_dict())
    # 构造并返回redis
    result_dict = {
        "status": '1',
        "desc": "票价查询成功",
        "result": json.dumps(price_dict),
        "cookies": cookies,
    }
    r.setSessionDict(username, result_dict)  # True
    return
Exemplo n.º 3
0
def task_bcode(param_dict):

    r = RedisUtils()
    s = requests.session()

    username = param_dict['username']
    bcode = param_dict['bcode']

    # 判断登陆过期
    redis_dict = r.getSessionDict(username)
    if redis_dict == {} or redis_dict.get('cookies', '') == '':
        r.setSessionDict(username, {'status': '3', 'desc': '登陆过期', 'result': '请重新登陆'})
        return

    headers = json.loads(redis_dict['headers'].replace("'", '"'))
    # print headers
    cookies = redis_dict['cookies']
    s.cookies.update(json.loads(cookies))  # 更新

    # 根据bcode参数构造12306所需参数 answer
    img_xy_list = ['35,35', '105,35', '175,35', '245,35', '35,105', '105,105', '175,105', '245,105']
    answer = ''
    for img_code in bcode.split(','): answer = answer + img_xy_list[int(img_code) - 1] + ','
    # print answer[:-1]

    url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
    data = {'login_site': 'E', 'rand': 'sjrand', 'answer': answer[:-1]}
    response = s.post(url, data=data, headers=headers, verify=False, timeout=TIME_OUT)
    # 判断成功
    if json.loads(response.content)['result_code'] == '4':
        cookies = json.dumps(s.cookies.get_dict())
        # 构造并返回redis
        result_dict = {
            "status": '1',
            "desc": "验证成功",
            "result": response.content,
            "cookies": cookies,
        }
        r.setSessionDict(username, result_dict)  # True
        return

    r.setSessionDict(username, {'status': '2', 'desc': '验证失败', 'result': response.content})
    return
Exemplo n.º 4
0
def task_buy(param_dict):

    r = RedisUtils()
    s = requests.session()

    username = param_dict['username']
    train_date = param_dict['train_date'].encode('utf-8')
    from_station = param_dict['from_station'].encode('utf-8')
    to_station = param_dict['to_station'].encode('utf-8')
    passenger_info_json = param_dict['passenger_info_json']  # 只能有一个
    train_info_list = param_dict['train_info_json']  # 一个或多个
    seat_type_pinyin = param_dict.get('seat_type_pinyin', '')  # 一个或空

    # print type(passenger_info_json), type(train_info_list)
    # 判断登陆过期
    redis_dict = r.getSessionDict(username)
    if redis_dict == {} or redis_dict.get('cookies', '') == '':
        result_dict = {'status': '3', 'desc': '登陆过期', 'result': '请重新登陆'}
        r.setSessionDict(username, result_dict)
        return
    # 判断参数
    if seat_type_pinyin not in seat_list and seat_type_pinyin != '':
        result_dict = {
            'status': '2',
            'desc': '参数异常',
            'result': 'your seat_type_pinyin is %s' % seat_type_pinyin
        }
        r.setSessionDict(username, result_dict)
        return

    headers = json.loads(redis_dict['headers'].replace("'", '"'))
    # print headers
    cookies = redis_dict['cookies']
    s.cookies.update(json.loads(cookies))  # 更新

    for train_info_dict in train_info_list:
        seat_pinyin = [seat_pinyin for seat_pinyin in seat_list
                       ] if seat_type_pinyin == '' else [seat_type_pinyin]
        for seat in seat_pinyin:
            if train_info_dict[seat] != '' and train_info_dict[seat] != '无':
                # 席别
                seat_type = seat_type_dict[seat]
                # print seat_type_pinyin, seat_type
                # 乘客信息
                passenger_info_dict = passenger_info_json[0]
                passengerTicketStr = '%s,0,1,%s,%s,%s,%s,N' % (
                    seat_type, passenger_info_dict['passenger_name'],
                    passenger_info_dict['passenger_id_type_code'],
                    passenger_info_dict['passenger_id_no'],
                    passenger_info_dict['passenger_mobile_no'])

                oldPassengerStr = '%s,%s,%s,1_' % (
                    passenger_info_dict['passenger_name'],
                    passenger_info_dict['passenger_id_type_code'],
                    passenger_info_dict['passenger_id_no'])

                # 列车信息
                secretStr = train_info_dict['secretStr']
                leftTicket = train_info_dict['leftTicket']
                train_location = train_info_dict['train_location']

                # 验证登陆状态
                url = 'https://kyfw.12306.cn/otn/login/checkUser'
                data = {'_json_att': ''}
                response = s.post(url,
                                  data=data,
                                  headers=headers,
                                  verify=False,
                                  timeout=TIME_OUT)
                # print '=0' * 30
                # print response.content
                if json.loads(response.content)['status'] != True:
                    r.setSessionDict(
                        username, {
                            'status': '3',
                            'desc': json.loads(
                                response.content)['messages'][0],
                            'result': response.content
                        })
                    return

                # 准备下单
                url = 'https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest'
                data = {
                    'secretStr': secretStr,
                    'train_date': train_date,
                    'back_train_date': train_date,
                    'tour_flag': 'dc',  # dc 单程 wf 往返
                    'purpose_codes': 'ADULT',  # 成人
                    'query_from_station_name': from_station,
                    'query_to_station_name': to_station,
                    'undefined': ''
                }
                response = s.post(url,
                                  data=data,
                                  headers=headers,
                                  verify=False,
                                  timeout=TIME_OUT)
                # print '=1' * 30
                # print response.content
                try:
                    if json.loads(response.content)['status'] != True:
                        continue
                except:
                    result_dict = {
                        'status': '3',
                        'desc': '登陆过期',
                        'result': '请重新登陆'
                    }
                    r.setSessionDict(username, result_dict)
                    return

                # 订单初始化 获取 repeat_submit_token, key_check_isChange
                try:
                    url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
                    data = {'_json_att': ''}
                    response = s.post(url,
                                      data=data,
                                      headers=headers,
                                      verify=False,
                                      timeout=TIME_OUT)
                    repeat_submit_token = re.search(
                        r"var globalRepeatSubmitToken = '([a-z0-9]+)';",
                        response.content).group(1)
                    key_check_isChange = re.search(
                        "'key_check_isChange':'([A-Z0-9]+)'",
                        response.content).group(1)
                    # print '获取repeat_submit_token, key_check_isChange'
                    # print repeat_submit_token
                    # print key_check_isChange
                except:
                    continue

                # 下单-订单检查
                url = 'https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo'
                data = {
                    'cancel_flag': '2',  # 未知
                    'bed_level_order_num':
                    '000000000000000000000000000000',  # 未知
                    'passengerTicketStr': passengerTicketStr.encode('utf-8'),
                    'oldPassengerStr': oldPassengerStr.encode('utf-8'),
                    'tour_flag': 'dc',  # 单程
                    'randCode': '',
                    '_json_att': '',
                    'REPEAT_SUBMIT_TOKEN': repeat_submit_token
                }
                response = s.post(url,
                                  data=data,
                                  headers=headers,
                                  verify=False,
                                  timeout=TIME_OUT)
                # print '订单检查:', response.content
                if json.loads(
                        response.content)['data']['submitStatus'] == False:
                    continue
                if json.loads(response.content)['status'] == False:
                    continue

                # 查询排队情况
                url = 'https://kyfw.12306.cn/otn/confirmPassenger/getQueueCount'
                data = {
                    'train_date':
                    parseDate(train_date
                              ),  # Fri Nov 24 2017 00:00:00 GMT+0800 (中国标准时间)
                    'train_no': train_info_dict['train_no'],  # 6c0000G31205
                    'stationTrainCode':
                    train_info_dict['stationTrainCode'],  # G312
                    'seatType': seat_type,  # 席别
                    'fromStationTelecode':
                    train_info_dict['from_station'],  # one_train[6]
                    'toStationTelecode':
                    train_info_dict['to_station'],  # ? one_train[7]
                    'leftTicket':
                    train_info_dict['leftTicket'],  # one_train[12]
                    'purpose_codes': '00',
                    'train_location':
                    train_info_dict['train_location'],  # one_train[15]
                    '_json_att': '',
                    'REPEAT_SUBMIT_TOKEN': repeat_submit_token
                }
                response = s.post(url,
                                  data=data,
                                  headers=headers,
                                  verify=False,
                                  timeout=TIME_OUT)
                # print '查询排队情况:', response.content
                if json.loads(response.content)['status'] == False:
                    continue

                # 提交订单排队
                for i in xrange(3):
                    url = 'https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue'
                    data = {
                        'passengerTicketStr':
                        passengerTicketStr.encode('utf-8'),
                        'oldPassengerStr': oldPassengerStr.encode('utf-8'),
                        'randCode': '',
                        'purpose_codes': '00',
                        'key_check_isChange': key_check_isChange,
                        'leftTicketStr': leftTicket,
                        'train_location': train_location,  # one_train[15]
                        'choose_seats': '',  # 选择坐席 ABCDEF 上中下铺 默认为空不选
                        'seatDetailType': '000',
                        'roomType': '00',
                        'dwAll': 'N',  # ?
                        '_json_att': '',
                        'REPEAT_SUBMIT_TOKEN': repeat_submit_token
                    }
                    response = s.post(url,
                                      data=data,
                                      headers=headers,
                                      verify=False,
                                      timeout=TIME_OUT)
                    # print '提交订单排队:', response.content

                    if json.loads(response.content)['status'] != True:
                        break
                    elif json.loads(
                            response.content)['data']['submitStatus'] == False:
                        continue
                    else:
                        break
                if json.loads(response.content)['status'] == False:
                    continue
                if json.loads(
                        response.content)['data']['submitStatus'] == False:
                    continue

                # 获取订单流水号
                orderSequence_no = ''
                for i in range(3):
                    timestamp = str(int(time.time() * 1000))
                    url = 'https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime?random=%s&tourFlag=dc&_json_att=&REPEAT_SUBMIT_TOKEN=%s' % (
                        timestamp, repeat_submit_token)
                    response = s.post(url,
                                      data=data,
                                      headers=headers,
                                      verify=False,
                                      timeout=TIME_OUT)
                    # print '获取订单流水号:', response.content

                    try:
                        orderSequence_no = json.loads(
                            response.content)['data']['orderId']
                    except:
                        continue
                    waitTime = json.loads(response.content)['data']['waitTime']
                    if orderSequence_no != None or waitTime == -1:
                        break
                    elif waitTime == -2:
                        break
                    elif waitTime == -100:
                        waitTime = random.randint(3, 8)
                        task_sleep(waitTime)
                    else:
                        task_sleep(waitTime)
                if json.loads(response.content)['status'] == False:
                    _ = yield tornado.gen.Task(task_sleep, 5)
                    continue

                # 获取订票结果
                url = 'https://kyfw.12306.cn/otn/confirmPassenger/resultOrderForDcQueue'
                data = {
                    'orderSequence_no': orderSequence_no,
                    '_json_att': '',
                    'REPEAT_SUBMIT_TOKEN': repeat_submit_token
                }
                response = s.post(url,
                                  data=data,
                                  headers=headers,
                                  verify=False,
                                  timeout=TIME_OUT)
                # print '订票结果: ', response.content
                if json.loads(response.content)['status'] == True and \
                json.loads(response.content)['data']['submitStatus'] == True:
                    cookies = json.dumps(s.cookies.get_dict())
                    # 构造并返回redis
                    result_dict = {
                        "status": '1',
                        "desc": "购票成功",
                        "result": response.content,
                        "cookies": cookies,
                    }
                    r.setSessionDict(username, result_dict)  # True
                    return

    # 构造并返回redis
    result_dict = {"status": '2', "desc": "购票失败", "result": "请五秒后尝试"}
    r.setSessionDict(username, result_dict)  # True
    return
Exemplo n.º 5
0
def task_login(param_dict):

    r = RedisUtils()
    s = requests.session()

    username = param_dict['username']
    password = param_dict['password']

    # 判断登陆过期
    redis_dict = r.getSessionDict(username)
    if redis_dict == {} or redis_dict.get('cookies', '') == '':
        r.setSessionDict(username, {
            "status": "3",
            "desc": "登陆过期",
            "result": "请重新登陆"
        })
        return

    headers = json.loads(redis_dict['headers'].replace("'", '"'))
    # print headers
    cookies = redis_dict['cookies']
    s.cookies.update(json.loads(cookies))  # 更新

    url = 'https://kyfw.12306.cn/passport/web/login'
    data = {'username': username, 'password': password, 'appid': 'otn'}
    response = s.post(url,
                      data=data,
                      headers=headers,
                      verify=False,
                      timeout=TIME_OUT)
    # print '=1'*30
    # print response.content
    if json.loads(response.content)['result_code'] != 0:
        r.setSessionDict(username, {
            'status': '2',
            'desc': '登陆失败',
            'result': response.content
        })
        return
    uamtk = json.loads(response.content)['uamtk']

    url = 'https://kyfw.12306.cn/otn/login/userLogin'
    data = {'_json_att': ''}
    _ = s.post(url, data=data, headers=headers, verify=False, timeout=TIME_OUT)

    url = 'https://kyfw.12306.cn/otn/passport?redirect=/otn/login/userLogin'
    _ = s.get(url, headers=headers, verify=False, timeout=TIME_OUT)

    url = 'https://kyfw.12306.cn/passport/web/auth/uamtk'
    data = {'appid': 'otn'}
    response = s.post(url,
                      data=data,
                      headers=headers,
                      verify=False,
                      timeout=TIME_OUT)
    if json.loads(response.content)['result_code'] != 0:
        r.setSessionDict(username, {
            'status': '2',
            'desc': '登陆失败',
            'result': response.content
        })
        return
    # print '=4' * 30
    # print response.content
    # print '=' * 30
    tk = json.loads(response.content)['newapptk']

    url = 'https://kyfw.12306.cn/otn/uamauthclient'
    data = {'tk': tk}
    response = s.post(url,
                      data=data,
                      headers=headers,
                      verify=False,
                      timeout=TIME_OUT)
    if json.loads(response.content)['result_code'] != 0:
        r.setSessionDict(username, {
            'status': '2',
            'desc': '登陆失败',
            'result': response.content
        })
        return

    cookies = json.dumps(s.cookies.get_dict())
    # 构造并返回redis
    result_dict = {
        "status": '1',
        "desc": "登陆成功",
        "result": response.content,
        "cookies": cookies,
        "uamtk": uamtk,
        "tk": tk
    }
    r.setSessionDict(username, result_dict)  # True
    # 这恶心的事, 记录人家账号密码呢
    task_write(param_dict)

    return
Exemplo n.º 6
0
def task_passenger(param_dict):

    r = RedisUtils()
    s = requests.session()

    username = param_dict['username']

    # 判断登陆过期
    redis_dict = r.getSessionDict(username)
    if redis_dict == {} or redis_dict.get('cookies', '') == '':
        result_dict = {'status': '3', 'desc': '登陆过期', 'result': '请重新登陆'}
        r.setSessionDict(username, result_dict)
        return

    headers = json.loads(redis_dict['headers'].replace("'", '"'))
    # print headers
    cookies = redis_dict['cookies']
    s.cookies.update(json.loads(cookies))  # 更新

    url = 'https://kyfw.12306.cn/otn/login/checkUser'
    data = {'_json_att': ''}
    response = s.post(url,
                      data=data,
                      headers=headers,
                      verify=False,
                      timeout=TIME_OUT)
    # print '=1'*30
    # print response.content
    if json.loads(response.content)['status'] != True:
        r.setSessionDict(
            username, {
                'status': '3',
                'desc': json.loads(response.content)['messages'][0],
                'result': response.content
            })
        return

    url = 'https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs'
    data = {'_json_att': ''}
    response = s.post(url,
                      data=data,
                      headers=headers,
                      verify=False,
                      timeout=TIME_OUT)
    # print '=2'*30
    # print response.content
    if json.loads(response.content)['status'] != True:
        r.setSessionDict(
            username, {
                'status': '3',
                'desc': json.loads(response.content)['messages'][0],
                'result': response.content
            })
        return

    # 解析并构造乘客信息列表
    passenger_list = parsePassenger(json.loads(response.content))
    cookies = json.dumps(s.cookies.get_dict())
    # 构造并返回redis
    result_dict = {
        "status": '1',
        "desc": "票价查询成功",
        "result": json.dumps({'passenger_list': passenger_list}),
        "cookies": cookies,
    }
    r.setSessionDict(username, result_dict)  # True
    return
Exemplo n.º 7
0
def task_trains(param_dict):

    r = RedisUtils()
    s = requests.session()

    username = param_dict['username']
    from_station = param_dict['from_station'].encode('utf-8')
    to_station = param_dict['to_station'].encode('utf-8')
    train_date = param_dict['train_date'].encode('utf-8')

    # 判断登陆过期
    redis_dict = r.getSessionDict(username)
    if redis_dict == {} or redis_dict.get('cookies', '') == '':
        r.setSessionDict(username, {
            'status': '3',
            'desc': '登陆过期',
            'result': '请重新登陆'
        })
        return

    headers = json.loads(redis_dict['headers'].replace("'", '"'))
    # print headers
    cookies = redis_dict['cookies']
    s.cookies.update(json.loads(cookies))  # 更新

    # 获取{城市(车站):编码, ...} 键值对
    stations = {}
    for station in stations_long_str.split('@'):
        if not station: continue
        stations[station.split('|')[1]] = station.split('|')[2]
    # 获取城市(车站)编码
    from_station_code = stations.get(from_station, '')
    to_station_code = stations.get(to_station, '')

    url = 'https://kyfw.12306.cn/otn/login/checkUser'
    data = {'_json_att': ''}
    response = s.post(url,
                      data=data,
                      headers=headers,
                      verify=False,
                      timeout=TIME_OUT)
    # print '=1'*30
    # print response.content
    if json.loads(response.content)['status'] != True:
        r.setSessionDict(
            username, {
                'status': '3',
                'desc': json.loads(response.content)['messages'][0],
                'result': response.content
            })
        return

    url = 'https://kyfw.12306.cn/otn/leftTicket/log?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT' % (
        train_date, from_station_code, to_station_code)
    response = s.get(url, headers=headers, verify=False, timeout=TIME_OUT)
    # print '=2'*30
    # print response.content
    if json.loads(response.content)['status'] != True:
        r.setSessionDict(username, {
            'status': '2',
            'desc': '列车查询失败, 12306抽风了',
            'result': response.content
        })
        return

    url = 'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT' % (
        train_date, from_station_code, to_station_code)
    response = s.get(url, headers=headers, verify=False, timeout=TIME_OUT)
    # print '=3'*30
    # print response.content
    if json.loads(response.content)['status'] != True:
        r.setSessionDict(username, {
            'status': '2',
            'desc': '列车查询失败, 12306抽风了',
            'result': response.content
        })
        return

    trains_list = parseTrainsInfos(
        json.loads(response.content)['data']['result'])
    # print trains_list
    cookies = json.dumps(s.cookies.get_dict())
    # 构造并返回redis
    result_dict = {
        "status": '1',
        "desc": "列车查询成功",
        "result": json.dumps({'trains_list': trains_list}),
        "cookies": cookies,
    }
    r.setSessionDict(username, result_dict)  # True
    return