2023-06-27, 03:45
Update: please check the comments in the thread for tips & fixes to the recipe being shared in this first post.
Using a personal add-on, for some series that have lots of episodes, the items list on screen becomes very long and tiring to navigate with the TV box remote.
This is especially so after Kodi starts up, I navigate to that list and need a lot of clicks to reach the specific next episode that I should watch.
I don't think there's a built-in Kodi feature for what I'm looking for ("auto-select the next episode, without playing"), so I wondered if there wasn't a way to use Python to make vanilla Kodi do that.
The below works (tested with Leia 18.9), and note that it's written for clarity rather than speed, you should rewrite it in your own way:
Notes:
- This could also be done with you implementing your own windowing with xbmcgui and WindowXML. This way is much more sophisticated since you know exactly when the window is navigated to, what list control has the items etc. but it takes way more coding and time to use.
- I tried using the xbmcgui functions to query the ListItem data, I even managed to access the list control from the built-in Kodi skin, but for some reason it came in as having no items at all.
Using a personal add-on, for some series that have lots of episodes, the items list on screen becomes very long and tiring to navigate with the TV box remote.
This is especially so after Kodi starts up, I navigate to that list and need a lot of clicks to reach the specific next episode that I should watch.
I don't think there's a built-in Kodi feature for what I'm looking for ("auto-select the next episode, without playing"), so I wondered if there wasn't a way to use Python to make vanilla Kodi do that.
The below works (tested with Leia 18.9), and note that it's written for clarity rather than speed, you should rewrite it in your own way:
Python:def myDirectoryFunction(...)
# Create and send your ListItem's to Kodi to finish the directory.
# ...
xbmcplugin.addDirectoryItems(PLUGIN_ID, allMyItems)
xbmcplugin.endOfDirectory(PLUGIN_ID)
# RIGHT AFTER finishing the directory, if your "auto-select" custom setting is on, queue
# the execution of some custom python code using the RunPlugin command.
# This command does work with Python add-ons.
#
# Note the 'True' for the "block" parameter, to avoid the user interacting with the
# UI in the meantime.
if autoSelectNextEpisode:
xbmc.executebuiltin('RunPlugin(plugin://plugin.video.myplugin/?action=actionAutoSelect)', True)
# The parameter / route from that RunPlugin command should lead to your auto-select
# function, something like the below.
def actionAutoSelect(params):
# By this point the directory listing is available, so we can query things with xbmc.getInfoLabel().
totalItems = int(xbmc.getInfoLabel('Container.NumItems'))
if not totalItems:
return
# Get the list control ID (the numbers below are from the Estuary skin XML files, in
# the Kodi source files).
viewMode = xbmc.getInfoLabel('Container.ViewMode')
if viewMode == 'WideList':
containerID = 55
elif viewMode == 'InfoWall':
containerID = 54
elif viewMode == 'Wall':
containerID = 500
else:
# Unsupported view mode.
return
# Go through all listed items, using their PlayCount & PercentPlayed properties as criteria
# to decide which should be auto-selected.
# You can avoid having to loop in this way if you keep track of what episodes your add-on
# plays. Then you don't need this loop since you already know the index of the ListItem.
for itemIndex in range(totalItems):
template = 'Container({id}).ListItemAbsolute({index}).'.format(id=containerID, index=itemIndex))
# PlayCount infolabel:
# A string that's either empty or a stringified number, the times the item
# has been completely played ("", "1", "2" etc). This leaves that "watched"
# checkmark on items that aren't partially played.
playcount = xbmc.getInfoLabel(template + 'PlayCount')
if playcount:
# This ListItem has been completely played.
pass
# PercentPlayed infolabel:
# A stringified number from 0 to 100, (eg "37"), which is how far the video has been played.
# If the number is non-zero then the video can be resumed (that half-filled circle icon).
percentPlayed = xbmc.getInfoLabel(template + 'PercentPlayed')
if percentPlayed != '0':
# This ListItem has been partially played.
pass
# Select the chosen item, whatever your criteria were.
try:
xbmc.executebuiltin('SetFocus({id},{selectedIndex},absolute)'.format(
id=containerID, selectedIndex=...))
except:
pass
Notes:
- This could also be done with you implementing your own windowing with xbmcgui and WindowXML. This way is much more sophisticated since you know exactly when the window is navigated to, what list control has the items etc. but it takes way more coding and time to use.
- I tried using the xbmcgui functions to query the ListItem data, I even managed to access the list control from the built-in Kodi skin, but for some reason it came in as having no items at all.
Note that the ID 55 used above is for the main list control on Estuary skin, WideList view mode (source reference).Python:window = xbmcgui.Window(xbmcgui.getCurrentWindowId())
list = window.getControl(55)
size = list.size()
items = [list.getListItem(index) for index in range(size)]
# 'size' is 0, 'items' is empty.