def list_share_files(cookie, tokens, uk, shareid, dirname, page=1): '''列举出用户共享的某一个目录中的文件信息 这个对所有用户都有效 uk - user key shareid - 共享文件的ID值 dirname - 共享目录, 如果dirname为None, 说明这有可能是一个单独共享的文件, 这里, 需要调用list_share_single_file() ''' if not dirname: return list_share_single_file(cookie, tokens, uk, shareid) url = ''.join([ const.PAN_URL, 'share/list?channel=chunlei&clienttype=0&web=1&num=50', '&t=', util.timestamp(), '&page=', str(page), '&dir=', encoder.encode_uri_component(dirname), '&t=', util.latency(), '&shareid=', shareid, '&order=time&desc=1', '&uk=', uk, '&_=', util.timestamp(), '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={ 'Cookie': cookie.header_output(), 'Referer': const.SHARE_REFERER, }) if req: content = req.data info = json.loads(content.decode()) if info['errno'] == 0: return info['list'] return list_share_single_file(cookie, tokens, uk, shareid)
def list_trash(cookie, tokens, path='/', page=1, num=100): '''获取回收站的信息. path - 目录的绝对路径, 默认是根目录 page - 页码, 默认是第一页 num - 每页有多少个文件, 默认是100个. 回收站里面的文件会被保存10天, 10天后会自动被清空. 回收站里面的文件不占用用户的存储空间. ''' url = ''.join([ const.PAN_API_URL, 'recycle/list?channel=chunlei&clienttype=0&web=1&app_id=250528', '&num=', str(num), '&t=', util.timestamp(), '&dir=', encoder.encode_uri_component(path), '&t=', util.latency(), '&order=time&desc=1', '&_=', util.timestamp(), '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def list_trash(cookie, tokens, path='/', page=1, num=100): '''获取回收站的信息. path - 目录的绝对路径, 默认是根目录 page - 页码, 默认是第一页 num - 每页有多少个文件, 默认是100个. 回收站里面的文件会被保存10天, 10天后会自动被清空. 回收站里面的文件不占用用户的存储空间. ''' url = ''.join([ const.PAN_API_URL, 'recycle/list?channel=chunlei&clienttype=0&web=1', '&num=', str(num), '&t=', util.timestamp(), '&dir=', encoder.encode_uri_component(path), '&t=', util.latency(), '&order=time&desc=1', '&_=', util.timestamp(), '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def list_share_path(cookie, tokens, uk, path, share_id, page): '''列举出用户共享的某一个目录中的文件信息 uk - user key path - 共享目录 share_id - 共享文件的ID值 ''' url = ''.join([ const.PAN_URL, 'share/list?channel=chunlei&clienttype=0&web=1&num=100', '&t=', util.timestamp(), '&page=', str(page), '&dir=', encoder.encode_uri_component(path), '&t=', util.latency(), '&shareid=', share_id, '&order=time&desc=1', '&uk=', uk, '&_=', util.timestamp(), '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={ 'Cookie': cookie.header_output(), 'Referer': const.SHARE_REFERER, }) if req: content = req.data return json.loads(content.decode()) else: return None
def cloud_query_sinfo(cookie, tokens, source_path): '''获取网盘中种子的信息, 比如里面的文件名, 文件大小等. source_path - BT种子的绝对路径. ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl?channel=chunlei', '&clienttype=', const.PC_CLIENT_TYPE, '&web=1', '&method=query_sinfo&app_id=250528', '&bdstoken=', tokens['bdstoken'], '&source_path=', encoder.encode_uri_component(source_path), '&type=2', '&t=', util.timestamp(), ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def get_category(cookie, tokens, category, page=1): '''获取一个分类中的所有文件信息, 比如音乐/图片 目前的有分类有: 视频 - 1 音乐 - 2 图片 - 3 文档 - 4 应用 - 5 其它 - 6 BT种子 - 7 ''' timestamp = util.timestamp() url = ''.join([ const.PAN_API_URL, 'categorylist?channel=chunlei&clienttype=0&web=1', '&category=', str(category), '&pri=-1&num=100', '&t=', timestamp, '&page=', str(page), '&order=time&desc=1', '&_=', timestamp, '&bdstoken=', cookie.get('STOKEN').value, ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def restore_trash(cookie, tokens, fidlist): '''从回收站中还原文件/目录. fildlist - 要还原的文件/目录列表, fs_id. ''' url = ''.join([ const.PAN_API_URL, 'recycle/restore?channel=chunlei&clienttype=0&web=1&app_id=250528', '&t=', util.timestamp(), '&bdstoken=', tokens['bdstoken'], ]) data = 'fidlist=' + encoder.encode_uri_component(json.dumps(fidlist)) req = net.urlopen(url, headers={ 'Cookie': cookie.header_output(), 'Content-type': const.CONTENT_FORM_UTF8, }, data=data.encode()) if req: content = req.data return json.loads(content.decode()) else: return None
def list_dir(cookie, tokens, path, page=1, num=100): '''得到一个目录中的所有文件的信息(最多100条记录).''' timestamp = util.timestamp() url = ''.join([ const.PAN_API_URL, 'list?channel=chunlei&clienttype=0&web=1&app_id=250528', '&num=', str(num), '&t=', timestamp, '&page=', str(page), '&dir=', encoder.encode_uri_component(path), '&t=', util.latency(), '&order=time&desc=1', '&_=', timestamp, '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={ 'Content-type': const.CONTENT_FORM_UTF8, 'Cookie': cookie.sub_output('BAIDUID', 'BDUSS', 'PANWEB', 'cflag', 'SCRC', 'STOKEN'), }) if req: content = req.data return json.loads(content.decode()) else: return None
def cloud_cancel_task(cookie, tokens, task_id): print( '/usr/local/lib/python3.4/dist-packages/bcloud/pcs.py:cloud_cancel_task 1088' ) '''取消离线下载任务. task_id - 之前建立离线下载任务时的task id, 也可以从cloud_list_task()里 获取. ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl', '?bdstoken=', tokens['bdstoken'], '&task_id=', str(task_id), '&method=cancel_task&app_id=250528', '&t=', util.timestamp(), '&channel=chunlei&clienttype=0&web=1', ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def cloud_delete_task(cookie, tokens, task_id): '''删除一个离线下载任务, 不管这个任务是否已完成下载. 同时还会把它从下载列表中删除. ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl', '?bdstoken=', tokens['bdstoken'], '&task_id=', str(task_id), '&method=delete_task&app_id=250528', '&t=', util.timestamp(), '&channel=chunlei&clienttype=', const.PC_CLIENT_TYPE, '&web=1', ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def list_my_share(cookie, tokens, page=1): url = ''.join([ const.PAN_URL, 'share/record?', '?page=', str(page), '&order=ctime&desc=1', '&_', util.timestamp(), '&bdstoken=', tokens['bdstoken'], '&channel=chunlei&clienttype=0&web=1&app_id=250528', ]) req = net.urlopen(url, headers={ 'Cookie': cookie.header_output(), 'Referer': const.SHARE_REFERER, }) if req: content = req.data return json.loads(content.decode()) else: return None
def get_category(cookie, tokens, category, page=1): '''获取一个分类中的所有文件信息, 比如音乐/图片 目前的有分类有: 视频 - 1 音乐 - 2 图片 - 3 文档 - 4 应用 - 5 其它 - 6 BT种子 - 7 ''' timestamp = util.timestamp() url = ''.join([ const.PAN_API_URL, 'categorylist?channel=chunlei&clienttype=0&web=1&app_id=250528&showempty=0', '&category=', str(category), '&num=100', '&t=', timestamp, '&page=', str(page), '&order=time&desc=1', '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def get_public_key(cookie, tokens): print( '/usr/local/lib/python3.4/dist-packages/bcloud/auth.py:get_public_key 180' ) '''获取RSA公钥, 这个用于加密用户的密码 返回的数据如下: {"errno":'0',"msg":'',"pubkey":'-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDk\/ufXg3IBW8+h5i8L8NoXUzcN\nMeKrh4zEupGBkyrURIPUXKDFLWjrv4n2j3RpMZ8GQn\/ETcfoIHGBoCUKJWcfcvmi\nG+OkYeqT6zyJasF0OlKesKfz0fGogMtdCQ6Kqq7X2vrzBPL+4SNU2wgU31g\/tVZl\n3zy5qAsBFkC70vs5FQIDAQAB\n-----END PUBLIC KEY-----\n',"key":'lwCISJnvs7HRNCTxpX7vi25bV9YslF2J'} ''' url = ''.join([ const.PASSPORT_BASE, 'v2/getpublickey', '?token=', tokens['token'], '&tpl=pp&apiver=v3&tt=', util.timestamp(), ]) headers = { 'Cookie': cookie.header_output(), 'Referer': const.REFERER, } req = net.urlopen(url, headers=headers) if req: data = req.data return util.json_loads_single(req.data.decode()) return None
def get_user_info(tokens, uk): print( '/usr/local/lib/python3.4/dist-packages/bcloud/pcs.py:get_user_info 55' ) '''获取用户的部分信息. 比如头像, 用户名, 自我介绍, 粉丝数等. 这个接口可用于查询任何用户的信息, 只要知道他/她的uk. ''' url = ''.join([ const.PAN_URL, 'pcloud/user/getinfo?channel=chunlei&clienttype=0&web=1', '&bdstoken=', tokens['bdstoken'], '&query_uk=', uk, '&t=', util.timestamp(), ]) req = net.urlopen(url) if req: info = json.loads(req.data.decode()) if info and info['errno'] == 0: return info['user_info'] return None
def cloud_delete_task(cookie, tokens, task_id): print( '/usr/local/lib/python3.4/dist-packages/bcloud/pcs.py:cloud_delete_task 1110' ) '''删除一个离线下载任务, 不管这个任务是否已完成下载. 同时还会把它从下载列表中删除. ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl', '?bdstoken=', tokens['bdstoken'], '&task_id=', str(task_id), '&method=delete_task&app_id=250528', '&t=', util.timestamp(), '&channel=chunlei&clienttype=0&web=1', ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def cloud_list_task(cookie, tokens, start=0): '''获取当前离线下载的任务信息 start - 从哪个任务开始, 从0开始计数, 会获取这50条任务信息 ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl?channel=chunlei&clienttype=', const.PC_CLIENT_TYPE, '&web=1', '&bdstoken=', tokens['bdstoken'], '&need_task_info=1&status=255', '&start=', str(start), '&limit=50&method=list_task&app_id=250528', '&t=', util.timestamp(), ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def list_share(cookie, tokens, uk, page=1): '''获取用户已经共享的所有文件的信息 uk - user key page - 页数, 默认为第一页. num - 一次性获取的共享文件的数量, 默认为100个. ''' num = 100 start = 100 * (page - 1) url = ''.join([ const.PAN_URL, 'pcloud/feed/getsharelist?', '&t=', util.timestamp(), '&categor=0&auth_type=1&request_location=share_home', '&start=', str(start), '&limit=', str(num), '&query_uk=', str(uk), '&channel=chunlei&clienttype=0&web=1&app_id=250528', '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={ 'Cookie': cookie.header_output(), 'Referer': const.SHARE_REFERER, }) if req: content = req.data return json.loads(content.decode()) else: return None
def cloud_cancel_task(cookie, tokens, task_id): '''取消离线下载任务. task_id - 之前建立离线下载任务时的task id, 也可以从cloud_list_task()里 获取. ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl', '?bdstoken=', tokens['bdstoken'], '&task_id=', str(task_id), '&method=cancel_task&app_id=250528', '&t=', util.timestamp(), '&channel=chunlei&clienttype=', const.PC_CLIENT_TYPE, '&web=1', ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def get_new_dv(): '''获得新的dv参数''' global dict1 global dict2 token = get_PageToken() loadTime = int(util.timestamp()) / 1000 dict1, dict2 = generate_dict(token) ## 用作生成dv参数的时间戳以及浏览器信息. ## 注意不能调整列表元素顺序. info = [ (0, "flashInfo", ""), # 空 (1, "mouseDown", ""), # 空 (2, "keyDown", ""), # 空 (3, "mouseMove", ""), # 空 (4, "version", 25), # 版本号 (5, "loadTime", loadTime), # 加载时间 (6, "browserInfo", get_browserInfo()), # 浏览器信息 (7, "token", token), # PageToken (8, "location", get_Location()), # 网页位置 (9, "screenInfo", get_screenInfo()), # 屏幕信息 (10, "powAnsw", ""), # 空 ] return token + '@' + generate_dv(info)
def check_login(cookie, tokens, username): '''进行登录验证, 主要是在服务器上验证这个帐户的状态. 如果帐户不存在, 或者帐户异常, 就不需要再进行最后一步的登录操作了. 这一步有可能需要输入验证码. 返回的信息如下: {"errInfo":{ "no": "0" }, "data": { "codeString" : "", "vcodetype" : "" }} ''' url = ''.join([ const.PASSPORT_URL, '?logincheck', '&token=', tokens['token'], '&tpl=mm&apiver=v3', '&tt=', util.timestamp(), '&username='******'&isphone=false', ]) headers={ 'Cookie': cookie.header_output(), 'Referer': const.REFERER, } req = net.urlopen(url, headers=headers) if req: ubi = req.headers.get_all('Set-Cookie') return ubi, json.loads(req.data.decode()) else: return None
def check_signin_vcode(cookie, tokens, verifycode, codeString): '''检查验证码是否正确.''' url = ''.join([ const.PASSPORT_BASE, 'v2/?checkvcode', '&token=', tokens['token'], '&tpl=netdisk&subpro=netdisk_web&apiver=v3', '&tt=', util.timestamp(), '&verifycode=', encoder.encode_uri_component(verifycode), '&codestring=', codeString, ]) headers = { 'Cookie': cookie.header_output(), 'Referer': const.REFERER, } req = net.urlopen(url, headers=headers) if req: content = req.data.decode() query = json.loads(content) return query['errInfo'] else: return None
def list_share(cookie, tokens, uk, page=1): '''获取用户已经共享的所有文件的信息 uk - user key page - 页数, 默认为第一页. num - 一次性获取的共享文件的数量, 默认为100个. ''' num = 100 start = 100 * (page - 1) url = ''.join([ const.PAN_URL, 'pcloud/feed/getsharelist?', '&t=', util.timestamp(), '&categor=0&auth_type=1&request_location=share_home', '&start=', str(start), '&limit=', str(num), '&query_uk=', str(uk), '&channel=chunlei&clienttype=0&web=1', '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={ 'Cookie': cookie.header_output(), 'Referer': const.SHARE_REFERER, }) if req: content = req.data return json.loads(content.decode()) else: return None
def refresh_signin_vcode(cookie, tokens, vcodetype): '''刷新验证码. vcodetype - 在调用check_login()时返回的vcodetype. ''' url = ''.join([ const.PASSPORT_BASE, 'v2/?reggetcodestr', '&token=', tokens['token'], '&tpl=pp&apiver=v3', '&tt=', util.timestamp(), '&fr=ligin', '&vcodetype=', encoder.encode_uri(vcodetype), ]) headers={ 'Cookie': cookie.header_output(), 'Referer': const.REFERER, } logger.debug('refresh vcode url: %s' % url) req = net.urlopen(url, headers=headers) if req: try: data = req.data.decode('gbk') logger.debug('refresh vcode: %s' % data) return json.loads(data) except ValueError: logger.error(traceback.format_exc()) return None
def get_token(cookie): '''获取一个页面访问的token, 这里需要之前得到的BAIDUID 这个cookie值 这个token的有效期还不确定. 返回的数据如下: {"errInfo":{"no": "0"}, "data": { "rememberedUserName" : "", "codeString" : "", "token" : "xxxxx", "cookie" : "1", "usernametype":"2", "spLogin" : "rate", "disable":"", "loginrecord":{ 'email':[ ], 'phone':[]} }} ''' url = ''.join([ const.PASSPORT_URL, '?getapi&tpl=pp&apiver=v3', '&tt=', util.timestamp(), '&class=login&logintype=basicLogin', ]) headers={ 'Cookie': cookie.header_output(), 'Accept': const.ACCEPT_HTML, 'Cache-control': 'max-age=0', } req = net.urlopen(url, headers=headers) if req: cookie = req.headers.get_all('Set-Cookie') content_obj = util.json_loads_single(req.data.decode()) if content_obj: return cookie, content_obj['data']['token'] return None
def cloud_query_task(cookie, tokens, task_ids): '''查询离线下载任务的信息, 比如进度, 是否完成下载等. 最好先用cloud_list_task() 来获取当前所有的任务, 然后调用这个函数来获取 某项任务的详细信息. task_ids - 一个list, 里面至少要有一个task_id, task_id 是一个字符串 ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl?method=query_task&app_id=250528', '&bdstoken=', tokens['bdstoken'], '&task_ids=', ','.join(task_ids), '&t=', util.timestamp(), '&channel=chunlei&clienttype=0&web=1', ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def post_login(cookie, token, username, password, rsakey, verifycode='', codestring=''): '''登录验证. password - 使用RSA加密后的base64字符串 rsakey - 与public_key相匹配的rsakey verifycode - 验证码, 默认为空 @return (status, info). 其中, status表示返回的状态: 0 - 正常, 这里, info里面存放的是auth_cookie -1 - 未知异常 4 - 密码错误 257 - 需要输入验证码, 此时info里面存放着(vcodetype, codeString)) ''' url = const.PASSPORT_LOGIN data = ''.join([ 'staticpage=https%3A%2F%2Fpassport.baidu.com%2Fstatic%2Fpasspc-account%2Fhtml%2Fv3Jump.html&charset=UTF-8', '&token=', token, '&tpl=pp&subpro=&apiver=v3', '&tt=', util.timestamp(), '&codestring=', codestring, '&safeflg=0&u=https%3A%2F%2Fpassport.baidu.com%2F&isPhone=', '&quick_user=0&logintype=basicLogin&logLoginType=pc_loginBasic&idc=', '&loginmerge=true', '&username='******'&password='******'&verifycode=', verifycode, '&mem_pass=on', '&rsakey=', rsakey, '&crypttype=12&ppui_logintime=',get_ppui_logintime(), '&callback=parent.bd__pcbs__m8g1kg', ]) logger.debug('auth.post_login: %s' % data) headers={ 'Cookie': cookie.header_output(), 'Content-Type': const.CONTENT_FORM, 'Referer': const.REFERER, 'Connection': 'Keep-Alive', } req = net.urlopen(url, headers=headers, data=data.encode()) if req: auth_cookie = req.headers.get_all('Set-Cookie') resp_content= req.data.decode() match = re.findall('"(err_no[^"]+)"', resp_content) if len(match) != 1: return (-1, None) query = dict(urllib.parse.parse_qsl(match[0])) err_no = int(query.get('err_no', '-1')) if err_no == 0: return (0, auth_cookie) if err_no != 257: return (err_no, None) vcodetype = query.get('vcodetype', '') codeString = query.get('codeString', '') if vcodetype and codeString: return (257, (vcodetype, codeString)) return (-1, None) else: return (-1, None) return (-1, None)
def cloud_add_bt_task(cookie, tokens, source_url, save_path, selected_idx, file_sha1='', vcode='', vcode_input=''): '''新建一个BT类的离线下载任务, 包括magent磁链. source_path - BT种子所在的绝对路径 save_path - 下载的文件要存放到的目录 selected_idx - BT种子中, 包含若干个文件, 这里, 来指定要下载哪些文件, 从1开始计数. file_sha1 - BT种子的sha1值, 如果是magent的话, 这个sha1值可以为空 vcode - 验证码的vcode vcode_input - 用户输入的四位验证码 ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl?channel=chunlei', '&clienttype=', const.PC_CLIENT_TYPE, '&web=1', '&bdstoken=', tokens['bdstoken'], ]) type_ = '2' url_type = 'source_path' if source_url.startswith('magnet:'): type_ = '4' url_type = 'source_url' if not save_path.endswith('/'): save_path = save_path + '/' data = [ 'method=add_task&app_id=250528', '&file_sha1=', file_sha1, '&save_path=', encoder.encode_uri_component(save_path), '&selected_idx=', ','.join(str(i) for i in selected_idx), '&task_from=1', '&t=', util.timestamp(), '&', url_type, '=', encoder.encode_uri_component(source_url), '&type=', type_ ] if vcode: data.append('&input=') data.append(vcode_input) data.append('&vcode=') data.append(vcode) data = ''.join(data) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}, data=data.encode()) if req: content = req.data return json.loads(content.decode()) else: return None
def get_quota(cookie, tokens): '''获取当前的存储空间的容量信息.''' url = ''.join([ const.PAN_API_URL, 'quota?channel=chunlei&clienttype=0&web=1', '&t=', util.timestamp(), ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def get_quota(cookie, tokens): print('/usr/local/lib/python3.4/dist-packages/bcloud/pcs.py:get_quota 28') '''获取当前的存储空间的容量信息.''' url = ''.join([ const.PAN_API_URL, 'quota?channel=chunlei&clienttype=0&web=1', '&t=', util.timestamp(), ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def cloud_clear_task(cookie, tokens): '''清空离线下载的历史(已经完成或者取消的).''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl?method=clear_task&app_id=250528', '&channel=chunlei&clienttype=0&web=1', '&t=', util.timestamp(), '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def get_BAIDUID(): '''获取一个cookie - BAIDUID. 这里, 我们访问百度首页, 返回的response header里面有我们需要的cookie ''' url = ''.join([ const.PASSPORT_URL, '?getapi&tpl=mn&apiver=v3', '&tt=', util.timestamp(), '&class=login&logintype=basicLogin', ]) req = net.urlopen(url, headers={'Referer': ''}) if req: return req.headers.get_all('Set-Cookie') else: return None
def cloud_add_bt_task(cookie, tokens, source_url, save_path, selected_idx, file_sha1='', vcode='', vcode_input=''): '''新建一个BT类的离线下载任务, 包括magent磁链. source_path - BT种子所在的绝对路径 save_path - 下载的文件要存放到的目录 selected_idx - BT种子中, 包含若干个文件, 这里, 来指定要下载哪些文件, 从1开始计数. file_sha1 - BT种子的sha1值, 如果是magent的话, 这个sha1值可以为空 vcode - 验证码的vcode vcode_input - 用户输入的四位验证码 ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl?channel=chunlei&clienttype=0&web=1', '&bdstoken=', tokens['bdstoken'], ]) type_ = '2' url_type = 'source_path' if source_url.startswith('magnet:'): type_ = '4' url_type = 'source_url' if not save_path.endswith('/'): save_path = save_path + '/' data = [ 'method=add_task&app_id=250528', '&file_sha1=', file_sha1, '&save_path=', encoder.encode_uri_component(save_path), '&selected_idx=', ','.join(str(i) for i in selected_idx), '&task_from=1', '&t=', util.timestamp(), '&', url_type, '=', encoder.encode_uri_component(source_url), '&type=', type_ ] if vcode: data.append('&input=') data.append(vcode_input) data.append('&vcode=') data.append(vcode) data = ''.join(data) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}, data=data.encode()) if req: content = req.data return json.loads(content.decode()) else: return None
def list_inbox(cookie, tokens, start=0, limit=20): '''获取收件箱里的文件信息.''' url = ''.join([ const.PAN_URL, 'inbox/object/list?type=1', '&start=', str(start), '&limit=', str(limit), '&_=', util.timestamp(), '&channel=chunlei&clienttype=0&web=1', '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def clear_trash(cookie, tokens): '''清空回收站, 将里面的所有文件都删除.''' url = ''.join([ const.PAN_API_URL, 'recycle/clear?channel=chunlei&clienttype=0&web=1', '&t=', util.timestamp(), '&bdstoken=', tokens['bdstoken'], ]) # 使用POST方式发送命令, 但data为空. req = net.urlopen(url, headers={ 'Cookie': cookie.header_output(), }, data=''.encode()) if req: content = req.data return json.loads(content.decode()) else: return None
def get_avatar(cookie): '''获取当前用户的头像''' def parse_avatar(content): img_sel = CSS('img.account-avatar-show') tree = html.fromstring(content) img_elems = img_sel(tree) if len(img_elems) == 1: return img_elems[0].attrib.get('src', None) else: return None url = 'http://passport.baidu.com/center?_t=' + util.timestamp() req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: return parse_avatar(req.data.decode()) else: return None
def get_user_info(tokens, uk): '''获取用户的部分信息. 比如头像, 用户名, 自我介绍, 粉丝数等. 这个接口可用于查询任何用户的信息, 只要知道他/她的uk. ''' url = ''.join([ const.PAN_URL, 'pcloud/user/getinfo?channel=chunlei&clienttype=0&web=1', '&bdstoken=', tokens['bdstoken'], '&query_uk=', uk, '&t=', util.timestamp(), ]) req = net.urlopen(url) if req: info = json.loads(req.data.decode()) if info and info['errno'] == 0: return info['user_info'] return None
def get_BAIDUID(): print( '/usr/local/lib/python3.4/dist-packages/bcloud/auth.py:get_BAIDUID 28') '''获取一个cookie - BAIDUID. 这里, 我们访问百度首页, 返回的response header里面有我们需要的cookie ''' url = ''.join([ const.PASSPORT_URL, '?getapi&tpl=mn&apiver=v3', '&tt=', util.timestamp(), '&class=login&logintype=basicLogin', ]) req = net.urlopen(url, headers={'Referer': ''}) if req: return req.headers.get_all('Set-Cookie') else: return None
def get_public_key(cookie, tokens): '''获取RSA公钥, 这个用于加密用户的密码 返回的数据如下: {"errno":'0',"msg":'',"pubkey":'-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDk\/ufXg3IBW8+h5i8L8NoXUzcN\nMeKrh4zEupGBkyrURIPUXKDFLWjrv4n2j3RpMZ8GQn\/ETcfoIHGBoCUKJWcfcvmi\nG+OkYeqT6zyJasF0OlKesKfz0fGogMtdCQ6Kqq7X2vrzBPL+4SNU2wgU31g\/tVZl\n3zy5qAsBFkC70vs5FQIDAQAB\n-----END PUBLIC KEY-----\n',"key":'lwCISJnvs7HRNCTxpX7vi25bV9YslF2J'} ''' url = ''.join([ const.PASSPORT_BASE, 'v2/getpublickey', '?token=', tokens['token'], '&tpl=pp&apiver=v3&tt=', util.timestamp(), ]) headers={ 'Cookie': cookie.header_output(), 'Referer': const.REFERER, } req = net.urlopen(url, headers=headers) if req: data = req.data return util.json_loads_single(req.data.decode()) return None
def cloud_list_task(cookie, tokens, start=0): '''获取当前离线下载的任务信息 start - 从哪个任务开始, 从0开始计数, 会获取这50条任务信息 ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl?channel=chunlei&clienttype=0&web=1', '&bdstoken=', tokens['bdstoken'], '&need_task_info=1&status=255', '&start=', str(start), '&limit=50&method=list_task&app_id=250528', '&t=', util.timestamp(), ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def cloud_query_sinfo(cookie, tokens, source_path): '''获取网盘中种子的信息, 比如里面的文件名, 文件大小等. source_path - BT种子的绝对路径. ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl?channel=chunlei&clienttype=0&web=1', '&method=query_sinfo&app_id=250528', '&bdstoken=', tokens['bdstoken'], '&source_path=', encoder.encode_uri_component(source_path), '&type=2', '&t=', util.timestamp(), ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def get_token(cookie): '''获取一个页面访问的token, 这里需要之前得到的BAIDUID 这个cookie值 这个token的有效期还不确定. ''' url = ''.join([ const.PASSPORT_URL, '?getapi&tpl=mn&apiver=v3', '&tt=', util.timestamp(), '&class=login&logintype=dialogLogin', #'&callback=bd__cbs__d1ypgy', ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data content = content.decode().replace("'", '"') content_obj = json.loads(content) return content_obj['data']['token'] else: return None
def cloud_delete_task(cookie, tokens, task_id): '''删除一个离线下载任务, 不管这个任务是否已完成下载. 同时还会把它从下载列表中删除. ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl', '?bdstoken=', tokens['bdstoken'], '&task_id=', str(task_id), '&method=delete_task&app_id=250528', '&t=', util.timestamp(), '&channel=chunlei&clienttype=0&web=1', ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def refresh_signin_vcode(cookie, token, vcodetype): '''刷新验证码. vcodetype - 在调用check_login()时返回的vcodetype. ''' url = ''.join([ const.PASSPORT_BASE, 'v2/?reggetcodestr', '&token=', token, '&tpl=pp&apiver=v3', '&tt=', util.timestamp(), '&fr=ligin', '&vcodetype=', vcodetype, ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: try: return json.loads(req.data.decode('gbk')) except ValueError as e: print('Error occurs in refresh_signin_vcode()', e) return None
def cloud_cancel_task(cookie, tokens, task_id): '''取消离线下载任务. task_id - 之前建立离线下载任务时的task id, 也可以从cloud_list_task()里 获取. ''' url = ''.join([ const.PAN_URL, 'rest/2.0/services/cloud_dl', '?bdstoken=', tokens['bdstoken'], '&task_id=', str(task_id), '&method=cancel_task&app_id=250528', '&t=', util.timestamp(), '&channel=chunlei&clienttype=0&web=1', ]) req = net.urlopen(url, headers={'Cookie': cookie.header_output()}) if req: content = req.data return json.loads(content.decode()) else: return None
def get_UBI(cookie, tokens): '''检查登录历史, 可以获得一个Cookie - UBI. 返回的信息类似于: {"errInfo":{ "no": "0" }, "data": {'displayname':['*****@*****.**']}} ''' url = ''.join([ const.PASSPORT_URL, '?loginhistory', '&token=', tokens['token'], '&tpl=pp&apiver=v3', '&tt=', util.timestamp(), ]) headers={ 'Cookie': cookie.header_output(), 'Referer': const.REFERER, } req = net.urlopen(url, headers=headers) if req: return req.headers.get_all('Set-Cookie') else: return None
def restore_trash(cookie, tokens, fidlist): '''从回收站中还原文件/目录. fildlist - 要还原的文件/目录列表, fs_id. ''' url = ''.join([ const.PAN_API_URL, 'recycle/restore?channel=chunlei&clienttype=0&web=1', '&t=', util.timestamp(), '&bdstoken=', tokens['bdstoken'], ]) data = 'fidlist=' + encoder.encode_uri_component(json.dumps(fidlist)) req = net.urlopen(url, headers={ 'Cookie': cookie.header_output(), 'Content-type': const.CONTENT_FORM_UTF8, }, data=data.encode()) if req: content = req.data return json.loads(content.decode()) else: return None
def list_dir(cookie, tokens, path, page=1, num=100): '''得到一个目录中的所有文件的信息(最多100条记录).''' timestamp = util.timestamp() url = ''.join([ const.PAN_API_URL, 'list?channel=chunlei&clienttype=0&web=1', '&num=', str(num), '&t=', timestamp, '&page=', str(page), '&dir=', encoder.encode_uri_component(path), '&t=', util.latency(), '&order=time&desc=1', '&_=', timestamp, '&bdstoken=', tokens['bdstoken'], ]) req = net.urlopen(url, headers={ 'Content-type': const.CONTENT_FORM_UTF8, 'Cookie': cookie.sub_output('BAIDUID', 'BDUSS', 'PANWEB', 'cflag'), }) if req: content = req.data return json.loads(content.decode()) else: return None
def post_login(cookie, tokens, username, password, rsakey, verifycode='', codestring=''): '''登录验证. password - 使用RSA加密后的base64字符串 rsakey - 与public_key相匹配的rsakey verifycode - 验证码, 默认为空 @return (status, info). 其中, status表示返回的状态: 0 - 正常, 这里, info里面存放的是auth_cookie -1 - 未知异常 4 - 密码错误 257 - 需要输入验证码, 此时info里面存放着(vcodetype, codeString)) ''' url = const.PASSPORT_LOGIN data = ''.join([ 'staticpage=https%3A%2F%2Fpassport.baidu.com%2Fstatic%2Fpasspc-account%2Fhtml%2Fv3Jump.html', '&charset=UTF-8', '&token=', tokens['token'], '&tpl=pp&subpro=&apiver=v3', '&tt=', util.timestamp(), '&codestring=', codestring, '&safeflg=0&u=http%3A%2F%2Fpassport.baidu.com%2F', '&isPhone=', '&quick_user=0&logintype=basicLogin&logLoginType=pc_loginBasic&idc=', '&loginmerge=true', '&username='******'&password='******'&verifycode=', verifycode, '&mem_pass=on', '&rsakey=', rsakey, '&crypttype=12', '&ppui_logintime=',get_ppui_logintime(), '&callback=parent.bd__pcbs__28g1kg', ]) headers={ 'Accept': const.ACCEPT_HTML, 'Cookie': cookie.sub_output('BAIDUID','HOSUPPORT', 'UBI'), 'Referer': const.REFERER, 'Connection': 'Keep-Alive', } req = net.urlopen(url, headers=headers, data=data.encode()) if req: content= req.data.decode() match = re.search('"(err_no[^"]+)"', content) if not match: return (-1, None) query = dict(urllib.parse.parse_qsl(match.group(1))) query['err_no'] = int(query['err_no']) err_no = query['err_no'] auth_cookie = req.headers.get_all('Set-Cookie') if err_no == 0: return (0, auth_cookie) # #!! not bind cellphone elif err_no == 18: return (0, auth_cookie) # 要输入验证码 elif err_no == 257: return (err_no, query) # 需要短信验证 elif err_no == 400031: return (err_no, query) else: return (err_no, None) else: return (-1, None) return (-1, None)