使用Alist网盘遍历文件并调用Aria2的API进行批量下载
Alist网盘遍历文件并调用Aria2的API进行批量下载,可以通过以下步骤实现: 1. 使用Python编写脚本,通过Alist API获取网盘中的文件列表。 2. 遍历文件列表,获取每个文件的下载链接。 3. 调用Aria2的API,将下载链接传递给Aria2进行批量下载。 4. 在Aria2中设置相关参数,如下载速度限制、并发数等。 5. 监控下载进度,确保所有文件都成功下载到本地。
importrequestsfromurllibimportparseimportjsonfromAria2_RPCimportAria2DownloadimporttimeclassAlistDownload:def__init__(self,url):self.headers={"Accept":"application/json,text/plain,*/*","User-Agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/109.0.0.0Safari/537.36","Content-Type":"application/json;charset=UTF-8","Accept-Encoding":"gzip,deflate","Accept-Language":"zh-CN,zh;q=0.9"}parseresult=parse.urlparse(url)scheme=parseresult.schemenetloc=parseresult.netlocpath=parse.unquote(parseresult.path)self.host=f"{scheme}://{netloc}"self.aria2=Aria2Download()self.get_list(path)print("遍历完成")defpost(self,url,data)->(bool,dict):req_json={}error_number=0whileTrue:status_code=0try:req=requests.post(url=url,data=json.dumps(data),headers=self.headers,timeout=15)status_code=req.status_codereq_json=req.json()req.close()except:passifstatus_code==200:breakeliferror_number>2:breakelse:error_number+=1time.sleep(1)ifstatus_code==200:returnTrue,req_jsonelse:returnFalse,req_jsondefget_list(self,path):url=self.host+"/api/fs/list"data={"path":path,"password":"","page":1,"per_page":0,"refresh":False}file_list=[]error_number=0whileTrue:req_type,req_json=self.post(url=url,data=data)ifreq_typeisFalse:returnelifreq_json.get("code")==200:breakeliferror_number>2:breakelse:print(req_json)error_number+=1time.sleep(2)ifreq_json.get("data")isNone:returncontent=req_json.get("data")["content"]ifcontentisNone:returnforfile_infoincontent:iffile_info["is_dir"]isTrue:file_download_url=path+"/"+file_info["name"]print("dir",file_download_url)file_list.append({"is_dir":True,"path":file_download_url})else:file_download_url=self.host+"/d"+path+"/"+file_info["name"]print("file",file_download_url)sign=file_info.get("sign")ifsignisnotNone:file_download_url=file_download_url+"?sign="+signfile_list.append({"is_dir":False,"url":file_download_url,"path":path,"file":file_info["name"]})forfileinfile_list:iffile["is_dir"]isTrue:self.get_list(file["path"])else:#D:\download文件下载的存储地址self.aria2.addUri(file["url"],r"D:\download"+file["path"],file["file"])passif__name__=='__main__':AlistDownload("http://此处填入Alist网盘的地址")