用Python改进迅雷离线
A Python Successful Story

Adieu
Dec 4, 2011

About Me

迅雷离线简介

迅雷离线不完美之处

Python comes to the rescue

改进1: 用Python写XBMC的插件,从迅雷点播视频文件

用到的Python库

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'})
            

改进2: 用Python脚本下载迅雷离线上的文件

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
          

用到的Python库

@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']
              

One more thing...

迅雷离线的非常规用法

两个ADSL用户如何快速分享大文件

改进3: 用Python脚本结合迅雷离线发送大文件

用Python计算ed2k链接

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()
Source: http://www.radicand.org/edonkey2000-hash-in-python/

谢谢

源码地址

Slides地址