From 4c383af36602612be745764463dd76fee17ae205 Mon Sep 17 00:00:00 2001 From: Sebastian Hahn Date: Sun, 16 Aug 2009 22:21:11 +0200 Subject: Add the ability to download files via BitTorrent The Client has learned a new option, --download-method, too specify whether we're downloading directly or via BitTorrent. This implementation has a few remaining issues, the biggest one is that seeding isn't implemented at all (when the download stops, Thandy stops sharing). Failure to download due to no available peers also doesn't work. --- lib/thandy/bt_compat.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'lib/thandy/bt_compat.py') diff --git a/lib/thandy/bt_compat.py b/lib/thandy/bt_compat.py index 43cecad..e9298a6 100644 --- a/lib/thandy/bt_compat.py +++ b/lib/thandy/bt_compat.py @@ -2,6 +2,7 @@ import os.path import time +import threading import thandy.master_keys @@ -74,3 +75,58 @@ class BtCompat: 'creation date': long(time.time())} return BitTorrent.bencode.bencode(data) + def getFileLength(self, file): + """Parse the .torrent metainfo file and return the length of the + file it refers to. + """ + f = open(file, 'rb') + metainfo = BitTorrent.bencode.bdecode(f.read())['info'] + f.close() + assert(metainfo['length']) + return metainfo['length'] + + def getFileHash(self, file): + """Parse the .torrent metainfo file and return the hash of the + file it refers to. + """ + f = open(file, 'rb') + metainfo = BitTorrent.bencode.bdecode(f.read())['info'] + f.close() + return sha(BitTorrent.bencode.bencode(metainfo)).hexdigest() + + def download(self, metaFile, saveTo ): + """Initiate a download via bittorrent.""" + + event = threading.Event() + + params = ['--responsefile', metaFile, '--saveas', saveTo] + + def filefunc(default, size, saveas, dir): + return saveas + + def statusfunc(dict): + # XXX we should see how fast we upload/download here. + # If we don't get a connection for quite a while, or we are + # _very_ slow, we should cancel bt, disable it, and start fetching + # via http. + pass + + def finfunc(): + # XXX here we can set a timer for how long to seed, or + # wait for statusfunc to have shared some data, or something. + # Not the real solution, though, because installation will be + # delayed by the time we sleep... + # time.sleep(60) + event.set() + pass + + def errorfunc(msg): + # XXX Not really sure how to encounter an error here. Our best bet + # is to cancel the download, stop bittorrent, and move on. + BtCompat.setUseBt(False) + event.set() + + + BitTorrent.download.download(params, filefunc, statusfunc, finfunc, + errorfunc, event, 80) + -- cgit v1.2.3