2015-12-25 07:07:00 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
###############################################################################
|
2016-09-02 03:41:55 +10:00
|
|
|
import logging
|
2015-12-25 07:07:00 +11:00
|
|
|
import json
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
|
2017-09-14 04:01:17 +10:00
|
|
|
from utils import window, DateToKodi, getUnixTimestamp, tryDecode, tryEncode
|
2015-12-25 07:07:00 +11:00
|
|
|
import downloadutils
|
2017-01-05 06:57:16 +11:00
|
|
|
import plexdb_functions as plexdb
|
2016-05-17 02:19:05 +10:00
|
|
|
import kodidb_functions as kodidb
|
2017-12-09 05:43:06 +11:00
|
|
|
import json_rpc as js
|
2017-01-25 02:04:42 +11:00
|
|
|
import variables as v
|
2017-05-18 04:22:16 +10:00
|
|
|
import state
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
###############################################################################
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG = logging.getLogger("PLEX." + __name__)
|
2016-09-02 03:41:55 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
class Player(xbmc.Player):
|
|
|
|
|
2017-12-08 03:25:48 +11:00
|
|
|
played_info = state.PLAYED_INFO
|
|
|
|
playStats = state.PLAYER_STATES
|
2015-12-25 07:07:00 +11:00
|
|
|
currentFile = None
|
|
|
|
|
|
|
|
def __init__(self):
|
2017-12-08 04:19:54 +11:00
|
|
|
self.doUtils = downloadutils.DownloadUtils
|
2016-07-22 23:54:03 +10:00
|
|
|
xbmc.Player.__init__(self)
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("Started playback monitor.")
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-17 19:13:37 +11:00
|
|
|
def onPlayBackStarted(self):
|
2016-03-17 03:02:22 +11:00
|
|
|
"""
|
2016-09-02 03:41:55 +10:00
|
|
|
Will be called when xbmc starts playing a file.
|
2016-03-17 03:02:22 +11:00
|
|
|
Window values need to have been set in Kodimonitor.py
|
|
|
|
"""
|
2015-12-25 07:07:00 +11:00
|
|
|
self.stopAll()
|
|
|
|
|
2016-04-12 03:18:55 +10:00
|
|
|
# Get current file (in utf-8!)
|
2015-12-25 07:07:00 +11:00
|
|
|
try:
|
2017-02-14 05:27:14 +11:00
|
|
|
currentFile = tryDecode(self.getPlayingFile())
|
2015-12-25 07:07:00 +11:00
|
|
|
xbmc.sleep(300)
|
|
|
|
except:
|
|
|
|
currentFile = ""
|
|
|
|
count = 0
|
|
|
|
while not currentFile:
|
|
|
|
xbmc.sleep(100)
|
|
|
|
try:
|
2017-02-14 05:27:14 +11:00
|
|
|
currentFile = tryDecode(self.getPlayingFile())
|
2016-03-17 03:02:22 +11:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if count == 20:
|
2015-12-25 07:07:00 +11:00
|
|
|
break
|
2016-03-17 03:02:22 +11:00
|
|
|
else:
|
|
|
|
count += 1
|
|
|
|
if not currentFile:
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.warn('Error getting currently playing file; abort reporting')
|
2016-03-17 03:02:22 +11:00
|
|
|
return
|
|
|
|
|
|
|
|
# Save currentFile for cleanup later and for references
|
|
|
|
self.currentFile = currentFile
|
2017-02-14 05:54:27 +11:00
|
|
|
window('plex_lastPlayedFiled', value=currentFile)
|
2016-03-17 03:02:22 +11:00
|
|
|
# We may need to wait for info to be set in kodi monitor
|
2017-02-14 05:27:14 +11:00
|
|
|
itemId = window("plex_%s.itemid" % tryEncode(currentFile))
|
2016-03-17 03:02:22 +11:00
|
|
|
count = 0
|
|
|
|
while not itemId:
|
|
|
|
xbmc.sleep(200)
|
2017-02-14 05:27:14 +11:00
|
|
|
itemId = window("plex_%s.itemid" % tryEncode(currentFile))
|
2016-07-22 23:33:27 +10:00
|
|
|
if count == 5:
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.warn("Could not find itemId, cancelling playback report!")
|
2016-03-17 03:02:22 +11:00
|
|
|
return
|
|
|
|
count += 1
|
|
|
|
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("ONPLAYBACK_STARTED: %s itemid: %s" % (currentFile, itemId))
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2017-02-14 05:27:14 +11:00
|
|
|
plexitem = "plex_%s" % tryEncode(currentFile)
|
2017-01-05 06:57:16 +11:00
|
|
|
runtime = window("%s.runtime" % plexitem)
|
|
|
|
refresh_id = window("%s.refreshid" % plexitem)
|
|
|
|
playMethod = window("%s.playmethod" % plexitem)
|
|
|
|
itemType = window("%s.type" % plexitem)
|
2016-05-17 02:19:05 +10:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
playcount = int(window("%s.playcount" % plexitem))
|
2016-05-17 02:19:05 +10:00
|
|
|
except ValueError:
|
|
|
|
playcount = 0
|
2017-01-09 01:03:41 +11:00
|
|
|
window('plex_skipWatched%s' % itemId, value="true")
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug("Playing itemtype is: %s" % itemType)
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2016-05-31 16:06:42 +10:00
|
|
|
customseek = window('plex_customplaylist.seektime')
|
2016-06-20 02:30:21 +10:00
|
|
|
if customseek:
|
2016-09-02 03:41:55 +10:00
|
|
|
# Start at, when using custom playlist (play to Kodi from
|
|
|
|
# webclient)
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("Seeking to: %s" % customseek)
|
2016-06-01 03:13:29 +10:00
|
|
|
try:
|
2016-07-24 02:16:44 +10:00
|
|
|
self.seekTime(int(customseek))
|
2016-06-01 03:13:29 +10:00
|
|
|
except:
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.error('Could not seek!')
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_customplaylist.seektime', clear=True)
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2016-06-01 03:02:11 +10:00
|
|
|
try:
|
2016-07-24 02:16:44 +10:00
|
|
|
seekTime = self.getTime()
|
2016-06-01 03:02:11 +10:00
|
|
|
except RuntimeError:
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.error('Could not get current seektime from xbmc player')
|
2016-06-01 03:02:11 +10:00
|
|
|
seekTime = 0
|
2017-12-09 05:43:06 +11:00
|
|
|
volume = js.get_volume()
|
|
|
|
muted = js.get_muted()
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
# Postdata structure to send to plex server
|
2016-03-17 03:02:22 +11:00
|
|
|
url = "{server}/:/timeline?"
|
|
|
|
postdata = {
|
|
|
|
|
|
|
|
'QueueableMediaTypes': "Video",
|
|
|
|
'CanSeek': True,
|
|
|
|
'ItemId': itemId,
|
|
|
|
'MediaSourceId': itemId,
|
|
|
|
'PlayMethod': playMethod,
|
|
|
|
'VolumeLevel': volume,
|
|
|
|
'PositionTicks': int(seekTime * 10000000),
|
|
|
|
'IsMuted': muted
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get the current audio track and subtitles
|
|
|
|
if playMethod == "Transcode":
|
|
|
|
# property set in PlayUtils.py
|
2017-02-14 05:27:14 +11:00
|
|
|
postdata['AudioStreamIndex'] = window("%sAudioStreamIndex"
|
|
|
|
% tryEncode(currentFile))
|
|
|
|
postdata['SubtitleStreamIndex'] = window("%sSubtitleStreamIndex"
|
|
|
|
% tryEncode(currentFile))
|
2016-03-17 03:02:22 +11:00
|
|
|
else:
|
2017-01-05 06:57:16 +11:00
|
|
|
# Get the current kodi audio and subtitles and convert to plex equivalent
|
2017-12-09 05:43:06 +11:00
|
|
|
indexAudio = js.current_audiostream(1).get('index', 0)
|
|
|
|
subsEnabled = js.subtitle_enabled(1)
|
|
|
|
if subsEnabled:
|
|
|
|
indexSubs = js.current_subtitle(1).get('index', 0)
|
|
|
|
else:
|
2016-03-17 03:02:22 +11:00
|
|
|
indexSubs = 0
|
|
|
|
|
|
|
|
# Postdata for the audio
|
|
|
|
postdata['AudioStreamIndex'] = indexAudio + 1
|
|
|
|
|
|
|
|
# Postdata for the subtitles
|
|
|
|
if subsEnabled and len(xbmc.Player().getAvailableSubtitleStreams()) > 0:
|
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
# Number of audiotracks to help get plex Index
|
2016-03-17 03:02:22 +11:00
|
|
|
audioTracks = len(xbmc.Player().getAvailableAudioStreams())
|
2017-01-05 06:57:16 +11:00
|
|
|
mapping = window("%s.indexMapping" % plexitem)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
if mapping: # Set in playbackutils.py
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug("Mapping for external subtitles index: %s"
|
2016-09-02 03:41:55 +10:00
|
|
|
% mapping)
|
2016-03-17 03:02:22 +11:00
|
|
|
externalIndex = json.loads(mapping)
|
|
|
|
|
|
|
|
if externalIndex.get(str(indexSubs)):
|
|
|
|
# If the current subtitle is in the mapping
|
|
|
|
postdata['SubtitleStreamIndex'] = externalIndex[str(indexSubs)]
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
2016-03-17 03:02:22 +11:00
|
|
|
# Internal subtitle currently selected
|
|
|
|
subindex = indexSubs - len(externalIndex) + audioTracks + 1
|
|
|
|
postdata['SubtitleStreamIndex'] = subindex
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
else: # Direct paths enabled scenario or no external subtitles set
|
|
|
|
postdata['SubtitleStreamIndex'] = indexSubs + audioTracks + 1
|
|
|
|
else:
|
|
|
|
postdata['SubtitleStreamIndex'] = ""
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
# Post playback to server
|
|
|
|
# log("Sending POST play started: %s." % postdata, 2)
|
|
|
|
# self.doUtils(url, postBody=postdata, type="POST")
|
|
|
|
|
|
|
|
# Ensure we do have a runtime
|
|
|
|
try:
|
|
|
|
runtime = int(runtime)
|
|
|
|
except ValueError:
|
2016-06-01 03:13:29 +10:00
|
|
|
try:
|
2016-07-24 02:16:44 +10:00
|
|
|
runtime = self.getTotalTime()
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.error("Runtime is missing, Kodi runtime: %s" % runtime)
|
2016-06-01 03:13:29 +10:00
|
|
|
except:
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.error('Could not get kodi runtime, setting to zero')
|
2016-06-01 03:13:29 +10:00
|
|
|
runtime = 0
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
with plexdb.Get_Plex_DB() as plex_db:
|
|
|
|
plex_dbitem = plex_db.getItem_byId(itemId)
|
2016-05-17 02:19:05 +10:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
fileid = plex_dbitem[1]
|
2016-05-17 02:19:05 +10:00
|
|
|
except TypeError:
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("Could not find fileid in plex db.")
|
2016-05-17 02:19:05 +10:00
|
|
|
fileid = None
|
2016-03-17 03:02:22 +11:00
|
|
|
# Save data map for updates and position calls
|
|
|
|
data = {
|
2016-06-06 02:39:26 +10:00
|
|
|
'runtime': runtime,
|
2016-03-17 03:02:22 +11:00
|
|
|
'item_id': itemId,
|
|
|
|
'refresh_id': refresh_id,
|
|
|
|
'currentfile': currentFile,
|
|
|
|
'AudioStreamIndex': postdata['AudioStreamIndex'],
|
|
|
|
'SubtitleStreamIndex': postdata['SubtitleStreamIndex'],
|
|
|
|
'playmethod': playMethod,
|
|
|
|
'Type': itemType,
|
2016-08-11 03:36:08 +10:00
|
|
|
'currentPosition': int(seekTime),
|
2016-05-17 02:19:05 +10:00
|
|
|
'fileid': fileid,
|
|
|
|
'itemType': itemType,
|
|
|
|
'playcount': playcount
|
2016-03-17 03:02:22 +11:00
|
|
|
}
|
2016-09-02 03:41:55 +10:00
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
self.played_info[currentFile] = data
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("ADDING_FILE: %s" % data)
|
2016-03-17 03:02:22 +11:00
|
|
|
|
|
|
|
# log some playback stats
|
|
|
|
'''if(itemType != None):
|
|
|
|
if(self.playStats.get(itemType) != None):
|
|
|
|
count = self.playStats.get(itemType) + 1
|
|
|
|
self.playStats[itemType] = count
|
|
|
|
else:
|
|
|
|
self.playStats[itemType] = 1
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
if(playMethod != None):
|
|
|
|
if(self.playStats.get(playMethod) != None):
|
|
|
|
count = self.playStats.get(playMethod) + 1
|
|
|
|
self.playStats[playMethod] = count
|
|
|
|
else:
|
|
|
|
self.playStats[playMethod] = 1'''
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-21 10:21:39 +11:00
|
|
|
def onPlayBackPaused(self):
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
currentFile = self.currentFile
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("PLAYBACK_PAUSED: %s" % currentFile)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
if self.played_info.get(currentFile):
|
|
|
|
self.played_info[currentFile]['paused'] = True
|
|
|
|
|
2016-02-21 10:21:39 +11:00
|
|
|
def onPlayBackResumed(self):
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
currentFile = self.currentFile
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("PLAYBACK_RESUMED: %s" % currentFile)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
if self.played_info.get(currentFile):
|
|
|
|
self.played_info[currentFile]['paused'] = False
|
|
|
|
|
2016-02-21 10:21:39 +11:00
|
|
|
def onPlayBackSeek(self, time, seekOffset):
|
2015-12-25 07:07:00 +11:00
|
|
|
# Make position when seeking a bit more accurate
|
|
|
|
currentFile = self.currentFile
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("PLAYBACK_SEEK: %s" % currentFile)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
if self.played_info.get(currentFile):
|
2016-06-01 03:02:11 +10:00
|
|
|
try:
|
2016-07-24 02:16:44 +10:00
|
|
|
position = self.getTime()
|
2016-06-01 03:02:11 +10:00
|
|
|
except RuntimeError:
|
|
|
|
# When Kodi is not playing
|
|
|
|
return
|
2016-08-11 03:36:08 +10:00
|
|
|
self.played_info[currentFile]['currentPosition'] = position
|
2016-06-01 03:02:11 +10:00
|
|
|
|
2016-02-21 10:21:39 +11:00
|
|
|
def onPlayBackStopped(self):
|
2016-03-17 04:01:07 +11:00
|
|
|
# Will be called when user stops xbmc playing a file
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("ONPLAYBACK_STOPPED")
|
2016-03-17 04:01:07 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
self.stopAll()
|
|
|
|
|
2016-11-07 01:37:22 +11:00
|
|
|
for item in ('plex_currently_playing_itemid',
|
|
|
|
'plex_customplaylist',
|
|
|
|
'plex_customplaylist.seektime',
|
|
|
|
'plex_playbackProps',
|
|
|
|
'plex_forcetranscode'):
|
|
|
|
window(item, clear=True)
|
2017-05-18 04:22:16 +10:00
|
|
|
# We might have saved a transient token from a user flinging media via
|
2017-05-25 22:21:27 +10:00
|
|
|
# Companion (if we could not use the playqueue to store the token)
|
2017-05-18 04:22:16 +10:00
|
|
|
state.PLEX_TRANSIENT_TOKEN = None
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug("Cleared playlist properties.")
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2016-03-17 04:01:07 +11:00
|
|
|
def onPlayBackEnded(self):
|
|
|
|
# Will be called when xbmc stops playing a file, because the file ended
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("ONPLAYBACK_ENDED")
|
2016-03-17 04:01:07 +11:00
|
|
|
self.onPlayBackStopped()
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
def stopAll(self):
|
|
|
|
if not self.played_info:
|
2016-09-02 03:41:55 +10:00
|
|
|
return
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("Played_information: %s" % self.played_info)
|
2015-12-25 07:07:00 +11:00
|
|
|
# Process each items
|
|
|
|
for item in self.played_info:
|
|
|
|
data = self.played_info.get(item)
|
2017-05-25 21:58:15 +10:00
|
|
|
if not data:
|
|
|
|
continue
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug("Item path: %s" % item)
|
|
|
|
LOG.debug("Item data: %s" % data)
|
2017-05-25 21:58:15 +10:00
|
|
|
|
|
|
|
runtime = data['runtime']
|
|
|
|
currentPosition = data['currentPosition']
|
|
|
|
itemid = data['item_id']
|
|
|
|
refresh_id = data['refresh_id']
|
|
|
|
currentFile = data['currentfile']
|
|
|
|
media_type = data['Type']
|
|
|
|
playMethod = data['playmethod']
|
|
|
|
|
|
|
|
# Prevent manually mark as watched in Kodi monitor
|
|
|
|
window('plex_skipWatched%s' % itemid, value="true")
|
|
|
|
|
2017-05-25 22:00:30 +10:00
|
|
|
if not currentPosition or not runtime:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
percentComplete = float(currentPosition) / float(runtime)
|
|
|
|
except ZeroDivisionError:
|
|
|
|
# Runtime is 0.
|
|
|
|
percentComplete = 0
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("Percent complete: %s Mark played at: %s"
|
2017-09-24 02:40:30 +10:00
|
|
|
% (percentComplete, v.MARK_PLAYED_AT))
|
|
|
|
if percentComplete >= v.MARK_PLAYED_AT:
|
2017-05-25 22:00:30 +10:00
|
|
|
# Tell Kodi that we've finished watching (Plex knows)
|
|
|
|
if (data['fileid'] is not None and
|
|
|
|
data['itemType'] in (v.KODI_TYPE_MOVIE,
|
|
|
|
v.KODI_TYPE_EPISODE)):
|
|
|
|
with kodidb.GetKodiDB('video') as kodi_db:
|
|
|
|
kodi_db.addPlaystate(
|
|
|
|
data['fileid'],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
data['playcount'] + 1,
|
|
|
|
DateToKodi(getUnixTimestamp()))
|
2016-02-07 22:38:50 +11:00
|
|
|
|
|
|
|
# Clean the WINDOW properties
|
2016-02-07 23:35:03 +11:00
|
|
|
for filename in self.played_info:
|
2017-02-14 05:27:14 +11:00
|
|
|
plex_item = 'plex_%s' % tryEncode(filename)
|
2016-02-07 23:35:03 +11:00
|
|
|
cleanup = (
|
2017-02-14 05:27:14 +11:00
|
|
|
'%s.itemid' % plex_item,
|
|
|
|
'%s.runtime' % plex_item,
|
|
|
|
'%s.refreshid' % plex_item,
|
|
|
|
'%s.playmethod' % plex_item,
|
|
|
|
'%s.type' % plex_item,
|
|
|
|
'%s.runtime' % plex_item,
|
|
|
|
'%s.playcount' % plex_item,
|
2017-05-02 03:51:10 +10:00
|
|
|
'%s.playlistPosition' % plex_item,
|
|
|
|
'%s.subtitle' % plex_item,
|
2016-02-07 23:35:03 +11:00
|
|
|
)
|
|
|
|
for item in cleanup:
|
2016-09-02 03:41:55 +10:00
|
|
|
window(item, clear=True)
|
2016-02-07 22:38:50 +11:00
|
|
|
|
2016-04-14 00:14:55 +10:00
|
|
|
# Stop transcoding
|
|
|
|
if playMethod == "Transcode":
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("Transcoding for %s terminating" % itemid)
|
2017-12-08 04:19:54 +11:00
|
|
|
self.doUtils().downloadUrl(
|
2016-04-14 00:14:55 +10:00
|
|
|
"{server}/video/:/transcode/universal/stop",
|
2017-01-25 02:53:50 +11:00
|
|
|
parameters={'session': window('plex_client_Id')})
|
2016-04-14 00:14:55 +10:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
self.played_info.clear()
|