Created
May 15, 2015 06:45
-
-
Save fangdingjun/4aa20013f433d0e61338 to your computer and use it in GitHub Desktop.
使用aria2替换bypy的下载
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import urllib2 | |
import urllib | |
import json | |
import xmlrpclib | |
class PcsLib(object): | |
def __init__(self, access_token, timeout = 10, basepath= "/apps/bypy"): | |
self.access_token = access_token | |
self.timeout = timeout | |
self.basepath = basepath | |
def get_filelist(self, d): | |
req_url="https://pcs.baidu.com/rest/2.0/pcs/file" | |
if isinstance(d, unicode): # utf-8 encode for uri | |
d = d.encode("utf-8") | |
params = { | |
"method": "list", | |
"access_token": self.access_token, | |
"path": d, | |
"by": "time", | |
"order": "asc" | |
} | |
# call pcs api | |
u = "%s?%s" % (req_url, urllib.urlencode(params)) | |
f = urllib2.urlopen(u, timeout=self.timeout) | |
d = f.read() | |
code = f.getcode() | |
if code != 200: # http error | |
raise OSError("pcs api call failed") | |
rs = json.loads(d) | |
return rs["list"] # file list | |
def get_download_uri(self, f): | |
if isinstance(f, unicode): # encode to utf-8 | |
f = f.encode("utf-8") | |
req_url = "https://d.pcs.baidu.com/rest/2.0/pcs/file" | |
params = { | |
"method":"download", | |
"access_token": self.access_token, | |
"path": f | |
} | |
u = "%s?%s" % (req_url, urllib.urlencode(params)) | |
return u | |
def walk_pcs(self, d): | |
for f in self.get_filelist(d): | |
if f["isdir"]: # directory | |
for f1 in self.walk_pcs(f["path"]): | |
yield f1 | |
else: # file | |
yield f["path"], f["md5"], self.get_download_uri(f["path"]) | |
def addto_aria2(self, f): | |
s = xmlrpclib.ServerProxy(rpc_url) | |
uris = "%s" % f[2] | |
opts = dict ( | |
out="%s" % (f[0].replace(self.basepath, "", 1)), # filename to save | |
checksum="md5=%s" % (f[1],), # checksum | |
header=["User-Agent: netdisk;5.2.6;PC;PC-Windows;6.2.9200;WindowsBaiduYunGuanJia"] # header | |
) | |
return s.aria2.addUri([uris], opts) # rpc call | |
if __name__ == "__main__": | |
# xml rpc path | |
rpc_url="http://127.0.0.1:6800/rpc" | |
# replace this to your access_token, normally in /home/$USER/.bypy.json | |
access_token= "21.73345bccecb2654af644519b00c.2592000.147859276-1572671" | |
# base path for download | |
base_dir = "/apps/bypy" | |
p = PcsLib(access_token, basepath=base_dir) | |
for a in p.walk_pcs(base_dir): | |
print "file path:", a[0], "\n\tmd5:", a[1], "\n\tdownload_url:", a[2] | |
print "\n\tgid:", p.addto_aria2(a) | |
经研究发现可以使用带用户密码的aria2,如何:http://username:[email protected]:6800/rpc,
但是如何使用启用secret token的aira2呢,搜索了半天没有找到办法,求指点
终于知道怎么使用了,aria2.addUri("token:$$secret$$", ["http://example.org/file"])
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用
--enable-rpc
启动 aria2c, 把access_token替换成你自己的,执行此脚本就可以使用aria2下载百度云的文件了.执行
bypy.py info
根据提示完成验证授权之后,就可以在/home/$USER/.bypy.json 文件找到access_token