def start(self): if not self._shutdown: log('(Torrent) Find free port', LOGLEVEL.INFO) port = get_free_port() log('(Torrent) Starting torrent2http', LOGLEVEL.INFO) startupinfo = None if Platform.system == "windows": startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= 1 startupinfo.wShowWindow = 0 if _settings.debug: self._logpipe = LogPipe(self._debug) torrent_options = self._mediaSettings.get_torrent_options(self._magnet, port) try: self._process = subprocess.Popen(torrent_options, stderr=self._logpipe, stdout=self._logpipe, startupinfo=startupinfo) except: raise TorrentError("Can't start torrent2http: %s" % str(sys.exc_info()[1])) self._url = "http://127.0.0.1:%s/" %port start = time.time() while not self._shutdown: if (time.time() - start) > 5 or not self.isAlive(): raise TorrentError("Can't start torrent2http") if not self.status(1)['state'] == self.NO_CONNECTION: log("(Torrent) torrent2http successfully started") return True return False
def status(self, timeout=10): if not self._shutdown: try: if not self.isAlive(): raise TorrentError("torrent2http are not running") self._last_status = self._json.request(self._url, "/status", timeout=timeout) or self._last_status if self._last_status.get('error'): raise TorrentError("torrent2http error: %s" %self._last_status['error']) except (JSONDecodeError, socket.timeout, IOError) as e: log('(Torrent) %s: %s' %(e.__class__.__name__, str(e)), LOGLEVEL.NOTICE) sys.exc_clear() return self._last_status
def files(self, timeout=10): if not self._shutdown: try: if not self.isAlive(): raise TorrentError("torrent2http are not running") self._last_files = self._json.request(self._url, "/ls", timeout=timeout)['files'] or self._last_files except (JSONDecodeError, socket.timeout, IOError) as e: log('(Torrent) %s: %s' %(e.__class__.__name__, str(e)), LOGLEVEL.NOTICE) sys.exc_clear() return self._last_files
def playFile(self, timeout=10): files = self.files(timeout) if not files: return {} if self._file_id is None: size = 0 for i, f in enumerate(files): mimeType = mimetypes.guess_type(f['name']) log('(Torrent) File name: %s, MIME info: %s' % (f['name'], str(mimeType))) if mimeType[0][:5] == 'video' and f['size'] > size: self._file_id = i try: return files[self._file_id] except (KeyError, TypeError): raise TorrentError("Can not find a file to play")
def playFile(self, timeout=10): files = self.files(timeout) if not files: return {} if self._file_id is None: size = 0 for i, f in enumerate(files): mimeType = mimetypes.guess_type(f['name']) log('(Torrent) File name: %s, MIME info: %s' %(f['name'], str(mimeType))) # if mimeType[0] and mimeType[0][:5] == 'video' and f['size'] > size: #if 'video' in str(mimeType) and f['size'] > size: if(re.match('.*\.avi|.*\.mp4|.*\.mkv',f['name'])): self._file_id = i urllib2.urlopen(f['url'], timeout=50) try: return files[self._file_id] except (KeyError, TypeError): raise TorrentError("Can not find a file to play")