def download_link(self, remotepath: str, pcs: bool = False): if pcs: return { "errno": 0, "urls": [{ "url": ("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 "" timestamp = str(int(time.time() * 1000)) devuid = "0|" + calu_md5(bduss).upper() enc = calu_sha1(bduss) rand = calu_sha1(enc + uid + "ebrcUYiuxaZv2XGu7KIYKxUrqfnOfpDF" + timestamp + devuid) url = self._form_url(PcsNode.File, domain=PCS_BAIDU_COM) params = { "method": "locatedownload", "ver": "2", "path": remotepath, "time": timestamp, "rand": rand, "devuid": devuid, } resp = self._request(Method.Get, url, params=params) 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"]