예제 #1
0
파일: auth.py 프로젝트: kktt007/bcloud
def get_auth_cookie(cookie):
    '''通过重定向获得授权cookie
    
    重定向的链接开头为:
    http://pan.baidu.com/disk/home?errno=0&errmsg=Auth%20Login%20Sucess&stoken=xxx
    '''
    url = ''.join([
        const.PASSPORT_LOGIN_V3, 'auth/?return_type=5',
        '&tpl=netdisk&u=https%3A%2F%2Fpan.baidu.com%2Fdisk%2Fhome'
    ])
    headers = {
        'Cookie': cookie.header_output(),
        'Referer': const.PAN_REFERER,
    }
    req = net.urlopen_without_redirect(url, headers=headers)
    if req and req.headers.get("Location"):
        redirect_url = req.headers.get("Location")
        redirect_req = net.urlopen_without_redirect(
            redirect_url, headers={'Cookie': cookie.header_output()})
        if redirect_req:
            return redirect_req.headers.get_all('Set-Cookie')
        else:
            return None
    else:
        return None
예제 #2
0
파일: pcs.py 프로젝트: yusiwen/bcloud
def get_download_link(cookie, tokens, path):
    '''在下载之前, 要先获取最终的下载链接.

    path - 一个文件的绝对路径.

    @return red_url, red_url 是重定向后的URL, 如果获取失败,
            就返回原来的dlink;
    '''
    metas = get_metas(cookie, tokens, path)
    if (not metas or metas.get('errno', -1) != 0 or 'info' not in metas
            or len(metas['info']) != 1):
        logger.error('pcs.get_download_link(): %s' % metas)
        return None
    dlink = metas['info'][0]['dlink']
    url = '{0}&cflg={1}'.format(dlink, cookie.get('cflag').value)
    req = net.urlopen_without_redirect(url,
                                       headers={
                                           'Cookie':
                                           cookie.sub_output(
                                               'BAIDUID', 'BDUSS', 'cflag'),
                                           'Accept':
                                           const.ACCEPT_HTML,
                                       })
    if not req:
        return url
    else:
        return req.getheader('Location', url)
예제 #3
0
파일: pcs.py 프로젝트: yusiwen/bcloud
def get_share_uk_and_shareid(cookie, url):
    '''从共享链接中提示uk和shareid.

    如果共享文件需要输入密码, 就会将need_pwd设为True
    如果成功, 返回(need_pwd, uk, shareid)
    如果失败, 就返回None

    目前支持的链接格式有:
      * http://pan.baidu.com/wap/link?uk=202032639&shareid=420754&third=0
      * http://pan.baidu.com/share/link?uk=202032639&shareid=420754
      * http://pan.baidu.com/s/1i3iQY48
    '''
    def parse_share_uk(content):
        '''代码片段如下:

        yunData.SHARE_ID = "677200861";
        yunData.SHARE_UK = "1295729848";
        '''
        uk_reg = re.compile('yunData.SHARE_UK\s*=\s*"(\d+)"')
        shareid_reg = re.compile('yunData.SHARE_ID\s*=\s*"(\d+)"')
        uk_match = uk_reg.search(content)
        shareid_match = shareid_reg.search(content)
        if uk_match and shareid_match:
            return False, uk_match.group(1), shareid_match.group(1)
        else:
            return None

    def parse_uk_from_url(url):
        uk_reg = re.compile('uk=(\d+)')
        uk_match = uk_reg.search(url)
        shareid_reg = re.compile('shareid=(\d+)')
        shareid_match = shareid_reg.search(url)
        if not uk_match or not shareid_match:
            return '', ''
        uk = uk_match.group(1)
        shareid = shareid_match.group(1)
        return uk, shareid

    # 识别加密链接
    req = net.urlopen_without_redirect(url,
                                       headers={
                                           'Cookie': cookie.header_output(),
                                       })
    if req and req.headers.get('Location'):
        init_url = req.headers.get('Location')
        if init_url.find('share/init') > -1:
            uk, shareid = parse_uk_from_url(init_url)
            return True, uk, shareid

    # 处理短链接
    if url.startswith('http://pan.baidu.com/s/'):
        req = net.urlopen(url, headers={
            'Cookie': cookie.header_output(),
        })
        if req:
            return parse_share_uk(req.data.decode())
    # 处理正常链接
    uk, shareid = parse_uk_from_url(url)
    return False, uk, shareid
예제 #4
0
파일: pcs.py 프로젝트: alex8224/bcloud
def get_share_uk_and_shareid(cookie, url):
    '''从共享链接中提示uk和shareid.

    如果共享文件需要输入密码, 就会将need_pwd设为True
    如果成功, 返回(need_pwd, uk, shareid)
    如果失败, 就返回None

    目前支持的链接格式有:
      * http://pan.baidu.com/wap/link?uk=202032639&shareid=420754&third=0
      * http://pan.baidu.com/share/link?uk=202032639&shareid=420754
      * http://pan.baidu.com/s/1i3iQY48
    '''
    def parse_share_uk(content):
        '''代码片段如下:

        yunData.SHARE_ID = "677200861";
        yunData.SHARE_UK = "1295729848";
        '''
        uk_reg = re.compile('yunData.SHARE_UK\s*=\s*"(\d+)"')
        shareid_reg = re.compile('yunData.SHARE_ID\s*=\s*"(\d+)"')
        uk_match = uk_reg.search(content)
        shareid_match = shareid_reg.search(content)
        if uk_match and shareid_match:
            return False, uk_match.group(1), shareid_match.group(1)
        else:
            return None

    def parse_uk_from_url(url):
        uk_reg = re.compile('uk=(\d+)')
        uk_match = uk_reg.search(url)
        shareid_reg = re.compile('shareid=(\d+)')
        shareid_match = shareid_reg.search(url)
        if not uk_match or not shareid_match:
            return '', ''
        uk = uk_match.group(1)
        shareid = shareid_match.group(1)
        return uk, shareid

    # 识别加密链接
    req = net.urlopen_without_redirect(url, headers={
        'Cookie': cookie.header_output(),
    })
    if req and req.headers.get('Location'):
        init_url = req.headers.get('Location')
        if init_url.find('share/init') > -1:
            uk, shareid = parse_uk_from_url(init_url)
            return True, uk, shareid

    # 处理短链接
    if url.startswith('http://pan.baidu.com/s/'):
        req = net.urlopen(url, headers={
            'Cookie': cookie.header_output(),
        })
        if req:
            return parse_share_uk(req.data.decode())
    # 处理正常链接
    uk, shareid = parse_uk_from_url(url)
    return False, uk, shareid
예제 #5
0
def wap_signin(cookie, form):
    '''进行WAP登录认证'''
    url = 'http://wappass.baidu.com/passport/login'
    req = net.urlopen_without_redirect(url, headers={
        'Cookie': cookie.header_output(),
        'Content-Type': const.CONTENT_FORM,
        'Referer': url,
        }, data=parse.urlencode(form).encode())
    if req:
        return req.headers.get_all('Set-Cookie')
    else:
        return None
예제 #6
0
def wap_signin(cookie, form):
    '''进行WAP登录认证'''
    url = 'http://wappass.baidu.com/passport/login'
    req = net.urlopen_without_redirect(url,
                                       headers={
                                           'Cookie': cookie.header_output(),
                                           'Content-Type': const.CONTENT_FORM,
                                           'Referer': url,
                                       },
                                       data=parse.urlencode(form).encode())
    if req:
        return req.headers.get_all('Set-Cookie')
    else:
        return None
예제 #7
0
파일: pcs.py 프로젝트: hou-dao/bcloud
def stream_download(cookie, tokens, path):
    '''下载流媒体文件.

    path - 流文件的绝对路径.
    '''
    url = ''.join([
        const.PCS_URL_D,
        'file?method=download',
        '&path=', encoder.encode_uri_component(path),
        '&app_id=250528',
    ])
    req = net.urlopen_without_redirect(url, headers=
            {'Cookie': cookie.header_output()})
    if req:
        return req
    else:
        return None
예제 #8
0
def stream_download(cookie, tokens, path):
    '''下载流媒体文件.

    path - 流文件的绝对路径.
    '''
    url = ''.join([
        const.PCS_URL_D,
        'file?method=download',
        '&path=', encoder.encode_uri_component(path),
        '&app_id=250528',
    ])
    req = net.urlopen_without_redirect(url, headers=
            {'Cookie': cookie.header_output()})
    if req:
        return req
    else:
        return None
예제 #9
0
def stream_download(cookie, tokens, path):
    print(
        '/usr/local/lib/python3.4/dist-packages/bcloud/pcs.py:stream_download 696'
    )
    '''下载流媒体文件.

    path - 流文件的绝对路径.
    '''
    url = ''.join([
        const.PCS_URL_D,
        'file?method=download',
        '&path=',
        encoder.encode_uri_component(path),
        '&app_id=250528',
    ])
    req = net.urlopen_without_redirect(
        url, headers={'Cookie': cookie.header_output()})
    if req:
        return req
    else:
        return None
예제 #10
0
파일: pcs.py 프로젝트: earthGavinLee/bcloud
def get_download_link(cookie, tokens, path):
    '''在下载之前, 要先获取最终的下载链接.

    path - 一个文件的绝对路径.

    @return red_url, red_url 是重定向后的URL, 如果获取失败,
            就返回原来的dlink;
    '''
    metas = get_metas(cookie, tokens, path)
    if (not metas or metas.get('errno', 1) != 0 or
            'info' not in metas or len(metas['info']) != 1):
        return None
    dlink = metas['info'][0]['dlink']
    url = '{0}&cflg={1}'.format(dlink, cookie.get('cflag').value)
    req = net.urlopen_without_redirect(url, headers={
        'Cookie': cookie.sub_output('BAIDUID', 'BDUSS', 'cflag'),
        'Accept': const.ACCEPT_HTML,
    })
    if not req:
        return url
    else:
        return req.getheader('Location', url)
예제 #11
0
def get_share_uk_and_shareid(cookie, url):
    '''从共享链接中提取uk和shareid.

    目前支持的链接格式有:
      * http(s)://pan.baidu.com/wap/link?uk=202032639&shareid=420754&third=0
      * http(s)://pan.baidu.com/share/link?uk=202032639&shareid=420754
      * http(s)://pan.baidu.com/wap/init?surl=pMi4xab
      * http(s)://pan.baidu.com/share/init?surl=pMi4xab
      * http(s)://pan.baidu.com/s/1i3iQY48

    有三种返回值: (need_pwd, surl), (need_pwd, surl, uk, shareid)和None

    如果共享文件需要输入密码, 就会将need_pwd设为True
    如果链接属于第三种或第四种, 返回(True, surl), 需要验证密码后才能提取uk和shareid
    如果是其他链接, 返回(need_pwd, surl, uk, shareid)
    如果失败, 就返回None

    '''
    def parse_share_uk(content):
        uk_reg = re.compile(',"uk":(\d+),')
        shareid_reg = re.compile(',"shareid":(\d+),')
        uk_match = uk_reg.search(content)
        shareid_match = shareid_reg.search(content)
        if uk_match and shareid_match:
            return uk_match.group(1), shareid_match.group(1)
        else:
            return None, None

    def parse_share_uk_from_url(url):
        uk_reg = re.compile('uk=(\d+)')
        uk_match = uk_reg.search(url)
        shareid_reg = re.compile('shareid=(\d+)')
        shareid_match = shareid_reg.search(url)
        if not uk_match or not shareid_match:
            return '', ''
        uk = uk_match.group(1)
        shareid = shareid_match.group(1)
        return uk, shareid

    def parse_surl(url):
        surl_reg1 = re.compile('surl=(.+)')
        surl_match1 = surl_reg1.search(url)
        surl_reg2 = re.compile('/s/(.+)')
        surl_match2 = surl_reg2.search(url)
        if surl_match1:
            return surl_match1.group(1)
        elif surl_match2:
            return surl_match2.group(1)
        else:
            return None

    # 处理重定向
    MAX_REDIRECT = 5
    for i in range(0, MAX_REDIRECT):
        req = net.urlopen_without_redirect(url,
                                           headers={
                                               'Cookie':
                                               cookie.header_output(),
                                           })
        if req:
            if (req.status == 301
                    or req.status == 302) and req.headers.get('Location'):
                url = req.headers.get('Location')
                continue
        break

    # 处理加密链接
    if url.find('share/init') > -1 or url.find('wap/init') > -1:
        if url.find('init?surl') > -1:
            surl = parse_surl(url)
            return True, surl
        else:
            uk, shareid = parse_share_uk_from_url(url)
            return True, None, uk, shareid

    # 处理短链接
    if url.startswith('http://pan.baidu.com/s/') or url.startswith(
            'https://pan.baidu.com/s/'):
        surl = parse_surl(url)
        req = net.urlopen(url, headers={
            'Cookie': cookie.header_output(),
        })
        if req:
            uk, shareid = parse_share_uk(req.data.decode())
            return False, surl, uk, shareid

    # 处理其他链接
    surl = parse_surl(url)
    uk, shareid = parse_share_uk_from_url(url)
    return False, surl, uk, shareid