def get_download_link(cookie, dlink): '''在下载之前, 要先获取最终的下载链接, 因为中间要进行一次302跳转. 这一步是为了得到最终的下载地址. 如果得不到最终的下载地址, 那下载速度就 会受到很大的限制. dlink - 在pcs_file里面的dlink项, 这个链接是一个临时下载链接. @return (red_url, request_id), red_url 是重定向后的URL, 如果获取失败, 就返回原来的dlink; request_id 是一个字符串, 用于下载文件时的认 证, 如果获取失败, 它的值就为空. ''' url = ''.join([ dlink, '&cflg=', 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, '') red_url = req.getheader('Location', url) req_id = req.getheader('x-pcs-request-id', '') return (red_url, req_id)
def get_download_link(cookie, tokens, path): '''在下载之前, 要先获取最终的下载链接. path - 一个文件的绝对路径. @return (red_url, request_id), red_url 是重定向后的URL, 如果获取失败, 就返回原来的dlink; request_id 是一个字符串, 用于下载文件时的认 证, 如果获取失败, 它的值就为空. ''' 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 = ''.join([ dlink, '&cflg=', 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)
def get_download_link(cookie, tokens, path): '''在下载之前, 要先获取最终的下载链接. path - 一个文件的绝对路径. @return (red_url, request_id), red_url 是重定向后的URL, 如果获取失败, 就返回原来的dlink; request_id 是一个字符串, 用于下载文件时的认 证, 如果获取失败, 它的值就为空. ''' 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 = ''.join([dlink, '&cflg=', 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)
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
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
def get_download_link(cookie, dlink): """在下载之前, 要先获取最终的下载链接, 因为中间要进行一次302跳转. 这一步是为了得到最终的下载地址. 如果得不到最终的下载地址, 那下载速度就 会受到很大的限制. dlink - 在pcs_file里面的dlink项, 这个链接是一个临时下载链接. @return (red_url, request_id), red_url 是重定向后的URL, 如果获取失败, 就返回原来的dlink; request_id 是一个字符串, 用于下载文件时的认 证, 如果获取失败, 它的值就为空. """ url = "".join([dlink, "&cflg=", 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, "") red_url = req.getheader("Location", url) req_id = req.getheader("x-pcs-request-id", "") return (red_url, req_id)