From f8c956ab0b935211d4f6a7f352584d742e373182 Mon Sep 17 00:00:00 2001 From: inpos Date: Sat, 12 Mar 2016 02:29:23 +0300 Subject: [PATCH] =?UTF-8?q?=D1=83=D1=81=D0=BA=D0=BE=D1=80=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addon.xml | 2 +- lib/pyrrent2http/engine.py | 27 ++++++++++++++++++++-- lib/pyrrent2http/pyrrent2http.py | 39 ++++++++------------------------ lib/pyrrent2http/util.py | 21 ++++++++++++++++- 4 files changed, 56 insertions(+), 33 deletions(-) diff --git a/addon.xml b/addon.xml index 177a80c..8afabcd 100644 --- a/addon.xml +++ b/addon.xml @@ -1,5 +1,5 @@ - + diff --git a/lib/pyrrent2http/engine.py b/lib/pyrrent2http/engine.py index 158033a..e12b352 100644 --- a/lib/pyrrent2http/engine.py +++ b/lib/pyrrent2http/engine.py @@ -12,7 +12,7 @@ import pyrrent2http import xbmc from error import Error from . import SessionStatus, FileStatus, PeerInfo, Encryption -from util import can_bind, find_free_port, localize_path +from util import can_bind, find_free_port, localize_path, uri2path import threading LOGGING = True @@ -130,6 +130,7 @@ class Engine: self.logger = logger self.uri = uri self.started = False + self.fullStart = True @staticmethod @@ -293,6 +294,28 @@ class Engine: if media_types is not None: res = filter(lambda fs: fs.media_type in media_types, res) return res + def list_from_info(self): + self.fullStart = False + try: + info = pyrrent2http.lt.torrent_info(uri2path(self.uri)) + except: + return [] + files = [] + for i in range(info.num_files()): + f = info.file_at(i) + files.append({ + 'name': localize_path(f.path), + 'size': f.size, + 'offset': f.offset, + 'media_type': '', + 'download': 0, + 'progress': 0.0, + 'save_path': '', + 'url': '' + }) + if files: + res = [FileStatus(index=index, **f) for index, f in enumerate(files)] + return res def file_status(self, file_index, timeout=10): """ @@ -342,7 +365,7 @@ class Engine: Shuts down pyrrent2http and stops logging. If wait_on_close() was called earlier, it will wait until pyrrent2http successfully exits. """ - if self.is_alive(): + if self.fullStart and self.is_alive(): self._log("Shutting down pyrrent2http...") self.pyrrent2http.shutdown() finished = False diff --git a/lib/pyrrent2http/pyrrent2http.py b/lib/pyrrent2http/pyrrent2http.py index d631df5..b70dc6d 100644 --- a/lib/pyrrent2http/pyrrent2http.py +++ b/lib/pyrrent2http/pyrrent2http.py @@ -24,7 +24,7 @@ import BaseHTTPServer import SocketServer import threading import io -from util import localize_path, Struct, detect_media_type +from util import localize_path, Struct, detect_media_type, uri2path, normalize_msg ###################################################################################### @@ -601,35 +601,16 @@ class Pyrrent2http(object): raise Exception('Не должно быть файла восстановления, если мы не храним файлы') def buildTorrentParams(self, uri): - if uri[1] == ':' and sys.platform.startswith('win'): - uri = 'file:///' + uri - fileUri = urlparse.urlparse(uri) + try: + absPath = uri2path(uri) + logging.info(normalize_msg('Opening local torrent file: %s', absPath)) + torrent_info = lt.torrent_info(absPath) + except Exception as e: + strerror = e.args + logging.error('Build torrent params error is (%s)' % (strerror,)) + raise torrentParams = {} - if self.magnet: - torrentParams['url'] = uri - elif fileUri.scheme == 'file': - uriPath = fileUri.path - if uriPath != '' and sys.platform.startswith('win') and (os.path.sep == uriPath[0] or uriPath[0] == '/'): - uriPath = uriPath[1:] - try: - absPath = os.path.abspath(urllib.unquote(uriPath)) - logging.info('Opening local torrent file: %s' % (absPath,)) - torrent_info = lt.torrent_info(absPath) - except Exception as e: - strerror = e.args - logging.error('Build torrent params error is (%s)' % (strerror,)) - raise - torrentParams['ti'] = torrent_info - else: - logging.info('Will fetch: %s' % (uri,)) - try: - torrent_raw = urllib.urlopen(uri).read() - torrent_info = lt.torrent_info(torrent_raw, len(torrent_raw)) - except Exception as e: - strerror = e.args - logging.error(strerror) - raise - torrentParams['ti'] = torrent_info + torrentParams['ti'] = torrent_info logging.info('Setting save path: %s' % (self.config.downloadPath,)) torrentParams['save_path'] = self.config.downloadPath diff --git a/lib/pyrrent2http/util.py b/lib/pyrrent2http/util.py index dc1d576..22d4f1f 100644 --- a/lib/pyrrent2http/util.py +++ b/lib/pyrrent2http/util.py @@ -4,6 +4,7 @@ import chardet import os from . import MediaType import mimetypes +import urlparse, urllib SUBTITLES_FORMATS = ['.aqt', '.gsub', '.jss', '.sub', '.ttxt', '.pjs', '.psb', '.rt', '.smi', '.stl', '.ssf', '.srt', '.ssa', '.ass', '.usf', '.idx'] @@ -14,6 +15,17 @@ class Struct(dict): def __setattr__(self, attr, value): self[attr] = value +def uri2path(uri): + if uri[1] == ':' and sys.platform.startswith('win'): + uri = 'file:///' + uri + fileUri = urlparse.urlparse(uri) + if fileUri.scheme == 'file': + uriPath = fileUri.path + if uriPath != '' and sys.platform.startswith('win') and (os.path.sep == uriPath[0] or uriPath[0] == '/'): + uriPath = uriPath[1:] + absPath = os.path.abspath(urllib.unquote(uriPath)) + return localize_path(absPath) + def detect_media_type(name): ext = os.path.splitext(name)[1] if ext in SUBTITLES_FORMATS: @@ -29,9 +41,16 @@ def detect_media_type(name): return MediaType.VIDEO else: return MediaType.UNKNOWN +def normalize_msg(tmpl, *args): + msg = tmpl.decode(chardet.detect(tmpl)['encoding']) + arg_ = [] + for a in args: + if not isinstance(a, unicode): arg_.append(a.decode(chardet.detect(a)['encoding'])) + return msg % tuple(arg_) + def localize_path(path): - path = path.decode(chardet.detect(path)['encoding']) + if not isinstance(path, unicode): path = path.decode(chardet.detect(path)['encoding']) if not sys.platform.startswith('win'): path = path.encode(True and sys.getfilesystemencoding() or 'utf-8') return path