Created
May 15, 2015 06:45
-
-
Save fangdingjun/4aa20013f433d0e61338 to your computer and use it in GitHub Desktop.
使用aria2替换bypy的下载
This file contains hidden or 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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
终于知道怎么使用了,aria2.addUri("token:$$secret$$", ["http://example.org/file"])