def _validate_save_path(path): """ Ensures download path can be accessed locally. :param path: Download path :return: Translated path """ import xbmc path = xbmc.translatePath(path) if "://" in path: if sys.platform.startswith('win') and path.lower().startswith("smb://"): path = path.replace("smb:", "").replace("/", "\\") else: raise Error("Downloading to an unmounted network share is not supported", Error.INVALID_DOWNLOAD_PATH) if not os.path.isdir(ensure_fs_encoding(path)): raise Error("Download path doesn't exist (%s)" % path, Error.INVALID_DOWNLOAD_PATH) return path
def start(self, start_index=None): """ Starts torrent2http client with specified settings. If it can be started in startup_timeout seconds, exception will be raised. :param start_index: File index to start download instantly, if not specified, downloading will be paused, until any file requested """ self.platform = self.platform or Platform() binary_path = self._get_binary_path(self.binaries_path) download_path = self._validate_save_path(self.download_path) if not can_bind(self.bind_host, self.bind_port): port = find_free_port(self.bind_host) if port is False: raise Error("Can't find port to bind torrent2http", Error.BIND_ERROR) self._log("Can't bind to %s:%s, so we found another port: %d" % (self.bind_host, self.bind_port, port)) self.bind_port = port kwargs = { '--bind': "%s:%s" % (self.bind_host, self.bind_port), '--uri': self.uri, '--file-index': start_index, '--dl-path': download_path, '--connections-limit': self.connections_limit, '--dl-rate': self.download_kbps, '--ul-rate': self.upload_kbps, '--enable-dht': self.enable_dht, '--enable-lsd': self.enable_lsd, '--enable-natpmp': self.enable_natpmp, '--enable-upnp': self.enable_upnp, '--enable-scrape': self.enable_scrape, '--encryption': self.encryption, '--show-stats': self.log_stats, '--files-progress': self.log_files_progress, '--overall-progress': self.log_overall_progress, '--pieces-progress': self.log_pieces_progress, '--listen-port': self.listen_port, '--random-port': self.use_random_port, '--keep-complete': self.keep_complete, '--keep-incomplete': self.keep_incomplete, '--keep-files': self.keep_files, '--max-idle': self.max_idle_timeout, '--no-sparse': self.no_sparse, '--resume-file': self.resume_file, '--user-agent': self.user_agent, '--state-file': self.state_file, '--enable-utp': self.enable_utp, '--enable-tcp': self.enable_tcp, '--debug-alerts': self.debug_alerts, '--torrent-connect-boost': self.torrent_connect_boost, '--connection-speed': self.connection_speed, '--peer-connect-timeout': self.peer_connect_timeout, '--request-timeout': self.request_timeout, '--min-reconnect-time': self.min_reconnect_time, '--max-failcount': self.max_failcount, '--dht-routers': ",".join(self.dht_routers), '--trackers': ",".join(self.trackers), } args = [binary_path] for k, v in kwargs.iteritems(): if v is not None: if isinstance(v, bool): if v: #args.append(k) args.append("%s=true" % k) else: args.append("%s=false" % k) else: #args.append(k) if isinstance(v, str) or isinstance(v, unicode): v = ensure_fs_encoding(v) else: v = str(v) #args.append(v) args.append("%s=%s" % (k, v)) self._log("Invoking %s" % " ".join(args)) startupinfo = None if self.platform.system == "windows": startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= 1 startupinfo.wShowWindow = 0 self.logpipe = logpipe.LogPipe(self._log) try: self.process = subprocess.Popen(args, stderr=self.logpipe, stdout=self.logpipe, startupinfo=startupinfo) except OSError, e: raise Error("Can't start torrent2http: %r" % e, Error.POPEN_ERROR)