Kodi Community Forum

Full Version: How to link a file to its parent folder
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello I'm figuring out how to link an episode to its parent folder
For example, in this code, I created two folders named "Serie1" and "Serie2"
My attempt was to create a file named "episode S01E01" inside "Serie1" folder but it failed
I can't see the "episode" file
Looking at Roman's example, I can't grasp the key step
How to link file to its parent folder ?

Code
addon xml

Thank you
So, there's a couple things you're misunderstanding about how a plugin works in the Kodi environment.
First, each time your addon is run it needs to add a set of list items for the current URL that is called for your plugin.
Second, you NEED to make use of the URL property on each list item. You don't just add infinite endless items and nest them in one program.

At the moment you're trying to do the following:

1. Create the folder list items
python:

# Content type
xbmcplugin.setContent(addon_handle, 'movies')
# List_item creation Serie1 (folder)
list_item1 = xbmcgui.ListItem('Serie1')
# List_item creation Serie 2 (folder)
list_item2 = xbmcgui.ListItem('Serie2')
# They are folders
is_folder = True
# Add items to virtual directory
xbmcplugin.addDirectoryItem(addon_handle, url1, list_item1, is_folder)
xbmcplugin.addDirectoryItem(addon_handle, url2, list_item2, is_folder)

2. You end the directory
python:

# Close
xbmcplugin.endOfDirectory(addon_handle)

3. And then you try to make more items (This won't work! You've already ended the directory)
python:

xbmcplugin.setPluginCategory(addon_handle, 'Serie1')
xbmcplugin.setContent(addon_handle, 'movies')
list_item_EP = xbmcgui.ListItem(label="Episode S01E01")

play_url = get_url(action='play', video=url1)
xbmc.log(f"[PLAY] -> {str(play_url)}")
list_item_EP.setProperty('IsPlayable', 'true')
# They are Files !
is_folder = False
# Add items to virtual directory
xbmcplugin.addDirectoryItem(addon_handle, play_url, list_item_EP, is_folder)
# Close
xbmcplugin.endOfDirectory(addon_handle)

So let's start at the beginning, and I think you should do a little more reading and playing around. Do you have much experience programming?
Either way, I think this is a great environment to learn in, there's just a few weird hangups about how it works in the Kodi world that's very unlike other implementations.

I'm going to change the fewest amount of things possible in your code to get what you want, but please keep reading and playing as there's lots you can improve on.
But hey, chastising you and critiquing everything is no fun so I want you to learn and explore how you're doing right now. It's the best way to learn.

Firstly when you click an item in your addon, it calls the URL property. It could point anywhere but you want it pointing to your own addon right?
So right now your two URL variables are blank:

python:

# These lines are why when you click either of your items it kicks you back to the home screen. Kodi doesn't know where to go because you've told it to go to ''
url1 = ''
url2 = ''

Let's change these to point at your addon and to also have information on which folder you've clicked. Let's use the get_url function you've copied from the tutorial:

python:

# You can put anything you want in here, folder, action, mode. Call it whatever you want. This is the URL that's generated and will be used when you click an item
url1 = get_url(folder='Serie1', action='navigate') #I've called it navigate right now because that's what I like to call it, you could call these anything you want
url2 = get_url(folder='Serie2', action='navigate')

So now when you click these folders it will now call your addon and pass through the values 'plugin://plugin_id?folder=Serie1&action=navigate' <--- THAT'S WHAT EVERY PLUGIN URL IN KODI LOOKS LIKE
But that long string isn't a nice set of parameters right? That's why 'parse_qsl' is imported at the top of your python script, for just this purpose!
Adding this line to the top turns that '?folder=Serie1&action=navigate' into a nice set of values:

python:

# sys.argv[2][1:] is just selecting everything in the URL after the question mark (?)
PARAMS = dict(parse_qsl(sys.argv[2][1:]))

So now we have two actions: Navigate and Play. Let's check if we're doing either of those each time the addon is ran:

python:

action = PARAMS.get('action', 'navigation') # Get the action from the PARAMS and default to navigation
if action == "navigate":
    url1 = get_url(folder='Serie1', action='navigate')
    url2 = get_url(folder='Serie2', action='navigate')
    ...

Now we want to check if there's a 'folder' parameter. If not we must be at the top and show Serie1 and Serie2 like you're already doing:

python:

    if folder == None: # If the folder is none we are at the top level
        url1 = get_url(folder='Serie1', action='navigate')
        url2 = get_url(folder='Serie2', action='navigate')
        ...

And then if the folder is equal to 'Serie1' then the user has clicked a one of those two folders and now this run of your addon you will return what's inside Serie1:

python:

     elif folder == "Serie1": #If the folder is Serie1 we will add playable items to the directory
        # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        # Create episode in Serie1 Folder
        xbmcplugin.setPluginCategory(addon_handle, 'Serie1')
        xbmcplugin.setContent(addon_handle, 'movies')
        ....

Finally I've moved the End directory to outside the if/else as you will want to end the directory every time you navigate. Additionally, you we're again using 'url1' which was blank for this item.

python:

        # Remember all this will do is call your addon again with the params of action=play and video=This should be a URL to a video file
        # You will need to create code to handle actually playing the video (Take a look again how it's done in the tutorial)
        play_url = get_url(action='play', video="This should be a URL to a video file")
        #Just because you have play and video in the get_url Kodi's not going to do anything with this, it's just going to call your addon again with '?action=play&video=This should be a URL to a video file'
        xbmc.log(f"[PLAY] -> {str(play_url)}")
        list_item_EP.setProperty('IsPlayable', 'true')
        # They are Files !
        is_folder = False
        # Add items to virtual directory
        xbmcplugin.addDirectoryItem(addon_handle, play_url, list_item_EP, is_folder)
    # When navigating we will end the directory every time
    xbmcplugin.endOfDirectory(addon_handle)

Now putting that all together you end up with: https://hastebin.com/share/oteheyajag.python

As a final note. There's tons I'd improve but you've taken some great first steps. I'm just writing up a starter project in Github that should help a bunch with people getting off the ground with their first plugins. I'll post here once I have the repo up and running and you should take a look as I'm sure it will help with you learning. There's tons of great resources out there for tutorials and you just need to do a bit of reading.

Stick at it and hope you make something you're proud of!

You're welcome to ask me for more help but I can only spare so much time on detailed guidance like this. My advice is keep playing, reading and looking at other people's code (very important!)