Kodi Community Forum

Full Version: Passing data around a multi folder menu?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm currently developing and addon for Kodi 19. The goal of this addon is the following:
-Load a given json database of movies, shorts, tv shows... with the following structure:
[
{"type": "movie", "id": "001", "director": "Director1", "gender": "Gender1", "year": "2018", "title": "Movie1"},
{"type": "short", "id": "002", "director": "Director2", "gender": "Gender1", "year": "1961", "title": "Short1"},
{"type": "movie", "id": "003", "director": "Director2", "gender": "Gender2", "year": "2019", "title": "Movie2"},
...
]
-Show a top menu with the categories (Movies, Shorts, TV Shows...).
-When selecting each category filter the json grouping by categories and show a new menu listing all the possible filters (By gender, by director, by year).
-When selecting a filter, filter again the filtered json of previous step, this time grouping by the chosen filter and show another menu listing all the possible values found on the json for that filter (with the sample json, if filtering by movies and by year this menu would contain 2018 and 2019).
-Finally, when selecting (in this case) the year, show a final menu with all titles matching that year.

My problem is the following:
I'm using the xbmcplugin.addDirectoryItem function to create the menus. However this method ends the plugin everytime it creates a menu, and reruns it when choosing a new option, using the URL parameters to redirect to the correct menu, so the only data which is persistent is the parameters of the URL, and with a json with more than 500 movies, 200 TV shows and 50 shorts it cannot be passed as an URL parameter.

I'll try to show this with an example.

Ideal situation:
-Plugin runs.
-Loads the json file.
-Shows the menu (Movies, TV Shows, Shorts).
-I choose the Movies option.
-The plugin filters the huge json, returns only the group of movies and prints the filters menu (Director, Gender, Year).
-I choose the Year option.
-The plugin filters the group of movies by year, returns a group for each year found and prints the menu with each year.
-I select 2018.
-The plugin filters the group of movies for the year 2018 and prints their titles.

The problem is that the execution ends with each option I select, so I'm forced to do the following instead:

Current situation:
-Plugin runs.
-Shows the menu (Movies, TV Shows, Shorts).
-I choose the Movies option.
-The plugin loads the json file, filters the huge json, returns only the group of movies and prints the filters menu (Director, Gender, Year).
-I choose the Year option.
-The plugin loads the json file, filters the huge json, returns only the group of movies, filters the group of movies by year, returns a group for each year found and prints the menu with each year.
-I select 2018.
-The plugin loads the json file, filters the huge json, returns only the group of movies, filters the group of movies by year, returns a group for each year found, filters the group of movies for the year 2018 and prints their titles.


My question is, how can I make the data generated by the intermediate steps persistent, in order to avoid all this calls to the same functions?
Hello,

I have exactly the same question...

Does anyone have an answer ?  It seems to me a common "problem"...


Sincerely yours,
Detobel36
xbmcgui.Window objects can store arbitrary string data in their properties so it can be used as in-memory storage available to all Python processes inside Kodi. Examples:
https://github.com/romanvm/kodi.external...storage.py
https://github.com/romanvm/kodi.tvmaze/b...ice.py#L31
Hello,

Thank you for this reply.

So, if I understand correctly, Kodi can't keep existing objects in memory. We must store them (convert them in JSON) before displaying the menu to the user. And load JSON from memory when the user accesses a URL.


Thanks again for your reply,
Have a good day,
Detobel36
Yes. The first example is a class with dicionary-like interface that does everyting "under the hood". You can do this:
python:
# First process
storage = MemStorage()
storage['some_unique_key'] = {'foo': 'bar'}
...
# Second process
storage = MemStorage()
foo = storage['some_unque_key']

The only thing that window properties are also used by other Kodi components, e.g. skins, so you need to pick some key(s) that won't collide with other properties, e.g. your addon ID.

You can also use pickle instead of JSON but then you need to use base64 encode for pickled data because in Python 3 pickles are binary data and windows properties accept Unicode strings in Python 3 Kodi API.