uploading works, but AV info is not automatically detected and video activation featu...
[living-lab-site.git] / cis / bt.py
1 #!/usr/bin/env python
2
3 from BaseLib.Core.API import *
4 import tempfile
5 import random
6 import config
7 import logger
8
9 def create_torrent(source):
10     """
11     Creates a torrent file for the video source file.
12     """
13
14     if isinstance(source, unicode):
15         usource = source
16     else:
17         usource = source.decode(sys.getfilesystemencoding())
18
19     duration = config.AVINFO_CLASS.get_video_duration(source, True)
20
21     tdef = TorrentDef()
22     tdef.add_content(usource, playtime=duration)
23     tdef.set_tracker(config.BT_TRACKER)
24
25     tdef.set_piece_length(32768)
26
27     tdef.finalize()
28     tdef.save(source + '.tstream')
29
30
31 class BitTorrent:
32     """
33     Implementation of BitTorrent operations that uses Next-Share library.
34     """
35
36     def __init__(self):
37
38         port = random.randint(10000, 65535)
39         
40         # setup session
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)
49         
50         self.session = Session(sscfg)
51
52     def start_torrent(self, torrent, output_dir='.'):
53         """
54         Download (leech or seed) a file via BitTorrent.
55         
56         The code is adapted from Next-Share's 'BaseLib/Tools/cmdlinedl.py'.
57
58         @param torrent .torrent file or URL
59         """
60
61         # setup and start download
62         dscfg = DownloadStartupConfig()
63         dscfg.set_dest_dir(output_dir)
64
65         if torrent.startswith("http") or torrent.startswith(P2PURL_SCHEME):
66             tdef = TorrentDef.load_from_url(torrent)
67         else: 
68             tdef = TorrentDef.load(torrent)
69         if tdef.get_live():
70             raise ValueError("CIS does not support live torrents")
71         
72         new_download = True
73         try:
74             d = self.session.start_download(tdef, dscfg)
75         except DuplicateDownloadException:
76             new_download = False
77         #d.set_state_callback(state_callback, getpeerlist=False)
78         
79         if new_download:
80             logger.log_msg('download of torrent "%s" started' % torrent)
81         #else:
82             #logger.log_msg('download of torrent "%s" already started' \
83                     #% torrent, logger.LOG_LEVEL_DEBUG)
84     
85     def stop_torrent(self, torrent, remove_content=False):
86         """
87         Stop leeching or seeding a file via BitTorrent.
88         
89         !!! Only tested with torrents started with .tstream files. Not tested
90         for torrents started with URLs.
91         
92         @param torrent .torrent file or URL
93         @param remove_content removes downloaded file
94         """
95         
96         downloads = self.session.get_downloads()
97         
98         for dl in downloads:
99             tdef = dl.get_def()
100             if torrent.find(tdef.get_name()) == 0:
101                 self.session.remove_download(dl, remove_content)
102                 logger.log_msg('torrent "%s" stopped' % torrent)
103                 break
104     
105     def get_torrent_list(self):
106         """
107         Returns a list of all torrents started.
108         """
109         
110         downloads = self.session.get_downloads()
111         torrent_list = []
112         
113         for dl in downloads:
114             tdef = dl.get_def()
115             torrent_list.append(tdef.get_name() + '.tstream')
116         
117         return torrent_list