3 from BaseLib.Core.API import *
7 def create_torrent(source):
9 Creates a torrent file for the video source file.
12 if isinstance(source, unicode):
15 usource = source.decode(sys.getfilesystemencoding())
17 duration = config.AVINFO_CLASS.get_video_duration(source, True)
20 tdef.add_content(usource, playtime=duration)
21 tdef.set_tracker(config.BT_TRACKER)
23 tdef.set_piece_length(32768)
26 tdef.save(source + '.tstream')
31 Implementation of BitTorrent operations that uses Next-Share library.
36 port = random.randint(10000, 65535)
39 sscfg = SessionStartupConfig()
40 statedir = tempfile.mkdtemp()
41 sscfg.set_state_dir(statedir)
42 sscfg.set_listen_port(port)
43 sscfg.set_megacache(False)
44 sscfg.set_overlay(False)
45 sscfg.set_dialback(True)
46 sscfg.set_internal_tracker(False)
48 self.session = Session(sscfg)
50 def download(self, torrent, output_dir='.'):
52 Download (leech or seed) a file via BitTorrent.
53 The code is adapted from Next-Share's 'BaseLib/Tools/cmdlinedl.py'.
55 @param torrent .torrent file or URL
58 # setup and start download
59 dscfg = DownloadStartupConfig()
60 dscfg.set_dest_dir(output_dir);
62 if torrent.startswith("http") or torrent.startswith(P2PURL_SCHEME):
63 tdef = TorrentDef.load_from_url(torrent)
65 tdef = TorrentDef.load(torrent)
67 raise ValueError("CIS does not support live torrents")
69 d = self.session.start_download(tdef, dscfg)
70 #d.set_state_callback(state_callback, getpeerlist=False)