diff --git a/platformcode/config.py b/platformcode/config.py index 54a59e81..af826fe3 100644 --- a/platformcode/config.py +++ b/platformcode/config.py @@ -219,25 +219,25 @@ def open_settings(): set_setting('adult_aux_new_password1', '') set_setting('adult_aux_new_password2', '') - from specials import move_videolibrary + from specials import videolibrary + from platformcode import xbmc_videolibrary if settings_pre.get('downloadpath', None) != settings_post.get('downloadpath', None): - move_videolibrary.update_sources(settings_pre.get('downloadpath', None), settings_post.get('downloadpath', None)) + xbmc_videolibrary.update_sources(settings_pre.get('downloadpath', None), settings_post.get('downloadpath', None)) # si se ha cambiado la ruta de la videoteca llamamos a comprobar directorios para que lo cree y pregunte # automaticamente si configurar la videoteca if settings_pre.get("videolibrarypath", None) != settings_post.get("videolibrarypath", None) or \ settings_pre.get("folder_movies", None) != settings_post.get("folder_movies", None) or \ settings_pre.get("folder_tvshows", None) != settings_post.get("folder_tvshows", None): - move_videolibrary.move_videolibrary(settings_pre.get("videolibrarypath", None), settings_post.get("videolibrarypath", None), - settings_pre.get("folder_movies", None), settings_post.get("folder_movies", None), - settings_pre.get("folder_tvshows", None), settings_post.get("folder_tvshows", None)) + videolibrary.move_videolibrary(settings_pre.get("videolibrarypath", None), settings_post.get("videolibrarypath", None), + settings_pre.get("folder_movies", None), settings_post.get("folder_movies", None), + settings_pre.get("folder_tvshows", None), settings_post.get("folder_tvshows", None)) # si se ha puesto que se quiere autoconfigurar y se había creado el directorio de la videoteca if not settings_pre.get("videolibrary_kodi", None) and settings_post.get("videolibrary_kodi", None): - from platformcode import xbmc_videolibrary xbmc_videolibrary.ask_set_content(silent=True) elif settings_pre.get("videolibrary_kodi", None) and not settings_post.get("videolibrary_kodi", None): - move_videolibrary.clear_db() + xbmc_videolibrary.clear_db() def get_setting(name, channel="", server="", default=None): diff --git a/platformcode/xbmc_videolibrary.py b/platformcode/xbmc_videolibrary.py index f9e631e0..3d4aa43e 100644 --- a/platformcode/xbmc_videolibrary.py +++ b/platformcode/xbmc_videolibrary.py @@ -20,6 +20,7 @@ from core import jsontools from platformcode import config, logger from platformcode import platformtools from core import scrapertools +from xml.dom import minidom def mark_auto_as_watched(item): @@ -794,6 +795,207 @@ def set_content(content_type, silent=False, custom=False): return continuar +def update_db(current_path, new_path, current_movies_folder, new_movies_folder, current_tvshows_folder, new_tvshows_folder, progress): + logger.info() + + new = new_path + old = current_path + + # rename main path for search in the DB + if new.startswith("special://") or scrapertools.find_single_match(new, r'(^\w+:\/\/)'): + new = new.replace('/profile/', '/%/').replace('/home/userdata/', '/%/') + sep = '/' + else: + sep = os.sep + if not new.endswith(sep): + new += sep + + if old.startswith("special://") or scrapertools.find_single_match(old, r'(^\w+:\/\/)'): + old = old.replace('/profile/', '/%/').replace('/home/userdata/', '/%/') + sep = '/' + else: + sep = os.sep + if not old.endswith(sep): + old += sep + + # search MAIN path in the DB + sql = 'SELECT idPath, strPath FROM path where strPath LIKE "%s"' % old + nun_records, records = execute_sql_kodi(sql) + + # change main path + if records: + idPath = records[0][0] + strPath = records[0][1].replace(current_path, new_path) + sql = 'UPDATE path SET strPath="%s" WHERE idPath=%s' % (strPath, idPath) + nun_records, records = execute_sql_kodi(sql) + + p = 80 + progress.update(p, config.get_localized_string(20000), config.get_localized_string(80013)) + + OLD = old + for OldPath, NewPath in [[current_movies_folder, new_movies_folder], [current_tvshows_folder, new_tvshows_folder]]: + old = OLD + OldPath + if not old.endswith(sep): old += sep + + # Search Main Sub Folder + sql = 'SELECT idPath, strPath FROM path where strPath LIKE "%s"' % old + nun_records, records = execute_sql_kodi(sql) + + # Change Main Sub Folder + if records: + for record in records: + idPath = record[0] + strPath = record[1].replace(filetools.join(current_path, OldPath), filetools.join(new_path, NewPath)) + sql = 'UPDATE path SET strPath="%s"WHERE idPath=%s' % (strPath, idPath) + nun_records, records = execute_sql_kodi(sql) + + # Search if Sub Folder exixt in all paths + old += '%' + sql = 'SELECT idPath, strPath FROM path where strPath LIKE "%s"' % old + nun_records, records = execute_sql_kodi(sql) + + #Change Sub Folder in all paths + if records: + for record in records: + idPath = record[0] + strPath = record[1].replace(filetools.join(current_path, OldPath), filetools.join(new_path, NewPath)) + sql = 'UPDATE path SET strPath="%s"WHERE idPath=%s' % (strPath, idPath) + nun_records, records = execute_sql_kodi(sql) + + + if OldPath == current_movies_folder: + # if is Movie Folder + # search and modify in "movie" + sql = 'SELECT idMovie, c22 FROM movie where c22 LIKE "%s"' % old + nun_records, records = execute_sql_kodi(sql) + if records: + for record in records: + idMovie = record[0] + strPath = record[1].replace(filetools.join(current_path, OldPath), filetools.join(new_path, NewPath)) + sql = 'UPDATE movie SET c22="%s" WHERE idMovie=%s' % (strPath, idMovie) + nun_records, records = execute_sql_kodi(sql) + # search and modify in "movie_view" + sql = 'SELECT idMovie, c22, strPath FROM movie_view where c22 LIKE "%s"' % old + nun_records, records = execute_sql_kodi(sql) + if records: + for record in records: + idMovie = record[0] + c22 = record[1].replace(filetools.join(current_path, OldPath), filetools.join(new_path, NewPath)) + strPath = record[2].replace(filetools.join(current_path, OldPath), filetools.join(new_path, NewPath)) + sql = 'UPDATE movie_view SET c22="%s", strPath="%s" WHERE idMovie=%s' % (c22, strPath, idMovie) + nun_records, records = execute_sql_kodi(sql) + else: + # if is Tv Show Folder + # search and modify in "episode" + sql = 'SELECT idEpisode, c18 FROM episode where c18 LIKE "%s"' % old + nun_records, records = execute_sql_kodi(sql) + if records: + for record in records: + idEpisode = record[0] + strPath = record[1].replace(filetools.join(current_path, OldPath), filetools.join(new_path, NewPath)) + sql = 'UPDATE episode SET c18="%s" WHERE idEpisode=%s' % (strPath, idEpisode) + nun_records, records = execute_sql_kodi(sql) + # search and modify in "episode_view" + sql = 'SELECT idEpisode, c18, strPath FROM episode_view where strPath LIKE "%s"' % old + nun_records, records = execute_sql_kodi(sql) + if records: + for record in records: + idEpisode = record[0] + c18 = record[1].replace(filetools.join(current_path, OldPath), filetools.join(new_path, NewPath)) + strPath = record[2].replace(filetools.join(current_path, OldPath), filetools.join(new_path, NewPath)) + sql = 'UPDATE episode_view SET c18="%s", strPath="%s" WHERE idEpisode=%s' % (c18, strPath, idEpisode) + nun_records, records = execute_sql_kodi(sql) + # search and modify in "season_view" + sql = 'SELECT idSeason, strPath FROM season_view where strPath LIKE "%s"' % old + nun_records, records = execute_sql_kodi(sql) + if records: + for record in records: + idSeason = record[0] + strPath = record[1].replace(filetools.join(current_path, OldPath), filetools.join(new_path, NewPath)) + sql = 'UPDATE season_view SET strPath="%s" WHERE idSeason=%s' % (strPath, idSeason) + nun_records, records = execute_sql_kodi(sql) + # search and modify in "tvshow_view" + sql = 'SELECT idShow, strPath FROM tvshow_view where strPath LIKE "%s"' % old + nun_records, records = execute_sql_kodi(sql) + if records: + for record in records: + idShow = record[0] + strPath = record[1].replace(filetools.join(current_path, OldPath), filetools.join(new_path, NewPath)) + sql = 'UPDATE tvshow_view SET strPath="%s" WHERE idShow=%s' % (strPath, idShow) + nun_records, records = execute_sql_kodi(sql) + p += 5 + progress.update(p, config.get_localized_string(20000), config.get_localized_string(80013)) + + +def clear_db(): + logger.info() + progress = platformtools.dialog_progress_bg(config.get_localized_string(20000), config.get_localized_string(80025)) + progress.update(0) + + + config.set_setting('videolibrary_kodi_flag', 1) + config.set_setting('videolibrary_kodi', False) + path = config.get_setting('videolibrarypath') + + # rename main path for search in the DB + if path.startswith("special://") or scrapertools.find_single_match(path, r'(^\w+:\/\/)'): + path = path.replace('/profile/', '/%/').replace('/home/userdata/', '/%/') + sep = '/' + else: + sep = os.sep + if not path.endswith(sep): + path += sep + + # search path in the db + sql = 'SELECT idPath, strPath FROM path where strPath LIKE "%s"' % path + nun_records, records = execute_sql_kodi(sql) + + # change main path + if records: + idPath = records[0][0] + sql = 'DELETE from path WHERE idPath=%s' % idPath + nun_records, records = execute_sql_kodi(sql) + progress.update(20) + + path += '%' + # search path in the db + sql = 'SELECT idPath, strPath FROM path where strPath LIKE "%s"' % path + nun_records, records = execute_sql_kodi(sql) + + # change main path + if records: + for record in records: + idPath = record[0] + sql = 'DELETE from path WHERE idPath=%s' % idPath + nun_records, records = execute_sql_kodi(sql) + # search path in the db + sql = 'SELECT idMovie, c22 FROM movie where c22 LIKE "%s"' % path + nun_records, records = execute_sql_kodi(sql) + progress.update(40) + # change main path + if records: + for record in records: + idMovie = record[0] + sql = 'DELETE from movie WHERE idMovie=%s' % idMovie + nun_records, records = execute_sql_kodi(sql) + + # search path in the db + sql = 'SELECT idEpisode, c18 FROM episode where c18 LIKE "%s"' % path + nun_records, records = execute_sql_kodi(sql) + progress.update(60) + # change main path + if records: + for record in records: + idEpisode = record[0] + sql = 'DELETE from episode WHERE idEpisode=%s' % idEpisode + nun_records, records = execute_sql_kodi(sql) + progress.update(80) + update_sources(path) + clear_cache() + progress.update(100) + progress.close() + + def execute_sql_kodi(sql): """ Ejecuta la consulta sql contra la base de datos de kodi @@ -862,8 +1064,6 @@ def execute_sql_kodi(sql): def add_sources(path): logger.info() try: - from xml.dom import minidom - SOURCES_PATH = xbmc.translatePath("special://userdata/sources.xml") if os.path.exists(SOURCES_PATH): @@ -935,6 +1135,93 @@ def add_sources(path): logger.debug("An error occurred. The path %s has not been added to sources.xml" % path) return False +def update_sources(old, new=''): + logger.info() + if new == old: return + + SOURCES_PATH = xbmc.translatePath("special://userdata/sources.xml") + if filetools.isfile(SOURCES_PATH): + xmldoc = minidom.parse(SOURCES_PATH) + + # collect nodes + # nodes = xmldoc.getElementsByTagName("video") + video_node = xmldoc.childNodes[0].getElementsByTagName("video")[0] + paths_node = video_node.getElementsByTagName("path") + + # delete old path + for node in paths_node: + if node.firstChild.data == old: + parent = node.parentNode + remove = parent.parentNode + remove.removeChild(parent) + + # write changes + if sys.version_info[0] >= 3: #PY3 + filetools.write(SOURCES_PATH, '\n'.join([x for x in xmldoc.toprettyxml().encode("utf-8").splitlines() if x.strip()])) + else: + filetools.write(SOURCES_PATH, b'\n'.join([x for x in xmldoc.toprettyxml().encode("utf-8").splitlines() if x.strip()]), vfs=False) + + if new: + # create new path + list_path = [p.firstChild.data for p in paths_node] + if new in list_path: + logger.info("path %s already exists in sources.xml" % new) + return + logger.info("path %s does not exist in sources.xml" % new) + + # if the path does not exist we create one + source_node = xmldoc.createElement("source") + + # Node + name_node = xmldoc.createElement("name") + sep = os.sep + if new.startswith("special://") or scrapertools.find_single_match(new, r'(^\w+:\/\/)'): + sep = "/" + name = new + if new.endswith(sep): + name = new[:-1] + name_node.appendChild(xmldoc.createTextNode(name.rsplit(sep)[-1])) + source_node.appendChild(name_node) + + # Node + path_node = xmldoc.createElement("path") + path_node.setAttribute("pathversion", "1") + path_node.appendChild(xmldoc.createTextNode(new)) + source_node.appendChild(path_node) + + # Node + allowsharing_node = xmldoc.createElement("allowsharing") + allowsharing_node.appendChild(xmldoc.createTextNode('true')) + source_node.appendChild(allowsharing_node) + + # Añadimos a