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 xbmc
|
|
|
|
|
import xbmcgui
|
|
|
|
|
import time
|
|
|
|
|
from datetime import datetime
|
2017-03-10 10:45:38 +11:00
|
|
|
|
2017-03-17 15:41:46 +11:00
|
|
|
from resources.lib.websocketclient import WebSocketThread
|
|
|
|
|
from resources.lib.downloadutils import DownloadUtils
|
|
|
|
|
from resources.lib.simple_logging import SimpleLogging
|
2017-04-14 20:21:20 +10:00
|
|
|
from resources.lib.play_utils import playFile
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-03-17 15:41:46 +11:00
|
|
|
log = SimpleLogging("EmbyCon.service")
|
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()
|
2014-10-03 19:46:33 +10:00
|
|
|
except Exception, e:
|
|
|
|
|
pass
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
websocket_thread = WebSocketThread()
|
|
|
|
|
websocket_thread.setDaemon(True)
|
|
|
|
|
websocket_thread.start()
|
|
|
|
|
|
|
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
def hasData(data):
|
2017-04-10 09:19:08 +10:00
|
|
|
if data is None or len(data) == 0 or data == "None":
|
2014-09-28 10:30:07 +10:00
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
return True
|
2017-04-10 09:19:08 +10:00
|
|
|
|
|
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
def stopAll(played_information):
|
|
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
if len(played_information) == 0:
|
|
|
|
|
return
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-04-08 12:02:12 +10:00
|
|
|
log.info("played_information : " + str(played_information))
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
for item_url in played_information:
|
|
|
|
|
data = played_information.get(item_url)
|
2017-04-10 09:19:08 +10:00
|
|
|
if data is not None:
|
2017-04-08 12:02:12 +10:00
|
|
|
log.info("item_url : " + item_url)
|
|
|
|
|
log.info("item_data : " + str(data))
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
current_possition = data.get("currentPossition")
|
|
|
|
|
emby_item_id = data.get("item_id")
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
if hasData(emby_item_id):
|
|
|
|
|
log.info("Playback Stopped at: " + str(int(current_possition * 10000000)))
|
|
|
|
|
websocket_thread.playbackStopped(emby_item_id, str(int(current_possition * 10000000)))
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
played_information.clear()
|
2014-10-03 19:46:33 +10:00
|
|
|
|
|
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
class Service(xbmc.Player):
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
played_information = {}
|
|
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
def __init__(self, *args):
|
|
|
|
|
log.info("Starting monitor service: " + str(args))
|
2014-09-28 10:30:07 +10:00
|
|
|
self.played_information = {}
|
|
|
|
|
pass
|
|
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
def onPlayBackStarted(self):
|
2014-09-28 10:30:07 +10:00
|
|
|
# Will be called when xbmc starts playing a file
|
|
|
|
|
stopAll(self.played_information)
|
|
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
current_playing_file = xbmc.Player().getPlayingFile()
|
2017-04-13 06:58:25 +10:00
|
|
|
log.info("onPlayBackStarted: " + current_playing_file)
|
2017-04-14 20:21:20 +10:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
window_handle = xbmcgui.Window(10000)
|
2017-04-13 06:58:25 +10:00
|
|
|
emby_item_id = window_handle.getProperty("item_id")
|
2017-04-09 10:25:58 +10:00
|
|
|
|
2017-04-14 20:21:20 +10:00
|
|
|
# if we could not find the ID of the current item then return
|
2017-04-10 09:19:08 +10:00
|
|
|
if emby_item_id is None or len(emby_item_id) == 0:
|
2014-09-28 10:30:07 +10:00
|
|
|
return
|
2017-04-14 20:21:20 +10:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
websocket_thread.playbackStarted(emby_item_id)
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2014-10-03 19:46:33 +10:00
|
|
|
data = {}
|
2017-04-10 09:19:08 +10:00
|
|
|
data["item_id"] = emby_item_id
|
|
|
|
|
self.played_information[current_playing_file] = data
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
log.info("ADDING_FILE : " + current_playing_file)
|
|
|
|
|
log.info("ADDING_FILE : " + str(self.played_information))
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
def onPlayBackEnded(self):
|
2014-09-28 10:30:07 +10:00
|
|
|
# Will be called when xbmc stops playing a file
|
2017-03-10 10:45:38 +11:00
|
|
|
log.info("EmbyCon Service -> onPlayBackEnded")
|
2014-09-28 10:30:07 +10:00
|
|
|
stopAll(self.played_information)
|
|
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
def onPlayBackStopped(self):
|
2014-09-28 10:30:07 +10:00
|
|
|
# Will be called when user stops xbmc playing a file
|
2017-04-10 09:19:08 +10:00
|
|
|
log.info("onPlayBackStopped")
|
2014-09-28 10:30:07 +10:00
|
|
|
stopAll(self.played_information)
|
|
|
|
|
|
|
|
|
|
monitor = Service()
|
2017-04-10 09:19:08 +10:00
|
|
|
last_progress_update = datetime.today()
|
2017-03-13 17:40:39 +11:00
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
while not xbmc.abortRequested:
|
2017-04-14 20:21:20 +10:00
|
|
|
|
|
|
|
|
window_handle = xbmcgui.Window(10000)
|
|
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
if xbmc.Player().isPlaying():
|
|
|
|
|
try:
|
|
|
|
|
# send update
|
2017-04-10 09:19:08 +10:00
|
|
|
td = datetime.today() - last_progress_update
|
|
|
|
|
sec_diff = td.seconds
|
|
|
|
|
if sec_diff > 5:
|
2017-03-13 17:40:39 +11:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
play_time = xbmc.Player().getTime()
|
|
|
|
|
current_file = xbmc.Player().getPlayingFile()
|
2017-03-13 17:40:39 +11:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
if monitor.played_information.get(current_file) is not None:
|
|
|
|
|
|
|
|
|
|
monitor.played_information[current_file]["currentPossition"] = play_time
|
2017-03-13 17:40:39 +11:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
if (monitor.played_information.get(current_file) is not None and
|
|
|
|
|
monitor.played_information.get(current_file).get("item_id") is not None):
|
|
|
|
|
|
|
|
|
|
item_id = monitor.played_information.get(current_file).get("item_id")
|
|
|
|
|
websocket_thread.sendProgressUpdate(item_id, str(int(play_time * 10000000)))
|
2017-03-13 17:40:39 +11:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
last_progress_update = datetime.today()
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
except Exception, e:
|
2017-04-10 09:19:08 +10:00
|
|
|
log.error("Exception in Playback Monitor : " + str(e))
|
2014-09-28 10:30:07 +10:00
|
|
|
pass
|
|
|
|
|
|
2017-04-14 20:21:20 +10:00
|
|
|
else:
|
|
|
|
|
emby_item_id = window_handle.getProperty("play_item_id")
|
|
|
|
|
emby_item_resume = window_handle.getProperty("play_item_resume")
|
|
|
|
|
if emby_item_id and emby_item_resume:
|
|
|
|
|
window_handle.clearProperty("play_item_id")
|
|
|
|
|
window_handle.clearProperty("play_item_resume")
|
|
|
|
|
playFile(emby_item_id, emby_item_resume)
|
|
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
xbmc.sleep(1000)
|
2017-03-08 21:02:10 +11:00
|
|
|
xbmcgui.Window(10000).setProperty("EmbyCon_Service_Timestamp", str(int(time.time())))
|
2014-09-28 10:30:07 +10:00
|
|
|
|
|
|
|
|
# stop the WebSocket client
|
2017-04-10 09:19:08 +10:00
|
|
|
websocket_thread.stopClient()
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
log.info("Service shutting down")
|