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-08-13 13:14:51 +10:00
|
|
|
import traceback
|
2017-03-10 10:45:38 +11:00
|
|
|
|
2018-01-04 08:25:39 +11:00
|
|
|
import xbmc
|
|
|
|
|
import xbmcaddon
|
2020-01-01 11:10:54 +11:00
|
|
|
import xbmcgui
|
2018-01-04 08:25:39 +11:00
|
|
|
|
2022-03-09 22:27:18 +01:00
|
|
|
from resources.lib.lazylogger import LazyLogger
|
2020-06-21 11:27:09 +10:00
|
|
|
from resources.lib.play_utils import Service, PlaybackService, send_progress
|
2021-12-30 09:44:25 -05:00
|
|
|
from resources.lib.kodi_utils import HomeWindow
|
2025-06-13 18:25:06 -04:00
|
|
|
from resources.lib.widgets import set_background_image, set_random_movies, set_random_tvshows, set_random_all
|
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
|
2022-02-27 22:15:54 -05:00
|
|
|
from resources.lib.server_detect import check_server, check_connection_speed
|
2021-12-30 09:44:25 -05:00
|
|
|
from resources.lib.monitors import LibraryChangeMonitor, ContextMonitor
|
2019-07-07 20:04:19 +10:00
|
|
|
from resources.lib.datamanager import clear_old_cache_data
|
2019-10-14 11:55:18 +11:00
|
|
|
from resources.lib.tracking import set_timing_enabled
|
2019-12-09 16:40:55 +11:00
|
|
|
from resources.lib.image_server import HttpImageServerThread
|
2020-07-01 14:34:27 +10:00
|
|
|
from resources.lib.playnext import PlayNextService
|
2025-06-12 18:55:26 -04:00
|
|
|
from resources.lib.intro_skipper import IntroSkipperService
|
2019-01-16 19:11:51 +11:00
|
|
|
|
|
|
|
|
settings = xbmcaddon.Addon()
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2019-10-14 11:55:18 +11:00
|
|
|
log_timing_data = settings.getSetting('log_timing') == "true"
|
|
|
|
|
if log_timing_data:
|
|
|
|
|
set_timing_enabled(True)
|
|
|
|
|
|
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()
|
2022-08-26 13:38:15 -04:00
|
|
|
home_window.clear_property("user_name")
|
2020-06-21 11:27:09 +10:00
|
|
|
home_window.clear_property("AccessToken")
|
|
|
|
|
home_window.clear_property("Params")
|
2017-04-29 10:37:20 +10:00
|
|
|
|
2020-07-25 01:01:30 -04:00
|
|
|
log = LazyLogger('service')
|
2019-01-18 09:18:30 +11:00
|
|
|
monitor = xbmc.Monitor()
|
2019-01-16 19:11:51 +11:00
|
|
|
|
2019-07-21 12:29:11 +10:00
|
|
|
try:
|
|
|
|
|
clear_old_cache_data()
|
|
|
|
|
except Exception as error:
|
2020-07-25 01:01:30 -04:00
|
|
|
log.error("Error in clear_old_cache_data() : {0}".format(error))
|
2019-07-21 12:29:11 +10: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)
|
|
|
|
|
|
2020-06-21 11:27:09 +10:00
|
|
|
check_server()
|
2019-01-16 19:11:51 +11:00
|
|
|
|
2019-12-09 16:40:55 +11:00
|
|
|
image_server = HttpImageServerThread()
|
|
|
|
|
image_server.start()
|
|
|
|
|
|
2018-01-03 19:01:01 +11:00
|
|
|
# set up all the services
|
2021-05-28 23:33:13 -04: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
|
2025-06-12 18:41:26 -04:00
|
|
|
last_random_tvshow_update = 0
|
2025-06-13 18:25:06 -04:00
|
|
|
last_random_all_update = 0
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2019-07-03 14:15:42 +10:00
|
|
|
# start the library update monitor
|
|
|
|
|
library_change_monitor = LibraryChangeMonitor()
|
|
|
|
|
library_change_monitor.start()
|
2018-07-14 13:37:38 +10:00
|
|
|
|
2018-01-04 15:32:11 +11:00
|
|
|
# start the WebSocket Client running
|
2019-07-08 12:05:57 +10:00
|
|
|
remote_control = settings.getSetting('websocket_enabled') == "true"
|
2019-07-03 14:15:42 +10:00
|
|
|
websocket_client = WebSocketClient(library_change_monitor)
|
2017-12-14 13:43:23 +11:00
|
|
|
if remote_control:
|
|
|
|
|
websocket_client.start()
|
2017-05-29 06:19:33 -04:00
|
|
|
|
2020-07-01 14:34:27 +10:00
|
|
|
play_next_service = None
|
|
|
|
|
play_next_trigger_time = int(settings.getSetting('play_next_trigger_time'))
|
|
|
|
|
if play_next_trigger_time > 0:
|
|
|
|
|
play_next_service = PlayNextService(monitor)
|
|
|
|
|
play_next_service.start()
|
|
|
|
|
|
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()
|
|
|
|
|
|
2025-06-12 18:55:26 -04:00
|
|
|
# Start the skip service monitor
|
|
|
|
|
intro_skipper = IntroSkipperService(monitor)
|
|
|
|
|
intro_skipper.start()
|
|
|
|
|
|
2018-10-25 15:44:35 +11:00
|
|
|
background_interval = int(settings.getSetting('background_interval'))
|
2019-07-08 12:05:57 +10:00
|
|
|
newcontent_interval = int(settings.getSetting('new_content_check_interval'))
|
|
|
|
|
random_movie_list_interval = int(settings.getSetting('random_movie_refresh_interval'))
|
2019-07-03 14:15:42 +10:00
|
|
|
random_movie_list_interval = random_movie_list_interval * 60
|
2018-10-25 15:44:35 +11:00
|
|
|
|
2020-01-01 11:10:54 +11:00
|
|
|
enable_logging = settings.getSetting('log_debug') == "true"
|
|
|
|
|
if enable_logging:
|
|
|
|
|
xbmcgui.Dialog().notification(settings.getAddonInfo('name'),
|
|
|
|
|
"Debug logging enabled!",
|
|
|
|
|
time=8000,
|
|
|
|
|
icon=xbmcgui.NOTIFICATION_WARNING)
|
|
|
|
|
|
2022-08-26 13:38:15 -04:00
|
|
|
prev_user = home_window.get_property("user_name")
|
2020-06-26 14:46:57 +10:00
|
|
|
first_run = True
|
2021-01-30 23:27:16 -05:00
|
|
|
home_window.set_property('exit', 'False')
|
2017-04-14 20:21:20 +10:00
|
|
|
|
2021-01-30 23:27:16 -05:00
|
|
|
while home_window.get_property('exit') == 'False':
|
2025-06-12 18:41:26 -04:00
|
|
|
|
2017-08-13 13:14:51 +10:00
|
|
|
try:
|
|
|
|
|
if xbmc.Player().isPlaying():
|
2019-07-04 14:57:26 +10:00
|
|
|
last_random_movie_update = time.time() - (random_movie_list_interval - 15)
|
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()
|
2021-01-26 22:35:37 -05:00
|
|
|
send_progress()
|
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
|
2022-08-26 13:38:15 -04:00
|
|
|
if prev_user != home_window.get_property("user_name"):
|
2019-02-06 16:05:02 +11:00
|
|
|
log.debug("user_change_detected")
|
2022-08-26 13:38:15 -04:00
|
|
|
prev_user = home_window.get_property("user_name")
|
2019-02-06 16:05:02 +11:00
|
|
|
user_changed = True
|
|
|
|
|
|
2020-06-26 14:46:57 +10:00
|
|
|
if user_changed or first_run:
|
2024-02-28 10:41:16 +02:00
|
|
|
settings = xbmcaddon.Addon()
|
2022-02-27 22:15:54 -05:00
|
|
|
server_speed_check_data = settings.getSetting("server_speed_check_data")
|
|
|
|
|
server_host = settings.getSetting('server_address')
|
2020-06-27 19:40:44 +10:00
|
|
|
if server_host is not None and server_host != "" and server_host != "<none>" and server_host not in server_speed_check_data:
|
2020-06-26 14:46:57 +10:00
|
|
|
message = "This is the first time you have connected to this server.\nDo you want to run a connection speed test?"
|
|
|
|
|
response = xbmcgui.Dialog().yesno("First Connection", message)
|
|
|
|
|
if response:
|
|
|
|
|
speed = check_connection_speed()
|
|
|
|
|
if speed > 0:
|
|
|
|
|
settings.setSetting("server_speed_check_data", server_host + "-" + str(speed))
|
|
|
|
|
else:
|
|
|
|
|
settings.setSetting("server_speed_check_data", server_host + "-skipped")
|
|
|
|
|
|
2019-07-04 14:57:26 +10:00
|
|
|
if user_changed or (random_movie_list_interval != 0 and (time.time() - last_random_movie_update) > random_movie_list_interval):
|
2025-06-12 18:41:26 -04:00
|
|
|
last_random_movie_update = time.time()
|
|
|
|
|
set_random_movies()
|
|
|
|
|
|
|
|
|
|
if user_changed or (random_movie_list_interval != 0 and (time.time() - last_random_tvshow_update) > random_movie_list_interval):
|
|
|
|
|
last_random_tvshow_update = time.time()
|
|
|
|
|
set_random_tvshows()
|
2019-02-06 16:05:02 +11:00
|
|
|
|
2025-06-13 18:25:06 -04:00
|
|
|
if user_changed or (random_movie_list_interval != 0 and (time.time() - last_random_all_update) > random_movie_list_interval):
|
|
|
|
|
last_random_all_update = time.time()
|
|
|
|
|
set_random_all()
|
|
|
|
|
|
2019-07-03 14:15:42 +10:00
|
|
|
if user_changed or (newcontent_interval != 0 and (time.time() - last_content_check) > newcontent_interval):
|
2019-02-06 16:05:02 +11:00
|
|
|
last_content_check = time.time()
|
2019-07-03 14:15:42 +10:00
|
|
|
library_change_monitor.check_for_updates()
|
2019-02-06 16:05:02 +11:00
|
|
|
|
2019-07-04 14:57:26 +10:00
|
|
|
if user_changed or (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_library_window_values(user_changed)
|
|
|
|
|
set_background_image(user_changed)
|
|
|
|
|
|
|
|
|
|
if remote_control and user_changed:
|
|
|
|
|
websocket_client.stop_client()
|
2019-07-03 14:15:42 +10:00
|
|
|
websocket_client = WebSocketClient(library_change_monitor)
|
2019-02-06 16:05:02 +11:00
|
|
|
websocket_client.start()
|
|
|
|
|
|
|
|
|
|
elif screen_saver_active:
|
2019-07-04 14:57:26 +10:00
|
|
|
last_random_movie_update = time.time() - (random_movie_list_interval - 15)
|
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:
|
2020-07-25 01:01:30 -04:00
|
|
|
log.error("Exception in Playback Monitor: {0}".format(error))
|
|
|
|
|
log.error("{0}".format(traceback.format_exc()))
|
2017-07-04 20:05:04 +10:00
|
|
|
|
2020-06-26 14:46:57 +10:00
|
|
|
first_run = False
|
2017-08-01 08:22:30 +10:00
|
|
|
xbmc.sleep(1000)
|
2017-04-15 15:54:14 +10:00
|
|
|
|
2019-12-09 16:40:55 +11:00
|
|
|
image_server.stop()
|
|
|
|
|
|
2021-01-30 23:27:16 -05:00
|
|
|
# stop the WebSocket Client
|
|
|
|
|
websocket_client.stop_client()
|
|
|
|
|
|
2019-07-03 14:15:42 +10:00
|
|
|
# call stop on the library update monitor
|
|
|
|
|
library_change_monitor.stop()
|
|
|
|
|
|
2023-01-19 12:44:43 +08:00
|
|
|
# stop the play next episode service
|
2020-07-01 14:34:27 +10:00
|
|
|
if play_next_service:
|
2023-01-19 12:44:43 +08:00
|
|
|
play_next_service.stop_service()
|
2020-07-01 14:34:27 +10:00
|
|
|
|
2018-07-14 13:37:38 +10:00
|
|
|
# call stop on the context menu monitor
|
|
|
|
|
if context_monitor:
|
|
|
|
|
context_monitor.stop_monitor()
|
|
|
|
|
|
2025-06-12 18:55:26 -04:00
|
|
|
if intro_skipper:
|
|
|
|
|
intro_skipper.stop_service()
|
|
|
|
|
|
2023-01-19 12:44:43 +08:00
|
|
|
# clear user and token when logging off
|
2022-08-26 13:38:15 -04:00
|
|
|
home_window.clear_property("user_name")
|
2020-06-21 11:27:09 +10:00
|
|
|
home_window.clear_property("AccessToken")
|
|
|
|
|
home_window.clear_property("userimage")
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2018-01-05 08:49:06 +11:00
|
|
|
log.debug("Service shutting down")
|