Files
jellycon/service.py

239 lines
7.3 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
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
from resources.lib.play_utils import playFile
2014-09-28 10:30:07 +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-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
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
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")
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)))
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()
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")
2017-05-25 18:16:01 +10:00
playback_type = window_handle.getProperty("PlaybackType_" + emby_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
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")
data = {}
data["item_id"] = emby_item_id
2017-05-25 18:16:01 +10:00
data["paused"] = False
data["playback_type"] = playback_type
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):
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)
def onPlayBackStopped(self):
2017-05-25 18:16:01 +10:00
# Will be called when user stops kodi playing a file
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()
last_progress_update = datetime.today()
2014-09-28 10:30:07 +10:00
while not xbmc.abortRequested:
2017-05-25 18:16:01 +10:00
home_window = 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
2017-05-25 18:16:01 +10:00
if sec_diff > 10:
sendProgress()
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:
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")
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")
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())))
# 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
log.info("Service shutting down")