Files
jellycon/service.py

121 lines
3.9 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
2018-02-25 11:23:23 +11:00
from resources.lib.widgets import checkForNewContent, set_background_image
2017-12-14 13:43:23 +11:00
from resources.lib.websocket_client import WebSocketClient
from resources.lib.menu_functions import set_library_window_values
from resources.lib.context_monitor import ContextMonitor
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()
2018-04-18 23:03:52 +10:00
download_utils.getUserId()
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()
last_background_update = 0
2017-12-14 13:43:23 +11:00
websocket_client = WebSocketClient()
# session id
# TODO: this is used to append to the end of PLAY urls, this is to stop mark watched from overriding the Emby ones
home_window.setProperty("session_id", str(time.time()))
2018-01-04 15:32:11 +11:00
# start the WebSocket Client running
settings = xbmcaddon.Addon()
2017-12-14 13:43:23 +11:00
remote_control = settings.getSetting('remoteControl') == "true"
if remote_control:
websocket_client.start()
# Start the context menu monitor
context_monitor = None
context_menu = settings.getSetting('override_contextmenu') == "true"
if context_menu:
context_monitor = ContextMonitor()
context_monitor.start()
background_interval = int(settings.getSetting('background_interval'))
newcontent_interval = int(settings.getSetting('newcontent_interval'))
# 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
2018-04-18 23:03:52 +10:00
prev_user_id = home_window.getProperty("userid")
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:
2018-10-26 09:27:28 +11:00
if newcontent_interval != 0 and (time.time() - last_content_check) > newcontent_interval:
2017-09-29 09:24:01 +10:00
last_content_check = time.time()
checkForNewContent()
2018-10-26 09:27:28 +11:00
if background_interval != 0 and (time.time() - last_background_update) > background_interval:
2018-02-25 11:23:23 +11:00
last_background_update = time.time()
set_library_window_values()
2018-02-25 11:23:23 +11:00
set_background_image()
2018-01-03 11:53:24 +11:00
2018-04-18 23:03:52 +10:00
if remote_control and prev_user_id != home_window.getProperty("userid"):
prev_user_id = home_window.getProperty("userid")
websocket_client.stop_client()
websocket_client = WebSocketClient()
websocket_client.start()
except Exception as error:
log.error("Exception in Playback Monitor: {0}", error)
log.error("{0}", traceback.format_exc())
xbmc.sleep(1000)
# call stop on the context menu monitor
if context_monitor:
context_monitor.stop_monitor()
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")
2018-11-10 10:41:45 +11:00
home_window.clearProperty("userimage")
2014-09-28 10:30:07 +10:00
2018-01-05 08:49:06 +11:00
log.debug("Service shutting down")