diff --git a/core/httptools.py b/core/httptools.py index 64cfef16..9f9fc82e 100755 --- a/core/httptools.py +++ b/core/httptools.py @@ -223,7 +223,7 @@ def show_infobox(info_dict): def downloadpage(url, **opt): - logger.info() + # logger.info() """ Open a url and return the data obtained diff --git a/core/jsontools.py b/core/jsontools.py index 57ea27a8..d12a5213 100644 --- a/core/jsontools.py +++ b/core/jsontools.py @@ -148,7 +148,7 @@ def check_to_backup(data, fname, dict_data): logger.debug("The file is empty: %s" % fname) -def update_node(dict_node, name_file, node, path=None): +def update_node(dict_node, name_file, node, path=None, silent=False): """ actualiza el json_data de un fichero con el diccionario pasado @@ -164,7 +164,7 @@ def update_node(dict_node, name_file, node, path=None): @return json_data @rtype: dict """ - logger.info() + if not silent: logger.info() from platformcode import config from core import filetools @@ -185,14 +185,14 @@ def update_node(dict_node, name_file, node, path=None): # es un dict if dict_data: if node in dict_data: - # logger.debug(" the key exists %s" % node) + if not silent: logger.debug(" the key exists %s" % node) dict_data[node] = dict_node else: - logger.debug(" The key does NOT exist %s" % node) + if not silent: logger.debug(" The key does NOT exist %s" % node) new_dict = {node: dict_node} dict_data.update(new_dict) else: - logger.debug(" It is NOT a dict") + if not silent: logger.debug(" It is NOT a dict") dict_data = {node: dict_node} json_data = dump(dict_data) result = filetools.write(fname, json_data) diff --git a/core/support.py b/core/support.py index c2dd03bc..9b0bf8c3 100755 --- a/core/support.py +++ b/core/support.py @@ -858,7 +858,6 @@ def match(item_url_string, **args): match: first match matches: all the matches ''' - log() matches = blocks = [] url = None diff --git a/platformcode/envtal.py b/platformcode/envtal.py index f413d281..1d03578b 100644 --- a/platformcode/envtal.py +++ b/platformcode/envtal.py @@ -359,16 +359,16 @@ def get_environment(): def list_env(environment={}): + sep = '-----------------------------------------------------------' if not environment: environment = get_environment() if environment['debug'] == 'False': logger.log_enable(True) - logger.info('----------------------------------------------') - logger.info('Variables de entorno Alfa: ' + environment['addon_version'] + - ' Debug: ' + environment['debug']) - logger.info("----------------------------------------------") + logger.info(sep) + logger.info('KoD environment variables: ' + environment['addon_version'] + ' Debug: ' + environment['debug']) + logger.info(sep) logger.info(environment['os_name'] + ' ' + environment['prod_model'] + ' ' + environment['os_release'] + ' ' + environment['machine'] + ' ' + @@ -381,45 +381,45 @@ def list_env(environment={}): logger.info('CPU: ' + environment['cpu_usage']) if environment['mem_total'] or environment['mem_free']: - logger.info('Memoria: Total: ' + environment['mem_total'] + ' MB / Disp.: ' + - environment['mem_free'] + ' MB / Buffers: ' + - str(int(environment['kodi_buffer']) * 3) + ' MB / Buffermode: ' + - environment['kodi_bmode'] + ' / Readfactor: ' + + logger.info('Memory: Total: ' + environment['mem_total'] + ' MB | Disp.: ' + + environment['mem_free'] + ' MB | Buffers: ' + + str(int(environment['kodi_buffer']) * 3) + ' MB | Buffermode: ' + + environment['kodi_bmode'] + ' | Readfactor: ' + environment['kodi_rfactor']) - logger.info('Userdata: ' + environment['userdata_path'] + ' - Libre: ' + + logger.info('Userdata: ' + environment['userdata_path'] + ' - Free: ' + environment['userdata_free'].replace('.', ',') + ' GB') - logger.info('Videoteca: Series/Epis: ' + environment['videolab_series'] + '/' + + logger.info('Videolibrary: Series/Episodes: ' + environment['videolab_series'] + '/' + environment['videolab_episodios'] + ' - Pelis: ' + environment['videolab_pelis'] + ' - Upd: ' + environment['videolab_update'] + ' - Path: ' + - environment['videolab_path'] + ' - Libre: ' + + environment['videolab_path'] + ' - Free: ' + environment['videolab_free'].replace('.', ',') + ' GB') - if environment['torrent_list']: - for x, cliente in enumerate(environment['torrent_list']): - if x == 0: - cliente_alt = cliente.copy() - del cliente_alt['Torrent_opt'] - logger.info('Torrent: Opt: %s, %s' % (str(cliente['Torrent_opt']), \ - str(cliente_alt).replace('{', '').replace('}', '') \ - .replace("'", '').replace('_', ' '))) - elif x == 1 and environment['torrent_error']: - logger.info('- ' + str(cliente).replace('{', '').replace('}', '') \ - .replace("'", '').replace('_', ' ')) - else: - cliente_alt = cliente.copy() - del cliente_alt['Plug_in'] - cliente_alt['Libre'] = cliente_alt['Libre'].replace('.', ',') + ' GB' - logger.info('- %s: %s' % (str(cliente['Plug_in']), str(cliente_alt) \ - .replace('{', '').replace('}', '').replace("'", '') \ - .replace('\\\\', '\\'))) + # if environment['torrent_list']: + # for x, cliente in enumerate(environment['torrent_list']): + # if x == 0: + # cliente_alt = cliente.copy() + # del cliente_alt['Torrent_opt'] + # logger.info('Torrent: Opt: %s, %s' % (str(cliente['Torrent_opt']), \ + # str(cliente_alt).replace('{', '').replace('}', '') \ + # .replace("'", '').replace('_', ' '))) + # elif x == 1 and environment['torrent_error']: + # logger.info('- ' + str(cliente).replace('{', '').replace('}', '') \ + # .replace("'", '').replace('_', ' ')) + # else: + # cliente_alt = cliente.copy() + # del cliente_alt['Plug_in'] + # cliente_alt['Libre'] = cliente_alt['Libre'].replace('.', ',') + ' GB' + # logger.info('- %s: %s' % (str(cliente['Plug_in']), str(cliente_alt) \ + # .replace('{', '').replace('}', '').replace("'", '') \ + # .replace('\\\\', '\\'))) - logger.info('Proxy: ' + environment['proxy_active']) + # logger.info('Proxy: ' + environment['proxy_active']) - logger.info('TAMAÑO del LOG: ' + environment['log_size'].replace('.', ',') + ' MB') - logger.info("----------------------------------------------") + logger.info('LOG Size: ' + environment['log_size'].replace('.', ',') + ' MB') + logger.info(sep) if environment['debug'] == 'False': logger.log_enable(False) diff --git a/platformcode/platformtools.py b/platformcode/platformtools.py index 265d2473..e915a94c 100644 --- a/platformcode/platformtools.py +++ b/platformcode/platformtools.py @@ -1342,6 +1342,7 @@ def torrent_client_installed(show_tuple=False): def play_torrent(item, xlistitem, mediaurl): logger.info() + # from core.support import dbg;dbg() import time from servers import torrent @@ -1369,9 +1370,13 @@ def play_torrent(item, xlistitem, mediaurl): if torr_client in ['quasar', 'elementum'] and item.downloadFilename: torrent.elementum_download(item) else: + time.sleep(3) xbmc.executebuiltin("PlayMedia(" + torrent_options[selection][1] % mediaurl + ")") - torrent.mark_auto_as_watched(item) + torrent.mark_auto_as_watched(item) + + while is_playing() and not xbmc.abortRequested: + time.sleep(3) def log(texto): diff --git a/servers/torrent.py b/servers/torrent.py index 37fb07f2..3cf0534e 100755 --- a/servers/torrent.py +++ b/servers/torrent.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -import os, sys +import re, os, sys # from builtins import str from builtins import range @@ -14,7 +14,7 @@ if PY3: else: import urllib -import time, requests, xbmc, xbmcgui, xbmcaddon +import time, requests, xbmc, xbmcgui, xbmcaddon, shutil from core import filetools, jsontools from core.support import dbg, log, match # from core import httptools @@ -22,10 +22,11 @@ from core.support import dbg, log, match from platformcode import config, platformtools from threading import Thread, currentThread from torrentool.api import Torrent +from lib.guessit import guessit elementum_setting = xbmcaddon.Addon(id='plugin.video.elementum') elementum_host = 'http://127.0.0.1:' + elementum_setting.getSetting('remote_port') + '/torrents/' -extensions_list = ['aaf', '3gp', 'asf', 'avi', 'flv', 'mpeg', 'm1v', 'm2v', 'm4v', 'mkv', 'mov', 'mpg', 'mpe', 'mp4', 'ogg', 'wmv'] +extensions_list = ['.aaf', '.3gp', '.asf', '.avi', '.flv', '.mpeg', '.m1v', '.m2v', '.m4v', '.mkv', '.mov', '.mpg', '.mpe', '.mp4', '.ogg', '.wmv'] # Returns an array of possible video url's from the page_url @@ -116,9 +117,13 @@ def elementum_monitor(): while not filetools.isfile(filetools.join(elementum_setting.getSetting('torrents_path'), TorrentName + '.torrent')): time.sleep(1) else: - data = match(elementum_host, alfa_s=True).data + log('Watch') + try: + data = requests.get(elementum_host).json() + except: + data = '' if data: - json = jsontools.load(data)['items'] + json = data['items'] for it in json: Partial = float(match(it['label'], patron=r'(\d+\.\d+)%').match) @@ -128,11 +133,8 @@ def elementum_monitor(): update_download_info(Partial, Title, TorrentName, File, Json) partials.append(Partial) - result = 0 - for p in partials: - result += p - - if result / len(partials) == 100: + partials.sort() + if len(partials) > 0 and partials[0] == 100: unset_elementum() time.sleep(1) @@ -156,55 +158,114 @@ def update_download_info(Partial, Title, TorrentName, File, Json): tpath = filetools.join(xbmc.translatePath(elementum_setting.getSetting('torrents_path')), TorrentName) if Json['downloadSize'] == 0: size = Torrent.from_file(tpath + '.torrent').total_size - jsontools.update_node(size, File, 'downloadSize', path) + jsontools.update_node(size, File, 'downloadSize', path, silent=True) if Json['downloadFilename'] != dlpath and 'backupFilename' not in Json: - jsontools.update_node(Json['downloadFilename'], File, 'backupFilename', path) - jsontools.update_node(dlpath, File, 'downloadFilename', path) + jsontools.update_node(Json['downloadFilename'], File, 'backupFilename', path, silent=True) + jsontools.update_node(dlpath, File, 'downloadFilename', path, silent=True) if Json['downloadProgress'] != Partial and Partial != 0: - jsontools.update_node(Partial, File, 'downloadProgress', path) - jsontools.update_node(4, File, 'downloadStatus', path) + jsontools.update_node(Partial, File, 'downloadProgress', path, silent=True) + jsontools.update_node(4, File, 'downloadStatus', path, silent=True) if Partial == 100: - jsontools.update_node(Json['downloadSize'], File, 'downloadCompleted', path) - jsontools.update_node(2, File, 'downloadStatus', path) + jsontools.update_node(Json['downloadSize'], File, 'downloadCompleted', path, silent=True) + jsontools.update_node(2, File, 'downloadStatus', path, silent=True) requests.get(elementum_host + 'pause/' + TorrentName) filetools.remove(tpath + '.torrent') - time.sleep(5) + time.sleep(1) rename(TorrentName, path) + # requests.get(elementum_host + 'delete/' + TorrentName + '?files=false') + def rename(TorrentName, Path): File, Json = find_file(TorrentName) path = Json['downloadFilename'] - if filetools.isdir(path): - extension = '' - files = filetools.listdir(path) - sep = '/' if path.lower().startswith("smb://") else os.sep - oldName = path.split(sep)[-1] - newName = Json['backupFilename'] - for f in files: - ext = f.split('.')[-1] - if ext in extensions_list: extension = '.' + ext - filetools.rename(filetools.join(path, f), f.replace(oldName, newName)) - filetools.rename(path, newName) - jsontools.update_node(filetools.join(newName,newName + extension), File, 'downloadFilename', Path) + if Json['infoLabels']['mediatype'] == 'movie': + if filetools.isdir(path): + extension = '' + files = filetools.listdir(path) + sep = '/' if path.lower().startswith("smb://") else os.sep + oldName = path.split(sep)[-1] + newName = Json['backupFilename'] + for f in files: + ext = os.path.splitext(f)[-1] + if ext in extensions_list: extension = ext + filetools.rename(filetools.join(path, f), f.replace(oldName, newName)) + filetools.rename(path, newName) + jsontools.update_node(filetools.join(newName,newName + extension), File, 'downloadFilename', Path) + else: + oldName = filetools.split(path)[-1] + newName = Json['backupFilename'] + os.path.splitext(oldName)[-1] + filetools.rename(path, newName) + jsontools.update_node(newName, File, 'downloadFilename', Path) else: - oldName = filetools.split(path)[-1] - newName = Json['backupFilename'] + '.' + oldName.split('.')[-1] - filetools.rename(path, newName) - jsontools.update_node(newName, File, 'downloadFilename', Path) + sep = '/' if path.lower().startswith("smb://") else os.sep + FolderName = Json['backupFilename'].split(sep)[0] + Title = re.sub(r'(\s*\[[^\]]+\])', '', FolderName) + if filetools.isdir(path): + files = filetools.listdir(path) + file_dict = {} + for f in files: + title = process_filename(f, Title, ext=False) + ext = os.path.splitext(f)[-1] + name = os.path.splitext(f)[0] + if title not in file_dict and ext in extensions_list: + file_dict[title] = name + for title, name in file_dict.items(): + for f in files: + if name in f: + # log('Name:',name,'Title:',title) + filetools.rename(filetools.join(path, f), f.replace(name, title)) + + filetools.rename(path, FolderName) + jsontools.update_node(FolderName, File, 'downloadFilename', Path) + else: + filename = filetools.split(path)[-1] + title = process_filename(filename, Title) + NewFolder = filetools.join(config.get_setting('downloadpath'), FolderName) + if not filetools.isdir(NewFolder): + filetools.mkdir(NewFolder) + from_folder = filetools.join(config.get_setting('downloadpath'), filename) + to_folder = filetools.join(FolderName, title) + filetools.move(from_folder, to_folder) + jsontools.update_node(filetools.join(config.get_setting('downloadpath'),to_folder), File, 'downloadFilename', Path) + +def process_filename(filename, Title, ext=True): + extension = os.path.splitext(filename)[-1] + parsedTitle = guessit(filename) + t = parsedTitle.get('title', '') + episode = '' + s = ' - ' + if parsedTitle.get('episode') and parsedTitle.get('season'): + if type(parsedTitle.get('season')) == list: + episode += str(parsedTitle.get('season')[0]) + '-' + str(parsedTitle.get('season')[-1]) + else: + episode += str(parsedTitle.get('season')) + + if type(parsedTitle.get('episode')) == list: + episode += 'x' + str(parsedTitle.get('episode')[0]).zfill(2) + '-' + str(parsedTitle.get('episode')[-1]).zfill(2) + else: + episode += 'x' + str(parsedTitle.get('episode')).zfill(2) + elif parsedTitle.get('season') and type(parsedTitle.get('season')) == list: + episode += s + config.get_localized_string(30140) + " " +str(parsedTitle.get('season')[0]) + '-' + str(parsedTitle.get('season')[-1]) + elif parsedTitle.get('season'): + episode += s + config.get_localized_string(60027) % str(parsedTitle.get('season')) + if parsedTitle.get('episode_title'): + episode += s + parsedTitle.get('episode_title') + title = (t if t else Title) + s + episode + (extension if ext else '') + return title def unset_elementum(): log('UNSET Elementum') Sleep = False - if config.get_setting('elementumtype'): + if config.get_setting('elementumtype') and config.get_setting('elementumtype') != elementum_setting.gerSetting('download_storage'): elementum_setting.setSetting('download_storage', str(config.get_setting('elementumtype'))) Sleep = True - if config.get_setting('elementumdl'): + if config.get_setting('elementumdl') and config.get_setting('elementumdl') != elementum_setting.gerSetting('download_path'): elementum_setting.setSetting('download_path', config.get_setting('elementumdl')) Sleep = True if Sleep: - time.sleep(3) + time.sleep(1) ########################## ELEMENTUM MONITOR TEST ########################## diff --git a/specials/downloads.py b/specials/downloads.py index 637a0c1b..7bbb6fa4 100644 --- a/specials/downloads.py +++ b/specials/downloads.py @@ -928,6 +928,7 @@ def write_json(item): item.downloadProgress = 0 item.downloadSize = 0 item.downloadCompleted = 0 + title = re.sub(r'(?:\[[^\]]+\]|%s[^-]+-\s*)' %config.get_localized_string(60356), '', item.title).strip() if not item.contentThumbnail: item.contentThumbnail = item.thumbnail @@ -935,7 +936,10 @@ def write_json(item): if name in item.__dict__: item.__dict__.pop(name) - naming = item.fulltitle + typo(item.infoLabels['IMDBNumber'], '_ []') + typo(channel, '_ []') + if item.contentType == 'episode': + naming = title + typo(item.infoLabels['IMDBNumber'], '_ []') + typo(channel, '_ []') + else: + naming = item.fulltitle + typo(item.infoLabels['IMDBNumber'], '_ []') + typo(channel, '_ []') naming += typo(item.contentLanguage, '_ []') if item.contentLanguage else '' naming += typo(item.quality, '_ []') if item.quality else ''