gordie
gordie

Reputation: 1955

Kodi addons : how to correctly set an URL using xbmcplugin.addDirectoryItems and xbmcgui.ListItem?

I'm trying to update a plugin for Kodi 19 (and Python3). But! Hell! Their documentation is a mess, and when you search the internet, a lot of code is outdated. I cannot understand how correctly create a virtual folder with items using xbmcplugin.addDirectoryItems.

here's my (simplified) code:

this is my KODI menu function

def menu_live():

  #this is were I get my datas (from internet)
  datas = api.get_live_videos()
  
  listing = datas_to_list(datas)

  sortable_by = (xbmcplugin.SORT_METHOD_DATE,
                 xbmcplugin.SORT_METHOD_DURATION)

  xbmcplugin.addDirectoryItems(common.plugin.handle, listing, len(listing))
  xbmcplugin.addSortMethod(common.plugin.handle, xbmcplugin.SORT_METHOD_LABEL)
  xbmcplugin.endOfDirectory(common.plugin.handle)

this builds a list of items for the virtual folder

def datas_to_list(datas):
    list_items = []

    if datas and len(datas):
        for data in datas:
            li = data_to_listitem(data)
            url = li.getPath()
            list_items.append((url, li, True))

    return list_items

this create a xbmcgui.ListItem for our listing

def data_to_listitem(data):

    #here I parse my data to build a xbmcgui.ListItem
    label = ...
    url = ...
    ...

    
    list_item = xbmcgui.ListItem(label)
    list_item.setPath(url)

    return list_item

I don't understand well how to interact with the media url. It seems that it can be defined within xbmcgui.ListItem using

list_item.setPath(url)

which seems ok to me (an url is set to the item itself)

but then, it seems that you also need to set the URL when adding the item to the list,

li = data_to_listitem(data)
list_items.append((url, li, True))

This looks weird since it means you have to know the URL outside the function that builds the item. So currently, my workaround is

li = data_to_listitem(data)
url = li.getPath() #I retrieve the URL defined in the above function
list_items.append((url, li, True))

That code works. But the question is: if I can define an URL on the ListItem using setPath(), then why should I also fill that URL when appending the ListItem to my listing list_items.append((url, li, True)) ?

Thanks a lot !

Upvotes: 0

Views: 1927

Answers (1)

Ikosse
Ikosse

Reputation: 151

I'm not exactly sure what your question is. But Video/audio add-on development is thoroughly explained in these guides: https://kodi.wiki/view/HOW-TO:Audio_addon, https://kodi.wiki/view/Audio-video_add-on_tutorial and https://kodi.wiki/view/HOW-TO:Video_addon. Have a look at them, especially the video-add-on guide (as pointed out by Roman), and try to adapt to your case.

Edit

But the question is: if I can define an URL on the ListItem using setPath(), then why should I also fill that URL when appending the ListItem to my listing?

I'm far from an expert, but from my understanding and in the context of https://kodi.wiki/view/HOW-TO:Video_addon tutorial, the url in

  list_items.append((url, li, is_folder))

is used to route your plugin to your playback function, as well as passing arguments to it (e.g. video url and possibly other useful stuff needed for playback). That is, the list item passed here doesn't need to have its path set.

  ListItem.setPath(video_url)

on the other hand, is for resolving the video url and start the playback after you have selected an item.

Upvotes: 1

Related Questions