def migrate_library(): # Migrate the Kodi library to the new format of STRM path # - Old STRM: '/play/show/xxxxxxxx/season/xxxxxxxx/episode/xxxxxxxx/' (used before ver 1.7.0) # - New STRM: '/play_strm/show/xxxxxxxx/season/xxxxxxxx/episode/xxxxxxxx/' (used from ver 1.7.0) folders = get_library_subfolders( FOLDER_NAME_MOVIES) + get_library_subfolders(FOLDER_NAME_SHOWS) if not folders: return LOG.debug('Start migrating STRM files') try: with ui.ProgressDialog(True, title='Migrating library to new format', max_value=len(folders)) as progress_bar: for folder_path in folders: folder_name = os.path.basename( G.py2_decode(xbmc.translatePath(folder_path))) progress_bar.set_message('PLEASE WAIT - Migrating: ' + folder_name) _migrate_strm_files(folder_path) except Exception as exc: # pylint: disable=broad-except LOG.error('Migrating failed: {}', exc) import traceback LOG.error(G.py2_decode(traceback.format_exc(), 'latin-1')) ui.show_ok_dialog('Migrating library to new format', ( 'Library migration has failed.[CR]' 'Before try play a Netflix video from library, you must run manually the library migration, ' 'otherwise you will have add-on malfunctions.[CR][CR]' 'Open add-on settings on "Library" section, and select "Import existing library".' ))
def import_library(self, path): """ Imports an already existing exported STRM library into the add-on library database, allows you to restore an existing library, by avoiding to recreate it from scratch. This operations also update the missing tv shows seasons and episodes, and automatically converts old STRM format type from add-on version 0.13.x or before 1.7.0 to new format. """ # If set ask to user if want to export NFO files nfo_settings = nfo.NFOSettings() nfo_settings.show_export_dialog() LOG.info('Start importing Kodi library') remove_folders = [ ] # List of failed imports paths to be optionally removed remove_titles = [ ] # List of failed imports titles to be optionally removed # Start importing STRM files folders = get_library_subfolders(FOLDER_NAME_MOVIES, path) + get_library_subfolders( FOLDER_NAME_SHOWS, path) with ui.ProgressDialog(True, max_value=len(folders)) as progress_bar: for folder_path in folders: folder_name = os.path.basename( G.py2_decode(translatePath(folder_path))) progress_bar.set_message(folder_name) try: videoid = self.import_videoid_from_existing_strm( folder_path, folder_name) if videoid is None: # Failed to import, add folder to remove list remove_folders.append(folder_path) remove_titles.append(folder_name) continue # Successfully imported, Execute the task for index, total_tasks, title in self.execute_library_task( videoid, self.export_item, nfo_settings=nfo_settings, notify_errors=True): label_partial_op = ' ({}/{})'.format( index + 1, total_tasks) if total_tasks > 1 else '' progress_bar.set_message(title + label_partial_op) if progress_bar.is_cancelled(): LOG.warn('Import library interrupted by User') return if self.monitor.abortRequested(): LOG.warn('Import library interrupted by Kodi') return except ImportWarning: # Ignore it, something was wrong in STRM file (see _import_videoid in library_jobs.py) pass progress_bar.perform_step() progress_bar.set_wait_message() delay_anti_ban() ret = self._import_library_remove(remove_titles, remove_folders) request_kodi_library_update(scan=True, clean=ret)
def _update_library(self, videoids_tasks, exp_tvshows_videoids_values, show_prg_dialog, show_nfo_dialog, clear_on_cancel): # If set ask to user if want to export NFO files (override user custom NFO settings for videoids) nfo_settings_override = None if show_nfo_dialog: nfo_settings_override = nfo.NFOSettings() nfo_settings_override.show_export_dialog() # Get the exported tvshows, but to be excluded from the updates excluded_videoids_values = G.SHARED_DB.get_tvshows_id_list( VidLibProp['exclude_update'], True) # Start the update operations with ui.ProgressDialog(show_prg_dialog, max_value=len(videoids_tasks)) as progress_bar: for videoid, task_handler in iteritems(videoids_tasks): # Check if current videoid is excluded from updates if int(videoid.value) in excluded_videoids_values: continue # Get the NFO settings for the current videoid if not nfo_settings_override and int( videoid.value) in exp_tvshows_videoids_values: # User custom NFO setting # it is possible that the user has chosen not to export NFO files for a specific tv show nfo_export = G.SHARED_DB.get_tvshow_property( videoid.value, VidLibProp['nfo_export'], False) nfo_settings = nfo.NFOSettings(nfo_export) else: nfo_settings = nfo_settings_override or nfo.NFOSettings() # Execute the task for index, total_tasks, title in self.execute_library_task( videoid, task_handler, nfo_settings=nfo_settings, notify_errors=show_prg_dialog): label_partial_op = ' ({}/{})'.format( index + 1, total_tasks) if total_tasks > 1 else '' progress_bar.set_message(title + label_partial_op) if progress_bar.is_cancelled(): LOG.warn( 'Auto update of the Kodi library interrupted by User') if clear_on_cancel: self.clear_library(True) return False if self.monitor.abortRequested(): LOG.warn( 'Auto update of the Kodi library interrupted by Kodi') return False progress_bar.perform_step() progress_bar.set_wait_message() delay_anti_ban() common.clean_library(show_prg_dialog) return True
def clear_library(self, show_prg_dialog=True): """ Delete all exported items to the library :param show_prg_dialog: if True, will be show a progress dialog window """ LOG.info('Start deleting exported library items') # This will clear all the add-on library data, to prevents possible inconsistencies when for some reason # such as improper use of the add-on, unexpected error or other has broken the library database data or files with ui.ProgressDialog(show_prg_dialog, common.get_local_string(30245), max_value=3) as progress_dlg: progress_dlg.perform_step() progress_dlg.set_wait_message() G.SHARED_DB.purge_library() for folder_name in [FOLDER_NAME_MOVIES, FOLDER_NAME_SHOWS]: progress_dlg.perform_step() progress_dlg.set_wait_message() section_root_dir = common.join_folders_paths(get_library_path(), folder_name) common.delete_folder_contents(section_root_dir, delete_subfolders=True) # Clean the Kodi library database common.clean_library(show_prg_dialog)
def _import_library_remove(self, remove_titles, remove_folders): if not remove_folders: return False # If there are STRM files that it was not possible to import them, # we will ask to user if you want to delete them tot_folders = len(remove_folders) if tot_folders > 50: remove_titles = remove_titles[:50] + ['...'] message = common.get_local_string(30246).format(tot_folders) + '[CR][CR]' + ', '.join(remove_titles) if not ui.ask_for_confirmation(common.get_local_string(30140), message): return False # Delete all folders LOG.info('Start deleting folders') with ui.ProgressDialog(True, max_value=tot_folders) as progress_bar: for file_path in remove_folders: progress_bar.set_message('{}/{}'.format(progress_bar.value, tot_folders)) LOG.debug('Deleting folder: {}', file_path) common.delete_folder(file_path) progress_bar.perform_step() return True