Example #1
0
def submit_train_ticket(session, method, url):
    """
    https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest
        POST        secretStr=口令, train_date=出发时间, back_train_date=返程时间, tour_flag=dc, purpose_codes=ADULT
                    , query_from_station_name=出发站名, query_to_station_name=到达站名, undefined=
    https://kyfw.12306.cn/otn/confirmPassenger/initDc       POST        _json_att=      找到返回数据中的globalRepeatSubmitToken = '278e64761b75a00f8426a10c3011649f'字段

    :return:
    """
    public12306.create_network_request(
        session,
        "POST",
    )
    pass
Example #2
0
def get_contact_information(session, method, url, **kwargs):
    """
    获取账号下的所有联系人信息,需要session
    :param session:
    :param method:
    :param url:
    :param kwargs:
    :return:
    """
    contactInformationDict = dict()
    contactInformationResponse = public12306.create_network_request(
        session, method, url, **kwargs)[1]
    contactInformationJson = contactInformationResponse.json()
    for contactInformation in contactInformationJson["data"][
            "normal_passengers"]:
        passenger_name = contactInformation.get("passenger_name")
        sex_name = contactInformation.get("sex_name")
        passenger_id_type_name = contactInformation.get(
            "passenger_id_type_name")
        passenger_id_no = contactInformation.get("passenger_id_no")
        contactInformationList = [
            sex_name, passenger_id_type_name, passenger_id_no
        ]
        contactInformationDict[passenger_name] = contactInformationList
        print(contactInformation)
    return contactInformationDict
Example #3
0
def submit_train_ticket(session,
                        method,
                        url,
                        secretS,
                        stationF,
                        stationT,
                        deaprtureD,
                        tourF="dc",
                        purposeC="ADULT",
                        returnD=date.today(),
                        **kwargs):
    """
    ---------------Steep1
    :param session:
    :param method:
    :param url:
    :param secretS: 口令
    :param stationF: 出发站
    :param stationT: 到达站
    :param deaprtureD: 出发日期
    :param tour_flag: dc单程,wc往返
    :param purpose_codes: ADULT成人,0X00学生
    :param returnD: 返程日期
    :param kwargs:
    :return:
    """
    kwargs["params"] = submitTrainParams[0].format(secretS, stationF, stationT,
                                                   deaprtureD, tourF, returnD,
                                                   purposeC)
    submitResponse = public12306.create_network_request(
        session, method, url, **kwargs)[1]
    submitJson = submitResponse.json()
    if not (submitJson["status"] & submitJson["httpstatus"] == 200):  # 需要重试
        pass
Example #4
0
def parse_station_code(method, url, **kwargs):
    """
    解析所有车站代码,无需session
    :param method:
    :param url:
    :param kwargs:
    :return:
    """
    stationResponse = public12306.create_network_request(
        None, method, url, **kwargs)[1]
    stationKey = list()
    stationValue = list()
    l1 = list()
    l2 = list()
    l3 = list()
    for i in re.finditer(r"@\w+\|(\w+)\|(\w+)\|(\w+)\|(\w+)",
                         stationResponse.text):
        l1.append(i.group(1))
        l2.append(i.group(3))
        l3.append(i.group(4))
        stationValue.append(i.group(2))
    stationKey.extend(l1)
    stationKey.extend(l2)
    stationKey.extend(l3)
    if len(stationValue) * 3 != len(stationKey):
        raise ValueError("parse_station_code() Error!")
    return stationKey, stationValue
Example #5
0
def get_login_user(userName=None, password=None):
    """
    获取登录的用户session
    :param userName:
    :param password:
    :return:
    """
    if not (userName & password):
        userName = input("输入用户帐号:")
        password = input("输入用户密码:")
    otherSession = get_other_cookies(None,
                                     *otherCookiesParamsUrl,
                                     headers=initRequestHeaders)
    initSession = public12306.create_network_request(
        otherSession, *baseUrl, headers=initRequestHeaders)[0]
    userSession, imgPosition = load_login_img(initSession,
                                              *imgUrl,
                                              headers=initRequestHeaders)
    userSession, imgCode = submit_login_img(
        userSession,
        *loginImgUrl,
        headers=commitRequestHeaders,
        params=imgRequestParams.format(imgPosition))
    userSession, userCode = submit_login_user(userSession,
                                              loginUerUrls,
                                              loginUserParams,
                                              userName,
                                              password,
                                              headers=commitRequestHeaders)
    return userSession
Example #6
0
def submit_login_user(session, methodAndUrls, loginUserParams, userName,
                      password, **kwargs):
    """
    提交用户登录,共1个提交,3个验证
    :param session:
    :param method:
    :param urls:
    :param loginUserParams:
    :param userName:
    :param password:
    :param kwargs:
    :return:
    """
    kwargs["params"] = loginUserParams[0].format(userName, password)
    loginSession, loginResponse = public12306.create_network_request(
        session, *methodAndUrls[0], **kwargs)
    kwargs["params"] = loginUserParams[1]
    loginSession, _ = public12306.create_network_request(
        loginSession, *methodAndUrls[1], **kwargs)
    kwargs["params"] = loginUserParams[2]
    loginSession, _ = public12306.create_network_request(
        loginSession, *methodAndUrls[2], **kwargs)
    kwargs["params"] = loginUserParams[3]
    loginSession, thirdResponse = public12306.create_network_request(
        loginSession, *methodAndUrls[3], **kwargs)
    subCookies = {"tk": thirdResponse.json()["newapptk"]}
    loginSession.cookies.update(subCookies)
    kwargs["params"] = subCookies
    loginSession, fourthResponse = public12306.create_network_request(
        loginSession, *methodAndUrls[4], **kwargs)
    rc = "result_code"
    if loginResponse.json()[rc] == 0 & thirdResponse.json(
    )[rc] == 0 & fourthResponse.json()[rc] == 0:
        print("用户登录成功!")
        return loginSession, stateCode[1]
    else:
        print("用户登录失败!")
        return loginSession, stateCode[0]
Example #7
0
def get_submit_token(session, method, url, **kwargs):
    """
    :param session:
    :param method:
    :param url:
    :param kwargs: params:_json_att=
    :return:
    """
    tokenResponse = public12306.create_network_request(session, method, url,
                                                       **kwargs)[1]
    token = re.search("globalRepeatSubmitToken = '(.*?)'",
                      tokenResponse).groups()[0]
    if not token:  # 需要重试
        pass
    return token
Example #8
0
def check_user_state(session, method, url, **kwargs):
    """
    :param session:
    :param method:
    :param url:
    :param kwargs: params:_json_att=
    :return:
    """
    checkResponse = public12306.create_network_request(session, method, url,
                                                       **kwargs)[1]
    if checkResponse.json()["data"]["flag"]:
        print("用户校验成功!")
    else:  # 重新登录
        session = login12306.get_login_user()
    return session
Example #9
0
def load_login_img(session, method, url, **kwargs):
    """
    加载图片验证码
    :param session:
    :param method:
    :param url:
    :param kwargs:
    :return:
    """
    immgSession, imgResponse = public12306.create_network_request(
        session, method, url, **kwargs)
    loginImage = Image.open(BytesIO(imgResponse.content))
    loginImage.show()
    imgPosition = input("输入验证码中对应图片的序号:")
    imgPosition = str(",".join([imgLocation[int(x)] for x in imgPosition]))
    return immgSession, imgPosition
Example #10
0
def submit_login_img(session, method, url, **kwargs):
    """
    提交验证码
    :param session:
    :param method:
    :param url:
    :param kwargs:
    :return:
    """
    imgSession, imgResponse = public12306.create_network_request(
        session, method, url, **kwargs)
    if imgResponse.json()["result_code"] == "4":
        print("验证码校验成功!")
        return imgSession, stateCode[1]
    else:

        print("验证码校验失败!")
        return imgSession, stateCode[0]
Example #11
0
def query_ticket(method, url, stationCode, date=date.today(), **kwargs):
    """
    查询车票,无需session
    :param method:
    :param url:
    :param stationCode:
    :param date:
    :param kwargs:
    :return:
    """
    queryJson = None
    while not queryJson:
        kwargs["params"] = queryTicketParams.format(date, *stationCode)
        queryResponse = public12306.create_network_request(
            None, method, url, **kwargs)[1]
        try:
            queryJson = queryResponse.json()
        except json.decoder.JSONDecodeError as e:
            # print("JSONDecodeError: " + str(e))
            # print(queryJson)
            pass
    return queryJson
Example #12
0
def get_other_cookies(session, method, url, **kwargs):
    """
    获取必要的cookies字段
    :param session:
    :param method:
    :param url:
    :param kwargs:
    :return:
    """
    otherSession, otherResponse = public12306.create_network_request(
        session, method, url, **kwargs)
    try:
        otherCookiesJson = json.loads(
            re.search(r"{.*?exp.*?dfp.*?}", otherResponse.text).group())
        otherCookiesDict = {
            "RAIL_EXPIRATION": otherCookiesJson["exp"],
            "RAIL_DEVICEID": otherCookiesJson["dfp"]
        }
        otherSession.cookies.update(otherCookiesDict)
    except AttributeError:
        return get_other_cookies(session, method, url, **kwargs)
    return otherSession
Example #13
0
def login_out_user(session):
    public12306.create_network_request(session, *loginOutUrl)