コード例 #1
0
ファイル: core.py プロジェクト: himself07/BilibiliUploader
def login(username, password):
    """
    bilibili login.
    Args:
        username: plain text username for bilibili.
        password: plain text password for bilibili.

    Returns:
        access_token: token for further operation.
        refresh_token: token for refresh access_token.
        sid: session id.
        mid: member id.
        expires_in: access token expire time (30 days)
    """
    hash, pubkey, sid = get_key()

    encrypted_password = cipher.encrypt_login_password(password, hash, pubkey)
    url_encoded_username = parse.quote_plus(username)
    url_encoded_password = parse.quote_plus(encrypted_password)

    post_data = {
        'appkey': APPKEY,
        'password': url_encoded_password,
        'platform': "pc",
        'ts': str(int(datetime.now().timestamp())),
        'username': url_encoded_username
    }

    post_data['sign'] = cipher.sign_dict(post_data, APPSECRET)
    # avoid multiple url parse
    post_data['username'] = username
    post_data['password'] = encrypted_password

    headers = {
        'Connection': 'keep-alive',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'User-Agent': '',
        'Accept-Encoding': 'gzip,deflate',
    }

    r = requests.post("https://passport.bilibili.com/api/oauth2/login",
                      headers=headers,
                      data=post_data,
                      cookies={'sid': sid})
    login_data = r.json()['data']
    return login_data['access_token'], login_data[
        'refresh_token'], sid, login_data['mid'], login_data["expires_in"]
コード例 #2
0
ファイル: core.py プロジェクト: Willy9899/DDRecoder
def login_captcha(username, password, sid):
    """
    bilibili login with captcha.
    depend on captcha recognize service, please do not use this as first choice.
    Args:
        username: plain text username for bilibili.
        password: plain text password for bilibili.
        sid: session id
    Returns:
        code: login response code (0: success, -105: captcha error, ...).
        access_token: token for further operation.
        refresh_token: token for refresh access_token.
        sid: session id.
        mid: member id.
        expires_in: access token expire time (30 days)
    """

    jsessionid, captcha_img = get_capcha(sid)
    captcha_str = recognize_captcha(captcha_img)

    hash, pubkey, sid = get_key(sid, jsessionid)

    encrypted_password = cipher.encrypt_login_password(password, hash, pubkey)
    url_encoded_username = parse.quote_plus(username)
    url_encoded_password = parse.quote_plus(encrypted_password)

    post_data = {
        'appkey': APPKEY,
        'captcha': captcha_str,
        'password': url_encoded_password,
        'platform': "pc",
        'ts': str(int(datetime.now().timestamp())),
        'username': url_encoded_username
    }

    post_data['sign'] = cipher.sign_dict(post_data, APPSECRET)
    # avoid multiple url parse
    post_data['username'] = username
    post_data['password'] = encrypted_password
    post_data['captcha'] = captcha_str

    headers = {
        'Connection': 'keep-alive',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'User-Agent': '',
        'Accept-Encoding': 'gzip,deflate',
    }

    r = requests.post("https://passport.bilibili.com/api/oauth2/login",
                      headers=headers,
                      data=post_data,
                      cookies={
                          'JSESSIONID': jsessionid,
                          'sid': sid
                      })
    response = r.json()
    if response['code'] == 0:
        login_data = response['data']
        return response['code'], login_data['access_token'], login_data[
            'refresh_token'], sid, login_data['mid'], login_data["expires_in"]
    else:
        return response['code'], None, None, sid, None, None
コード例 #3
0
def login(username, password):
    """
    bilibili login.
    Args:
        username: plain text username for bilibili.
        password: plain text password for bilibili.

    Returns:
        code: login response code (0: success, -105: captcha error, ...).
        access_token: token for further operation.
        refresh_token: token for refresh access_token.
        sid: session id.
        mid: member id.
        expires_in: access token expire time (30 days)
    """
    hash, pubkey, sid = get_key()

    encrypted_password = cipher.encrypt_login_password(password, hash, pubkey)
    url_encoded_username = parse.quote_plus(username)
    url_encoded_password = parse.quote_plus(encrypted_password)

    post_data = {
        'appkey': LOGIN_APPKEY,
        'password': url_encoded_password,
        'ts': str(int(datetime.now().timestamp())),
        'username': url_encoded_username
    }

    post_data['sign'] = cipher.login_sign_dict_bin(post_data)
    # avoid multiple url parse
    post_data['username'] = username
    post_data['password'] = encrypted_password

    headers = {
        'Connection': 'keep-alive',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'User-Agent': '',
        'Accept-Encoding': 'gzip,deflate',
    }

    r = requests.post(
        # "https://passport.bilibili.com/api/v3/oauth2/login",
        "https://passport.bilibili.com/x/passport-login/oauth2/login",
        headers=headers,
        data=post_data,
    )
    response = r.json()
    response_code = response['code']
    if response_code == 0:
        login_data = response['data']['token_info']
        return response_code, login_data['access_token'], login_data['refresh_token'], sid, login_data['mid'], \
               login_data["expires_in"]
    elif response_code == -105:  # captcha error, retry=5
        retry_cnt = 5
        while response_code == -105 and retry_cnt > 0:
            response_code, access_token, refresh_token, sid, mid, expire_in = login_captcha(
                username, password, sid)
            if response_code == 0:
                return response_code, access_token, refresh_token, sid, mid, expire_in
            retry_cnt -= 1

    # other error code
    return response_code, None, None, sid, None, None