Files
jellycon/service.py

117 lines
3.6 KiB
Python
Raw Normal View History

# coding=utf-8
2017-03-17 15:41:46 +11:00
# Gnu General Public License - see LICENSE.TXT
2014-09-28 10:30:07 +10:00
import time
import json
import traceback
import binascii
2018-02-12 18:41:05 +11:00
from threading import Timer
2017-03-10 10:45:38 +11:00
import xbmc
import xbmcaddon
import xbmcgui
2017-03-17 15:41:46 +11:00
from resources.lib.downloadutils import DownloadUtils
from resources.lib.simple_logging import SimpleLogging
from resources.lib.play_utils import Service, PlaybackService, sendProgress
from resources.lib.kodi_utils import HomeWindow
2017-09-29 09:24:01 +10:00
from resources.lib.widgets import checkForNewContent
2017-12-14 13:43:23 +11:00
from resources.lib.websocket_client import WebSocketClient
2014-09-28 10:30:07 +10:00
# clear user and token when logging in
home_window = HomeWindow()
2017-05-25 18:16:01 +10:00
home_window.clearProperty("userid")
home_window.clearProperty("AccessToken")
home_window.clearProperty("Params")
log = SimpleLogging('service')
download_utils = DownloadUtils()
2014-09-28 10:30:07 +10:00
# auth the service
try:
download_utils.authenticate()
except Exception as error:
log.error("Error with initial service auth: {0}", error)
2014-09-28 10:30:07 +10:00
# set up all the services
2014-09-28 10:30:07 +10:00
monitor = Service()
playback_service = PlaybackService(monitor)
home_window = HomeWindow()
2017-09-29 09:42:59 +10:00
last_progress_update = time.time()
2017-09-29 09:24:01 +10:00
last_content_check = time.time()
2017-12-14 13:43:23 +11:00
websocket_client = WebSocketClient()
2018-01-04 15:32:11 +11:00
# start the WebSocket Client running
2017-12-14 13:43:23 +11:00
settings = xbmcaddon.Addon(id='plugin.video.embycon')
remote_control = settings.getSetting('remoteControl') == "true"
if remote_control:
websocket_client.start()
def get_now_playing():
2018-01-03 11:53:24 +11:00
# Get the active player
result = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "id": 1, "method": "Player.GetActivePlayers"}')
result = unicode(result, 'utf-8', errors='ignore')
log.debug("Got active player: {0}", result)
result = json.loads(result)
if 'result' in result and len(result["result"]) > 0:
playerid = result["result"][0]["playerid"]
# Get details of the playing media
log.debug("Getting details of now playing media")
result = xbmc.executeJSONRPC(
'{"jsonrpc": "2.0", "id": 1, "method": "Player.GetItem", "params": {"playerid": ' + str(
playerid) + ', "properties": ["showtitle", "tvshowid", "episode", "season", "playcount", "genre", "plotoutline", "uniqueid"] } }')
2018-01-03 11:53:24 +11:00
result = unicode(result, 'utf-8', errors='ignore')
log.debug("playing_item_details: {0}", result)
result = json.loads(result)
return result
2018-01-03 11:53:24 +11:00
2018-02-12 18:41:05 +11:00
def check_version():
download_utils.checkVersion()
t = Timer(5.0, check_version)
t.start()
# monitor.abortRequested() is causes issues, it currently triggers for all addon cancelations which causes
# the service to exit when a user cancels an addon load action. This is a bug in Kodi.
# I am switching back to xbmc.abortRequested approach until kodi is fixed or I find a work arround
while not xbmc.abortRequested:
try:
if xbmc.Player().isPlaying():
2017-09-29 09:24:01 +10:00
# if playing every 10 seconds updated the server with progress
2017-07-08 10:34:30 +10:00
if (time.time() - last_progress_update) > 10:
last_progress_update = time.time()
sendProgress(monitor)
else:
2017-09-29 09:24:01 +10:00
# if not playing every 60 seonds check for new widget content
if (time.time() - last_content_check) > 60:
last_content_check = time.time()
checkForNewContent()
#get_now_playing()
2018-01-03 11:53:24 +11:00
except Exception as error:
log.error("Exception in Playback Monitor: {0}", error)
log.error("{0}", traceback.format_exc())
xbmc.sleep(1000)
2017-12-14 13:43:23 +11:00
# stop the WebSocket Client
websocket_client.stop_client()
# clear user and token when loggin off
2017-05-25 18:16:01 +10:00
home_window.clearProperty("userid")
home_window.clearProperty("AccessToken")
home_window.clearProperty("Params")
2014-09-28 10:30:07 +10:00
2018-01-05 08:49:06 +11:00
log.debug("Service shutting down")