예제 #1
0
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)
예제 #2
0
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
예제 #3
0
파일: pcs.py 프로젝트: stepheny/bcloud-core
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 프로젝트: stepheny/bcloud-core
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
예제 #5
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
예제 #6
0
파일: pcs.py 프로젝트: stepheny/bcloud-core
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)