меньше жрём, быстрее двигаемся...
This commit is contained in:
		
							parent
							
								
									453ed45166
								
							
						
					
					
						commit
						8ea64f3cd3
					
				@ -9,10 +9,9 @@ import urllib2
 | 
				
			|||||||
import httplib
 | 
					import httplib
 | 
				
			||||||
from os.path import dirname
 | 
					from os.path import dirname
 | 
				
			||||||
import pyrrent2http
 | 
					import pyrrent2http
 | 
				
			||||||
import mimetypes
 | 
					 | 
				
			||||||
import xbmc
 | 
					import xbmc
 | 
				
			||||||
from error import Error
 | 
					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
 | 
					from util import can_bind, find_free_port, localize_path
 | 
				
			||||||
import threading
 | 
					import threading
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -22,9 +21,6 @@ class Engine:
 | 
				
			|||||||
    """
 | 
					    """
 | 
				
			||||||
    This is python binding class to pyrrent2http client.
 | 
					    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):
 | 
					    def _log(self, message):
 | 
				
			||||||
        if self.logger:
 | 
					        if self.logger:
 | 
				
			||||||
            self.logger(message)
 | 
					            self.logger(message)
 | 
				
			||||||
@ -278,21 +274,7 @@ class Engine:
 | 
				
			|||||||
        status = SessionStatus(**status)
 | 
					        status = SessionStatus(**status)
 | 
				
			||||||
        return 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):
 | 
					    def list(self, media_types=None, timeout=10):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
@ -307,7 +289,7 @@ class Engine:
 | 
				
			|||||||
        """
 | 
					        """
 | 
				
			||||||
        files = self.pyrrent2http.Ls()['files']
 | 
					        files = self.pyrrent2http.Ls()['files']
 | 
				
			||||||
        if 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)]
 | 
					                   for index, f in enumerate(files)]
 | 
				
			||||||
            if media_types is not None:
 | 
					            if media_types is not None:
 | 
				
			||||||
                res = filter(lambda fs: fs.media_type in media_types, res)
 | 
					                res = filter(lambda fs: fs.media_type in media_types, res)
 | 
				
			||||||
 | 
				
			|||||||
@ -24,7 +24,7 @@ import BaseHTTPServer
 | 
				
			|||||||
import SocketServer
 | 
					import SocketServer
 | 
				
			||||||
import threading
 | 
					import threading
 | 
				
			||||||
import io
 | 
					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
 | 
					    closed      =   True
 | 
				
			||||||
    save_path    =   str()
 | 
					    save_path    =   str()
 | 
				
			||||||
    fileEntry   =   None
 | 
					    fileEntry   =   None
 | 
				
			||||||
    index       =   int()
 | 
					    index       =   0
 | 
				
			||||||
    filePtr     =   None
 | 
					    filePtr     =   None
 | 
				
			||||||
    downloaded  =   int()
 | 
					    downloaded  =   0
 | 
				
			||||||
    progress    =   float()
 | 
					    progress    =   0.0
 | 
				
			||||||
    pdl_thread  =   None
 | 
					    pdl_thread  =   None
 | 
				
			||||||
    def __init__(self, tfs, fileEntry, savePath, index):
 | 
					    def __init__(self, tfs, fileEntry, savePath, index):
 | 
				
			||||||
        self.tfs = tfs
 | 
					        self.tfs = tfs
 | 
				
			||||||
        self.fileEntry = fileEntry
 | 
					        self.fileEntry = fileEntry
 | 
				
			||||||
        self.name = self.Name()
 | 
					        self.name = self.Name()
 | 
				
			||||||
        self.unicode_name = self.name.decode(chardet.detect(self.name)['encoding'])
 | 
					        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.save_path = savePath
 | 
				
			||||||
        self.index = index
 | 
					        self.index = index
 | 
				
			||||||
        self.piece_length = int(self.pieceLength())
 | 
					        self.piece_length = int(self.pieceLength())
 | 
				
			||||||
@ -277,7 +278,8 @@ class TorrentFS(object):
 | 
				
			|||||||
        self.handle = handle
 | 
					        self.handle = handle
 | 
				
			||||||
        self.waitForMetadata()
 | 
					        self.waitForMetadata()
 | 
				
			||||||
        self.save_path = localize_path(self.root.torrentParams['save_path'])
 | 
					        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:
 | 
					        if startIndex < 0:
 | 
				
			||||||
            logging.info('No -file-index specified, downloading will be paused until any file is requested')
 | 
					            logging.info('No -file-index specified, downloading will be paused until any file is requested')
 | 
				
			||||||
        for i in range(self.TorrentInfo().num_files()):
 | 
					        for i in range(self.TorrentInfo().num_files()):
 | 
				
			||||||
@ -325,22 +327,26 @@ class TorrentFS(object):
 | 
				
			|||||||
        return self.info
 | 
					        return self.info
 | 
				
			||||||
    def LoadFileProgress(self):
 | 
					    def LoadFileProgress(self):
 | 
				
			||||||
        self.progresses = self.handle.file_progress()
 | 
					        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):
 | 
					    def getFileDownloadedBytes(self, i):
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            bytes = self.progresses[i]
 | 
					            bytes_ = self.progresses[i]
 | 
				
			||||||
        except IndexError:
 | 
					        except IndexError:
 | 
				
			||||||
            bytes = 0
 | 
					            bytes_ = 0
 | 
				
			||||||
        return bytes
 | 
					        return bytes_
 | 
				
			||||||
    def Files(self):
 | 
					    def Files(self):
 | 
				
			||||||
 | 
					        if len(self.files) > 0:
 | 
				
			||||||
 | 
					            return self.files
 | 
				
			||||||
        info = self.TorrentInfo()
 | 
					        info = self.TorrentInfo()
 | 
				
			||||||
        files = [None for x in range(info.num_files())]
 | 
					 | 
				
			||||||
        for i in range(info.num_files()):
 | 
					        for i in range(info.num_files()):
 | 
				
			||||||
            file_ = self.FileAt(i)
 | 
					            file_ = self.FileAt(i)
 | 
				
			||||||
            file_.downloaded = self.getFileDownloadedBytes(i)
 | 
					            file_.downloaded = self.getFileDownloadedBytes(i)
 | 
				
			||||||
            if file_.Size() > 0:
 | 
					            if file_.Size() > 0:
 | 
				
			||||||
                file_.progress = float(file_.downloaded)/float(file_.Size())
 | 
					                file_.progress = float(file_.downloaded)/float(file_.Size())
 | 
				
			||||||
            files[i] = file_
 | 
					            self.files.append(file_)
 | 
				
			||||||
        return files
 | 
					        return self.files
 | 
				
			||||||
    def FileAt(self, index):
 | 
					    def FileAt(self, index):
 | 
				
			||||||
        info = self.TorrentInfo()
 | 
					        info = self.TorrentInfo()
 | 
				
			||||||
        if index < 0 or index >= info.num_files():
 | 
					        if index < 0 or index >= info.num_files():
 | 
				
			||||||
@ -839,10 +845,11 @@ class Pyrrent2http(object):
 | 
				
			|||||||
                Url = 'http://' + self.config.bindAddress + '/files/' + urllib.quote(file_.name)
 | 
					                Url = 'http://' + self.config.bindAddress + '/files/' + urllib.quote(file_.name)
 | 
				
			||||||
                fi = {
 | 
					                fi = {
 | 
				
			||||||
                      'name':       file_.unicode_name,
 | 
					                      'name':       file_.unicode_name,
 | 
				
			||||||
 | 
					                      'media_type': file_.media_type,
 | 
				
			||||||
                      'size':       file_.size,
 | 
					                      'size':       file_.size,
 | 
				
			||||||
                      'offset':     file_.offset,
 | 
					                      'offset':     file_.offset,
 | 
				
			||||||
                      'download':   file_.Downloaded(),
 | 
					                      'download':   file_.downloaded,
 | 
				
			||||||
                      'progress':   file_.Progress(),
 | 
					                      'progress':   file_.progress,
 | 
				
			||||||
                      'save_path':  file_.save_path,
 | 
					                      'save_path':  file_.save_path,
 | 
				
			||||||
                      'url':        Url
 | 
					                      'url':        Url
 | 
				
			||||||
                      }
 | 
					                      }
 | 
				
			||||||
@ -900,6 +907,7 @@ class Pyrrent2http(object):
 | 
				
			|||||||
        for alert in alerts:
 | 
					        for alert in alerts:
 | 
				
			||||||
            if isinstance(alert, lt.save_resume_data_alert):
 | 
					            if isinstance(alert, lt.save_resume_data_alert):
 | 
				
			||||||
                self.processSaveResumeDataAlert(alert)
 | 
					                self.processSaveResumeDataAlert(alert)
 | 
				
			||||||
 | 
					                break
 | 
				
			||||||
    def waitForAlert(self, alertClass, timeout):
 | 
					    def waitForAlert(self, alertClass, timeout):
 | 
				
			||||||
        start = time.time()
 | 
					        start = time.time()
 | 
				
			||||||
        while True:
 | 
					        while True:
 | 
				
			||||||
@ -920,7 +928,7 @@ class Pyrrent2http(object):
 | 
				
			|||||||
            if self.forceShutdown:
 | 
					            if self.forceShutdown:
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
            if time.time() - time_start > 0.5:
 | 
					            if time.time() - time_start > 0.5:
 | 
				
			||||||
                #self.consumeAlerts()
 | 
					                self.consumeAlerts()
 | 
				
			||||||
                self.TorrentFS.LoadFileProgress()
 | 
					                self.TorrentFS.LoadFileProgress()
 | 
				
			||||||
                state = self.torrentHandle.status().state
 | 
					                state = self.torrentHandle.status().state
 | 
				
			||||||
                if self.config.exitOnFinish and (state == state.finished or state == state.seeding):
 | 
					                if self.config.exitOnFinish and (state == state.finished or state == state.seeding):
 | 
				
			||||||
@ -932,6 +940,7 @@ class Pyrrent2http(object):
 | 
				
			|||||||
                self.stats()
 | 
					                self.stats()
 | 
				
			||||||
            if self.saveResumeDataTicker.true:
 | 
					            if self.saveResumeDataTicker.true:
 | 
				
			||||||
                self.saveResumeData(True)
 | 
					                self.saveResumeData(True)
 | 
				
			||||||
 | 
					            time.sleep(0.3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def processSaveResumeDataAlert(self, alert):
 | 
					    def processSaveResumeDataAlert(self, alert):
 | 
				
			||||||
        logging.info('Saving resume data to: %s' % (self.config.resumeFile))
 | 
					        logging.info('Saving resume data to: %s' % (self.config.resumeFile))
 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +1,12 @@
 | 
				
			|||||||
import sys
 | 
					import sys
 | 
				
			||||||
import socket
 | 
					import socket
 | 
				
			||||||
import chardet
 | 
					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):
 | 
					class Struct(dict):
 | 
				
			||||||
    def __getattr__(self, attr):
 | 
					    def __getattr__(self, attr):
 | 
				
			||||||
@ -9,6 +14,21 @@ class Struct(dict):
 | 
				
			|||||||
    def __setattr__(self, attr, value):
 | 
					    def __setattr__(self, attr, value):
 | 
				
			||||||
        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):
 | 
					def localize_path(path):
 | 
				
			||||||
    path = path.decode(chardet.detect(path)['encoding'])
 | 
					    path = path.decode(chardet.detect(path)['encoding'])
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user