3 from BaseLib.Core.API import *
8 def create_torrent(source):
10 Creates a torrent file for the video source file.
13 if isinstance(source, unicode):
16 usource = source.decode(sys.getfilesystemencoding())
18 duration = config.AVINFO_CLASS.get_video_duration(source, True)
21 tdef.add_content(usource, playtime=duration)
22 tdef.set_tracker(config.BT_TRACKER)
24 tdef.set_piece_length(32768)
27 tdef.save(source + '.tstream')
32 Implementation of BitTorrent operations that uses Next-Share library.
37 port = random.randint(10000, 65535)
40 sscfg = SessionStartupConfig()
41 statedir = tempfile.mkdtemp()
42 sscfg.set_state_dir(statedir)
43 sscfg.set_listen_port(port)
44 sscfg.set_megacache(False)
45 sscfg.set_overlay(False)
46 sscfg.set_dialback(True)
47 sscfg.set_internal_tracker(False)
49 self.session = Session(sscfg)
51 def start_download(self, torrent, output_dir='.'):
53 Download (leech or seed) a file via BitTorrent.
54 The code is adapted from Next-Share's 'BaseLib/Tools/cmdlinedl.py'.
56 @param torrent .torrent file or URL
59 # setup and start download
60 dscfg = DownloadStartupConfig()
61 dscfg.set_dest_dir(output_dir);
63 if torrent.startswith("http") or torrent.startswith(P2PURL_SCHEME):
64 tdef = TorrentDef.load_from_url(torrent)
66 tdef = TorrentDef.load(torrent)
68 raise ValueError("CIS does not support live torrents")
70 d = self.session.start_download(tdef, dscfg)
71 #d.set_state_callback(state_callback, getpeerlist=False)