summaryrefslogtreecommitdiff
path: root/lib/thandy/bt_compat.py
diff options
context:
space:
mode:
authorSebastian Hahn <sebastian@torproject.org>2009-08-16 22:21:11 +0200
committerSebastian Hahn <sebastian@torproject.org>2009-08-25 20:10:54 +0200
commit4c383af36602612be745764463dd76fee17ae205 (patch)
tree0986176d08d51c17d83d387fdacd8ee23e8f13c4 /lib/thandy/bt_compat.py
parentaa0d32f4b675e155e6e004604bf8b6ee4e607873 (diff)
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.
Diffstat (limited to 'lib/thandy/bt_compat.py')
-rw-r--r--lib/thandy/bt_compat.py56
1 files changed, 56 insertions, 0 deletions
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)
+