Advertisement
Guest User

bdplaylister.py

a guest
Feb 18th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. import os
  2. import sys
  3. import time
  4. from xbmcjson import XBMC, PLAYER_VIDEO
  5.  
  6. REQUEST_URL = "http://localhost:8080/jsonrpc"
  7. USER = "kodiuser"
  8. PASSWORD = "kodipassword"
  9.  
  10. # Return list of playlist numbers (in string) that have srt file next to them.
  11. def find_srt(bd_path):
  12.     srt_list = []
  13.     if os.path.exists(bd_path) and os.path.isdir(bd_path):
  14.         playlist_dir = os.path.join(bd_path, "PLAYLIST")
  15.         files = os.listdir(playlist_dir)
  16.         for file in files:
  17.             if file.lower().endswith(".srt"):
  18.                 playlist = file.lower().split(".")[0]
  19.                 if len(playlist) == 5:
  20.                     try:
  21.                         int(playlist)
  22.                         srt_list.append(playlist)
  23.                     except ValueError:
  24.                         print "SRT filename ", playlist, " did not contain a valid playlist number."
  25.                 else:
  26.                     print "SRT filename ", playlist, " had invalid length."
  27.     return srt_list
  28.  
  29. # Check playlist.txt file for preferred playlist.
  30. def check_playlist_txt(bd_path):
  31.     playlist = ""
  32.     pl_path = bd_path + "playlist.txt"
  33.     if os.path.exists(pl_path) and os.path.isfile(pl_path):
  34.         content = ""
  35.         with open(pl_path, "r") as fp:
  36.             content = fp.read()
  37.             content = content.strip()
  38.         if len(content) == 5:
  39.             try:
  40.                 int(content)
  41.                 playlist = content
  42.             except ValueError:
  43.                 print "playlist.txt content '", content, "' was not a number"
  44.         else:
  45.           print "playlist.txt content '", content, "' has invalid length."
  46.     return playlist
  47.  
  48. def stop_playback(xbmc, player_id):
  49.     #time.sleep(3)
  50.     time.sleep(0.1)
  51.     stop_result = xbmc.Player.Stop({"playerid": player_id})
  52.     print "Called stop playback with result:", stop_result
  53.     time.sleep(0.1)
  54.     #time.sleep(3)
  55.  
  56. def play_bd(bdmv_index_path):
  57.     play_target = ""
  58.     xbmc_play_path = ""
  59.     player_id = None
  60.  
  61.     if USER and PASSWORD:
  62.         xbmc = XBMC(REQUEST_URL, USER, PASSWORD)
  63.     else:
  64.         xbmc = XBMC(REQUEST_URL)
  65.     result = xbmc.Player.GetActivePlayers().get("result")
  66.     #print "Active Players: ", result
  67.     if result and len(result) > 0:
  68.         player_id = result[0].get("playerid")
  69.         if player_id != None:
  70.             result = xbmc.Player.GetItem({"properties": ["file"], "playerid": player_id}).get("result")
  71.             #print "Got item result: ", result
  72.             if result and len(result) > 0:
  73.                 item = result.get("item")
  74.                 if item:
  75.                     file = item.get("file")
  76.                     if file:
  77.                         print "Found file: ", file
  78.                         xbmc_play_path = file
  79.     if not xbmc_play_path:
  80.         print "Did not get currently playing file path from JSON-RPC."
  81.  
  82.     if bdmv_index_path.endswith("index.bdmv") and xbmc_play_path.endswith("index.bdmv"):
  83.         bdmv_root_path = bdmv_index_path[:-10]
  84.         xbmc_play_root_path = xbmc_play_path[:-10]
  85.         print "Found Blu-ray in path", bdmv_root_path
  86.  
  87.         srt_list = find_srt(bdmv_root_path)
  88.         playlist_txt = check_playlist_txt(bdmv_root_path)
  89.  
  90.         # Playlist path change assumes that the internal path from Kodi uses / as directory separator. Works at least for smb:// sources.
  91.         if playlist_txt:
  92.             print "Found playlist", playlist_txt, "from playlist.txt"
  93.             play_target = xbmc_play_root_path + "PLAYLIST/" + playlist_txt + ".mpls"
  94.         elif len(srt_list) > 0:
  95.             print "Found SRTs", srt_list
  96.             play_target = xbmc_play_root_path + "PLAYLIST/" + srt_list[0] + ".mpls"
  97.         else:
  98.             print "Did not find overrides, proceeding with index.bdmv"
  99.             play_target = ""
  100.  
  101.     pid = player_id if player_id != None else PLAYER_VIDEO
  102.    
  103.     if play_target:
  104.         print "Found play target: ", play_target
  105.         stop_playback(xbmc, pid)
  106.         result = xbmc.Player.Open({"item": {"file": play_target}})
  107.         print "Commanded playback with result: ", result
  108.     elif xbmc_play_path:
  109.         print "Using bdmv ", xbmc_play_path
  110.         stop_playback(xbmc, pid)
  111.         result = xbmc.Player.GetPlayers({"media": "video"}).get("result")
  112.         print "Found players: ", result
  113.         if result and len(result) > 0:
  114.             dsplayer_id = dvdplayer_id = None
  115.             for player in result:
  116.                 print "Player: ", player
  117.                 if player.get("name") == "DSPlayer":
  118.                     dsplayer_id = player.get("playercoreid")
  119.                 elif player.get("name") == "DVDPlayer":
  120.                     dvdplayer_id = player.get("playercoreid")
  121.             if dsplayer_id is not None or dvdplayer_id is not None:
  122.                 print "Got internal player id, continuing"
  123.                 pid = dsplayer_id if dsplayer_id is not None else dvdplayer_id
  124.                 result = xbmc.Player.Open({"item": {"file": xbmc_play_path}, "options": {"playercoreid": pid}})
  125.                 print "Open returned result", result
  126.             else:
  127.                 print "Didn't find an internal player from Kodi JSON-RPC."
  128.     else:
  129.         print "Failed to find Kodi internal Blu-ray path for playback."
  130.  
  131. if __name__ == "__main__":
  132.     print "BDPlaylister called with args: ", sys.argv
  133.     play_bd(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement