Files
jellycon/service.py

148 lines
4.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 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
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")
download_utils = DownloadUtils()
2014-09-28 10:30:07 +10:00
# auth the service
try:
download_utils.authenticate()
except Exception, e:
pass
2014-09-28 10:30:07 +10:00
websocket_thread = WebSocketThread()
websocket_thread.setDaemon(True)
websocket_thread.start()
2014-09-28 10:30:07 +10:00
def hasData(data):
if data is None or len(data) == 0 or data == "None":
2014-09-28 10:30:07 +10:00
return False
else:
return True
2014-09-28 10:30:07 +10:00
def stopAll(played_information):
if len(played_information) == 0:
return
2014-09-28 10:30:07 +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)
if data is not None:
log.info("item_url : " + item_url)
log.info("item_data : " + str(data))
2014-09-28 10:30:07 +10:00
current_possition = data.get("currentPossition")
emby_item_id = data.get("item_id")
2014-09-28 10:30:07 +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()
class Service(xbmc.Player):
2014-09-28 10:30:07 +10:00
played_information = {}
def __init__(self, *args):
log.info("Starting monitor service: " + str(args))
2014-09-28 10:30:07 +10:00
self.played_information = {}
pass
def onPlayBackStarted(self):
2014-09-28 10:30:07 +10:00
# Will be called when xbmc starts playing a file
stopAll(self.played_information)
current_playing_file = xbmc.Player().getPlayingFile()
2017-04-13 06:58:25 +10:00
log.info("onPlayBackStarted: " + current_playing_file)
window_handle = xbmcgui.Window(10000)
2017-04-13 06:58:25 +10:00
emby_item_id = window_handle.getProperty("item_id")
# if we could not find the ID of the current item then return
if emby_item_id is None or len(emby_item_id) == 0:
2014-09-28 10:30:07 +10:00
return
websocket_thread.playbackStarted(emby_item_id)
2014-09-28 10:30:07 +10:00
data = {}
data["item_id"] = emby_item_id
self.played_information[current_playing_file] = data
2014-09-28 10:30:07 +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
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)
def onPlayBackStopped(self):
2014-09-28 10:30:07 +10:00
# Will be called when user stops xbmc playing a file
log.info("onPlayBackStopped")
2014-09-28 10:30:07 +10:00
stopAll(self.played_information)
monitor = Service()
last_progress_update = datetime.today()
2014-09-28 10:30:07 +10:00
while not xbmc.abortRequested:
window_handle = xbmcgui.Window(10000)
2014-09-28 10:30:07 +10:00
if xbmc.Player().isPlaying():
try:
# send update
td = datetime.today() - last_progress_update
sec_diff = td.seconds
if sec_diff > 5:
play_time = xbmc.Player().getTime()
current_file = xbmc.Player().getPlayingFile()
if monitor.played_information.get(current_file) is not None:
monitor.played_information[current_file]["currentPossition"] = play_time
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)))
last_progress_update = datetime.today()
2014-09-28 10:30:07 +10:00
except Exception, e:
log.error("Exception in Playback Monitor : " + str(e))
2014-09-28 10:30:07 +10:00
pass
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
websocket_thread.stopClient()
2014-09-28 10:30:07 +10:00
log.info("Service shutting down")