2023-01-14, 15:06
I was trying to make a Kodi 19.1 Add-on which plays random videos within a genre and display a Genre Icon in the corner of the screen, disable player controls and use left/right/up/down to switch genres, finally return to main menu when the back/b button is pressed.
It was supposed to choose shows tagged with "morning" "midday" and "night" depending on the time of day.
I managed to find documentation to play a video but I couldn't understand how to define the paths without giving an explicit path like "E:/Kodi/Video/ExampleShow/ExampleSeason/S01E01.mp4"
One example which didn't work was "videodb://tvshows/genres/comedy/exampleshow/exampleseason/s01e01.mp4"
I ended up trying to run a playRandomVideo script using smart playlists but this caused an error message "can not play from non-permanent video source" and I couldn't add the playlist location as a source because it's in the hidden folder AppData ("special://profile/playlists/video/test.xsp"). Sometimes my addon would start the playlist and other times nothing would happen other than the error message.. I had no luck displaying an image using xbmcgui.ControlImage(10, 10, 200, 200, genre_icon_path).
I asked OpenAI to write some example code and spent 14 hours trying to fix it, discovering that most of the functionality was non-existent after fixing indentation and attempting some of the syntax errors..
This is the output from OpenAI
and this is my add-on code which sometimes plays the playlist, never shows any picture and randomly gives an error message
Please help me someone, I will pay for your time
It was supposed to choose shows tagged with "morning" "midday" and "night" depending on the time of day.
I managed to find documentation to play a video but I couldn't understand how to define the paths without giving an explicit path like "E:/Kodi/Video/ExampleShow/ExampleSeason/S01E01.mp4"
One example which didn't work was "videodb://tvshows/genres/comedy/exampleshow/exampleseason/s01e01.mp4"
I ended up trying to run a playRandomVideo script using smart playlists but this caused an error message "can not play from non-permanent video source" and I couldn't add the playlist location as a source because it's in the hidden folder AppData ("special://profile/playlists/video/test.xsp"). Sometimes my addon would start the playlist and other times nothing would happen other than the error message.. I had no luck displaying an image using xbmcgui.ControlImage(10, 10, 200, 200, genre_icon_path).
I asked OpenAI to write some example code and spent 14 hours trying to fix it, discovering that most of the functionality was non-existent after fixing indentation and attempting some of the syntax errors..
This is the output from OpenAI
Code:
import xbmc
import xbmcgui
import random
import datetime
# Connect to the Kodi JSON-RPC API
kodi = xbmc.Kodi()
# Get the list of TV shows from the video library
shows = kodi.VideoLibrary.GetTVShows()
# Create a function to play random episodes
def play_random_episode(show_list, genre_filter):
filtered_shows = []
# Filter shows by genre
for show in show_list:
for genre in show["genre"]:
if genre["label"] == genre_filter:
filtered_shows.append(show)
break
# Get a random show from the filtered list
random_show = random.choice(filtered_shows)
# Get the seasons of the show
seasons = kodi.VideoLibrary.GetSeasons(tvshowid=random_show["tvshowid"])
# Get a random season from the list
random_season = random.choice(seasons["result"]["seasons"])
# Get the episodes of the season
episodes = kodi.VideoLibrary.GetEpisodes(tvshowid=random_show["tvshowid"], season=random_season["season"])
# Get a random number of episodes between 1 and 3
random_number = random.randint(1,3)
# Get a random episode from the list
random_episodes = random.sample(episodes["result"]["episodes"], random_number)
# Play the episodes
for episode in random_episodes:
kodi.Player.Open(item={"episodeid": episode["episodeid"]})
if genre_filter != current_genre:
# start playing the video from a random point
kodi.Player.Seek(value=random.randint(0, episode["runtime"]))
current_genre = genre_filter
else:
# Show the genre icon in the corner of the screen
genre_icon_path = "special://userdata/addon_data/plugin.video.kodi/Genre Icons/" + genre_filter + ".png"
genre_icon = xbmcgui.ControlImage(10, 10, 200, 200, genre_icon_path)
xbmc.getCurrentWindowDialogId()
genre_icon.setVisible(True)
# Wait for the episode to end
while not xbmc.abortRequested:
xbmc.sleep(100)
if xbmc.Player().isPlaying() == False:
genre_icon.setVisible(False)
break
# Get current time
now = datetime.datetime.now()
# loop to play random episodes depending on the time of day
while not xbmc.abortRequested:
if now.hour >= 6 and now.hour < 12:
# Morning, play comedy
play_random_episode(shows["result"]["tvshows"], "Comedy")
elif now.hour >= 12 and now.hour < 18:
# Afternoon, play drama
play_random_episode(shows["result"]["tvshows"], "Drama")
elif now.hour >= 18 and now.hour < 22:
# Evening, play Action
play_random_episode(shows["result"]["tvshows"], "Action")
else:
# Night, play horror
play_random_episode(shows["result"]["tvshows"], "Horror")
now = datetime.datetime.now()
# Disable player controls
kodi.Player.SetPlayerControls(enable=False)
# Wait for the back button to be pressed
while not xbmc.abortRequested:
xbmc.sleep(100)
if xbmc.getCondVisibility("Input.IsButtonPressed(9)"):
# Stop playback
kodi.Player.Stop()
break
Code:
import xbmc
import xbmcgui
import os
player = xbmc.Player()
genre_filter = "Comedy"
# Show the genre icon in the corner of the screen
genre_icon_path = "special://home/Genre Icons/" + genre_filter + ".jpg"
genre_icon = xbmcgui.ControlImage(10, 10, 200, 200, genre_icon_path)
genre_icon.setVisible(True)
runscript = 'RunScript(script.playrandomvideos, "special://profile/playlists/video/testcomedy.xsp")'
xbmc.executebuiltin(runscript)
# Wait for the videos to end
while not xbmc.Monitor().abortRequested:
xbmc.sleep(100)
if player.isPlaying() == False:
genre_icon.setVisible(False)
break