def execImport(handle, options): if not 'path' in options: log('cannot execute "import" without path', xbmc.LOGERROR) return # parse all necessary options mediaTypes = mediaTypesFromOptions(options) if not mediaTypes: log('cannot execute "import" without media types', xbmc.LOGERROR) return # retrieve the media import mediaImport = xbmcmediaimport.getImport(handle) if not mediaImport: log('cannot retrieve media import', xbmc.LOGERROR) return # prepare and get the media import settings importSettings = mediaImport.prepareSettings() if not importSettings: log('cannot prepare media import settings', xbmc.LOGERROR) return # retrieve the media provider mediaProvider = mediaImport.getProvider() if not mediaProvider: log('cannot retrieve media provider', xbmc.LOGERROR) return log('importing {} items from {}...'.format(mediaTypes, mediaProvider2str(mediaProvider))) # prepare the media provider settings mediaProviderSettings = mediaProvider.prepareSettings() if not mediaProviderSettings: log('cannot prepare media provider settings', xbmc.LOGERROR) return # create an Emby server instance embyServer = Server(mediaProvider) if not embyServer.Authenticate(): log('failed to authenticate on media provider {}'.format(mediaProvider2str(mediaProvider)), xbmc.LOGERROR) return # build the base URL to retrieve items baseUrl = embyServer.BuildUserUrl(emby.constants.URL_ITEMS) baseUrlOptions = { 'Recursive': 'true', 'Fields': ','.join(EMBY_ITEM_FIELDS), 'ExcludeLocationTypes': 'Virtual,Offline', 'Limit': ITEM_REQUEST_LIMIT } baseUrl = Url.addOptions(baseUrl, baseUrlOptions) # get all (matching) library views selectedViews = getLibraryViewsFromSettings(importSettings) views = getMatchingLibraryViews(embyServer, mediaTypes, selectedViews) if not views: log('cannot retrieve items without any library views', xbmc.LOGERROR) return # determine whether Direct Play is allowed allowDirectPlay = mediaProviderSettings.getBool(emby.constants.SETTING_PROVIDER_PLAYBACK_ALLOW_DIRECT_PLAY) # determine whether to import collections importCollections = importSettings.getBool(emby.constants.SETTING_IMPORT_IMPORT_COLLECTIONS) # loop over all media types to be imported progress = 0 progressTotal = len(mediaTypes) for mediaType in mediaTypes: if xbmcmediaimport.shouldCancel(handle, progress, progressTotal): return progress += 1 if mediaType == xbmcmediaimport.MediaTypeVideoCollection and not importCollections: log('importing {} items from {} is disabled'.format(mediaType, mediaProvider2str(mediaProvider)), xbmc.LOGDEBUG) continue log('importing {} items from {}...'.format(mediaType, mediaProvider2str(mediaProvider))) mappedMediaType = kodi.Api.getEmbyMediaType(mediaType) if not mappedMediaType: log('cannot import unsupported media type "{}"'.format(mediaType), xbmc.LOGERROR) continue (_, embyMediaType, localizedMediaType) = mappedMediaType xbmcmediaimport.setProgressStatus(handle, __addon__.getLocalizedString(32001).format(__addon__.getLocalizedString(localizedMediaType))) urlOptions = { 'IncludeItemTypes': embyMediaType } url = Url.addOptions(baseUrl, urlOptions) boxsetUrlOptions = { 'IncludeItemTypes': kodi.EMBY_MEDIATYPE_BOXSET } boxsetUrl = Url.addOptions(baseUrl, boxsetUrlOptions) items = [] boxsets = {} # handle library views for view in views: log('importing {} items from "{}" view from {}...'.format(mediaType, view.name, mediaProvider2str(mediaProvider))) items.extend(importItems(handle, embyServer, url, mediaType, view.id, embyMediaType=embyMediaType, viewName=view.name, allowDirectPlay=allowDirectPlay)) if importCollections and items and mediaType == xbmcmediaimport.MediaTypeMovie: # retrieve all BoxSets / collections matching the current media type boxsetObjs = importItems(handle, embyServer, boxsetUrl, mediaType, view.id, raw=True, allowDirectPlay=allowDirectPlay) for boxsetObj in boxsetObjs: if not emby.constants.PROPERTY_ITEM_ID in boxsetObj or not emby.constants.PROPERTY_ITEM_NAME in boxsetObj: continue boxsetId = boxsetObj[emby.constants.PROPERTY_ITEM_ID] boxsetName = boxsetObj[emby.constants.PROPERTY_ITEM_NAME] boxsets[boxsetId] = boxsetName # handle BoxSets / collections if importCollections and items: for (boxsetId, boxsetName) in iteritems(boxsets): # get all items belonging to the BoxSet boxsetItems = importItems(handle, embyServer, url, mediaType, boxsetId, embyMediaType=embyMediaType, viewName=boxsetName, allowDirectPlay=allowDirectPlay) for boxsetItem in boxsetItems: # find the matching retrieved item for (index, item) in enumerate(items): if boxsetItem.getPath() == item.getPath(): # set the BoxSet / collection kodi.Api.setCollection(item, boxsetName) items[index] = item log('{} {} items imported from {}'.format(len(items), mediaType, mediaProvider2str(mediaProvider))) if items: xbmcmediaimport.addImportItems(handle, items, mediaType) xbmcmediaimport.finishImport(handle)
def execImport(handle, options): if not 'path' in options: log('cannot execute "import" without path', xbmc.LOGERROR) return # parse all necessary options mediaTypes = mediaTypesFromOptions(options) if not mediaTypes: log('cannot execute "import" without media types', xbmc.LOGERROR) return # retrieve the media import mediaImport = xbmcmediaimport.getImport(handle) if not mediaImport: log('cannot retrieve media import', xbmc.LOGERROR) return # prepare and get the media import settings importSettings = mediaImport.prepareSettings() if not importSettings: log('cannot prepare media import settings', xbmc.LOGERROR) return # retrieve the media provider mediaProvider = mediaImport.getProvider() if not mediaProvider: log('cannot retrieve media provider', xbmc.LOGERROR) return log('importing {} items from {}...'.format( mediaTypes, mediaProvider2str(mediaProvider))) # prepare the media provider settings mediaProviderSettings = mediaProvider.prepareSettings() if not mediaProviderSettings: log('cannot prepare media provider settings', xbmc.LOGERROR) return # create an Emby server instance embyServer = Server(mediaProvider) # build the base URL to retrieve items baseUrl = embyServer.BuildUserUrl(emby.constants.URL_ITEMS) baseUrlOptions = { emby.constants.URL_QUERY_ITEMS_RECURSIVE: 'true', emby.constants.URL_QUERY_ITEMS_FIELDS: ','.join(EMBY_ITEM_FIELDS), emby.constants.URL_QUERY_ITEMS_EXCLUDE_LOCATION_TYPES: 'Virtual,Offline', emby.constants.URL_QUERY_ITEMS_LIMIT: ITEM_REQUEST_LIMIT } baseUrl = Url.addOptions(baseUrl, baseUrlOptions) # get all (matching) library views selectedViews = ImportSettings.GetLibraryViews(importSettings) views = getMatchingLibraryViews(embyServer, mediaTypes, selectedViews) if not views: log('cannot retrieve items without any library views', xbmc.LOGERROR) return # determine whether Direct Play is allowed allowDirectPlay = mediaProviderSettings.getBool( emby.constants.SETTING_PROVIDER_PLAYBACK_ALLOW_DIRECT_PLAY) # determine whether to import collections importCollections = importSettings.getBool( emby.constants.SETTING_IMPORT_IMPORT_COLLECTIONS) # determine the last sync time and whether we should perform a fast sync fastSync = False syncUrlOptions = {} # check if synchronization related settings have changed; if yes we have to perform a full synchronization if SynchronizationSettings.HaveChanged(mediaTypes, mediaProviderSettings, importSettings, save=True): log('forcing a full synchronization to import {} items from {} because some related settings have changed' \ .format(mediaTypes, mediaProvider2str(mediaProvider))) else: # check if we # have already performed a (full) synchronization before # should use the Kodi Companion Emby server plugin lastSync = mediaImport.getLastSynced() if lastSync and mediaProviderSettings.getBool( emby.constants. SETTING_PROVIDER_SYNCHRONIZATION_USE_KODI_COMPANION): if KodiCompanion.IsInstalled(embyServer): fastSync = True # convert the last sync datetime string to ISO 8601 lastSync = parser.parse(lastSync).astimezone(utc).isoformat( timespec='seconds') syncUrlOptions.update({ # only set MinDateLastSavedForUser because it already covers DateLastSaved, RatingLastModified # and PlaystateLastModified. Setting both MinDateLastSaved and MinDateLastSavedForUser will # cause issues, see https://emby.media/community/index.php?/topic/82258-retrieving-changeset-when-client-returns-online-mediaimport/ emby.constants.URL_QUERY_ITEMS_MIN_DATE_LAST_SAVED_FOR_USER: lastSync }) log('using fast synchronization to import {} items from {} with Kodi companion plugin' \ .format(mediaTypes, mediaProvider2str(mediaProvider)), xbmc.LOGDEBUG) # retrieving the sync queue from Kodi companion syncQueue = KodiCompanion.SyncQueue.GetItems( embyServer, lastSync) else: log('Kodi companion usage is enabled to import {} items from {} but the server plugin is not installed' \ .format(mediaTypes, mediaProvider2str(mediaProvider)), xbmc.LOGWARNING) # loop over all media types to be imported progress = 0 progressTotal = len(mediaTypes) for mediaType in mediaTypes: if xbmcmediaimport.shouldCancel(handle, progress, progressTotal): return progress += 1 if mediaType == xbmcmediaimport.MediaTypeVideoCollection and not importCollections: log( 'importing {} items from {} is disabled'.format( mediaType, mediaProvider2str(mediaProvider)), xbmc.LOGDEBUG) continue log('importing {} items from {}...'.format( mediaType, mediaProvider2str(mediaProvider))) mappedMediaType = kodi.Api.getEmbyMediaType(mediaType) if not mappedMediaType: log('cannot import unsupported media type "{}"'.format(mediaType), xbmc.LOGERROR) continue (_, embyMediaType, localizedMediaType) = mappedMediaType xbmcmediaimport.setProgressStatus( handle, __addon__.getLocalizedString(32001).format( __addon__.getLocalizedString(localizedMediaType))) urlOptions = syncUrlOptions.copy() urlOptions.update( {emby.constants.URL_QUERY_ITEMS_INCLUDE_ITEM_TYPES: embyMediaType}) url = Url.addOptions(baseUrl, urlOptions) boxsetUrlOptions = { emby.constants.URL_QUERY_ITEMS_INCLUDE_ITEM_TYPES: kodi.EMBY_MEDIATYPE_BOXSET } boxsetUrl = Url.addOptions(baseUrl, boxsetUrlOptions) items = [] boxsets = {} # handle library views for view in views: log('importing {} items from "{}" view from {}...'.format( mediaType, view.name, mediaProvider2str(mediaProvider))) items.extend( importItems(handle, embyServer, url, mediaType, view.id, embyMediaType=embyMediaType, viewName=view.name, allowDirectPlay=allowDirectPlay)) if importCollections and items and mediaType == xbmcmediaimport.MediaTypeMovie: # retrieve all BoxSets / collections matching the current media type boxsetObjs = importItems(handle, embyServer, boxsetUrl, mediaType, view.id, raw=True, allowDirectPlay=allowDirectPlay) for boxsetObj in boxsetObjs: if not emby.constants.PROPERTY_ITEM_ID in boxsetObj or not emby.constants.PROPERTY_ITEM_NAME in boxsetObj: continue boxsetId = boxsetObj[emby.constants.PROPERTY_ITEM_ID] boxsetName = boxsetObj[emby.constants.PROPERTY_ITEM_NAME] boxsets[boxsetId] = boxsetName # handle BoxSets / collections if importCollections and items: for (boxsetId, boxsetName) in iteritems(boxsets): # get all items belonging to the BoxSet boxsetItems = importItems(handle, embyServer, url, mediaType, boxsetId, embyMediaType=embyMediaType, viewName=boxsetName, allowDirectPlay=allowDirectPlay) for boxsetItem in boxsetItems: # find the matching retrieved item for index, item in enumerate(items): if boxsetItem.getPath() == item.getPath(): # set the BoxSet / collection kodi.Api.setCollection(item, boxsetName) items[index] = item # in a fast sync we need to get the removed items from Kodi companion if fastSync: if items: log('{} changed {} items imported from {}'.format( len(items), mediaType, mediaProvider2str(mediaProvider))) # handle removed items through Kodi companion's sync queue if syncQueue.itemsRemoved: # retrieve all local items matching the current media type from the current import localItems = xbmcmediaimport.getImportedItems( handle, mediaType) # match the local items against the changed items removedItems, = kodi.Api.matchImportedItemIdsToLocalItems( localItems, syncQueue.itemsRemoved) # pylint: disable=unbalanced-tuple-unpacking # erase all removed items matching the current media type from the sync queue syncQueue.itemsRemoved = [ removedItem for removedItem in syncQueue.itemsRemoved if removedItem in removedItems ] if removedItems: log('{} previously imported {} items removed from {}'. format(len(removedItems), mediaType, mediaProvider2str(mediaProvider))) xbmcmediaimport.addImportItems( handle, removedItems, mediaType, xbmcmediaimport.MediaImportChangesetTypeRemoved) else: log('{} {} items imported from {}'.format( len(items), mediaType, mediaProvider2str(mediaProvider))) # pass the imported items back to Kodi if items: xbmcmediaimport.addImportItems(handle, items, mediaType) xbmcmediaimport.finishImport(handle, fastSync)