def access_shared(self, shared_url: str, password: str, vcode_str: str, vcode: str): """Pass password to the session WARNING: this method is not threadsafe. """ url = "https://pan.baidu.com/share/verify" init_url = self.shared_init_url(shared_url) params = { "surl": init_url.split("surl=")[-1], "t": str(now_timestamp() * 1000), "channel": "chunlei", "web": "1", "bdstoken": "null", "clienttype": "0", } data = { "pwd": password, "vcode": vcode, "vcode_str": vcode_str, } hdrs = dict(PAN_HEADERS) hdrs["Referer"] = init_url resp = self._request(Method.Post, url, headers=hdrs, params=params, data=data) # These cookies must be included through all sub-processes self._cookies_update(resp.cookies.get_dict()) return resp.json()
def combine_slices( self, slice_md5s: List[str], remotepath: str, local_ctime: Optional[int] = None, local_mtime: Optional[int] = None, ondup="overwrite", ): url = PcsNode.File.url() params = { "method": "createsuperfile", "path": remotepath, "ondup": ondup, "BDUSS": self._bduss, } ntp = now_timestamp() data = { "param": dump_json({"block_list": slice_md5s}), "local_ctime": str(local_ctime or ntp), "local_mtime": str(local_mtime or ntp), } resp = self._request(Method.Post, url, params=params, data=data) return resp.json()
def access_shared(self, shared_url: str, password: str, vcode_str: str, vcode: str): """Pass password to the session""" url = "https://pan.baidu.com/share/verify" init_url = self.shared_init_url(shared_url) params = { "surl": init_url.split("surl=")[-1], "t": str(now_timestamp() * 1000), "channel": "chunlei", "web": "1", "bdstoken": "null", "clienttype": "0", } data = { "pwd": password, "vcode": vcode, "vcode_str": vcode_str, } hdrs = dict(PAN_HEADERS) hdrs["Referer"] = init_url resp = self._request(Method.Post, url, headers=hdrs, params=params, data=data) info = resp.json() err = parse_errno(info.get("errno", 0), str(info)) if err: raise err
def rapid_upload_file( self, slice_md5: str, content_md5: str, content_crc32: int, # not needed io_len: int, remotepath: str, local_ctime: Optional[int] = None, local_mtime: Optional[int] = None, ondup="overwrite", ): """Rapid Upload File slice_md5 (32 bytes): the md5 of pre 256KB of content. content_md5 (32 bytes): the md5 of total content. content_crc32 (int): the crc32 of total content (Not Needed), if content_crc32 is 0, the params of the api will be ignored. io_len (int): the length of total content. remotepath (str): the absolute remote path to save the content. """ assert remotepath.startswith( "/"), "`remotepath` must be an absolute path" url = PcsNode.File.url() params = { "method": "rapidupload", "BDUSS": self._bduss, } ntp = now_timestamp() data = { "path": remotepath, "content-length": io_len, "content-md5": content_md5, "slice-md5": slice_md5, "content-crc32": content_crc32, "local_ctime": str(local_ctime or ntp), "local_mtime": str(local_mtime or ntp), "ondup": ondup, } # Not needed if content_crc32 == 0: del data["content-crc32"] resp = self._request(Method.Post, url, params=params, data=data) return resp.json()
def tieba_user_info(self, user_id: int): params = f"has_plist=0&need_post_count=1&rn=1&uid={user_id}" params += "&sign=" + calu_md5( params.replace("&", "") + "tiebaclient!!!") url = "http://c.tieba.baidu.com/c/u/user/profile?" + params headers = { "Content-Type": "application/x-www-form-urlencoded", "Cookie": "ka=open", "net": "1", "User-Agent": "bdtb for Android 6.9.2.1", "client_logid": str(now_timestamp() * 1000), "Connection": "Keep-Alive", } resp = requests.get(url, headers=headers, params=None) return resp.json()
def download_link(self, remotepath: str, pcs: bool = False) -> Optional[str]: if pcs: return ( "http://c.pcs.baidu.com/rest/2.0/pcs/file" f"?method=download&app_id={PCS_APP_ID}&path={quote_plus(remotepath)}" "&ver=2.0&clienttype=1") bduss = self._bduss uid = str(self._user_id) or "" devuid = "0|" + calu_md5(bduss).upper() enc = calu_sha1(bduss) while True: timestamp = str(now_timestamp() * 1000) rand = calu_sha1(enc + uid + "ebrcUYiuxaZv2XGu7KIYKxUrqfnOfpDF" + timestamp + devuid) url = PcsNode.File.url() params = { "method": "locatedownload", "ver": "2", "path": remotepath, "time": timestamp, "rand": rand, "devuid": devuid, } resp = self._request(Method.Get, url, params=params) # Error: "user is not authorized" # This error occurs when the method is called by too many times if not resp.ok: time.sleep(2) continue info = resp.json() # This error is gotten when remote path is blocked if info.get("host") == "issuecdn.baidupcs.com": return None if not info.get("urls"): return None else: return info["urls"][0]["url"]
def user_info(self): bduss = self._bduss timestamp = str(now_timestamp()) model = get_phone_model(bduss) phoneIMEIStr = sum_IMEI(bduss) data = { "bdusstoken": bduss + "|null", "channel_id": "", "channel_uid": "", "stErrorNums": "0", "subapp_type": "mini", "timestamp": timestamp + "922", } data["_client_type"] = "2" data["_client_version"] = "7.0.0.0" data["_phone_imei"] = phoneIMEIStr data["from"] = "mini_ad_wandoujia" data["model"] = model data["cuid"] = ( calu_md5(bduss + "_" + data["_client_version"] + "_" + data["_phone_imei"] + "_" + data["from"]).upper() + "|" + phoneIMEIStr[::-1]) data["sign"] = calu_md5( "".join([k + "=" + data[k] for k in sorted(data.keys())]) + "tiebaclient!!!").upper() headers = { "Content-Type": "application/x-www-form-urlencoded", "Cookie": "ka=open", "net": "1", "User-Agent": "bdtb for Android 6.9.2.1", "client_logid": timestamp + "416", "Connection": "Keep-Alive", } resp = requests.post("http://tieba.baidu.com/c/s/login", headers=headers, data=data) return resp.json()