Adieu
Dec 4, 2011


browser = mechanize.UserAgent()
cj = mechanize.LWPCookieJar()
try:
cj.load(COOKIE_FILE, ignore_discard=True, ignore_expires=True)
except IOError:
pass
browser.set_cookiejar(cj)
browser.addheaders = [
('User-agent', USER_AGENT),
]
...
response = browser.open(url, data)
...
page = BeautifulSoup(response.read())
raw_items = page.findAll('div', attrs={'class':'rw_list'})
mba ~: xunlei_cli download -h
Usage:
xunlei_cli download [--option] - download task from xunlei
Set global settings in ~/.xunleirc or pass in settings from cmd line.
Options:
-h, --help show this help message and exit
-u USERNAME, --username=USERNAME
xunlei username
-p PASSWORD, --password=PASSWORD
xunlei password
-c COOKIE_FILE, --cookie-file=COOKIE_FILE
file to store cookies. default is ~/.xunlei_cookie
-w, --wait wait for input before exit
@optfunc.arghelp('cookie_file', 'file to store cookies. default is ~/.xunlei_cookie')
@optfunc.arghelp('password', 'xunlei password')
@optfunc.arghelp('username', 'xunlei username')
def dashboard(username=None, password=None, cookie_file=None):
"""Usage: %prog dashboard - list tasks from xunlei"""
xunlei_obj = get_xunlei(username, password, cookie_file)
items = xunlei_obj.dashboard()
for (index, item) in enumerate(items):
print index, item['name']
def hash_filehash(filename):
""" Returns the ed2k hash of a given file. """
md4 = hashlib.new('md4').copy
def gen(f):
while True:
x = f.read(9728000)
if x: yield x
else: return
def md4_hash(data):
m = md4()
m.update(data)
return m
with open(filename, 'rb') as f:
a = gen(f)
hashes = [md4_hash(data).digest() for data in a]
if len(hashes) == 1:
return hashes[0].encode("hex")
else: return md4_hash(reduce(lambda a,d: a + d, hashes, "")).hexdigest()
源码地址
Slides地址