From 8ea64f3cd3e3f20fcb6e3310b90482aea7b84cb3 Mon Sep 17 00:00:00 2001 From: inpos Date: Sat, 12 Mar 2016 00:18:11 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=8C=D1=88=D0=B5=20=D0=B6?= =?UTF-8?q?=D1=80=D1=91=D0=BC,=20=D0=B1=D1=8B=D1=81=D1=82=D1=80=D0=B5?= =?UTF-8?q?=D0=B5=20=D0=B4=D0=B2=D0=B8=D0=B3=D0=B0=D0=B5=D0=BC=D1=81=D1=8F?= =?UTF-8?q?...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pyrrent2http/engine.py | 24 +++----------------- lib/pyrrent2http/pyrrent2http.py | 39 ++++++++++++++++++++------------ lib/pyrrent2http/util.py | 20 ++++++++++++++++ 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/lib/pyrrent2http/engine.py b/lib/pyrrent2http/engine.py index e9dd011..62aa4d7 100644 --- a/lib/pyrrent2http/engine.py +++ b/lib/pyrrent2http/engine.py @@ -9,10 +9,9 @@ import urllib2 import httplib from os.path import dirname import pyrrent2http -import mimetypes import xbmc from error import Error -from . import SessionStatus, FileStatus, PeerInfo, MediaType, Encryption +from . import SessionStatus, FileStatus, PeerInfo, Encryption from util import can_bind, find_free_port, localize_path import threading @@ -22,9 +21,6 @@ class Engine: """ This is python binding class to pyrrent2http client. """ - SUBTITLES_FORMATS = ['.aqt', '.gsub', '.jss', '.sub', '.ttxt', '.pjs', '.psb', '.rt', '.smi', '.stl', - '.ssf', '.srt', '.ssa', '.ass', '.usf', '.idx'] - def _log(self, message): if self.logger: self.logger(message) @@ -278,21 +274,7 @@ class Engine: status = SessionStatus(**status) return status - def _detect_media_type(self, name): - ext = os.path.splitext(name)[1] - if ext in self.SUBTITLES_FORMATS: - return MediaType.SUBTITLES - else: - mime_type = mimetypes.guess_type(name)[0] - if not mime_type: - return MediaType.UNKNOWN - mime_type = mime_type.split("/")[0] - if mime_type == 'audio': - return MediaType.AUDIO - elif mime_type == 'video': - return MediaType.VIDEO - else: - return MediaType.UNKNOWN + def list(self, media_types=None, timeout=10): """ @@ -307,7 +289,7 @@ class Engine: """ files = self.pyrrent2http.Ls()['files'] if files: - res = [FileStatus(index=index, media_type=self._detect_media_type(f['name']), **f) + res = [FileStatus(index=index, **f) for index, f in enumerate(files)] if media_types is not None: res = filter(lambda fs: fs.media_type in media_types, res) diff --git a/lib/pyrrent2http/pyrrent2http.py b/lib/pyrrent2http/pyrrent2http.py index eb49049..2280750 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 +from util import localize_path, Struct, detect_media_type ###################################################################################### @@ -135,16 +135,17 @@ class TorrentFile(object): closed = True save_path = str() fileEntry = None - index = int() + index = 0 filePtr = None - downloaded = int() - progress = float() + downloaded = 0 + progress = 0.0 pdl_thread = None def __init__(self, tfs, fileEntry, savePath, index): self.tfs = tfs self.fileEntry = fileEntry self.name = self.Name() self.unicode_name = self.name.decode(chardet.detect(self.name)['encoding']) + self.media_type = detect_media_type(self.unicode_name) self.save_path = savePath self.index = index self.piece_length = int(self.pieceLength()) @@ -277,7 +278,8 @@ class TorrentFS(object): self.handle = handle self.waitForMetadata() self.save_path = localize_path(self.root.torrentParams['save_path']) - self.priorities = [[i, p] for i,p in enumerate(self.handle.file_priorities())] + self.priorities = list(self.handle.file_priorities()) + self.files = [] if startIndex < 0: logging.info('No -file-index specified, downloading will be paused until any file is requested') for i in range(self.TorrentInfo().num_files()): @@ -325,22 +327,26 @@ class TorrentFS(object): return self.info def LoadFileProgress(self): self.progresses = self.handle.file_progress() + for i, f in enumerate(self.files): + f.downloaded = self.getFileDownloadedBytes(i) + if f.size > 0: f.progress = float(f.downloaded)/float(f.size) def getFileDownloadedBytes(self, i): try: - bytes = self.progresses[i] + bytes_ = self.progresses[i] except IndexError: - bytes = 0 - return bytes + bytes_ = 0 + return bytes_ def Files(self): + if len(self.files) > 0: + return self.files info = self.TorrentInfo() - files = [None for x in range(info.num_files())] for i in range(info.num_files()): file_ = self.FileAt(i) file_.downloaded = self.getFileDownloadedBytes(i) if file_.Size() > 0: file_.progress = float(file_.downloaded)/float(file_.Size()) - files[i] = file_ - return files + self.files.append(file_) + return self.files def FileAt(self, index): info = self.TorrentInfo() if index < 0 or index >= info.num_files(): @@ -839,11 +845,12 @@ class Pyrrent2http(object): Url = 'http://' + self.config.bindAddress + '/files/' + urllib.quote(file_.name) fi = { 'name': file_.unicode_name, + 'media_type': file_.media_type, 'size': file_.size, 'offset': file_.offset, - 'download': file_.Downloaded(), - 'progress': file_.Progress(), - 'save_path': file_.save_path, + 'download': file_.downloaded, + 'progress': file_.progress, + 'save_path': file_.save_path, 'url': Url } retFiles['files'].append(fi) @@ -900,6 +907,7 @@ class Pyrrent2http(object): for alert in alerts: if isinstance(alert, lt.save_resume_data_alert): self.processSaveResumeDataAlert(alert) + break def waitForAlert(self, alertClass, timeout): start = time.time() while True: @@ -920,7 +928,7 @@ class Pyrrent2http(object): if self.forceShutdown: return if time.time() - time_start > 0.5: - #self.consumeAlerts() + self.consumeAlerts() self.TorrentFS.LoadFileProgress() state = self.torrentHandle.status().state if self.config.exitOnFinish and (state == state.finished or state == state.seeding): @@ -932,6 +940,7 @@ class Pyrrent2http(object): self.stats() if self.saveResumeDataTicker.true: self.saveResumeData(True) + time.sleep(0.3) def processSaveResumeDataAlert(self, alert): logging.info('Saving resume data to: %s' % (self.config.resumeFile)) diff --git a/lib/pyrrent2http/util.py b/lib/pyrrent2http/util.py index ded31cd..dc1d576 100644 --- a/lib/pyrrent2http/util.py +++ b/lib/pyrrent2http/util.py @@ -1,7 +1,12 @@ import sys import socket import chardet +import os +from . import MediaType +import mimetypes +SUBTITLES_FORMATS = ['.aqt', '.gsub', '.jss', '.sub', '.ttxt', '.pjs', '.psb', '.rt', '.smi', '.stl', + '.ssf', '.srt', '.ssa', '.ass', '.usf', '.idx'] class Struct(dict): def __getattr__(self, attr): @@ -9,6 +14,21 @@ class Struct(dict): def __setattr__(self, attr, value): self[attr] = value +def detect_media_type(name): + ext = os.path.splitext(name)[1] + if ext in SUBTITLES_FORMATS: + return MediaType.SUBTITLES + else: + mime_type = mimetypes.guess_type(name)[0] + if not mime_type: + return MediaType.UNKNOWN + mime_type = mime_type.split("/")[0] + if mime_type == 'audio': + return MediaType.AUDIO + elif mime_type == 'video': + return MediaType.VIDEO + else: + return MediaType.UNKNOWN def localize_path(path): path = path.decode(chardet.detect(path)['encoding'])