def invoker(obj, *args, **kwargs): # 必要参数 get_data = { 'app_id' : _APP_ID, 'channel' : 'chunlei', 'bdstoken' : obj.bdstoken, 'clienttype' : 0, 'web' : 1, '_': util.timstamp() } # 处理API预设参数 if preset: get_data.update(preset) # 处理传入的参数 call_args = inspect.getcallargs(func, obj, *args, **kwargs) for k,v in call_args.items(): if k == 'self' or v is None: continue get_data[k] = v # 处理需要post的参数 post_data = {} if post_field: for field in post_field: if not get_data.has_key(field): continue post_data[field] = get_data[field] del get_data[field] resp = obj._execute_request(url, get_data, post_data) result = json.load(resp) return result
def invoker(obj, *args, **kwargs): # 必要参数 get_data = { 'app_id' : _APP_ID, 'channel' : 'chunlei', 'bdstoken' : obj.bdstoken, 'clienttype' : 0, 'web' : 1, '_': util.timstamp(), 't': util.timstamp() } # 处理API预设参数 if preset: get_data.update(preset) # 处理传入的参数 call_args = inspect.getcallargs(func, obj, *args, **kwargs) for k,v in call_args.items(): if k == 'self' or v is None: continue if type(v) is unicode: v = v.encode('utf-8') get_data[k] = v # 处理需要post的参数 post_data = {} if post_field: for field in post_field: if not get_data.has_key(field): continue post_data[field] = get_data[field] del get_data[field] # 发送请求,出现网络问题时重试 retry = True while retry: try: resp = obj.execute_request(url, get_data, post_data) result = json.load(resp) retry = False except urllib2.HTTPError as he: result = json.load(he) if result.get('error_code', 0) == -19: # 下载验证码图片 vcode_file = obj.download_vcode_image(result['img']) post_data['vcode'] = result['vcode'] post_data['input'] = obj.vcode_handler(vcode_file) retry = True else: retry = False except: retry = True return result
def _login_check(self, token, account): ''' 登陆检查,访问该页面主要是为了获取cookie ''' cbs = 'bd__cbs__%s' % util.random_hex_str(6) url = 'https://passport.baidu.com/v2/api/?logincheck' query = [('token', token), ('tpl', 'netdisk'), ('apiver', 'v3'), ('tt', util.timstamp()), ('username', account), ('isphone', 'false'), ('callback', cbs)] self.execute_request(url, query)
def invoker(obj, *args, **kwargs): # 必要参数 get_data = { 'app_id': _APP_ID, 'channel': 'chunlei', 'bdstoken': obj.bdstoken, 'clienttype': 0, 'web': 1, '_': util.timstamp(), 't': util.timstamp() } # 处理API预设参数 if preset: get_data.update(preset) # 处理传入的参数 call_args = inspect.getcallargs(func, obj, *args, **kwargs) for k, v in call_args.items(): if k == 'self' or v is None: continue if type(v) is unicode: v = v.encode('utf-8') get_data[k] = v # 处理需要post的参数 post_data = {} if post_field: for field in post_field: if not get_data.has_key(field): continue post_data[field] = get_data[field] del get_data[field] # 发送请求,出现网络问题时重试 while True: try: resp = obj.execute_request(url, get_data, post_data) result = json.load(resp) return result except urllib2.HTTPError as he: result = json.load(he) raise APIException(func.func_name, result.get('error_code'), result) except: pass
def invoker(obj, *args, **kwargs): # 必要参数 get_data = { 'app_id' : _APP_ID, 'channel' : 'chunlei', 'bdstoken' : obj.bdstoken, 'clienttype' : 0, 'web' : 1, '_': util.timstamp(), 't': util.timstamp() } # 处理API预设参数 if preset: get_data.update(preset) # 处理传入的参数 call_args = inspect.getcallargs(func, obj, *args, **kwargs) for k,v in call_args.items(): if k == 'self' or v is None: continue if type(v) is unicode: v = v.encode('utf-8') get_data[k] = v # 处理需要post的参数 post_data = {} if post_field: for field in post_field: if not get_data.has_key(field): continue post_data[field] = get_data[field] del get_data[field] # 发送请求,出现网络问题时重试 while True: try: resp = obj.execute_request(url, get_data, post_data) result = json.load(resp) return result except urllib2.HTTPError as he: result = json.load(he) raise APIException(func.func_name, result.get('error_code'), result) except: pass
def _do_login(self, account, password, token, key_info): url = 'https://passport.baidu.com/v2/api/?login' form = { 'staticpage': 'http://pan.baidu.com/res/static/thirdparty/pass_v3_jump.html', 'charset': 'utf-8', 'tpl': 'netdisk', 'subpro': '', 'apiver': 'v3', 'tt': util.timstamp(), 'codestring': '', 'safeflg': '0', 'u': 'http://pan.baidu.com/', 'isPhone': 'false', 'quick_user': '******', 'logintype': 'basicLogin', 'logLoginType': 'pc_loginBasic', 'loginmerge': 'true', 'verifycode': '', 'mem_pass': '******', 'crypttype': '12', 'ppui_logintime': random.randint(5000, 10000), 'callback': 'parent.bd__cbs__%s' % util.random_hex_str(6), 'username': account, 'token': token, 'rsakey': key_info['key'] } # 使用rsa加密密码 pubkey = key_info['pubkey'].replace('-----BEGIN PUBLIC KEY-----\n', '').replace( '\n-----END PUBLIC KEY-----', '') pubkey = rsa.key.PublicKey.load_pkcs1_openssl_der( base64.decodestring(pubkey)) form['password'] = base64.b64encode(rsa.encrypt(password, pubkey)) # 发送登陆请求 resp = self.execute_request(url, None, form) # 解析登陆结果 body = resp.read() m = re.search('err_no=(\d+)&', body) if m is not None: err_no = int(m.group(1)) # err_no=18 表示需要手机认证,这个错误目前可忽略 if err_no != 0 and err_no != 18: raise APIException('login', err_no) else: raise APIException('login', -1) _logger.debug('login successed!')
def _get_public_key(self, token): ''' 获取加密密码用的rsa公钥 ''' cbs = 'bd__cbs__%s' % util.random_hex_str(6) url = 'https://passport.baidu.com/v2/getpublickey' query = [ ('token', token), ('tpl', 'netdisk'), ('apiver', 'v3'), ('tt', util.timstamp()), ('callback', cbs), ] resp = self.execute_request(url, query) result = (resp.read()[len(cbs)+1:-1]).replace('\'', '"') return json.loads(result)
def _login_check(self, token, account): ''' 登陆检查,访问该页面主要是为了获取cookie ''' cbs = 'bd__cbs__%s' % util.random_hex_str(6) url = 'https://passport.baidu.com/v2/api/?logincheck' query = [ ('token', token), ('tpl', 'netdisk'), ('apiver', 'v3'), ('tt', util.timstamp()), ('username', account), ('isphone', 'false'), ('callback', cbs) ] self.execute_request(url, query)
def _get_public_key(self, token): ''' 获取加密密码用的rsa公钥 ''' cbs = 'bd__cbs__%s' % util.random_hex_str(6) url = 'https://passport.baidu.com/v2/getpublickey' query = [ ('token', token), ('tpl', 'netdisk'), ('apiver', 'v3'), ('tt', util.timstamp()), ('callback', cbs), ] resp = self.execute_request(url, query) result = (resp.read()[len(cbs) + 1:-1]).replace('\'', '"') return json.loads(result)
def _get_login_token(self): ''' 获取登陆用token ''' # 需要先访问网盘首页,获得一个cookie self.execute_request('http://pan.baidu.com/') # 请求token cbs = 'bd__cbs__%s' % util.random_hex_str(6) url = 'https://passport.baidu.com/v2/api/?getapi' query = [('tpl', 'netdisk'), ('apiver', 'v3'), ('tt', util.timstamp()), ('class', 'login'), ('logintype', 'basicLogin'), ('callback', cbs)] resp = self.execute_request(url, query) result = (resp.read()[len(cbs) + 1:-1]).replace('\'', '"') result = json.loads(result) return result['data']['token']
def _do_login(self, account, password, token, key_info): url = 'https://passport.baidu.com/v2/api/?login' form = { 'staticpage' : 'http://pan.baidu.com/res/static/thirdparty/pass_v3_jump.html', 'charset' : 'utf-8', 'tpl' : 'netdisk', 'subpro' : '', 'apiver' : 'v3', 'tt' : util.timstamp(), 'codestring' : '', 'safeflg' : '0', 'u' : 'http://pan.baidu.com/', 'isPhone' : 'false', 'quick_user' : '0', 'logintype' : 'basicLogin', 'logLoginType' : 'pc_loginBasic', 'loginmerge' : 'true', 'verifycode' : '', 'mem_pass' : 'on', 'crypttype' : '12', 'ppui_logintime' : random.randint(5000, 10000), 'callback' : 'parent.bd__cbs__%s' % util.random_hex_str(6), 'username' : account, 'token' : token, 'rsakey' : key_info['key'] } # 使用rsa加密密码 pubkey = key_info['pubkey'].replace('-----BEGIN PUBLIC KEY-----\n', '').replace('\n-----END PUBLIC KEY-----', '') pubkey = rsa.key.PublicKey.load_pkcs1_openssl_der(base64.decodestring(pubkey)) form['password'] = base64.b64encode(rsa.encrypt(password, pubkey)) # 发送登陆请求 resp = self.execute_request(url, None, form) # 解析登陆结果 body = resp.read() m = re.search('err_no=(\d+)&', body) if m is not None: err_no = int(m.group(1)) # err_no=18 表示需要手机认证,这个错误目前可忽略 if err_no != 0 and err_no != 18: raise APIException('login', err_no) else: raise APIException('login', -1) _logger.debug('login successed!')
def _get_login_token(self): ''' 获取登陆用token ''' # 需要先访问网盘首页,获得一个cookie self.execute_request('http://pan.baidu.com/') # 请求token cbs = 'bd__cbs__%s' % util.random_hex_str(6) url = 'https://passport.baidu.com/v2/api/?getapi' query = [ ('tpl', 'netdisk'), ('apiver', 'v3'), ('tt', util.timstamp()), ('class', 'login'), ('logintype', 'basicLogin'), ('callback', cbs) ] resp = self.execute_request(url, query) result = (resp.read()[len(cbs)+1:-1]).replace('\'', '"') result = json.loads(result) return result['data']['token']