3 from BaseLib.Core.API import *
9 def create_torrent(source):
11 Creates a torrent file for the video source file.
14 if isinstance(source, unicode):
17 usource = source.decode(sys.getfilesystemencoding())
19 duration = config.AVINFO_CLASS.get_video_duration(source, True)
22 tdef.add_content(usource, playtime=duration)
23 tdef.set_tracker(config.BT_TRACKER)
25 tdef.set_piece_length(32768)
28 tdef.save(source + '.tstream')
33 Implementation of BitTorrent operations that uses Next-Share library.
38 port = random.randint(10000, 65535)
41 sscfg = SessionStartupConfig()
42 statedir = tempfile.mkdtemp()
43 sscfg.set_state_dir(statedir)
44 sscfg.set_listen_port(port)
45 sscfg.set_megacache(False)
46 sscfg.set_overlay(False)
47 sscfg.set_dialback(True)
48 sscfg.set_internal_tracker(False)
50 self.session = Session(sscfg)
52 def start_download(self, torrent, output_dir='.'):
54 Download (leech or seed) a file via BitTorrent.
55 The code is adapted from Next-Share's 'BaseLib/Tools/cmdlinedl.py'.
57 @param torrent .torrent file or URL
60 # setup and start download
61 dscfg = DownloadStartupConfig()
62 dscfg.set_dest_dir(output_dir)
64 if torrent.startswith("http") or torrent.startswith(P2PURL_SCHEME):
65 tdef = TorrentDef.load_from_url(torrent)
67 tdef = TorrentDef.load(torrent)
69 raise ValueError("CIS does not support live torrents")
73 d = self.session.start_download(tdef, dscfg)
74 except DuplicateDownloadException:
76 #d.set_state_callback(state_callback, getpeerlist=False)
79 logger.log_msg('download of torrent "%s" started' % torrent)
81 #logger.log_msg('download of torrent "%s" already started' \
82 #% torrent, logger.LOG_LEVEL_DEBUG)