Ejemplo n.º 1
0
def get_params(session, studentid):
    """Get elect params.

    Parse elect params.

    Args:
        session: requests session, login session.
        studentid: str, student id.

    Returns:
        dict:
            xkkz_id: dict, [cnt]:[id]
            njdm_id: str, njdm_id
            zyh_id: str, zyh_id
    """
    params = {'gnmkdm': 'N253512', 'layout': 'default', 'su': studentid}
    req = _request(session,
                   'GET',
                   'https://i.sjtu.edu.cn/xsxk/zzxkyzb_cxZzxkYzbIndex.html',
                   params=params)
    xkkz_id = {}
    xkkz = re.findall(
        r'\'(.*?)\',\'(.*?)\',\'.*?\',\'.*?\'\)" role="tab" data-toggle="tab">',
        req)
    for i in xkkz:
        xkkz_id[i[0]] = i[1]

    njdm_id = re_search(r'id="njdm_id" value="(.*?)"/>', req)
    zyh_id = re_search(r'id="zyh_id" value="(.*?)"/>', req)
    return {'xkkz_id': xkkz_id, 'njdm_id': njdm_id, 'zyh_id': zyh_id}
Ejemplo n.º 2
0
def get_studentid(session):
    """Get student id.

    Parse student id.

    Args:
        session: requests session, login session.

    Returns:
        str, student id.
    """
    params = {'jsdm': '', '_t': get_timestamp()}
    req = _request(
        session, 'GET', 'http://i.sjtu.edu.cn/xtgl/index_initMenu.html', params=params)
    return re_search(r'sessionUserKey" value="(.*?)"', req)
Ejemplo n.º 3
0
def login(url, useocr=False):
    """Call this function to login.

    Captcha picture will be stored in captcha.jpeg.
    WARNING: From 0.2.0, username and password will not be allowed to pass as params, all done by this function itself.

    Args:
        url: string, direct login url
        useocr=False: bool, True to use ocr to autofill captcha

    Returns:
        requests login session.
    """
    while True:
        username = input('Username: '******'Password(no echo): ')

        while True:
            session = _create_session()
            req = _get_login_page(session, url)
            captcha_id = re_search(r'img.src = \'captcha\?(.*)\'', req)
            if not captcha_id:
                print('Captcha not found! Retrying...')
                sleep(3)
                continue
            captcha_id += get_timestamp()
            captcha_url = 'https://jaccount.sjtu.edu.cn/jaccount/captcha?' + captcha_id
            code = _bypass_captcha(session, captcha_url, useocr)

            sid = re_search(r'sid" value="(.*?)"', req)
            returl = re_search(r'returl" value="(.*?)"', req)
            se = re_search(r'se" value="(.*?)"', req)
            client = re_search(r'client" value="(.*?)"', req)
            uuid = re_search(r'captcha\?uuid=(.*?)&t=', req)
            if not (sid and returl and se and uuid):
                print('Params not found! Retrying...')
                sleep(3)
                continue

            res = _login(session, sid, returl, se, client, username, password,
                         code, uuid)

            if res == 2:
                if not useocr:
                    print('Wrong captcha! Try again!')
                continue
            elif res == 1:
                print('Wrong username or password! Try again!')
                break
            elif res == 3:
                print('Opps! You are banned for 30s...Waiting...')
                sleep(30)
                continue
            else:
                return session