diff --git a/addon.xml b/addon.xml index e030ec22..95d5adad 100644 --- a/addon.xml +++ b/addon.xml @@ -1,7 +1,7 @@ - + diff --git a/lib/btserver/__init__.py b/lib/btserver/__init__.py deleted file mode 100644 index d8828c0f..00000000 --- a/lib/btserver/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -# -*- coding: utf-8 -*- - -from client import Client - -__all__ = ["Client"] diff --git a/lib/btserver/cache.py b/lib/btserver/cache.py deleted file mode 100644 index 81605bdb..00000000 --- a/lib/btserver/cache.py +++ /dev/null @@ -1,92 +0,0 @@ -# -*- coding: utf-8 -*- -# ------------------------------------------------------------ -# Gestiona el cache del servidor torrent: -# Guarda los .torrent generado -# Guarda los .resume de cada torrent -# ------------------------------------------------------------ -import base64 -import os.path -import re -import traceback - -try: - import xbmc, xbmcgui -except: - pass - -from platformcode import config -LIBTORRENT_PATH = config.get_setting("libtorrent_path", server="torrent", default='') - -from servers import torrent as torr -lt, e, e1, e2 = torr.import_libtorrent(LIBTORRENT_PATH) - - -class Cache(object): - CACHE_DIR = '.cache' - - def __init__(self, path): - - if not os.path.isdir(path): - os.makedirs(path) - self.path = os.path.join(path, Cache.CACHE_DIR) - if not os.path.isdir(self.path): - os.makedirs(self.path) - - def _tname(self, info_hash): - return os.path.join(self.path, info_hash.upper() + '.torrent') - - def _rname(self, info_hash): - return os.path.join(self.path, info_hash.upper() + '.resume') - - def save_resume(self, info_hash, data): - f = open(self._rname(info_hash), 'wb') - f.write(data) - f.close() - - def get_resume(self, url=None, info_hash=None): - if url: - info_hash = self._index.get(url) - if not info_hash: - return - rname = self._rname(info_hash) - if os.access(rname, os.R_OK): - f = open(rname, 'rb') - v = f.read() - f.close() - return v - - def file_complete(self, torrent): - info_hash = str(torrent.info_hash()) - nt = lt.create_torrent(torrent) - tname = self._tname(info_hash) - f = open(tname, 'wb') - f.write(lt.bencode(nt.generate())) - f.close() - - def get_torrent(self, url=None, info_hash=None): - if url: - info_hash = self._index.get(url) - if not info_hash: - return - tname = self._tname(info_hash) - if os.access(tname, os.R_OK): - return tname - - magnet_re = re.compile('xt=urn:btih:([0-9A-Za-z]+)') - hexa_chars = re.compile('^[0-9A-F]+$') - - @staticmethod - def hash_from_magnet(m): - res = Cache.magnet_re.search(m) - if res: - ih = res.group(1).upper() - if len(ih) == 40 and Cache.hexa_chars.match(ih): - return res.group(1).upper() - elif len(ih) == 32: - s = base64.b32decode(ih) - return "".join("{:02X}".format(ord(c)) for c in s) - else: - raise ValueError('Not BT magnet link') - - else: - raise ValueError('Not BT magnet link') diff --git a/lib/btserver/client.py b/lib/btserver/client.py deleted file mode 100644 index 3233dfaf..00000000 --- a/lib/btserver/client.py +++ /dev/null @@ -1,669 +0,0 @@ -# -*- coding: utf-8 -*- - -import os -import pickle -import random -import time -import urllib - -try: - import xbmc, xbmcgui -except: - pass - -from platformcode import config, logger -LIBTORRENT_PATH = config.get_setting("libtorrent_path", server="torrent", default='') - -from servers import torrent as torr -lt, e, e1, e2 = torr.import_libtorrent(LIBTORRENT_PATH) - -from cache import Cache -from dispatcher import Dispatcher -from file import File -from handler import Handler -from monitor import Monitor -from resume_data import ResumeData -from server import Server - -try: - BUFFER = int(config.get_setting("bt_buffer", server="torrent", default="50")) -except: - BUFFER = 50 - config.set_setting("bt_buffer", "50", server="torrent") -DOWNLOAD_PATH = config.get_setting("bt_download_path", server="torrent", default=config.get_setting("downloadpath")) -BACKGROUND = config.get_setting("mct_background_download", server="torrent", default=True) -RAR = config.get_setting("mct_rar_unpack", server="torrent", default=True) -msg_header = 'Client Torrent BT' - - -class Client(object): - INITIAL_TRACKERS = ['udp://tracker.openbittorrent.com:80', - 'udp://tracker.istole.it:80', - 'udp://open.demonii.com:80', - 'udp://tracker.coppersurfer.tk:80', - 'udp://tracker.leechers-paradise.org:6969', - 'udp://exodus.desync.com:6969', - 'udp://tracker.publicbt.com:80', - 'http://tracker.torrentbay.to:6969/announce', - 'http://tracker.pow7.com/announce', - 'udp://tracker.ccc.de:80/announce', - 'udp://open.demonii.com:1337', - 'http://9.rarbg.com:2710/announce', - 'http://bt.careland.com.cn:6969/announce', - 'http://explodie.org:6969/announce', - 'http://mgtracker.org:2710/announce', - 'http://tracker.best-torrents.net:6969/announce', - 'http://tracker.tfile.me/announce', - 'http://tracker1.wasabii.com.tw:6969/announce', - 'udp://9.rarbg.com:2710/announce', - 'udp://9.rarbg.me:2710/announce', - 'udp://coppersurfer.tk:6969/announce', - 'http://www.spanishtracker.com:2710/announce', - 'http://www.todotorrents.com:2710/announce' - ] ### Added some trackers from MCT - - VIDEO_EXTS = {'.avi': 'video/x-msvideo', '.mp4': 'video/mp4', '.mkv': 'video/x-matroska', - '.m4v': 'video/mp4', '.mov': 'video/quicktime', '.mpg': 'video/mpeg', '.ogv': 'video/ogg', - '.ogg': 'video/ogg', '.webm': 'video/webm', '.ts': 'video/mp2t', '.3gp': 'video/3gpp', - '.rar': 'video/unrar'} - - def __init__(self, url=None, port=None, ip=None, auto_shutdown=True, wait_time=20, timeout=5, auto_delete=True, - temp_path=None, is_playing_fnc=None, print_status=False): - - # server - if port: - self.port = port - else: - self.port = random.randint(8000, 8099) - if ip: - self.ip = ip - else: - self.ip = "127.0.0.1" - self.server = Server((self.ip, self.port), Handler, client=self) - - # Options - if temp_path: - self.temp_path = temp_path - else: - self.temp_path = DOWNLOAD_PATH - self.is_playing_fnc = is_playing_fnc - self.timeout = timeout - self.auto_delete = auto_delete - self.wait_time = wait_time - self.auto_shutdown = auto_shutdown - self.buffer_size = BUFFER - self.first_pieces_priorize = BUFFER - self.last_pieces_priorize = 5 - self.state_file = "state" - try: - self.torrent_paramss = {'save_path': self.temp_path, 'storage_mode': lt.storage_mode_t.storage_mode_allocate} - except Exception as e: - try: - do = xbmcgui.Dialog() - e = e1 or e2 - do.ok(config.get_localized_string(30035) + 'BT Libtorrent', config.get_localized_string(30036), config.get_localized_string(60015), str(e)) - except: - pass - return - - # State - self.has_meta = False - self.meta = None - self.start_time = None - self.last_connect = 0 - self.connected = False - self.closed = False - self.file = None - self.files = None - self._th = None - self.seleccion = 0 - self.index = 0 - - # Sesion - self._cache = Cache(self.temp_path) - self._ses = lt.session() - #self._ses.listen_on(0, 0) ### ALFA: it blocks repro of some .torrents - # Cargamos el archivo de estado (si existe) - """ ### ALFA: it blocks repro of some .torrents - if os.path.exists(os.path.join(self.temp_path, self.state_file)): - try: - f = open(os.path.join(self.temp_path, self.state_file), "rb") - state = pickle.load(f) - self._ses.load_state(state) - f.close() - except: - pass - """ - - self._start_services() - - # Monitor & Dispatcher - self._monitor = Monitor(self) - if print_status: - self._monitor.add_listener(self.print_status) - self._monitor.add_listener(self._check_meta) - self._monitor.add_listener(self.save_state) - self._monitor.add_listener(self.priorize_start_file) - self._monitor.add_listener(self.announce_torrent) - - if self.auto_shutdown: - self._monitor.add_listener(self._auto_shutdown) - - self._dispatcher = Dispatcher(self) - self._dispatcher.add_listener(self._update_ready_pieces) - - # Iniciamos la URL - if url: - self.start_url(url) - - def set_speed_limits(self, download=0, upload=0): - """ - Función encargada de poner límites a la velocidad de descarga o subida - """ - if isinstance(download, int) and download > 0: - self._th.set_download_limit(download * 1024) - if isinstance(upload, int) and download > 0: - self._th.set_upload_limit(upload * 1024) - - def get_play_list(self): - """ - Función encargada de generar el playlist - """ - # Esperamos a lo metadatos - while not self.has_meta: - time.sleep(1) - - # Comprobamos que haya archivos de video - if self.files: - if len(self.files) > 1: - return "http://" + self.ip + ":" + str(self.port) + "/playlist.pls" - else: - return "http://" + self.ip + ":" + str(self.port) + "/" + urllib.quote(self.files[0].path) - - def get_files(self): - """ - Función encargada de genera el listado de archivos - """ - # Esperamos a lo metadatos - while not self.has_meta: - time.sleep(1) - files = [] - - # Comprobamos que haya archivos de video - if self.files: - # Creamos el dict con los archivos - for file in self.files: - n = file.path - u = "http://" + self.ip + ":" + str(self.port) + "/" + urllib.quote(n) - s = file.size - files.append({"name": n, "url": u, "size": s}) - - return files - - def _find_files(self, files, search=None): - """ - Función encargada de buscar los archivos reproducibles del torrent - """ - self.total_size = 0 - # Obtenemos los archivos que la extension este en la lista - videos = filter(lambda f: self.VIDEO_EXTS.has_key(os.path.splitext(f.path)[1]), files) - - if not videos: - raise Exception('No video files in torrent') - for v in videos: - self.total_size += v.size ### ALFA - videos[videos.index(v)].index = files.index(v) - return videos - - def set_file(self, f): - """ - Función encargada de seleccionar el archivo que vamos a servir y por tanto, priorizar su descarga - """ - # Seleccionamos el archivo que vamos a servir - fmap = self.meta.map_file(f.index, 0, 1) - self.file = File(f.path, self.temp_path, f.index, f.size, fmap, self.meta.piece_length(), self) - if self.seleccion < 0: ### ALFA - self.file.first_piece = 0 ### ALFA - self.file.last_piece = self.meta.num_pieces() ### ALFA - self.file.size = self.total_size ### ALFA - self.prioritize_file() - - def prioritize_piece(self, pc, idx): - """ - Función encargada de priorizar una determinada pieza - """ - piece_duration = 1000 - min_deadline = 2000 - dl = idx * piece_duration + min_deadline - """ ### ALFA - try: - self._th.set_piece_deadline(pc, dl, lt.deadline_flags.alert_when_available) - except: - pass - """ - - if idx == 0: - tail_pieces = 9 - # Piezas anteriores a la primera se desactivan - if (self.file.last_piece - pc) > tail_pieces: - for i in xrange(self.file.first_piece, pc): - self._th.piece_priority(i, 0) - self._th.reset_piece_deadline(i) - - # Piezas siguientes a la primera se activan - for i in xrange(pc + 1, self.file.last_piece + 1): - #self._th.piece_priority(i, 0) - self._th.piece_priority(i, 1) - - def prioritize_file(self): - """ - Función encargada de priorizar las piezas correspondientes al archivo seleccionado en la funcion set_file() - """ - priorities = [] - for i in xrange(self.meta.num_pieces()): - if i >= self.file.first_piece and i <= self.file.last_piece: - priorities.append(1) - else: - if self.index < 0: - priorities.append(1) ### ALFA - else: - priorities.append(0) ### ALFA - - self._th.prioritize_pieces(priorities) - - x = 0 - for i, _set in enumerate(self._th.piece_priorities()): - if _set > 0: x += 1 - #logger.info("***** Nº Pieza: %s: %s" % (i, str(_set))) - logger.info("***** Piezas %s : Activas: %s" % (str(i+1), str(x))) - logger.info("***** first_piece %s : last_piece: %s" % (str(self.file.first_piece), str(self.file.last_piece))) - - def download_torrent(self, url): - """ - Función encargada de descargar un archivo .torrent - """ - from core import httptools - - data = httptools.downloadpage(url).data - return data - - def start_url(self, uri): - """ - Función encargada iniciar la descarga del torrent desde la url, permite: - - Url apuntando a un .torrent - - Url magnet - - Archivo .torrent local - """ - - if self._th: - raise Exception('Torrent is already started') - - if uri.startswith('http://') or uri.startswith('https://'): - torrent_data = self.download_torrent(uri) - info = lt.torrent_info(lt.bdecode(torrent_data)) - tp = {'ti': info} - resume_data = self._cache.get_resume(info_hash=str(info.info_hash())) - if resume_data: - tp['resume_data'] = resume_data - - elif uri.startswith('magnet:'): - tp = {'url': uri} - resume_data = self._cache.get_resume(info_hash=Cache.hash_from_magnet(uri)) - if resume_data: - tp['resume_data'] = resume_data - - elif os.path.isfile(uri): - if os.access(uri, os.R_OK): - info = lt.torrent_info(uri) - tp = {'ti': info} - resume_data = self._cache.get_resume(info_hash=str(info.info_hash())) - if resume_data: - tp['resume_data'] = resume_data - else: - raise ValueError('Invalid torrent path %s' % uri) - else: - raise ValueError("Invalid torrent %s" % uri) - - tp.update(self.torrent_paramss) - self._th = self._ses.add_torrent(tp) - - for tr in self.INITIAL_TRACKERS: - self._th.add_tracker({'url': tr}) - - self._th.set_sequential_download(True) - self._th.force_reannounce() - self._th.force_dht_announce() - - self._monitor.start() - self._dispatcher.do_start(self._th, self._ses) - self.server.run() - - def stop(self): - """ - Función encargada de de detener el torrent y salir - """ - self._dispatcher.stop() - self._dispatcher.join() - self._monitor.stop() - self.server.stop() - self._dispatcher.stop() - if self._ses: - self._ses.pause() - if self._th: - self.save_resume() - self.save_state() - self._stop_services() - self._ses.remove_torrent(self._th, self.auto_delete) - del self._ses - self.closed = True - - def pause(self): - """ - Función encargada de de pausar el torrent - """ - self._ses.pause() - - def _start_services(self): - """ - Función encargada de iniciar los servicios de libtorrent: dht, lsd, upnp, natpnp - """ - self._ses.add_dht_router("router.bittorrent.com", 6881) - self._ses.add_dht_router("router.bitcomet.com", 554) - self._ses.add_dht_router("router.utorrent.com", 6881) - self._ses.add_dht_router("dht.transmissionbt.com",6881) ### from MCT - self._ses.start_dht() - self._ses.start_lsd() - self._ses.start_upnp() - self._ses.start_natpmp() - - def _stop_services(self): - """ - Función encargada de detener los servicios de libtorrent: dht, lsd, upnp, natpnp - """ - self._ses.stop_natpmp() - self._ses.stop_upnp() - self._ses.stop_lsd() - self._ses.stop_dht() - - def save_resume(self): - """ - Función encargada guardar los metadatos para continuar una descarga mas rapidamente - """ - if self._th.need_save_resume_data() and self._th.is_valid() and self.meta: - r = ResumeData(self) - start = time.time() - while (time.time() - start) <= 5: - if r.data or r.failed: - break - time.sleep(0.1) - if r.data: - self._cache.save_resume(self.unique_file_id, lt.bencode(r.data)) - - @property - def status(self): - """ - Función encargada de devolver el estado del torrent - """ - if self._th: - s = self._th.status() - # Download Rate - s._download_rate = s.download_rate / 1024 - - # Progreso del archivo - if self.file: - pieces = s.pieces[self.file.first_piece:self.file.last_piece] ### ALFA - progress = float(sum(pieces)) / len(pieces) - s.pieces_len = len(pieces) ### ALFA - s.pieces_sum = sum(pieces) ### ALFA - #logger.info('***** Estado piezas: %s' % pieces) - else: - progress = 0 - s.pieces_len = 0 ### ALFA - s.pieces_sum = 0 ### ALFA - - s.progress_file = progress * 100 - - # Tamaño del archivo - s.file_name = '' ### ALFA - s.seleccion = '' ### ALFA - - if self.file: - s.seleccion = self.seleccion ### ALFA - s.file_name = self.file.path ### ALFA - s.file_size = self.file.size / 1048576.0 - else: - s.file_size = 0 - - # Estado del buffer - if self.file and self.file.cursor: # Con una conexion activa: Disponible vs Posicion del reproductor - percent = len(self.file.cursor.cache) - percent = percent * 100 / self.buffer_size - s.buffer = int(percent) - - elif self.file: # Sin una conexion activa: Pre-buffer antes de iniciar - # El Pre-buffer consta de dos partes_ - # 1. Buffer al inicio del archivo para que el reproductor empieze sin cortes - # 2. Buffer al final del archivo (en algunos archivos el reproductor mira el final del archivo antes de comenzar) - bp = [] - - # El tamaño del buffer de inicio es el tamaño del buffer menos el tamaño del buffer del final - first_pieces_priorize = self.buffer_size - self.last_pieces_priorize - - # Comprobamos qué partes del buffer del inicio estan disponibles - for x in range(first_pieces_priorize): - if self._th.have_piece(self.file.first_piece + x): - bp.append(True) - else: - bp.append(False) - - # Comprobamos qué partes del buffer del final estan disponibles - for x in range(self.last_pieces_priorize): - if self._th.have_piece(self.file.last_piece - x): - bp.append(True) - else: - bp.append(False) - - s.buffer = int(sum(bp) * 100 / self.buffer_size) - - else: # Si no hay ningun archivo seleccionado: No hay buffer - s.buffer = 0 - - # Tiempo restante para cerrar en caso de tener el timeout activo - if self.auto_shutdown: - if self.connected: - if self.timeout: - s.timeout = int(self.timeout - (time.time() - self.last_connect - 1)) - if self.file and self.file.cursor: - s.timeout = self.timeout - if s.timeout < 0: s.timeout = "Cerrando" - else: - s.timeout = "---" - else: - if self.start_time and self.wait_time: - s.timeout = int(self.wait_time - (time.time() - self.start_time - 1)) - if s.timeout < 0: s.timeout = "Cerrando" - else: - s.timeout = "---" - - else: - s.timeout = "Off" - - # Estado de la descarga - STATE_STR = ['Queued', 'Checking', 'Downloading Metadata', \ - 'Downloading', 'Finalized', 'Seeding', 'Allocating', 'Checking Fastresume'] - s.str_state = STATE_STR[s.state] - - # Estado DHT - if self._ses.dht_state() is not None: - s.dht_state = "On" - s.dht_nodes = self._ses.status().dht_nodes - else: - s.dht_state = "Off" - s.dht_nodes = 0 - - # Cantidad de Trackers - s.trackers = len(self._th.trackers()) - - # Origen de los peers - s.dht_peers = 0 - s.trk_peers = 0 - s.pex_peers = 0 - s.lsd_peers = 0 - - for peer in self._th.get_peer_info(): - if peer.source & 1: - s.trk_peers += 1 - if peer.source & 2: - s.dht_peers += 1 - if peer.source & 4: - s.pex_peers += 1 - if peer.source & 8: - s.lsd_peers += 1 - - return s - - """ - Servicios: - - Estas funciones se ejecutan de forma automatica cada x tiempo en otro Thread. - - Estas funciones son ejecutadas mientras el torrent esta activo algunas pueden desactivarse - segun la configuracion como por ejemplo la escritura en el log - """ - - def _auto_shutdown(self, *args, **kwargs): - """ - Servicio encargado de autoapagar el servidor - """ - if self.file and self.file.cursor: - self.last_connect = time.time() - self.connected = True - - if self.is_playing_fnc and self.is_playing_fnc(): - self.last_connect = time.time() - self.connected = True - - if self.auto_shutdown: - # shudown por haber cerrado el reproductor - if self.connected and self.is_playing_fnc and not self.is_playing_fnc(): - if time.time() - self.last_connect - 1 > self.timeout: - self.stop() - - # shutdown por no realizar ninguna conexion - if (not self.file or not self.file.cursor) and self.start_time and self.wait_time and not self.connected: - if time.time() - self.start_time - 1 > self.wait_time: - self.stop() - - # shutdown tras la ultima conexion - if (not self.file or not self.file.cursor) and self.timeout and self.connected and not self.is_playing_fnc: - if time.time() - self.last_connect - 1 > self.timeout: - self.stop() - - def announce_torrent(self): - """ - Servicio encargado de anunciar el torrent - """ - self._th.force_reannounce() - self._th.force_dht_announce() - - def save_state(self): - """ - Servicio encargado de guardar el estado - """ - state = self._ses.save_state() - f = open(os.path.join(self.temp_path, self.state_file), 'wb') - pickle.dump(state, f) - f.close() - - def _update_ready_pieces(self, alert_type, alert): - """ - Servicio encargado de informar que hay una pieza disponible - """ - if alert_type == 'read_piece_alert' and self.file: - self.file.update_piece(alert.piece, alert.buffer) - - def _check_meta(self): - """ - Servicio encargado de comprobar si los metadatos se han descargado - """ - if self.status.state >= 3 and self.status.state <= 5 and not self.has_meta: - - # Guardamos los metadatos - self.meta = self._th.get_torrent_info() - - # Obtenemos la lista de archivos del meta - fs = self.meta.files() - if isinstance(fs, list): - files = fs - else: - files = [fs.at(i) for i in xrange(fs.num_files())] - - # Guardamos la lista de archivos - self.files = self._find_files(files) - - # Si hay varios vídeos (no RAR), se selecciona el vídeo o "todos" - lista = [] - seleccion = 0 - for file in self.files: - if '.rar' in str(file.path): - seleccion = -9 - lista += [os.path.split(str(file.path))[1]] - if len(lista) > 1 and seleccion >= 0: - d = xbmcgui.Dialog() - seleccion = d.select(msg_header + config.get_localized_string(30034), lista) - - if seleccion < 0: - index = 0 - self.index = seleccion - else: - index = seleccion - self.index = self.files[index].index - self.seleccion = seleccion - - # Marcamos el primer archivo como activo - self.set_file(self.files[index]) - - # Damos por iniciada la descarga - self.start_time = time.time() - - # Guardamos el .torrent en el cache - self._cache.file_complete(self._th.get_torrent_info()) - - self.has_meta = True - - def priorize_start_file(self): - ''' - Servicio encargado de priorizar el principio y final de archivo cuando no hay conexion - ''' - if self.file and not self.file.cursor: - num_start_pieces = self.buffer_size - self.last_pieces_priorize # Cantidad de piezas a priorizar al inicio - num_end_pieces = self.last_pieces_priorize # Cantidad de piezas a priorizar al final - - pieces_count = 0 - # Priorizamos las ultimas piezas - for y in range(self.file.last_piece - num_end_pieces, self.file.last_piece + 1): - if not self._th.have_piece(y): - self.prioritize_piece(y, pieces_count) - pieces_count += 1 - - # Priorizamos las primeras piezas - for y in range(self.file.first_piece, self.file.last_piece + 1): - if not self._th.have_piece(y): - if pieces_count == self.buffer_size: - break - self.prioritize_piece(y, pieces_count) - pieces_count += 1 - - def print_status(self): - ''' - Servicio encargado de mostrar en el log el estado de la descarga - ''' - s = self.status ### ALFA - if self.seleccion >= 0: - archivo = self.seleccion + 1 - else: - archivo = self.seleccion - - logger.info( - '%.2f%% de %.1fMB %s | %.1f kB/s | #%s %d%% | AutoClose: %s | S: %d(%d) P: %d(%d)) | TRK: %d DHT: %d PEX: %d LSD %d | DHT:%s (%d) | Trakers: %d | Pieces: %d (%d)' % \ - (s.progress_file, s.file_size, s.str_state, s._download_rate, archivo, s.buffer, s.timeout, s.num_seeds, \ - s.num_complete, s.num_peers, s.num_incomplete, s.trk_peers, s.dht_peers, s.pex_peers, s.lsd_peers, - s.dht_state, s.dht_nodes, s.trackers, s.pieces_sum, s.pieces_len)) ### ALFA diff --git a/lib/btserver/cursor.py b/lib/btserver/cursor.py deleted file mode 100644 index 5ecccfda..00000000 --- a/lib/btserver/cursor.py +++ /dev/null @@ -1,106 +0,0 @@ -# -*- coding: utf-8 -*- - -from threading import Lock, Event - - -class Cursor(object): - def __init__(self, file): - self._file = file - self.pos = 0 - self.timeout = 30 - self.cache_size = self._file._client.buffer_size - self.cache = {} - self.lock = Lock() - self.event = Event() - self.cache_first = 0 - - def fill_cache(self, first): - self.cache_first = first - - with self.lock: - for p in sorted(self.cache): - if p < first: del self.cache[p] - - self.event.clear() - for i in xrange(first, first + self.cache_size): - if i <= self._file.last_piece: - self._file._client.prioritize_piece(i, i - first) - - def has_piece(self, n): - with self.lock: - return n in self.cache - - def _wait_piece(self, pc_no): - while not self.has_piece(pc_no): - self.fill_cache(pc_no) - self.event.wait(self.timeout) - - def _get_piece(self, n): - with self.lock: - if not n in self.cache: - raise ValueError('index of of scope of current cache') - return self.cache[n] - - def get_piece(self, n): - self._wait_piece(n) - return self._get_piece(n) - - def close(self): - self._file.cursor = None - - def read(self, size=None): - data = "" - max_size = self._file.size - self.pos - if not size: - size = max_size - else: - size = min(size, max_size) - - if size: - pc_no, ofs = self._file.map_piece(self.pos) - data = self.get_piece(pc_no)[ofs: ofs + size] - - if len(data) < size: - remains = size - len(data) - pc_no += 1 - self.fill_cache(pc_no) - while remains and self.has_piece(pc_no): - sz = min(remains, self._file.piece_size) - data += self.get_piece(pc_no)[:sz] - remains -= sz - if remains: - pc_no += 1 - self.fill_cache(pc_no) - - self.pos += len(data) - - return data - - def seek(self, n): - if n > self._file.size: - n = self._file.size - elif n < 0: - raise ValueError('Seeking negative') - self.pos = n - - def tell(self): - return self.pos - - def update_piece(self, n, data): - with self.lock: - pcs = sorted(self.cache) - if len(pcs) < self.cache_size: - if len(pcs): - new = max(pcs) + 1 - else: - new = self.cache_first - if n == new: - self.cache[n] = data - if n == self.cache_first: - self.event.set() - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_val, exc_tb): - self.close() diff --git a/lib/btserver/dispatcher.py b/lib/btserver/dispatcher.py deleted file mode 100644 index c0edfa01..00000000 --- a/lib/btserver/dispatcher.py +++ /dev/null @@ -1,39 +0,0 @@ -# -*- coding: utf-8 -*- - -from monitor import Monitor - -import traceback - -try: - import xbmc, xbmcgui -except: - pass - -from platformcode import config -LIBTORRENT_PATH = config.get_setting("libtorrent_path", server="torrent", default='') - -from servers import torrent as torr -lt, e, e1, e2 = torr.import_libtorrent(LIBTORRENT_PATH) - - -class Dispatcher(Monitor): - def __init__(self, client): - super(Dispatcher, self).__init__(client) - - def do_start(self, th, ses): - self._th = th - self._ses = ses - self.start() - - def run(self): - if not self._ses: - raise Exception('Invalid state, session is not initialized') - - while self.running: - a = self._ses.wait_for_alert(1000) - if a: - alerts = self._ses.pop_alerts() - for alert in alerts: - with self.lock: - for cb in self.listeners: - cb(lt.alert.what(alert), alert) diff --git a/lib/btserver/file.py b/lib/btserver/file.py deleted file mode 100644 index 0d8d9742..00000000 --- a/lib/btserver/file.py +++ /dev/null @@ -1,39 +0,0 @@ -# -*- coding: utf-8 -*- - -import os - -from cursor import Cursor - - -class File(object): - def __init__(self, path, base, index, size, fmap, piece_size, client): - self._client = client - self.path = path - self.base = base - self.index = index - self.size = size - - self.piece_size = piece_size - - self.full_path = os.path.join(base, path) - self.first_piece = fmap.piece - self.offset = fmap.start - self.last_piece = self.first_piece + max((size - 1 + fmap.start), 0) // piece_size - - self.cursor = None - - def create_cursor(self, offset=None): - self.cursor = Cursor(self) - if offset: - self.cursor.seek(offset) - return self.cursor - - def map_piece(self, ofs): - return self.first_piece + (ofs + self.offset) // self.piece_size, (ofs + self.offset) % self.piece_size - - def update_piece(self, n, data): - if self.cursor: - self.cursor.update_piece(n, data) - - def __str__(self): - return self.path diff --git a/lib/btserver/handler.py b/lib/btserver/handler.py deleted file mode 100644 index 7ddb9705..00000000 --- a/lib/btserver/handler.py +++ /dev/null @@ -1,135 +0,0 @@ -# -*- coding: utf-8 -*- - -import BaseHTTPServer -import os -import re -import time -import types -import urllib -import urlparse - -RANGE_RE = re.compile(r'bytes=(\d+)-') - - -def parse_range(range): # @ReservedAssignment - if range: - m = RANGE_RE.match(range) - if m: - try: - return int(m.group(1)) - except: - pass - return 0 - - -class Handler(BaseHTTPServer.BaseHTTPRequestHandler): - protocol_version = 'HTTP/1.1' - - def log_message(self, format, *args): - pass - - def do_GET(self): - if self.server.request: - self.server.request.wfile.close() - self.server.request = self - - if self.do_HEAD(): - f = self.server.file.create_cursor(self.offset) - while f == self.server.file.cursor: - buf = f.read(1024) - if buf: - try: - self.wfile.write(buf) - except: - break - else: - break - f.close() - - def send_pls(self, files): - playlist = "[playlist]\n\n" - for x, f in enumerate(files): - playlist += "File" + str(x + 1) + "=http://127.0.0.1:" + str(self.server._client.port) + "/" + urllib.quote( - f.path) + "\n" - playlist += "Title" + str(x + 1) + "=" + f.path + "\n" - playlist += "NumberOfEntries=" + str(len(files)) - playlist += "Version=2" - self.send_response(200, 'OK') - self.send_header("Content-Length", str(len(playlist))) - self.finish_header() - self.wfile.write(playlist) - - def do_HEAD(self): - url = urlparse.urlparse(self.path).path - - '''Whait to list of files ''' - while not self.server._client.files: - time.sleep(1) - - files = self.server._client.files - self.server.file = self.server._client.file - - '''Creates PLS playlist ''' - if url == "/playlist.pls": - self.send_pls(files) - return False - - '''Change File to download ''' - if not self.server.file or urllib.unquote(url) != '/' + self.server.file.path: - file = urllib.unquote(url) - client = self.server._client - for f in client.files: - if file == '/' + f.path: - client.set_file(f) - self.server.file = client.file - break - - while not self.server._client.has_meta: - time.sleep(1) - if self.server.file and urllib.unquote(url) == '/' + self.server.file.path: - self.offset = 0 - size, mime = self._file_info() - range = parse_range(self.headers.get('Range', None)) - if range: - self.offset = range - range = (range, size - 1, size) - - self.send_resp_header(mime, size, range) - return True - - else: - self.send_error(404, 'Not Found') - - def _file_info(self): - size = self.server.file.size - ext = os.path.splitext(self.server.file.path)[1] - mime = self.server._client.VIDEO_EXTS.get(ext) - if not mime: - mime = 'application/octet-stream' - return size, mime - - def send_resp_header(self, cont_type, cont_length, range=False): # @ReservedAssignment - if range: - self.send_response(206, 'Partial Content') - else: - self.send_response(200, 'OK') - - self.send_header('Content-Type', cont_type) - self.send_header('transferMode.dlna.org', 'Streaming') - self.send_header('contentFeatures.dlna.org', - 'DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000') - self.send_header('Accept-Ranges', 'bytes') - - if range: - if isinstance(range, (types.TupleType, types.ListType)) and len(range) == 3: - self.send_header('Content-Range', 'bytes %d-%d/%d' % range) - self.send_header('Content-Length', range[1] - range[0] + 1) - else: - raise ValueError('Invalid range value') - else: - self.send_header('Content-Length', cont_length) - self.finish_header() - - def finish_header(self): - self.send_header('Connection', 'close') - self.end_headers() diff --git a/lib/btserver/monitor.py b/lib/btserver/monitor.py deleted file mode 100644 index 9fd40c70..00000000 --- a/lib/btserver/monitor.py +++ /dev/null @@ -1,44 +0,0 @@ -# -*- coding: utf-8 -*- - -from threading import Thread, Lock, Event - - -class Monitor(Thread): - def __init__(self, client): - Thread.__init__(self) - self.daemon = True - self.listeners = [] - self.lock = Lock() - self.wait_event = Event() - self.running = True - self.client = client - self.ses = None - self.client = client - - def stop(self): - self.running = False - self.wait_event.set() - - def add_listener(self, cb): - with self.lock: - if not cb in self.listeners: - self.listeners.append(cb) - - def remove_listener(self, cb): - with self.lock: - try: - self.listeners.remove(cb) - except ValueError: - pass - - def remove_all_listeners(self): - with self.lock: - self.listeners = [] - - def run(self): - while (self.running): - with self.lock: - for cb in self.listeners: - cb() - - self.wait_event.wait(1.0) diff --git a/lib/btserver/resume_data.py b/lib/btserver/resume_data.py deleted file mode 100644 index a0c9d3eb..00000000 --- a/lib/btserver/resume_data.py +++ /dev/null @@ -1,15 +0,0 @@ -# -*- coding: utf-8 -*- - -class ResumeData(object): - def __init__(self, client): - self.data = None - self.failed = False - client._dispatcher.add_listener(self._process_alert) - client._th.save_resume_data() - - def _process_alert(self, t, alert): - if t == 'save_resume_data_failed_alert': - self.failed = True - - elif t == 'save_resume_data_alert': - self.data = alert.resume_data diff --git a/lib/btserver/server.py b/lib/btserver/server.py deleted file mode 100644 index 17afecd4..00000000 --- a/lib/btserver/server.py +++ /dev/null @@ -1,37 +0,0 @@ -# -*- coding: utf-8 -*- - -import BaseHTTPServer -import traceback -from SocketServer import ThreadingMixIn -from threading import Thread - - -class Server(ThreadingMixIn, BaseHTTPServer.HTTPServer): - daemon_threads = True - timeout = 1 - - def __init__(self, address, handler, client): - BaseHTTPServer.HTTPServer.__init__(self, address, handler) - self._client = client - self.file = None - self.running = True - self.request = None - - def stop(self): - self.running = False - - def serve(self): - while self.running: - try: - self.handle_request() - except: - print traceback.format_exc() - - def run(self): - t = Thread(target=self.serve, name='HTTP Server') - t.daemon = True - t.start() - - def handle_error(self, request, client_address): - if not "socket.py" in traceback.format_exc(): - print traceback.format_exc() diff --git a/lib/python_libtorrent/linux_x86_64/1.1.1/libtorrent.so b/lib/python_libtorrent/linux_x86_64/1.1.1/libtorrent.so deleted file mode 100644 index bbfecbad..00000000 Binary files a/lib/python_libtorrent/linux_x86_64/1.1.1/libtorrent.so and /dev/null differ diff --git a/platformcode/custom_code.py b/platformcode/custom_code.py index 46ce5009..0881057f 100644 --- a/platformcode/custom_code.py +++ b/platformcode/custom_code.py @@ -70,11 +70,11 @@ def init(): verify_Kodi_video_DB() #LIBTORRENT: se descarga el binario de Libtorrent cada vez que se actualiza Alfa - try: - threading.Thread(target=update_libtorrent).start() # Creamos un Thread independiente, hasta el fin de Kodi - time.sleep(2) # Dejamos terminar la inicialización... - except: # Si hay problemas de threading, nos vamos - logger.error(traceback.format_exc()) + # try: + # threading.Thread(target=update_libtorrent).start() # Creamos un Thread independiente, hasta el fin de Kodi + # time.sleep(2) # Dejamos terminar la inicialización... + # except: # Si hay problemas de threading, nos vamos + # logger.error(traceback.format_exc()) # #QUASAR: Preguntamos si se hacen modificaciones a Quasar # if not filetools.exists(filetools.join(config.get_data_path(), "quasar.json")) \ @@ -245,102 +245,102 @@ def update_external_addon(addon_name): return False -def update_libtorrent(): - logger.info() +# def update_libtorrent(): +# logger.info() - if not config.get_setting("mct_buffer", server="torrent", default=""): - default = config.get_setting("torrent_client", server="torrent", default=0) - config.set_setting("torrent_client", default, server="torrent") - config.set_setting("mct_buffer", "50", server="torrent") - if config.get_setting("mct_download_path", server="torrent", default=config.get_setting("downloadpath")): - config.set_setting("mct_download_path", config.get_setting("downloadpath"), server="torrent") - config.set_setting("mct_background_download", True, server="torrent") - config.set_setting("mct_rar_unpack", True, server="torrent") - config.set_setting("bt_buffer", "50", server="torrent") - if config.get_setting("bt_download_path", server="torrent", default=config.get_setting("downloadpath")): - config.set_setting("bt_download_path", config.get_setting("downloadpath"), server="torrent") - config.set_setting("mct_download_limit", "", server="torrent") - config.set_setting("magnet2torrent", False, server="torrent") +# if not config.get_setting("mct_buffer", server="torrent", default=""): +# default = config.get_setting("torrent_client", server="torrent", default=0) +# config.set_setting("torrent_client", default, server="torrent") +# config.set_setting("mct_buffer", "50", server="torrent") +# if config.get_setting("mct_download_path", server="torrent", default=config.get_setting("downloadpath")): +# config.set_setting("mct_download_path", config.get_setting("downloadpath"), server="torrent") +# config.set_setting("mct_background_download", True, server="torrent") +# config.set_setting("mct_rar_unpack", True, server="torrent") +# config.set_setting("bt_buffer", "50", server="torrent") +# if config.get_setting("bt_download_path", server="torrent", default=config.get_setting("downloadpath")): +# config.set_setting("bt_download_path", config.get_setting("downloadpath"), server="torrent") +# config.set_setting("mct_download_limit", "", server="torrent") +# config.set_setting("magnet2torrent", False, server="torrent") - if not filetools.exists(filetools.join(config.get_runtime_path(), "custom_code.json")) or not \ - config.get_setting("unrar_path", server="torrent", default=""): +# if not filetools.exists(filetools.join(config.get_runtime_path(), "custom_code.json")) or not \ +# config.get_setting("unrar_path", server="torrent", default=""): - path = filetools.join(config.get_runtime_path(), 'lib', 'rarfiles') - creationflags = '' - sufix = '' - unrar = '' - for device in filetools.listdir(path): - if xbmc.getCondVisibility("system.platform.android") and 'android' not in device: continue - if xbmc.getCondVisibility("system.platform.windows") and 'windows' not in device: continue - if not xbmc.getCondVisibility("system.platform.windows") and not xbmc.getCondVisibility("system.platform.android") \ - and ('android' in device or 'windows' in device): continue - if 'windows' in device: - creationflags = 0x08000000 - sufix = '.exe' - else: - creationflags = '' - sufix = '' - unrar = filetools.join(path, device, 'unrar%s') % sufix - if not filetools.exists(unrar): unrar = '' - if unrar: - if not xbmc.getCondVisibility("system.platform.windows"): - try: - if xbmc.getCondVisibility("system.platform.android"): - # Para Android copiamos el binario a la partición del sistema - unrar_org = unrar - unrar = filetools.join(xbmc.translatePath('special://xbmc/'), 'files').replace('/cache/apk/assets', '') - if not filetools.exists(unrar): - filetools.mkdir(unrar) - unrar = filetools.join(unrar, 'unrar') - filetools.copy(unrar_org, unrar, silent=True) +# path = filetools.join(config.get_runtime_path(), 'lib', 'rarfiles') +# creationflags = '' +# sufix = '' +# unrar = '' +# for device in filetools.listdir(path): +# if xbmc.getCondVisibility("system.platform.android") and 'android' not in device: continue +# if xbmc.getCondVisibility("system.platform.windows") and 'windows' not in device: continue +# if not xbmc.getCondVisibility("system.platform.windows") and not xbmc.getCondVisibility("system.platform.android") \ +# and ('android' in device or 'windows' in device): continue +# if 'windows' in device: +# creationflags = 0x08000000 +# sufix = '.exe' +# else: +# creationflags = '' +# sufix = '' +# unrar = filetools.join(path, device, 'unrar%s') % sufix +# if not filetools.exists(unrar): unrar = '' +# if unrar: +# if not xbmc.getCondVisibility("system.platform.windows"): +# try: +# if xbmc.getCondVisibility("system.platform.android"): +# # Para Android copiamos el binario a la partición del sistema +# unrar_org = unrar +# unrar = filetools.join(xbmc.translatePath('special://xbmc/'), 'files').replace('/cache/apk/assets', '') +# if not filetools.exists(unrar): +# filetools.mkdir(unrar) +# unrar = filetools.join(unrar, 'unrar') +# filetools.copy(unrar_org, unrar, silent=True) - command = ['chmod', '777', '%s' % unrar] - p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - output_cmd, error_cmd = p.communicate() - command = ['ls', '-l', unrar] - p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - output_cmd, error_cmd = p.communicate() - xbmc.log('######## UnRAR file: %s' % str(output_cmd), xbmc.LOGNOTICE) - except: - xbmc.log('######## UnRAR ERROR in path: %s' % str(unrar), xbmc.LOGNOTICE) - logger.error(traceback.format_exc(1)) +# command = ['chmod', '777', '%s' % unrar] +# p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) +# output_cmd, error_cmd = p.communicate() +# command = ['ls', '-l', unrar] +# p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) +# output_cmd, error_cmd = p.communicate() +# xbmc.log('######## UnRAR file: %s' % str(output_cmd), xbmc.LOGNOTICE) +# except: +# xbmc.log('######## UnRAR ERROR in path: %s' % str(unrar), xbmc.LOGNOTICE) +# logger.error(traceback.format_exc(1)) - try: - if xbmc.getCondVisibility("system.platform.windows"): - p = subprocess.Popen(unrar, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=creationflags) - else: - p = subprocess.Popen(unrar, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - output_cmd, error_cmd = p.communicate() - if p.returncode != 0 or error_cmd: - xbmc.log('######## UnRAR returncode in module %s: %s, %s in %s' % \ - (device, str(p.returncode), str(error_cmd), unrar), xbmc.LOGNOTICE) - unrar = '' - else: - xbmc.log('######## UnRAR OK in %s: %s' % (device, unrar), xbmc.LOGNOTICE) - break - except: - xbmc.log('######## UnRAR ERROR in module %s: %s' % (device, unrar), xbmc.LOGNOTICE) - logger.error(traceback.format_exc(1)) - unrar = '' +# try: +# if xbmc.getCondVisibility("system.platform.windows"): +# p = subprocess.Popen(unrar, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=creationflags) +# else: +# p = subprocess.Popen(unrar, stdout=subprocess.PIPE, stderr=subprocess.PIPE) +# output_cmd, error_cmd = p.communicate() +# if p.returncode != 0 or error_cmd: +# xbmc.log('######## UnRAR returncode in module %s: %s, %s in %s' % \ +# (device, str(p.returncode), str(error_cmd), unrar), xbmc.LOGNOTICE) +# unrar = '' +# else: +# xbmc.log('######## UnRAR OK in %s: %s' % (device, unrar), xbmc.LOGNOTICE) +# break +# except: +# xbmc.log('######## UnRAR ERROR in module %s: %s' % (device, unrar), xbmc.LOGNOTICE) +# logger.error(traceback.format_exc(1)) +# unrar = '' - if unrar: config.set_setting("unrar_path", unrar, server="torrent") +# if unrar: config.set_setting("unrar_path", unrar, server="torrent") - if filetools.exists(filetools.join(config.get_runtime_path(), "custom_code.json")) and \ - config.get_setting("libtorrent_path", server="torrent", default="") : - return +# if filetools.exists(filetools.join(config.get_runtime_path(), "custom_code.json")) and \ +# config.get_setting("libtorrent_path", server="torrent", default="") : +# return - try: - from lib.python_libtorrent.python_libtorrent import get_libtorrent - except Exception as e: - logger.error(traceback.format_exc(1)) - if not PY3: - e = unicode(str(e), "utf8", errors="replace").encode("utf8") - config.set_setting("libtorrent_path", "", server="torrent") - if not config.get_setting("libtorrent_error", server="torrent", default=''): - config.set_setting("libtorrent_error", str(e), server="torrent") +# try: +# from lib.python_libtorrent.python_libtorrent import get_libtorrent +# except Exception as e: +# logger.error(traceback.format_exc(1)) +# if not PY3: +# e = unicode(str(e), "utf8", errors="replace").encode("utf8") +# config.set_setting("libtorrent_path", "", server="torrent") +# if not config.get_setting("libtorrent_error", server="torrent", default=''): +# config.set_setting("libtorrent_error", str(e), server="torrent") - return +# return def verify_Kodi_video_DB(): diff --git a/platformcode/envtal.py b/platformcode/envtal.py index 1d03578b..c68342e8 100644 --- a/platformcode/envtal.py +++ b/platformcode/envtal.py @@ -4,7 +4,6 @@ # ------------------------------------------------------------ from __future__ import division -# from builtins import str from past.utils import old_div import sys @@ -31,8 +30,8 @@ from platformcode import logger, config, platformtools def get_environment(): """ - Devuelve las variables de entorno del OS, de Kodi y de Alfa más habituales, - necesarias para el diagnóstico de fallos + Returns the most common OS, Kodi and Alpha environment variables, +    necessary for fault diagnosis """ try: @@ -56,9 +55,9 @@ def get_environment(): try: for label_a in subprocess.check_output('getprop').split('\n'): if 'build.version.release' in label_a: - environment['os_release'] = str(scrapertools.find_single_match(label_a, ':\s*\[(.*?)\]$')) + environment['os_release'] = str(scrapertools.find_single_match(label_a, r':\s*\[(.*?)\]$')) if 'product.model' in label_a: - environment['prod_model'] = str(scrapertools.find_single_match(label_a, ':\s*\[(.*?)\]$')) + environment['prod_model'] = str(scrapertools.find_single_match(label_a, r':\s*\[(.*?)\]$')) except: try: for label_a in filetools.read(os.environ['ANDROID_ROOT'] + '/build.prop').split(): @@ -196,87 +195,87 @@ def get_environment(): except: environment['videolab_free'] = '?' - environment['torrent_list'] = [] - environment['torrentcli_option'] = '' - environment['torrent_error'] = '' - environment['torrentcli_rar'] = config.get_setting("mct_rar_unpack", server="torrent", default=True) - environment['torrentcli_backgr'] = config.get_setting("mct_background_download", server="torrent", default=True) - environment['torrentcli_lib_path'] = config.get_setting("libtorrent_path", server="torrent", default="") - if environment['torrentcli_lib_path']: - lib_path = 'Activo' - else: - lib_path = 'Inactivo' - environment['torrentcli_unrar'] = config.get_setting("unrar_path", server="torrent", default="") - if environment['torrentcli_unrar']: - if xbmc.getCondVisibility("system.platform.Android"): - unrar = 'Android' - else: - unrar, bin = filetools.split(environment['torrentcli_unrar']) - unrar = unrar.replace('\\', '/') - if not unrar.endswith('/'): - unrar = unrar + '/' - unrar = scrapertools.find_single_match(unrar, '\/([^\/]+)\/$').capitalize() - else: - unrar = 'Inactivo' - torrent_id = config.get_setting("torrent_client", server="torrent", default=0) - environment['torrentcli_option'] = str(torrent_id) - torrent_options = platformtools.torrent_client_installed() - if lib_path == 'Activo': - torrent_options = ['MCT'] + torrent_options - torrent_options = ['BT'] + torrent_options - environment['torrent_list'].append({'Torrent_opt': str(torrent_id), 'Libtorrent': lib_path, \ - 'RAR_Auto': str(environment['torrentcli_rar']), \ - 'RAR_backgr': str(environment['torrentcli_backgr']), \ - 'UnRAR': unrar}) - environment['torrent_error'] = config.get_setting("libtorrent_error", server="torrent", default="") - if environment['torrent_error']: - environment['torrent_list'].append({'Libtorrent_error': environment['torrent_error']}) + # environment['torrent_list'] = [] + # environment['torrentcli_option'] = '' + # environment['torrent_error'] = '' + # environment['torrentcli_rar'] = config.get_setting("mct_rar_unpack", server="torrent", default=True) + # environment['torrentcli_backgr'] = config.get_setting("mct_background_download", server="torrent", default=True) + # environment['torrentcli_lib_path'] = config.get_setting("libtorrent_path", server="torrent", default="") + # if environment['torrentcli_lib_path']: + # lib_path = 'Activo' + # else: + # lib_path = 'Inactivo' + # environment['torrentcli_unrar'] = config.get_setting("unrar_path", server="torrent", default="") + # if environment['torrentcli_unrar']: + # if xbmc.getCondVisibility("system.platform.Android"): + # unrar = 'Android' + # else: + # unrar, bin = filetools.split(environment['torrentcli_unrar']) + # unrar = unrar.replace('\\', '/') + # if not unrar.endswith('/'): + # unrar = unrar + '/' + # unrar = scrapertools.find_single_match(unrar, '\/([^\/]+)\/$').capitalize() + # else: + # unrar = 'Inactivo' + # torrent_id = config.get_setting("torrent_client", server="torrent", default=0) + # environment['torrentcli_option'] = str(torrent_id) + # torrent_options = platformtools.torrent_client_installed() + # if lib_path == 'Activo': + # torrent_options = ['MCT'] + torrent_options + # torrent_options = ['BT'] + torrent_options + # environment['torrent_list'].append({'Torrent_opt': str(torrent_id), 'Libtorrent': lib_path, \ + # 'RAR_Auto': str(environment['torrentcli_rar']), \ + # 'RAR_backgr': str(environment['torrentcli_backgr']), \ + # 'UnRAR': unrar}) + # environment['torrent_error'] = config.get_setting("libtorrent_error", server="torrent", default="") + # if environment['torrent_error']: + # environment['torrent_list'].append({'Libtorrent_error': environment['torrent_error']}) - for torrent_option in torrent_options: - cliente = dict() - cliente['D_load_Path'] = '' - cliente['Libre'] = '?' - cliente['Plug_in'] = torrent_option.replace('Plugin externo: ', '') - if cliente['Plug_in'] == 'BT': - cliente['D_load_Path'] = str(config.get_setting("bt_download_path", server="torrent", default='')) - if not cliente['D_load_Path']: continue - cliente['Buffer'] = str(config.get_setting("bt_buffer", server="torrent", default=50)) - elif cliente['Plug_in'] == 'MCT': - cliente['D_load_Path'] = str(config.get_setting("mct_download_path", server="torrent", default='')) - if not cliente['D_load_Path']: continue - cliente['Buffer'] = str(config.get_setting("mct_buffer", server="torrent", default=50)) - elif xbmc.getCondVisibility('System.HasAddon("plugin.video.%s")' % cliente['Plug_in']): - __settings__ = xbmcaddon.Addon(id="plugin.video.%s" % cliente['Plug_in']) - cliente['Plug_in'] = cliente['Plug_in'].capitalize() - if cliente['Plug_in'] == 'Torrenter': - cliente['D_load_Path'] = str(xbmc.translatePath(__settings__.getSetting('storage'))) - if not cliente['D_load_Path']: - cliente['D_load_Path'] = str(filetools.join(xbmc.translatePath("special://home/"), \ - "cache", "xbmcup", "plugin.video.torrenter", - "Torrenter")) - cliente['Buffer'] = str(__settings__.getSetting('pre_buffer_bytes')) - else: - cliente['D_load_Path'] = str(xbmc.translatePath(__settings__.getSetting('download_path'))) - cliente['Buffer'] = str(__settings__.getSetting('buffer_size')) - if __settings__.getSetting('download_storage') == '1' and __settings__.getSetting('memory_size'): - cliente['Memoria'] = str(__settings__.getSetting('memory_size')) + # for torrent_option in torrent_options: + # cliente = dict() + # cliente['D_load_Path'] = '' + # cliente['Libre'] = '?' + # cliente['Plug_in'] = torrent_option.replace('Plugin externo: ', '') + # if cliente['Plug_in'] == 'BT': + # cliente['D_load_Path'] = str(config.get_setting("bt_download_path", server="torrent", default='')) + # if not cliente['D_load_Path']: continue + # cliente['Buffer'] = str(config.get_setting("bt_buffer", server="torrent", default=50)) + # elif cliente['Plug_in'] == 'MCT': + # cliente['D_load_Path'] = str(config.get_setting("mct_download_path", server="torrent", default='')) + # if not cliente['D_load_Path']: continue + # cliente['Buffer'] = str(config.get_setting("mct_buffer", server="torrent", default=50)) + # elif xbmc.getCondVisibility('System.HasAddon("plugin.video.%s")' % cliente['Plug_in']): + # __settings__ = xbmcaddon.Addon(id="plugin.video.%s" % cliente['Plug_in']) + # cliente['Plug_in'] = cliente['Plug_in'].capitalize() + # if cliente['Plug_in'] == 'Torrenter': + # cliente['D_load_Path'] = str(xbmc.translatePath(__settings__.getSetting('storage'))) + # if not cliente['D_load_Path']: + # cliente['D_load_Path'] = str(filetools.join(xbmc.translatePath("special://home/"), \ + # "cache", "xbmcup", "plugin.video.torrenter", + # "Torrenter")) + # cliente['Buffer'] = str(__settings__.getSetting('pre_buffer_bytes')) + # else: + # cliente['D_load_Path'] = str(xbmc.translatePath(__settings__.getSetting('download_path'))) + # cliente['Buffer'] = str(__settings__.getSetting('buffer_size')) + # if __settings__.getSetting('download_storage') == '1' and __settings__.getSetting('memory_size'): + # cliente['Memoria'] = str(__settings__.getSetting('memory_size')) - if cliente['D_load_Path']: - try: - if environment['os_name'].lower() == 'windows': - free_bytes = ctypes.c_ulonglong(0) - ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(cliente['D_load_Path']), - None, None, ctypes.pointer(free_bytes)) - cliente['Libre'] = str(round(float(free_bytes.value) / \ - (1024 ** 3), 3)).replace('.', ',') - else: - disk_space = os.statvfs(cliente['D_load_Path']) - if not disk_space.f_frsize: disk_space.f_frsize = disk_space.f_frsize.f_bsize - cliente['Libre'] = str(round((float(disk_space.f_bavail) / \ - (1024 ** 3)) * float(disk_space.f_frsize), 3)).replace('.', ',') - except: - pass - environment['torrent_list'].append(cliente) + # if cliente['D_load_Path']: + # try: + # if environment['os_name'].lower() == 'windows': + # free_bytes = ctypes.c_ulonglong(0) + # ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(cliente['D_load_Path']), + # None, None, ctypes.pointer(free_bytes)) + # cliente['Libre'] = str(round(float(free_bytes.value) / \ + # (1024 ** 3), 3)).replace('.', ',') + # else: + # disk_space = os.statvfs(cliente['D_load_Path']) + # if not disk_space.f_frsize: disk_space.f_frsize = disk_space.f_frsize.f_bsize + # cliente['Libre'] = str(round((float(disk_space.f_bavail) / \ + # (1024 ** 3)) * float(disk_space.f_frsize), 3)).replace('.', ',') + # except: + # pass + # environment['torrent_list'].append(cliente) environment['proxy_active'] = '' try: diff --git a/resources/settings.xml b/resources/settings.xml index 086a78f9..d8e9cb72 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -68,7 +68,7 @@ - + @@ -102,9 +102,6 @@ - - - diff --git a/specials/setting.py b/specials/setting.py index d6bc273b..27826152 100644 --- a/specials/setting.py +++ b/specials/setting.py @@ -107,142 +107,142 @@ def autostart(item): # item necessario launcher.py linea 265 # xbmcgui.Dialog().ok(config.get_localized_string(20000), config.get_localized_string(70710)) -def setting_torrent(item): - logger.info() +# def setting_torrent(item): +# logger.info() - LIBTORRENT_PATH = config.get_setting("libtorrent_path", server="torrent", default="") - LIBTORRENT_ERROR = config.get_setting("libtorrent_error", server="torrent", default="") - default = config.get_setting("torrent_client", server="torrent", default=0) - BUFFER = config.get_setting("mct_buffer", server="torrent", default="50") - DOWNLOAD_PATH = config.get_setting("mct_download_path", server="torrent", default=config.get_setting("downloadpath")) - if not DOWNLOAD_PATH: DOWNLOAD_PATH = filetools.join(config.get_data_path(), 'downloads') - BACKGROUND = config.get_setting("mct_background_download", server="torrent", default=True) - RAR = config.get_setting("mct_rar_unpack", server="torrent", default=True) - DOWNLOAD_LIMIT = config.get_setting("mct_download_limit", server="torrent", default="") - BUFFER_BT = config.get_setting("bt_buffer", server="torrent", default="50") - DOWNLOAD_PATH_BT = config.get_setting("bt_download_path", server="torrent", default=config.get_setting("downloadpath")) - if not DOWNLOAD_PATH_BT: DOWNLOAD_PATH_BT = filetools.join(config.get_data_path(), 'downloads') - MAGNET2TORRENT = config.get_setting("magnet2torrent", server="torrent", default=False) +# LIBTORRENT_PATH = config.get_setting("libtorrent_path", server="torrent", default="") +# LIBTORRENT_ERROR = config.get_setting("libtorrent_error", server="torrent", default="") +# default = config.get_setting("torrent_client", server="torrent", default=0) +# BUFFER = config.get_setting("mct_buffer", server="torrent", default="50") +# DOWNLOAD_PATH = config.get_setting("mct_download_path", server="torrent", default=config.get_setting("downloadpath")) +# if not DOWNLOAD_PATH: DOWNLOAD_PATH = filetools.join(config.get_data_path(), 'downloads') +# BACKGROUND = config.get_setting("mct_background_download", server="torrent", default=True) +# RAR = config.get_setting("mct_rar_unpack", server="torrent", default=True) +# DOWNLOAD_LIMIT = config.get_setting("mct_download_limit", server="torrent", default="") +# BUFFER_BT = config.get_setting("bt_buffer", server="torrent", default="50") +# DOWNLOAD_PATH_BT = config.get_setting("bt_download_path", server="torrent", default=config.get_setting("downloadpath")) +# if not DOWNLOAD_PATH_BT: DOWNLOAD_PATH_BT = filetools.join(config.get_data_path(), 'downloads') +# MAGNET2TORRENT = config.get_setting("magnet2torrent", server="torrent", default=False) - torrent_options = [config.get_localized_string(30006), config.get_localized_string(70254), config.get_localized_string(70255)] - torrent_options.extend(platformtools.torrent_client_installed()) +# torrent_options = [config.get_localized_string(30006), config.get_localized_string(70254), config.get_localized_string(70255)] +# torrent_options.extend(platformtools.torrent_client_installed()) - list_controls = [ - { - "id": "libtorrent_path", - "type": "text", - "label": "Libtorrent path", - "default": LIBTORRENT_PATH, - "enabled": True, - "visible": False - }, - { - "id": "libtorrent_error", - "type": "text", - "label": "libtorrent error", - "default": LIBTORRENT_ERROR, - "enabled": True, - "visible": False - }, - { - "id": "list_torrent", - "type": "list", - "label": config.get_localized_string(70256), - "default": default, - "enabled": True, - "visible": True, - "lvalues": torrent_options - }, - { - "id": "mct_buffer", - "type": "text", - "label": "MCT - " + config.get_localized_string(70758), - "default": BUFFER, - "enabled": True, - "visible": "eq(-1,%s)" % torrent_options[2] - }, - { - "id": "mct_download_path", - "type": "text", - "label": "MCT - " + config.get_localized_string(30017), - "default": DOWNLOAD_PATH, - "enabled": True, - "visible": "eq(-2,%s)" % torrent_options[2] - }, - { - "id": "bt_buffer", - "type": "text", - "label": "BT - " + config.get_localized_string(70758), - "default": BUFFER_BT, - "enabled": True, - "visible": "eq(-3,%s)" % torrent_options[1] - }, - { - "id": "bt_download_path", - "type": "text", - "label": "BT - " + config.get_localized_string(30017), - "default": DOWNLOAD_PATH_BT, - "enabled": True, - "visible": "eq(-4,%s)" % torrent_options[1] - }, - { - "id": "mct_download_limit", - "type": "text", - "label": config.get_localized_string(70759), - "default": DOWNLOAD_LIMIT, - "enabled": True, - "visible": "eq(-5,%s) | eq(-5,%s)" % (torrent_options[1], torrent_options[2]) - }, - { - "id": "mct_rar_unpack", - "type": "bool", - "label": config.get_localized_string(70760), - "default": RAR, - "enabled": True, - "visible": True - }, - { - "id": "mct_background_download", - "type": "bool", - "label": config.get_localized_string(70761), - "default": BACKGROUND, - "enabled": True, - "visible": True - }, - { - "id": "magnet2torrent", - "type": "bool", - "label": config.get_localized_string(70762), - "default": MAGNET2TORRENT, - "enabled": True, - "visible": True - } - ] +# list_controls = [ +# { +# "id": "libtorrent_path", +# "type": "text", +# "label": "Libtorrent path", +# "default": LIBTORRENT_PATH, +# "enabled": True, +# "visible": False +# }, +# { +# "id": "libtorrent_error", +# "type": "text", +# "label": "libtorrent error", +# "default": LIBTORRENT_ERROR, +# "enabled": True, +# "visible": False +# }, +# { +# "id": "list_torrent", +# "type": "list", +# "label": config.get_localized_string(70256), +# "default": default, +# "enabled": True, +# "visible": True, +# "lvalues": torrent_options +# }, +# { +# "id": "mct_buffer", +# "type": "text", +# "label": "MCT - " + config.get_localized_string(70758), +# "default": BUFFER, +# "enabled": True, +# "visible": "eq(-1,%s)" % torrent_options[2] +# }, +# { +# "id": "mct_download_path", +# "type": "text", +# "label": "MCT - " + config.get_localized_string(30017), +# "default": DOWNLOAD_PATH, +# "enabled": True, +# "visible": "eq(-2,%s)" % torrent_options[2] +# }, +# { +# "id": "bt_buffer", +# "type": "text", +# "label": "BT - " + config.get_localized_string(70758), +# "default": BUFFER_BT, +# "enabled": True, +# "visible": "eq(-3,%s)" % torrent_options[1] +# }, +# { +# "id": "bt_download_path", +# "type": "text", +# "label": "BT - " + config.get_localized_string(30017), +# "default": DOWNLOAD_PATH_BT, +# "enabled": True, +# "visible": "eq(-4,%s)" % torrent_options[1] +# }, +# { +# "id": "mct_download_limit", +# "type": "text", +# "label": config.get_localized_string(70759), +# "default": DOWNLOAD_LIMIT, +# "enabled": True, +# "visible": "eq(-5,%s) | eq(-5,%s)" % (torrent_options[1], torrent_options[2]) +# }, +# { +# "id": "mct_rar_unpack", +# "type": "bool", +# "label": config.get_localized_string(70760), +# "default": RAR, +# "enabled": True, +# "visible": True +# }, +# { +# "id": "mct_background_download", +# "type": "bool", +# "label": config.get_localized_string(70761), +# "default": BACKGROUND, +# "enabled": True, +# "visible": True +# }, +# { +# "id": "magnet2torrent", +# "type": "bool", +# "label": config.get_localized_string(70762), +# "default": MAGNET2TORRENT, +# "enabled": True, +# "visible": True +# } +# ] - platformtools.show_channel_settings(list_controls=list_controls, callback='save_setting_torrent', item=item, - caption=config.get_localized_string(70257), custom_button={'visible': False}) +# platformtools.show_channel_settings(list_controls=list_controls, callback='save_setting_torrent', item=item, +# caption=config.get_localized_string(70257), custom_button={'visible': False}) -def save_setting_torrent(item, dict_data_saved): - if dict_data_saved and "list_torrent" in dict_data_saved: - config.set_setting("torrent_client", dict_data_saved["list_torrent"], server="torrent") - if dict_data_saved and "mct_buffer" in dict_data_saved: - config.set_setting("mct_buffer", dict_data_saved["mct_buffer"], server="torrent") - if dict_data_saved and "mct_download_path" in dict_data_saved: - config.set_setting("mct_download_path", dict_data_saved["mct_download_path"], server="torrent") - if dict_data_saved and "mct_background_download" in dict_data_saved: - config.set_setting("mct_background_download", dict_data_saved["mct_background_download"], server="torrent") - if dict_data_saved and "mct_rar_unpack" in dict_data_saved: - config.set_setting("mct_rar_unpack", dict_data_saved["mct_rar_unpack"], server="torrent") - if dict_data_saved and "mct_download_limit" in dict_data_saved: - config.set_setting("mct_download_limit", dict_data_saved["mct_download_limit"], server="torrent") - if dict_data_saved and "bt_buffer" in dict_data_saved: - config.set_setting("bt_buffer", dict_data_saved["bt_buffer"], server="torrent") - if dict_data_saved and "bt_download_path" in dict_data_saved: - config.set_setting("bt_download_path", dict_data_saved["bt_download_path"], server="torrent") - if dict_data_saved and "magnet2torrent" in dict_data_saved: - config.set_setting("magnet2torrent", dict_data_saved["magnet2torrent"], server="torrent") +# def save_setting_torrent(item, dict_data_saved): +# if dict_data_saved and "list_torrent" in dict_data_saved: +# config.set_setting("torrent_client", dict_data_saved["list_torrent"], server="torrent") +# if dict_data_saved and "mct_buffer" in dict_data_saved: +# config.set_setting("mct_buffer", dict_data_saved["mct_buffer"], server="torrent") +# if dict_data_saved and "mct_download_path" in dict_data_saved: +# config.set_setting("mct_download_path", dict_data_saved["mct_download_path"], server="torrent") +# if dict_data_saved and "mct_background_download" in dict_data_saved: +# config.set_setting("mct_background_download", dict_data_saved["mct_background_download"], server="torrent") +# if dict_data_saved and "mct_rar_unpack" in dict_data_saved: +# config.set_setting("mct_rar_unpack", dict_data_saved["mct_rar_unpack"], server="torrent") +# if dict_data_saved and "mct_download_limit" in dict_data_saved: +# config.set_setting("mct_download_limit", dict_data_saved["mct_download_limit"], server="torrent") +# if dict_data_saved and "bt_buffer" in dict_data_saved: +# config.set_setting("bt_buffer", dict_data_saved["bt_buffer"], server="torrent") +# if dict_data_saved and "bt_download_path" in dict_data_saved: +# config.set_setting("bt_download_path", dict_data_saved["bt_download_path"], server="torrent") +# if dict_data_saved and "magnet2torrent" in dict_data_saved: +# config.set_setting("magnet2torrent", dict_data_saved["magnet2torrent"], server="torrent") def menu_servers(item): logger.info()