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_torrent(self, torrent, output_dir='.'):
54 Download (leech or seed) a file via BitTorrent.
56 The code is adapted from Next-Share's 'BaseLib/Tools/cmdlinedl.py'.
58 @param torrent .torrent file or URL
61 # setup and start download
62 dscfg = DownloadStartupConfig()
63 dscfg.set_dest_dir(output_dir)
65 if torrent.startswith("http") or torrent.startswith(P2PURL_SCHEME):
66 tdef = TorrentDef.load_from_url(torrent)
68 tdef = TorrentDef.load(torrent)
70 raise ValueError("CIS does not support live torrents")
74 d = self.session.start_download(tdef, dscfg)
75 except DuplicateDownloadException:
77 #d.set_state_callback(state_callback, getpeerlist=False)
80 logger.log_msg('download of torrent "%s" started' % torrent)
82 #logger.log_msg('download of torrent "%s" already started' \
83 #% torrent, logger.LOG_LEVEL_DEBUG)
85 def stop_torrent(self, torrent, remove_content=False):
87 Stop leeching or seeding a file via BitTorrent.
89 !!! Only tested with torrents started with .tstream files. Not tested
90 for torrents started with URLs.
92 @param torrent .torrent file or URL
93 @param remove_content removes downloaded file
96 downloads = self.session.get_downloads()
100 if torrent.find(tdef.get_name()) == 0:
101 self.session.remove_download(dl, remove_content)
102 logger.log_msg('torrent "%s" stopped' % torrent)
105 def get_torrent_list(self):
107 Returns a list of all torrents started.
110 downloads = self.session.get_downloads()
115 torrent_list.append(tdef.get_name() + '.tstream')