2017-04-10 09:19:08 +10:00
|
|
|
# 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
|
2017-06-10 13:01:04 +10:00
|
|
|
import json
|
2017-08-13 13:14:51 +10:00
|
|
|
import traceback
|
2018-01-03 19:01:01 +11:00
|
|
|
import binascii
|
2018-02-12 18:41:05 +11:00
|
|
|
from threading import Timer
|
2017-03-10 10:45:38 +11:00
|
|
|
|
2018-01-04 08:25:39 +11:00
|
|
|
import xbmc
|
|
|
|
|
import xbmcaddon
|
|
|
|
|
import xbmcgui
|
|
|
|
|
|
2019-01-16 19:11:51 +11:00
|
|
|
from resources.lib.downloadutils import DownloadUtils, save_user_details
|
2017-03-17 15:41:46 +11:00
|
|
|
from resources.lib.simple_logging import SimpleLogging
|
2018-01-04 08:25:39 +11:00
|
|
|
from resources.lib.play_utils import Service, PlaybackService, sendProgress
|
2017-05-26 03:48:48 -04:00
|
|
|
from resources.lib.kodi_utils import HomeWindow
|
2018-12-19 13:24:41 +11:00
|
|
|
from resources.lib.widgets import checkForNewContent, set_background_image, set_random_movies
|
2017-12-14 13:43:23 +11:00
|
|
|
from resources.lib.websocket_client import WebSocketClient
|
2018-02-24 11:23:02 +11:00
|
|
|
from resources.lib.menu_functions import set_library_window_values
|
2018-07-14 13:37:38 +10:00
|
|
|
from resources.lib.context_monitor import ContextMonitor
|
2019-01-16 19:11:51 +11:00
|
|
|
from resources.lib.server_detect import checkServer
|
|
|
|
|
|
|
|
|
|
settings = xbmcaddon.Addon()
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-04-29 10:37:20 +10:00
|
|
|
# clear user and token when logging in
|
2017-05-26 03:48:48 -04:00
|
|
|
home_window = HomeWindow()
|
2017-05-25 18:16:01 +10:00
|
|
|
home_window.clearProperty("userid")
|
|
|
|
|
home_window.clearProperty("AccessToken")
|
2017-05-26 03:48:48 -04:00
|
|
|
home_window.clearProperty("Params")
|
2017-04-29 10:37:20 +10:00
|
|
|
|
2017-05-29 06:19:33 -04:00
|
|
|
log = SimpleLogging('service')
|
2019-01-18 09:18:30 +11:00
|
|
|
monitor = xbmc.Monitor()
|
2019-01-16 19:11:51 +11:00
|
|
|
|
2019-01-18 07:20:22 +11:00
|
|
|
# wait for 10 seconds for the Kodi splash screen to close
|
2019-01-18 09:18:30 +11:00
|
|
|
i = 0
|
|
|
|
|
while not monitor.abortRequested():
|
|
|
|
|
if i == 100 or not xbmc.getCondVisibility("Window.IsVisible(startup)"):
|
2019-01-18 07:20:22 +11:00
|
|
|
break
|
2019-01-18 09:18:30 +11:00
|
|
|
i += 1
|
2019-01-18 07:20:22 +11:00
|
|
|
xbmc.sleep(100)
|
|
|
|
|
|
2019-01-16 19:11:51 +11:00
|
|
|
checkServer()
|
|
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
download_utils = DownloadUtils()
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2014-10-03 19:46:33 +10:00
|
|
|
# auth the service
|
|
|
|
|
try:
|
2017-04-10 09:19:08 +10:00
|
|
|
download_utils.authenticate()
|
2018-04-18 23:03:52 +10:00
|
|
|
download_utils.getUserId()
|
2017-07-24 08:33:04 +10:00
|
|
|
except Exception as error:
|
2017-12-30 19:13:31 +11:00
|
|
|
log.error("Error with initial service auth: {0}", error)
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2018-01-03 19:01:01 +11:00
|
|
|
# set up all the services
|
2014-09-28 10:30:07 +10:00
|
|
|
monitor = Service()
|
2018-01-04 12:27:50 +11:00
|
|
|
playback_service = PlaybackService(monitor)
|
2018-01-03 19:01:01 +11:00
|
|
|
|
2017-08-01 08:22:30 +10:00
|
|
|
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()
|
2018-02-25 16:27:47 +11:00
|
|
|
last_background_update = 0
|
2018-12-19 13:24:41 +11:00
|
|
|
last_random_movie_update = 0
|
2017-12-14 13:43:23 +11:00
|
|
|
websocket_client = WebSocketClient()
|
|
|
|
|
|
2018-03-04 15:13:00 +11:00
|
|
|
# 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-07-14 13:37:38 +10:00
|
|
|
|
|
|
|
|
|
2018-01-04 15:32:11 +11:00
|
|
|
# start the WebSocket Client running
|
2019-01-16 19:11:51 +11:00
|
|
|
|
2017-12-14 13:43:23 +11:00
|
|
|
remote_control = settings.getSetting('remoteControl') == "true"
|
|
|
|
|
if remote_control:
|
|
|
|
|
websocket_client.start()
|
2017-05-29 06:19:33 -04:00
|
|
|
|
2018-07-14 13:37:38 +10:00
|
|
|
# Start the context menu monitor
|
|
|
|
|
context_monitor = None
|
|
|
|
|
context_menu = settings.getSetting('override_contextmenu') == "true"
|
|
|
|
|
if context_menu:
|
|
|
|
|
context_monitor = ContextMonitor()
|
|
|
|
|
context_monitor.start()
|
|
|
|
|
|
2018-10-25 15:44:35 +11:00
|
|
|
background_interval = int(settings.getSetting('background_interval'))
|
|
|
|
|
newcontent_interval = int(settings.getSetting('newcontent_interval'))
|
2019-02-27 10:18:03 +11:00
|
|
|
random_movie_list_interval = int(settings.getSetting('random_movie_list_interval'))
|
2018-10-25 15:44:35 +11:00
|
|
|
|
2017-08-01 08:22:30 +10:00
|
|
|
# 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")
|
2017-04-14 20:21:20 +10:00
|
|
|
|
2017-08-01 08:22:30 +10:00
|
|
|
while not xbmc.abortRequested:
|
2017-05-29 06:19:33 -04:00
|
|
|
|
2017-08-13 13:14:51 +10:00
|
|
|
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()
|
2018-01-04 08:25:39 +11:00
|
|
|
sendProgress(monitor)
|
2018-10-25 15:44:35 +11:00
|
|
|
|
2017-08-13 13:14:51 +10:00
|
|
|
else:
|
2019-02-06 16:05:02 +11:00
|
|
|
screen_saver_active = xbmc.getCondVisibility("System.ScreenSaverActive")
|
|
|
|
|
|
|
|
|
|
if not screen_saver_active:
|
|
|
|
|
user_changed = False
|
|
|
|
|
if prev_user_id != home_window.getProperty("userid"):
|
|
|
|
|
log.debug("user_change_detected")
|
|
|
|
|
prev_user_id = home_window.getProperty("userid")
|
|
|
|
|
user_changed = True
|
|
|
|
|
|
2019-02-27 10:18:03 +11:00
|
|
|
if random_movie_list_interval != 0 and (user_changed or (time.time() - last_random_movie_update) > random_movie_list_interval):
|
2019-02-06 16:05:02 +11:00
|
|
|
last_random_movie_update = time.time()
|
|
|
|
|
set_random_movies()
|
|
|
|
|
|
2019-02-27 10:18:03 +11:00
|
|
|
if newcontent_interval != 0 and (user_changed or (time.time() - last_content_check) > newcontent_interval):
|
2019-02-06 16:05:02 +11:00
|
|
|
last_content_check = time.time()
|
|
|
|
|
checkForNewContent()
|
|
|
|
|
|
2019-02-27 10:18:03 +11:00
|
|
|
if background_interval != 0 and (user_changed or (time.time() - last_background_update) > background_interval):
|
2019-02-06 16:05:02 +11:00
|
|
|
last_background_update = time.time()
|
|
|
|
|
set_library_window_values(user_changed)
|
|
|
|
|
set_background_image(user_changed)
|
|
|
|
|
|
|
|
|
|
if remote_control and user_changed:
|
|
|
|
|
websocket_client.stop_client()
|
|
|
|
|
websocket_client = WebSocketClient()
|
|
|
|
|
websocket_client.start()
|
|
|
|
|
|
|
|
|
|
elif screen_saver_active:
|
2019-02-27 10:18:03 +11:00
|
|
|
if background_interval != 0 and ((time.time() - last_background_update) > background_interval):
|
2019-02-06 16:05:02 +11:00
|
|
|
last_background_update = time.time()
|
|
|
|
|
set_background_image(False)
|
2018-04-18 23:03:52 +10:00
|
|
|
|
2017-08-13 13:14:51 +10:00
|
|
|
except Exception as error:
|
2017-12-30 19:13:31 +11:00
|
|
|
log.error("Exception in Playback Monitor: {0}", error)
|
|
|
|
|
log.error("{0}", traceback.format_exc())
|
2017-07-04 20:05:04 +10:00
|
|
|
|
2017-08-01 08:22:30 +10:00
|
|
|
xbmc.sleep(1000)
|
2017-04-15 15:54:14 +10:00
|
|
|
|
2018-07-14 13:37:38 +10:00
|
|
|
# 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()
|
|
|
|
|
|
2017-04-15 15:54:14 +10:00
|
|
|
# clear user and token when loggin off
|
2017-05-25 18:16:01 +10:00
|
|
|
home_window.clearProperty("userid")
|
|
|
|
|
home_window.clearProperty("AccessToken")
|
2017-05-26 03:48:48 -04:00
|
|
|
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")
|