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
|
2017-05-25 18:16:01 +10:00
|
|
|
import xbmcaddon
|
2014-09-28 10:30:07 +10:00
|
|
|
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.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-04-29 10:37:20 +10:00
|
|
|
# clear user and token when logging in
|
2017-05-25 18:16:01 +10:00
|
|
|
home_window = xbmcgui.Window(10000)
|
|
|
|
|
home_window.clearProperty("userid")
|
|
|
|
|
home_window.clearProperty("AccessToken")
|
|
|
|
|
home_window.clearProperty("EmbyConParams")
|
2017-04-29 10:37:20 +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
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-05-25 18:16:01 +10:00
|
|
|
def sendProgress():
|
|
|
|
|
|
|
|
|
|
playing_file = xbmc.Player().getPlayingFile()
|
|
|
|
|
play_data = monitor.played_information.get(playing_file)
|
|
|
|
|
|
|
|
|
|
if play_data is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
log.info("Sending Progress Update")
|
|
|
|
|
|
|
|
|
|
play_time = xbmc.Player().getTime()
|
|
|
|
|
play_data["currentPossition"] = play_time
|
|
|
|
|
|
|
|
|
|
item_id = play_data.get("item_id")
|
|
|
|
|
if item_id is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
ticks = int(play_time * 10000000)
|
|
|
|
|
paused = play_data.get("paused", False)
|
|
|
|
|
playback_type = play_data.get("playback_type")
|
|
|
|
|
|
|
|
|
|
postdata = {
|
|
|
|
|
'QueueableMediaTypes': "Video",
|
|
|
|
|
'CanSeek': True,
|
|
|
|
|
'ItemId': item_id,
|
|
|
|
|
'MediaSourceId': item_id,
|
|
|
|
|
'PositionTicks': ticks,
|
|
|
|
|
'IsPaused': paused,
|
|
|
|
|
'IsMuted': False,
|
|
|
|
|
'PlayMethod': playback_type
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.debug("Sending POST progress started: %s." % postdata)
|
|
|
|
|
|
|
|
|
|
settings = xbmcaddon.Addon(id='plugin.video.embycon')
|
|
|
|
|
port = settings.getSetting('port')
|
|
|
|
|
host = settings.getSetting('ipaddress')
|
|
|
|
|
server = host + ":" + port
|
|
|
|
|
|
|
|
|
|
url = "http://" + server + "/emby/Sessions/Playing/Progress"
|
|
|
|
|
download_utils.downloadUrl(url, postBody=postdata, type="POST")
|
|
|
|
|
|
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)))
|
2017-05-25 18:16:01 +10:00
|
|
|
|
|
|
|
|
settings = xbmcaddon.Addon(id='plugin.video.embycon')
|
|
|
|
|
port = settings.getSetting('port')
|
|
|
|
|
host = settings.getSetting('ipaddress')
|
|
|
|
|
server = host + ":" + port
|
|
|
|
|
|
|
|
|
|
url = "http://" + server + "/emby/Sessions/Playing/Stopped"
|
|
|
|
|
postdata = {
|
|
|
|
|
'ItemId': emby_item_id,
|
|
|
|
|
'MediaSourceId': emby_item_id,
|
|
|
|
|
'PositionTicks': int(current_possition * 10000000)
|
|
|
|
|
}
|
|
|
|
|
download_utils.downloadUrl(url, postBody=postdata, type="POST")
|
|
|
|
|
|
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-05-25 18:16:01 +10:00
|
|
|
playback_type = window_handle.getProperty("PlaybackType_" + emby_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-05-25 18:16:01 +10:00
|
|
|
log.info("Sending Playback Started")
|
|
|
|
|
postdata = {
|
|
|
|
|
'QueueableMediaTypes': "Video",
|
|
|
|
|
'CanSeek': True,
|
|
|
|
|
'ItemId': emby_item_id,
|
|
|
|
|
'MediaSourceId': emby_item_id,
|
|
|
|
|
'PlayMethod': playback_type
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.debug("Sending POST play started: %s." % postdata)
|
|
|
|
|
|
|
|
|
|
settings = xbmcaddon.Addon(id='plugin.video.embycon')
|
|
|
|
|
port = settings.getSetting('port')
|
|
|
|
|
host = settings.getSetting('ipaddress')
|
|
|
|
|
server = host + ":" + port
|
|
|
|
|
|
|
|
|
|
url = "http://" + server + "/emby/Sessions/Playing"
|
|
|
|
|
download_utils.downloadUrl(url, postBody=postdata, type="POST")
|
|
|
|
|
|
2014-10-03 19:46:33 +10:00
|
|
|
data = {}
|
2017-04-10 09:19:08 +10:00
|
|
|
data["item_id"] = emby_item_id
|
2017-05-25 18:16:01 +10:00
|
|
|
data["paused"] = False
|
|
|
|
|
data["playback_type"] = playback_type
|
2017-04-10 09:19:08 +10:00
|
|
|
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):
|
2017-05-25 18:16:01 +10:00
|
|
|
# Will be called when kodi 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):
|
2017-05-25 18:16:01 +10:00
|
|
|
# Will be called when user stops kodi 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)
|
|
|
|
|
|
2017-05-25 18:16:01 +10:00
|
|
|
def onPlayBackPaused(self):
|
|
|
|
|
# Will be called when kodi pauses the video
|
|
|
|
|
log.info("onPlayBackPaused")
|
|
|
|
|
current_file = xbmc.Player().getPlayingFile()
|
|
|
|
|
play_data = monitor.played_information.get(current_file)
|
|
|
|
|
|
|
|
|
|
if play_data is not None:
|
|
|
|
|
play_data['paused'] = True
|
|
|
|
|
sendProgress()
|
|
|
|
|
|
|
|
|
|
def onPlayBackResumed(self):
|
|
|
|
|
# Will be called when kodi resumes the video
|
|
|
|
|
log.info("onPlayBackResumed")
|
|
|
|
|
current_file = xbmc.Player().getPlayingFile()
|
|
|
|
|
play_data = monitor.played_information.get(current_file)
|
|
|
|
|
|
|
|
|
|
if play_data is not None:
|
|
|
|
|
play_data['paused'] = False
|
|
|
|
|
sendProgress()
|
|
|
|
|
|
|
|
|
|
def onPlayBackSeek(self, time, seekOffset):
|
|
|
|
|
# Will be called when kodi seeks in video
|
|
|
|
|
log.info("onPlayBackSeek")
|
|
|
|
|
sendProgress()
|
|
|
|
|
|
|
|
|
|
|
2014-09-28 10:30:07 +10:00
|
|
|
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
|
|
|
|
2017-05-25 18:16:01 +10:00
|
|
|
home_window = xbmcgui.Window(10000)
|
2017-04-14 20:21:20 +10:00
|
|
|
|
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
|
2017-05-25 18:16:01 +10:00
|
|
|
if sec_diff > 10:
|
|
|
|
|
sendProgress()
|
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:
|
2017-05-25 18:16:01 +10:00
|
|
|
emby_item_id = home_window.getProperty("play_item_id")
|
|
|
|
|
emby_item_resume = home_window.getProperty("play_item_resume")
|
2017-04-14 20:21:20 +10:00
|
|
|
if emby_item_id and emby_item_resume:
|
2017-05-25 18:16:01 +10:00
|
|
|
home_window.clearProperty("play_item_id")
|
|
|
|
|
home_window.clearProperty("play_item_resume")
|
2017-04-14 20:21:20 +10:00
|
|
|
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())))
|
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 = xbmcgui.Window(10000)
|
|
|
|
|
home_window.clearProperty("userid")
|
|
|
|
|
home_window.clearProperty("AccessToken")
|
|
|
|
|
home_window.clearProperty("EmbyConParams")
|
2014-09-28 10:30:07 +10:00
|
|
|
|
2017-04-10 09:19:08 +10:00
|
|
|
log.info("Service shutting down")
|