Skip to content

kodi.mediaimporter extension point

Montellese edited this page Apr 11, 2020 · 9 revisions

The add-on importer provided by Kodi core requires add-ons to implement the kodi.mediaimporter extension point to be able to support media importing. This page describes what an add-on has to provide / implement to become a media importer.

addon.xml

To implement the kodi.mediaimporter extension point an add-on must contain the following definition in addon.xml:

<extension point="kodi.mediaimporter" discovery="<DISCOVERY SCRIPT>" library="<IMPORTER SCRIPT>" observer="<OBSERVER SCRIPT>">
    <canlookupprovider protocol="<PROTOCOL>">true</canlookupprovider>
    <automaticallyaddasprovider>
        <supportedmediatypes>movies musicvideos tvshows seasons episodes</supportedmediatypes>
    </automaticallyaddasprovider>
    <providersettings>providersettings.xml</providersettings>
    <importsettings prepare="false">importsettings.xml</importsettings>
</extension>

<extension>

Attribute Description Example
point Must be "kodi.mediaimporter"
discovery Service script executed to discover and track providers. discovery.py
library Script executed with different actions to import media items. library.py
observer Service script executed to observe registered providers for changes to imported media items. observer.py

<canlookupprovider>

This tag expects a boolean value (true or false) and tells Kodi whether the media importer implementation supports manually looking up providers using the discoverprovider and lookupprovider actions / endpoints.

An optional protocol attribute can be provided (defaults to the add-on's name) which is shown to the user when he wants to manually add a provider.

<automaticallyaddasprovider>

If this tag is present and contains the <supportedmediatypes> sub-tag containing at least one valid media type Kodi will automatically add a (new) media provider with the add-ons details (ID, name, icon) and the media types from <supportedmediatypes> as the possible media types to import.

<providersettings>

This tag expects the path to the settings XML file for registered providers.

An optional prepare attribute can be provided (defaults to true) which indicates whether Kodi should execute the loadprovidersettings or not.

<importsettings>

This tag expects the path to the settings XML file for registered imports.

An optional prepare attribute can be provided (defaults to true) which indicates whether Kodi should execute the loadimportsettings or not.

Discovery service

The discovery service doesn't have to implement any strict interface. It must be written as a service and use xbmc.Monitor to detect when the service must be terminated.

Adding and updating providers

Once a valid provider has been detected it must be described as a MediaProvider instance and passed to Kodi using either the xbmcmediaimport.addProvider() or xbmcmediaimport.addAndActivateProvider() method.

If an already discovered provider has changed one of its properties it can be updated using the xbmcmediaimport.updateProvider() method.

Activating and deactivating providers

Providers are considered active as soon as they can be used to potentially import media items from. An already discovered provider can be activated and deactivated using the xbmcmediaimport.activateProvider() and xbmcmediaimport.deactivateProvider() methods.

It is good practice to keep track of discovered and active providers and use a reasonable timeout to deactivate providers that haven't been accessible for a while (e.g. 10 seconds).

Library script

The library script is executed separately for every action / interaction with Kodi's media import logic. Each execution receives the following arguments:

  • sys.argv[0] contains the path of the library script execution
  • sys.argv[1] contains the handle of the library script execution required for calling the xbmcmediaimport methods.
  • sys.argv[2] is optional and contains additional arguments as key-value pairs in query string format (starting with a leading ?).

The actual action / endpoint to execute is provided as the path component of the path of the library script execution (sys.argv[0]).

Actions / Endpoints

Defined by Kodi

The following actions are defined and executed by Kodi:

Action Required Description Additional Arguments
discoverprovider If <canlookupprovider> is true. Ask the user for details (e.g. a URL) to discover a new provider. The discovered provider must not be fully accessible (e.g. user authentication) yet. The media importer add-on must have collected enough information to be able to lookup and communicate with the discovered provider. -
lookupprovider If <canlookupprovider> is true. Try to lookup and communicate with the given provider. -
canimport Yes Tell Kodi whether the media importer add-on is capable of importing media items from the given path. path
isproviderready Yes Tell Kodi whether the available MediaProvider is ready for importing media items. This includes all required access (e.g. user authentication) to the provider. -
isimportready Yes Tell Kodi whether the available MediaImport is ready for importing media items. This includes all required access to the provider and import specific configuration. -
loadprovidersettings If <providersettings prepare="true"> or prepare is omitted. Load the Settings of the available MediaProvider. -
loadimportsettings If <importsettings prepare="true"> or prepare is omitted. Load the Settings of the available MediaImport. -
canupdatemetadataonprovider Yes Tell Kodi whether the media importer add-on supports updating metadata of imported media items on the provider. -
canupdateplaycountonprovider Yes Tell Kodi whether the media importer add-on supports updating the playcount of imported media items on the provider. -
canupdatelastplayedonprovider Yes Tell Kodi whether the media importer add-on supports updating the last played date of imported media items on the provider. -
canupdateresumepositiononprovider Yes Tell Kodi whether the media importer add-on supports updating the resume point of imported media items on the provider. -
import Yes Import media items of the given media type from the available MediaImport. path, mediatypes or mediatypes[]
updateonprovider If canupdatemetadataonprovider, canupdateplaycountonprovider, canupdatelastplayedonprovider or canupdateresumepositiononprovider returns True. Update the metadata and / or playback related properties of the available media item on the available MediaProvider. -

Defined by the add-on

Using features of the Settings class for MediaProviders and MediaImports it is possible to define custom actions / endpoints.

Workflows

The following workflows are very common:

Discover a new provider

When a user wants to discover a new provider Kodi provides a list of protocols (see <extension>). Once the user has selected a protocol the respective media importer (add-on) is called with the discoverprovider action / endpoint. The add-on now has the full flexibility to ask the user for details required to discover a specific provider. Common questions are e.g. the URL of the provider. Once the user has provided all necessary details and the add-on can contact (not necessarily authenticated) the provider it must create a new MediaProvider instance and fill in its details. This MediaProvider instance must then be passed to Kodi using the xbmcmediaimport.setDiscoveredProvider() method.

Lookup a manually discovered provider

Once a user has manually discovered a new provider Kodi must be able to keep track of the provider to be able to detect whether it is still active or not. This is done by periodically calling the media importer (add-on) with the lookupprovider action / endpoint. The add-on must retrieve the available MediaProvider using the xbmcmediaimport.getProvider() method. Based on the available details of the MediaProvider the add-on must try to contact the provider. The result must be passed back to Kodi using the xbmcmediaimport.setProviderFound() method.

Check if a provider / import is ready

For every registered provider and import Kodi must know if it is ready to import media items. Depending on the provider and import readyness is determined by different factors like successful authentication, additional configuration of the items to import etc. For this Kodi calls the media importer add-on with the isproviderready and isimportready actions / endpoints. The add-on must retrieve the available MediaProvider using the xbmcmediaimport.getProvider() method or the available MediaImport using the xbmcmediaimport.getImport() method. Based on the details and settings the add-on must determine if all necessary information is available. The result must be passed back to Kodi using the xbmcmediaimport.setProviderReady() or xbmcmediaimport.setImportReady() method.

Load provider / import specific settings

Whenever a user or the media importer add-on wants to access the MediaProvider or MediaImport specific settings it must first load and prepare these settings. For this Kodi calls the media importer add-on with the loadprovidersettings and loadimportsettings actions / endpoints. The add-on must retrieve the available MediaProvider using the xbmcmediaimport.getProvider() method or the available MediaImport using the xbmcmediaimport.getImport() method. Using the MediaProvider.getSettings() or MediaImport.getSettings() method it must retrieve the current settings, check their validity and register any required callbacks (see Settings and here). Once the settings have been successfully loaded they must be marked as such using the Settings.setLoaded() method.

Import media items

When Kodi wants to import media items from a specific import it calls the media importer add-on with the import action / endpoint. The add-on must

  1. retrieve the path and mediaTypes arguments
  2. retrieve the available MediaImport using the xbmcmediaimport.getImport() method
  3. prepare the MediaImport settings using the MediaImport.prepareSettings() method
  4. retrieve the available MediaProvider using the xbmcmediaimport.getProvider() method
  5. prepare the MediaProvider settings using the MediaProvider.prepareSettings() method
  6. loop through the given media types and
    1. inform Kodi about the current progress and check whether the import should be cancelled using the xbmcmediaimport.shouldCancel() and xbmcmediaimport.setProgressStatus() methods
    2. optionally retrieve the previously imported media items from the available MediaImport using the xbmcmediaimport.getImportedItems() method
    3. retrieve the media items to be imported from the provider
    4. add the retrieved media items to the available MediaImport using either the xbmcmediaimport.addImportItem() or xbmcmediaimport.addImportItems() method
  7. tell Kodi that the import has been finished using the xbmcmediaimport.finishImport() method

Depending on the capabilities of the provider the add-on can either retrieve all available media items for the MediaImport and pass them to Kodi without a specific changeset type or it can only retrieve the added, changed, removed media items and pass them to Kodi with the matching changeset type.

Update a media item on its provider

When the metadata or playback related properties of an imported media item change Kodi calls the media importer add-on with the updateonprovider action / endpoint. The add-on must

  1. retrieve the available MediaImport using the xbmcmediaimport.getImport() method
  2. prepare the MediaImport settings using the MediaImport.prepareSettings() method
  3. retrieve the available MediaProvider using the xbmcmediaimport.getProvider() method
  4. prepare the MediaProvider settings using the MediaProvider.prepareSettings() method
  5. retrieve the updated media item using the xbmcmediaimport.getUpdatedItem() method
  6. update the changed metadata and / or playback related properties on the provider
  7. tell Kodi that the media item's metadata and / or playback related properties have been updated on the provider using the xbmcmediaimport.finishUpdateOnProvider() method

Observer service

The observer service must implement the xbmcmediaimport.Observer interface and its callbacks. It must be written as a service and use xbmc.Monitor to detect when the service must be terminated.

The observer service must be capable of observing multiple providers and / or imports. Through the callbacks triggered by Kodi it is notified about new, updated and removed providers and imports. These callbacks should be used to start and stop observing providers and imports.

Workflow

The usual workflow for the observer service for a (new) provider and import is:

  1. The onProviderAdded() callback is executed. This callback can be used to initialize observing the given provider. However the observation shouldn't be started yet because the given provider might not be ready / active yet.
  2. The onProviderActivated() callback is executed. This callback can be used to actually start observing the given provider since its fully ready now. Remember that a provider without any imports doesn't really require observing yet.
  3. The onImportAdded() callback is executed. This callback can be used to observe specific media items of the media types belonging to the import.

Callbacks

See Observer for the documentation of all available callbacks.

Thread safety

Due to the fact that the observer service is running as a service while also receiving callbacks from Kodi thread safety must be guaranteed. The callbacks from Kodi will be executed in a separate thread and therefore any actions triggered by a callback from Kodi must be synchronized with the observer service thread.

Python API

Kodi's Python API has been extended with an additional xbmcmediaimporter containing specific methods and classes. Furthermore some methods have been added to existing Python API classes of the xbmc module.

xbmcmediaimporter module

The xbmcmediaimporter module contains specific methods and classes to perform media import actions and interactions.

The following static methods exist directly in the xbmcmediaimporter module. Most of these methods are only valid in a certain context of the media import workflow.

Constants

Media Types

Name Type Description
MediaTypeNone str Unknown media type.
MediaTypeMovie str Unknown media type.
MediaTypeVideoCollection str Unknown media type.
MediaTypeMusicVideo str Unknown media type.
MediaTypeTvShow str Unknown media type.
MediaTypeSeason str Unknown media type.
MediaTypeEpisode str Unknown media type.

Changeset Types

Name Type Description
MediaImportChangesetTypeNone int The media item has not changed or its state is unknown.
MediaImportChangesetTypeAdded int The media item has been added.
MediaImportChangesetTypeChanged int The media item has changed.
MediaImportChangesetTypeRemoved int The media item has been removed.

Discovery service

The following methods can only be used in the discovery service.

Method Return Type Description
addProvider(provider) bool Add a (new) MediaProvider from which MediaImports can be imported.
addAndActivateProvider(provider) bool Add a (new) a MediaProvider from which MediaImports can be imported and activate it.
activateProvider(provider) bool Activate a MediaProvider from which MediaImports can be imported.
deactivateProvider(providerId) - Deactivate a MediaProvider.

Library script

The following methods can only be used in the library script. Every method requires the specific handle of the library script execution to be passed as an argument. The handle is passed as the first argument (sys.argv[1]) to the library script.

Method Return Type Endpoint Description
setDiscoveredProvider(handle, providerDiscovered, provider) - discoverprovider Tells Kodi whether a MediaProvider has been discovered and if so provides the discovered MediaProvider.
setProviderFound(handle, providerFound) - discoverprovider Tells Kodi whether a manually discovered MediaProvider has been found or not.
setCanImport(handle, canImport) - canimport Tells Kodi whether the media importer add-on can import media items from the given path.
setProviderReady(handle, providerReady) - isproviderready Tells Kodi whether the given MediaProvider is ready for importing media items.
setImportReady(handle, importReady) - isimportready Tells Kodi whether the given MediaImport is ready for importing media items.
setCanUpdateMetadataOnProvider(handle, canUpdateMetadataOnProvider) - canupdatemetadataonprovider Tells Kodi whether the media importer add-on can update an imported media item's metadata on the provider.
setCanUpdatePlaycountOnProvider(handle, canUpdatePlaycountOnProvider) - canupdateplaycountonprovider Tells Kodi whether the media importer add-on can update an imported media item's playcount on the provider.
setCanUpdateLastPlayedOnProvider(handle, canUpdateLastPlayedOnProvider) - canupdatelastplayedonprovider Tells Kodi whether the media importer add-on can update an imported media item's last played date on the provider.
setCanUpdateResumePositionOnProvider(handle, canUpdateResumePositionOnProvider) - canupdateresumepositiononprovider Tells Kodi whether the media importer add-on can update an imported media item's resume point on the provider.
getProvider(handle) MediaProvider any Retrieves the MediaProvider to process / import media items from.
getImport(handle) MediaImport any Retrieves the Media Import to process / import media items from.
shouldCancel(handle, progress, total) bool import Tells Kodi the progress of the current import task and asks whether it should be cancelled or not.
The progress is displayed as part of the task's progress bar.
setProgressStatus(handle, status) - import Tells Kodi the status (as str) of the current import task.
The status is displayed as part of the task's progress bar.
getImportedItems(handle, mediaType) [ListItem] import Retrieves all media items of the given media type previously imported from the MediaImport being processed.
addImportItem(handle, item, mediaType,
  changesetType = MediaImportChangesetTypeNone)
- import Adds the given item (as ListItem) with the given media type and changeset type as an imported item from the MediaImport being processed.
addImportItems(handle, items, mediaType,
  changesetType = MediaImportChangesetTypeNone)
- import Adds the given items (as [ListItem]) with the given media type and changeset type as an imported item from the MediaImport being processed.
finishImport(handle) - import Tells Kodi that the current import task is finished.
getUpdatedItem(handle) ListItem updateonprovider Retrieves the imported item for which the metadata and / or playback related properties should be updated on the provider.
finishUpdateOnProvider(handle) - updateonprovider Tells Kodi that the provider has been updated.

General

The following methods can be used anywhere in a media import service or script.

Method Return Type Description
changeImportedItems(import, changedItems) bool Provides the given changed media items belonging to the given MediaImport to Kodi for processing.
The changed media types must be provided as [(int, ListItem)] where int must be a MediaImportChangesetType.
getProviderById(providerId) MediaProvider Returns the MediaProvider with the given identifier.
getImportedItemsByProvider(provider) [ListItem] Returns all imported media items (as [ListItem]) belonging to the given MediaProvider.
getImportedItemsByImport(import) [ListItem] Returns all imported media items (as [ListItem]) belonging to the given MediaImport.

MediaProvider

This class represents a (registered) media provider. Depending on the workflow it can either be created manually or it can be retrieved from one of the static methods in the xbmcmediaimporter module.

Method Return Type Description
MediaProvider(identifier,
  basePath = '',
  friendlyName = '',
  iconUrl = '',
  mediaTypes = [],
  lastSynced = '',
  handle = -1)
MediaProvider Creates a new MediaProvider with the given details.
getIdentifier() str Returns the MediaProvider's identifier.
getBasePath() str Returns the MediaProvider's base path.
setBasePath(basePath) - Sets the MediaProvider's base path.
getFriendlyName() str Returns the MediaProvider's human readable name.
setFriendlyName(friendlyName) - Sets the MediaProvider's human readable name.
getIconUrl() str Returns the MediaProvider's icon URL.
setIconUrl(iconUrl) - Sets the MediaProvider's icon URL.
getAvailableMediaTypes() [str] Returns the MediaProvider's available media types.
setAvailableMediaTypes(mediaTypes) - Sets the MediaProvider's icon URL.
getLastSynced() str Returns when the MediaProvider was last synchronized in W3C date time format in UTC.
prepareSettings() Settings Prepares and returns the Settings of the MediaProvider.
getSettings() Settings Returns the Settings of the MediaProvider.
isActive bool Returns whether the MediaProvider is active or not.
getImports() [MediaImport] Returns all MediaImports belonging to the MediaProvider.

MediaImport

This class represents a (registered) media import. Depending on the workflow it can either be created manually or it can be retrieved from one of the static methods in the xbmcmediaimporter module.

Method Return Type Description
MediaImport(importPath) MediaImport Creates a new MediaImport with the given import path.
getPath() str Returns the MediaImport's path.
getProvider() MediaProvider Returns the MediaProvider the MediaImport belongs to.
setProvider(provider) - Sets the MediaProvider the MediaImport belongs to.
getMediaTypes() [str] Returns the MediaImport's media types.
setMediaTypes(mediaTypes) - Sets the MediaImport's media types.
getLastSynced() str Returns when the MediaImport was last synchronized in W3C date time format in UTC.
prepareSettings() Settings Prepares and returns the Settings of the MediaImport.
getSettings() Settings Returns the Settings of the MediaImport.

Observer

This class is a base class defining different callbacks used by Kodi to communicate with the observer service. The media importer add-on must provide a class deriving from the Observer and implementing the callbacks.

Method Return Type Description
onProviderAdded(provider) - Will be called when a new MediaProvider has been added.
Hint: Wait for the onProviderActivated() before actually starting to observe the MediaProvider.
onProviderUpdated(provider) - Will be called when a MediaProvider has been updated.
onProviderRemoved(provider) - Will be called when a MediaProvider has been removed.
onProviderActivated(provider) - Will be called when a MediaProvider has been activated.
onProviderDeactivated(provider) - Will be called when a MediaProvider has been deactivated.
onImportAdded(provider) - Will be called when a new MediaImport has been added.
onImportUpdated(provider) - Will be called when a MediaImport has been updated.
onImportRemoved(provider) - Will be called when a MediaImport has been removed.

xbmc module

The following methods have been added to existing classes in the xbmc module:

ListItem

Method Return Type Description
setDynamicPath(dynPath) - Sets the dynamic path of the ListItem.
isFolder() bool Returns whether the ListItem is a folder or not.
getDateTime() str Returns the date and time of the ListItem.
setDateTime(dateTime) - Sets the date time of the ListItem.

InfoTagVideo

Method Return Type Description
getFilenameAndPath(dynPath) str Returns the filename and path of the ListItem.
getUniqueID(key) str Returns the unique ID for the given key of the ListITem.
setUniqueIDs(uniqueIDs) - Sets the dict of key - unique ID pairs of the ListItem.

Settings

This class represents the settings of a MediaProvider or MediaImport. It cannot be created manually but must be retrieved from one of the classes mentioned above.

Method Return Type Description
save() bool Saves the Settings.
setLoaded() - Marks the Settings as loaded.
getBool(id) bool Returns the value of the bool setting with the given id.
Throws a WrongTypeException if the value of the setting with the given id is not bool.
getInt(id) int Returns the value of the int setting with the given id.
Throws a WrongTypeException if the value of the setting with the given id is not int.
getNumber(id) float Returns the value of the float setting with the given id.
Throws a WrongTypeException if the value of the setting with the given id is not float.
getString(id) str Returns the value of the str setting with the given id.
Throws a WrongTypeException if the value of the setting with the given id is not str.
getBoolList(id) [bool] Returns the value of the [bool] setting with the given id.
Throws a WrongTypeException if the value of the setting with the given id is not [bool].
getIntList(id) [int] Returns the value of the [int] setting with the given id.
Throws a WrongTypeException if the value of the setting with the given id is not [int].
getNumberList(id) [float] Returns the value of the [float] setting with the given id.
Throws a WrongTypeException if the value of the setting with the given id is not [float].
getStringList(id) [str] Returns the value of the [str] setting with the given id.
Throws a WrongTypeException if the value of the setting with the given id is not [str].
setBool(id, value) - Sets the value of the bool setting with the given id to the given bool value.
Throws a WrongTypeException if the value of the setting with the given id is not bool.
setInt(id, value) - Sets the value of the int setting with the given id to the given int value.
Throws a WrongTypeException if the value of the setting with the given id is not int.
setNumber(id, value) - Sets the value of the float setting with the given id to the given float value.
Throws a WrongTypeException if the value of the setting with the given id is not float.
setString(id, value) - Sets the value of the str setting with the given id to the given str value.
Throws a WrongTypeException if the value of the setting with the given id is not str.
setBoolList(id, values) - Sets the value of the [bool] setting with the given id to the given [bool] value.
Throws a WrongTypeException if the value of the setting with the given id is not [bool].
setIntList(id, values) - Sets the value of the [int] setting with the given id to the given [int] value.
Throws a WrongTypeException if the value of the setting with the given id is not [int].
setNumberList(id, values) - Sets the value of the [float] setting with the given id to the given [float] value.
Throws a WrongTypeException if the value of the setting with the given id is not [float].
setStringList(id, values) - Sets the value of the [str] setting with the given id to the given [str] value.
Throws a WrongTypeException if the value of the setting with the given id is not [str].
registerActionCallback(id, callback) bool Registers the given callback (as str) for the given action setting.
When the user activates the action setting in Kodi's user interface the library script is executed with the given callback as the script's execution endpoint.
registerOptionsFillerCallback(id, callback) bool Registers the given callback (as str) for the given setting as its option filler.
When Kodi needs to display the options for the given setting in its user interface the library script is executed with the given callback as the script's execution endpoint.
This is only valid for int and str settings.
setIntegerOptions(id, options) bool Passes the given optionsas the available options to choose from for the given int setting.
options is of type [(str, int)] where the str is the (translated) label of an option and the int is the value.
Kodi displays these values in the user interface.
setStringOptions(id, opptions) bool Passes the given optionsas the available options to choose from for the given int setting.
options is of type [(str, str)] where the first str is the (translated) label of an option and the second str is the value.
Kodi displays these values in the user interface.

Loading settings

The common use case of loading settings usually consists of the following workflow:

  1. Set dynamic (default) values if necessary using setBool|Int|Number|String()
  2. Register action callbacks if necessary using registerActionCallback()
  3. Register option filler callbacks if necessary using registerOptionsFillerCallback()
  4. Mark the settings as loaded using setLoaded()

xbmcgui module

Player

Method Return Type Description
getPlayingItem() ListItem Returns the currently playing item or throws a PlayerException if nothing is playing.

Info Labels

The following info labels have been added to Kodi core and can be used by skins and for context menu items:

Info Label Type Description
ListItem.IsEnabled bool Whether the ListItem is enabled or not.
A ListItem is enabled if it is a local item or if its MediaProvider and MediaImport are active and ready.
ListItem.IsImported bool Whether the ListItem is imported or not.
ListItem.MediaImporter string If thie ListItem is imported this returns the unique identifier of the media importer used for importing the ListItem.