def purge(): """Purge all items exported to Kodi library and delete internal library database""" common.debug('Purging library: {}'.format(g.library())) for library_item in g.library().values(): execute_library_tasks(library_item['videoid'], remove_item, common.get_local_string(30030), sync_mylist=False)
def _get_library_entry(videoid): """Get the first leaf-entry for videoid from the library. For shows and seasons this will return the first contained episode""" if videoid.mediatype in [common.VideoId.MOVIE, common.VideoId.EPISODE]: return (common.get_path(videoid.to_list(), g.library()), videoid.mediatype) elif videoid.mediatype == common.VideoId.SHOW: return (_any_child_library_entry( _any_child_library_entry(g.library()[videoid.tvshowid])), common.VideoId.EPISODE) elif videoid.mediatype == common.VideoId.SEASON: return (_any_child_library_entry( g.library()[videoid.tvshowid][videoid.seasonid]), common.VideoId.EPISODE) else: # Items of other mediatype are never in library raise ItemNotFound
def remove_item(item_task, library_home=None): """Remove an item from the library and delete if from disk""" # pylint: disable=unused-argument, broad-except if item_task[ 'is_strm']: # We don't take care of a tvshow.nfo task if we are running an update common.debug('Removing {} from library'.format(item_task['title'])) if not is_in_library(item_task['videoid']): common.warn('cannot remove {}, item not in library'.format( item_task['title'])) return id_path = item_task['videoid'].to_list() exported_filename = xbmc.translatePath( common.get_path(id_path, g.library())['file']).decode("utf-8") parent_folder = os.path.dirname(exported_filename) try: xbmcvfs.delete( xbmc.translatePath(exported_filename).decode("utf-8")) # Remove the NFO files if exists nfo_file = os.path.splitext( xbmc.translatePath(exported_filename).decode( "utf-8"))[0] + '.nfo' if xbmcvfs.exists(nfo_file): xbmcvfs.delete(nfo_file) dirs, files = xbmcvfs.listdir( xbmc.translatePath(parent_folder).decode("utf-8")) tvshow_nfo_file = xbmc.makeLegalFilename( os.path.join( xbmc.translatePath(parent_folder).decode("utf-8"), 'tvshow.nfo')) # Remove tvshow_nfo_file only when is the last file (users have the option of removing even single seasons) if xbmcvfs.exists(tvshow_nfo_file) and not dirs and len( files) == 1: xbmcvfs.delete(tvshow_nfo_file) # Delete parent folder xbmcvfs.rmdir( xbmc.translatePath(parent_folder).decode("utf-8")) # Delete parent folder when empty if not dirs and not files: xbmcvfs.rmdir( xbmc.translatePath(parent_folder).decode("utf-8")) except Exception: common.debug('Cannot delete {}, file does not exist'.format( exported_filename)) common.remove_path(id_path, g.library(), lambda e: e.keys() == ['videoid']) g.save_library()
def remove_item(item_task, library_home=None): """Remove an item from the library and delete if from disk""" # pylint: disable=unused-argument, broad-except common.debug('Removing {} from library'.format(item_task['title'])) if not is_in_library(item_task['videoid']): common.warn('cannot remove {}, item not in library'.format( item_task['title'])) return id_path = item_task['videoid'].to_list() exported_filename = xbmc.translatePath( common.get_path(id_path, g.library())['file']).decode("utf-8") parent_folder = os.path.dirname(exported_filename) try: xbmcvfs.delete(xbmc.translatePath(exported_filename).decode("utf-8")) if not os.listdir(parent_folder): os.rmdir(parent_folder) except Exception: common.debug( 'Cannot delete {}, file does not exist'.format(exported_filename)) common.remove_path(id_path, g.library(), lambda e: e.keys() == ['videoid']) g.save_library()
def _add_to_library(videoid, export_filename): """Add an exported file to the library""" library_node = g.library() for depth, id_item in enumerate(videoid.to_list()): if id_item not in library_node: # No entry yet at this level, create a new one and assign # it an appropriate videoid for later reference library_node[id_item] = {'videoid': videoid.derive_parent(depth)} library_node = library_node[id_item] library_node['file'] = export_filename library_node['videoid'] = videoid g.save_library()
def is_in_library(videoid): """Return True if the video is in the local Kodi library, else False""" return common.get_path_safe(videoid.to_list(), g.library()) is not None
def list_contents(): """Return a list of all top-level video IDs (movies, shows) contained in the library""" return g.library().keys()