X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=cis%2Fbt.py;h=894ea947692ec4b7fd3bee1d18e75482fa60027e;hb=7cda91b00cd3d7247bd8b317d5c9c97613082921;hp=2184fa5d279fd8f37b9c8e04f66700068f8c7504;hpb=17430bb7ba2fdc1ffcd6e08ffd9dc4f027c7d4fc;p=living-lab-site.git diff --git a/cis/bt.py b/cis/bt.py index 2184fa5..894ea94 100644 --- a/cis/bt.py +++ b/cis/bt.py @@ -4,6 +4,7 @@ from BaseLib.Core.API import * import tempfile import random import config +import logger def create_torrent(source): """ @@ -48,9 +49,10 @@ class BitTorrent: self.session = Session(sscfg) - def start_download(self, torrent, output_dir='.'): + def start_torrent(self, torrent, output_dir='.'): """ Download (leech or seed) a file via BitTorrent. + The code is adapted from Next-Share's 'BaseLib/Tools/cmdlinedl.py'. @param torrent .torrent file or URL @@ -58,7 +60,7 @@ class BitTorrent: # setup and start download dscfg = DownloadStartupConfig() - dscfg.set_dest_dir(output_dir); + dscfg.set_dest_dir(output_dir) if torrent.startswith("http") or torrent.startswith(P2PURL_SCHEME): tdef = TorrentDef.load_from_url(torrent) @@ -66,6 +68,50 @@ class BitTorrent: tdef = TorrentDef.load(torrent) if tdef.get_live(): raise ValueError("CIS does not support live torrents") - - d = self.session.start_download(tdef, dscfg) + + new_download = True + try: + d = self.session.start_download(tdef, dscfg) + except DuplicateDownloadException: + new_download = False #d.set_state_callback(state_callback, getpeerlist=False) + + if new_download: + logger.log_msg('download of torrent "%s" started' % torrent) + #else: + #logger.log_msg('download of torrent "%s" already started' \ + #% torrent, logger.LOG_LEVEL_DEBUG) + + def stop_torrent(self, torrent, remove_content=False): + """ + Stop leeching or seeding a file via BitTorrent. + + !!! Only tested with torrents started with .tstream files. Not tested + for torrents started with URLs. + + @param torrent .torrent file or URL + @param remove_content removes downloaded file + """ + + downloads = self.session.get_downloads() + + for dl in downloads: + tdef = dl.get_def() + if torrent.find(tdef.get_name()) == 0: + self.session.remove_download(dl, remove_content) + logger.log_msg('torrent "%s" stopped' % torrent) + break + + def get_torrent_list(self): + """ + Returns a list of all torrents started. + """ + + downloads = self.session.get_downloads() + torrent_list = [] + + for dl in downloads: + tdef = dl.get_def() + torrent_list.append(tdef.get_name() + '.tstream') + + return torrent_list