Sunday, October 30, 2016

Kodi addon, run script on playback action

Addon version of a kodi-script I never published here ... so yeah.

Run scripts when you start playing a video in kodi. In my case I turn off some HUE-lights on play and turn them on, in red, on pause.

2021-03-14: Made this a bit less shit, maybe. Added some debug-logging. Also in a sort of "auto-like" for youtube-videos, very hacky.

addon.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.playback.actions" 
  name="Playback Actions Service" 
  version="0.0.1" 
  provider-name="me/you/whoever">
 <requires>
  <import addon="xbmc.python" version="2.14.0"/>
 </requires>
 <!-- <extension point="xbmc.python.script" library="addon.py">
  <provides>executable</provides>
 </extension> -->
 <extension point="xbmc.service" library="service.py" start="login" />
 <extension point="xbmc.addon.metadata">
  <platform>all</platform>
  <summary lang="en">Does stuff on playback actions</summary>
  <description lang="en">Fancy that eh?</description>
  <license>GNU General Public License, v2</license>
  <language></language>
 </extension>
</addon>

service.py
import xbmc, xbmcgui, os

class fluffPlayer(xbmc.Player):
  def __init__ (self):
    xbmc.Player.__init__(self)
  #

  def onPlayBackStarted(self):
    xbmc.sleep(2000)

    global videoTotalTime
    videoTotalTime = self.getTotalTime()

    xbmc.log(f"[{logName}] EVENT : Playback started")
    xbmc.log(f"[{logName}] VALUES: videoTotalTime: {videoTotalTime}")

    if self.isPlayingVideo():
      if videoTotalTime > 600:
        xbmc.sleep(4000)
        state = "playing"
        xbmc.log(f"[{logName}] STATE : {state}")
        os.system(f"echo kodi_state {state} >{udp}")
  #

  def onPlayBackPaused(self):
    xbmc.sleep(500)
    xbmc.log(f"[{logName}] EVENT : Playback paused")
    xbmc.log(f"[{logName}] VALUES: videoTotalTime: {videoTotalTime}")

    if self.isPlayingVideo():
      if videoTotalTime > 600:
        state = "paused"
        xbmc.log(f"[{logName}] STATE : {state}")
        os.system(f"echo kodi_state {state} >{udp}")
  #

  def onPlayBackResumed(self):
    xbmc.sleep(500)
    xbmc.log(f"[{logName}] EVENT : Playback resumed")
    xbmc.log(f"[{logName}] VALUES: videoTotalTime: {videoTotalTime}")

    if self.isPlayingVideo():
      if videoTotalTime > 600:
        state = "playing"
        xbmc.log(f"[{logName}] STATE : {state}")
        os.system(f"echo kodi_state {state} >{udp}")
  #

  def onPlayBackStopped(self):
    xbmc.sleep(500)
    xbmc.log(f"[{logName}] EVENT : Playback stopped")
    xbmc.log(f"[{logName}] VALUES: videoTotalTime: {videoTotalTime}")

    if wasVideo == 1:
      if not self.isPlayingVideo():
        if videoTotalTime > 600:
          state = "stopped"
          xbmc.log(f"[{logName}] STATE : {state}")
          os.system(f"echo kodi_state {state} >{udp}")
      if wasYoutube == 1:
        for x in range(50):
          xbmc.sleep(100)
          if xbmc.getCondVisibility("Window.IsVisible(DialogSelect.xml)"):
            xbmc.sleep(200)
            os.system("xdotool key Return")
            break
  #

  def onPlayBackEnded(self):
    xbmc.sleep(500)
    xbmc.log(f"[{logName}] EVENT : Playback ended")
    xbmc.log(f"[{logName}] VALUES: videoTotalTime: {videoTotalTime}")

    if wasVideo == 1:
      if not self.isPlayingVideo():
        if videoTotalTime > 600:
          state = "stopped"
          xbmc.log(f"[{logName}] STATE : {state}")
          os.system(f"echo kodi_state {state} >{udp}")
      if wasYoutube == 1:
        for x in range(50):
          xbmc.sleep(100)
          if xbmc.getCondVisibility("Window.IsVisible(DialogSelect.xml)"):
            xbmc.sleep(200)
            os.system("xdotool key Return")
            break
  #
#

if __name__ == '__main__':
  monitor = xbmc.Monitor()
  fluff = fluffPlayer()

  logName = "script.playback.actions"
  udp = "/dev/udp/hostname/2222"

  wasVideo = 0
  wasYoutube = 0
  videoTotalTime = 0

  while not monitor.abortRequested():
    if monitor.waitForAbort(3):
      break
    
    if fluff.isPlaying():
      if fluff.isPlayingVideo():
        wasVideo = 1
        uri = fluff.getPlayingFile()

        if "youtube" in uri:
            wasYoutube = 1
        else:
            wasYoutube = 0

        xbmc.log(f"[{logName}] PLAYING URI: {uri} (isyoutube: {wasYoutube})")
      else:
        wasVideo = 0
        wasYoutube = 0
    
    xbmc.sleep(500)
  #
#