From 3ecaa41dbbf6d5ba27ae8a0811900194ad1649b5 Mon Sep 17 00:00:00 2001 From: inpos Date: Sat, 19 Mar 2016 22:00:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=BC=D1=8F=20=D0=B2=20=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D1=82=D1=83=D1=81=D0=B5=20=D1=81=D0=B5=D1=80=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pyrrent2http/engine.py | 14 ++++--- lib/pyrrent2http/pyrrent2http.py | 65 ++++++++++++++++---------------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/lib/pyrrent2http/engine.py b/lib/pyrrent2http/engine.py index bac3f20..210de6d 100644 --- a/lib/pyrrent2http/engine.py +++ b/lib/pyrrent2http/engine.py @@ -196,7 +196,7 @@ class Engine: 'maxFailCount': self.max_failcount, 'showPiecesProgress': self.log_pieces_progress, 'idleTimeout': self.max_idle_timeout, - 'fileIndex': start_index, + #'fileIndex': start_index, 'connectionsLimit': self.connections_limit, 'enableScrape': self.enable_scrape, 'enableUTP': self.enable_utp, @@ -245,6 +245,9 @@ class Engine: raise Error("Can't start pyrrent2http, time is out", Error.TIMEOUT) self._log("pyrrent2http successfully started.") + def activate_file(self, index): + self.pyrrent2http.TorrentFS.file(index) + def check_torrent_error(self, status=None): """ It is recommended to call this method periodically to check if any libtorrent errors occurred. @@ -324,11 +327,10 @@ class Engine: :return: File with specified index :rtype: FileStatus """ - files = self.pyrrent2http.Ls()['files'] - if files: - for f in files: - if f['index'] == file_index: - return FileStatus(**f) + filestatus = self.pyrrent2http.Ls(file_index) + try: + return FileStatus(**filestatus) + except: raise Error("Requested file index (%d) is invalid" % (file_index,), Error.INVALID_FILE_INDEX, file_index=file_index) diff --git a/lib/pyrrent2http/pyrrent2http.py b/lib/pyrrent2http/pyrrent2http.py index ab01873..13aecff 100644 --- a/lib/pyrrent2http/pyrrent2http.py +++ b/lib/pyrrent2http/pyrrent2http.py @@ -261,25 +261,21 @@ class TorrentFS(object): progresses = list() save_path = None - def __init__(self, root, handle, startIndex): + def __init__(self, root, handle): self.root = root self.handle = handle self.waitForMetadata() self.save_path = localize_path(self.root.torrentParams['save_path']) self.priorities = list(self.handle.file_priorities()) - file_ = self.__file_at_(startIndex) + num_files = self.info.num_files() + for i in range(num_files): + self.setPriority(i, 0) + def file(self, index): + file_ = self.__file_at_(index) self.files = {file_.name: file_} #self.handle.set_piece_deadline(self.files[startIndex].startPiece, 50) - if startIndex < 0: - logging.info('No -file-index specified, downloading will be paused until any file is requested') - - num_files = self.info.num_files() - - for i in range(num_files): - if startIndex == i: - self.setPriority(i, 1) - else: - self.setPriority(i, 0) + self.setPriority(index, 1) + return file_ def Shutdown(self): self.shuttingDown = True @@ -462,7 +458,7 @@ def HttpHandlerFactory(): class Pyrrent2http(object): def __init__(self, uri = '', bindAddress = 'localhost:5001', downloadPath = '.', - idleTimeout = -1, fileIndex = -1, keepComplete = False, + idleTimeout = -1, keepComplete = False, keepIncomplete = False, keepFiles = False, showAllStats = False, showOverallProgress = False, showFilesProgress = False, showPiecesProgress = False, debugAlerts = False, @@ -484,7 +480,7 @@ class Pyrrent2http(object): self.config.bindAddress = bindAddress self.config.downloadPath = downloadPath self.config.idleTimeout = idleTimeout - self.config.fileIndex = fileIndex + #self.config.fileIndex = fileIndex self.config.keepComplete = keepComplete self.config.keepIncomplete = keepIncomplete self.config.keepFiles = keepFiles @@ -582,9 +578,12 @@ class Pyrrent2http(object): info = self.torrentHandle.get_torrent_info() logging.info('Downloading torrent: %s' % (info.name(),)) try: - self.TorrentFS = TorrentFS(self, self.torrentHandle, self.config.fileIndex) + self.TorrentFS = TorrentFS(self, self.torrentHandle) except Exception as e: logging.error(e.args) + #_ = self.TorrentFS.file(self.config.fileIndex) + name = self.TorrentFS.info.name() + self.torrent_name = name.decode(chardet.detect(name)['encoding']) def startHTTP(self): #def http_server_loop(listener, alive): @@ -720,7 +719,7 @@ class Pyrrent2http(object): tstatus = self.torrentHandle.status() status = { - 'name' : info.name(), + 'name' : self.torrent_name, 'state' : int(tstatus.state), 'state_str' : str(tstatus.state), 'error' : tstatus.error, @@ -735,25 +734,25 @@ class Pyrrent2http(object): 'total_peers' : tstatus.num_incomplete } return status - def Ls(self): - retFiles = {'files': []} + def Ls(self, index): + fi = {} if self.TorrentFS.HasTorrentInfo(): + x = [n for n in self.TorrentFS.files.keys() if self.TorrentFS.files[n].index == index] + name = x[0] files = self.TorrentFS.files - for name in files.keys(): - Url = 'http://' + self.config.bindAddress + '/files/' + urllib.quote(name) - fi = { - 'index': files[name].index, - 'name': files[name].unicode_name, - 'media_type': files[name].media_type, - 'size': files[name].size, - 'offset': files[name].offset, - 'download': files[name].downloaded, - 'progress': files[name].progress, - 'save_path': files[name].save_path, - 'url': Url - } - retFiles['files'].append(fi) - return retFiles + Url = 'http://' + self.config.bindAddress + '/files/' + urllib.quote(name) + fi = { + 'index': files[name].index, + 'name': files[name].unicode_name, + 'media_type': files[name].media_type, + 'size': files[name].size, + 'offset': files[name].offset, + 'download': files[name].downloaded, + 'progress': files[name].progress, + 'save_path': files[name].save_path, + 'url': Url + } + return fi def Peers(self): peers = {'peers': []} for peer in self.torrentHandle.get_peer_info():