2018-07-13 02:46:02 +10:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2017-12-15 03:39:50 +11:00
|
|
|
"""
|
|
|
|
PKC Kodi Monitoring implementation
|
|
|
|
"""
|
2017-09-03 20:46:41 +10:00
|
|
|
from logging import getLogger
|
2017-01-09 01:03:41 +11:00
|
|
|
from json import loads
|
2018-03-12 04:54:05 +11:00
|
|
|
import copy
|
2019-07-14 20:00:07 +10:00
|
|
|
import json
|
|
|
|
import binascii
|
2019-02-22 17:51:27 +11:00
|
|
|
|
2018-04-09 16:13:54 +10:00
|
|
|
import xbmc
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2019-07-14 20:00:07 +10:00
|
|
|
from .plex_api import API
|
2018-10-25 02:17:02 +11:00
|
|
|
from .plex_db import PlexDB
|
2021-09-09 21:55:20 +10:00
|
|
|
from .kodi_db import KodiVideoDB
|
2018-11-09 07:22:16 +11:00
|
|
|
from . import kodi_db
|
2018-06-22 03:24:37 +10:00
|
|
|
from .downloadutils import DownloadUtils as DU
|
2019-10-29 19:59:10 +11:00
|
|
|
from . import utils, timing, plex_functions as PF
|
2018-11-19 00:59:17 +11:00
|
|
|
from . import json_rpc as js, playqueue as PQ, playlist_func as PL
|
|
|
|
from . import backgroundthread, app, variables as v
|
2021-09-13 19:24:06 +10:00
|
|
|
from . import exceptions
|
2016-01-13 00:14:49 +11:00
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
LOG = getLogger('PLEX.kodimonitor')
|
2016-08-31 00:57:47 +10:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2018-04-09 16:13:54 +10:00
|
|
|
class KodiMonitor(xbmc.Monitor):
|
2017-12-15 03:39:50 +11:00
|
|
|
"""
|
|
|
|
PKC implementation of the Kodi Monitor class. Invoke only once.
|
|
|
|
"""
|
2018-01-07 01:19:12 +11:00
|
|
|
def __init__(self):
|
2018-03-29 16:33:07 +11:00
|
|
|
self._already_slept = False
|
2021-09-13 00:33:18 +10:00
|
|
|
self._switched_to_plex_streams = True
|
2018-04-09 16:13:54 +10:00
|
|
|
xbmc.Monitor.__init__(self)
|
2018-11-19 00:59:17 +11:00
|
|
|
for playerid in app.PLAYSTATE.player_states:
|
|
|
|
app.PLAYSTATE.player_states[playerid] = copy.deepcopy(app.PLAYSTATE.template)
|
|
|
|
app.PLAYSTATE.old_player_states[playerid] = copy.deepcopy(app.PLAYSTATE.template)
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.info("Kodi monitor started.")
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
def onScanStarted(self, library):
|
2017-12-15 03:39:50 +11:00
|
|
|
"""
|
|
|
|
Will be called when Kodi starts scanning the library
|
|
|
|
"""
|
2018-02-04 22:06:39 +11:00
|
|
|
LOG.debug("Kodi library scan %s running.", library)
|
2016-02-20 06:03:06 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def onScanFinished(self, library):
|
2017-12-15 03:39:50 +11:00
|
|
|
"""
|
|
|
|
Will be called when Kodi finished scanning the library
|
|
|
|
"""
|
2018-02-04 22:06:39 +11:00
|
|
|
LOG.debug("Kodi library scan %s finished.", library)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-01-09 21:53:04 +11:00
|
|
|
def onSettingsChanged(self):
|
2016-10-23 02:15:10 +11:00
|
|
|
"""
|
|
|
|
Monitor the PKC settings for changes made by the user
|
|
|
|
"""
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.debug('PKC settings change detected')
|
2016-01-09 21:53:04 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def onNotification(self, sender, method, data):
|
2017-12-15 03:39:50 +11:00
|
|
|
"""
|
|
|
|
Called when a bunch of different stuff happens on the Kodi side
|
|
|
|
"""
|
2015-12-25 07:07:00 +11:00
|
|
|
if data:
|
2020-12-19 17:28:31 +11:00
|
|
|
data = loads(data)
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.debug("Method: %s Data: %s", method, data)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
if method == "Player.OnPlay":
|
2018-11-19 00:59:17 +11:00
|
|
|
with app.APP.lock_playqueues:
|
2018-06-22 04:43:39 +10:00
|
|
|
self.PlayBackStart(data)
|
2021-09-04 01:42:59 +10:00
|
|
|
elif method == 'Player.OnAVChange':
|
|
|
|
with app.APP.lock_playqueues:
|
2021-09-13 00:33:18 +10:00
|
|
|
self._on_av_change(data)
|
2016-04-08 21:51:29 +10:00
|
|
|
elif method == "Player.OnStop":
|
2019-10-29 19:59:10 +11:00
|
|
|
with app.APP.lock_playqueues:
|
|
|
|
_playback_cleanup(ended=data.get('end'))
|
2017-12-21 19:28:06 +11:00
|
|
|
elif method == 'Playlist.OnAdd':
|
2019-01-20 02:56:10 +11:00
|
|
|
if 'item' in data and data['item'].get('type') == v.KODI_TYPE_SHOW:
|
|
|
|
# Hitting the "browse" button on tv show info dialog
|
|
|
|
# Hence show the tv show directly
|
|
|
|
xbmc.executebuiltin("Dialog.Close(all, true)")
|
|
|
|
js.activate_window('videos',
|
|
|
|
'videodb://tvshows/titles/%s/' % data['item']['id'])
|
2018-11-19 00:59:17 +11:00
|
|
|
with app.APP.lock_playqueues:
|
2018-06-22 04:43:39 +10:00
|
|
|
self._playlist_onadd(data)
|
2017-12-21 19:28:06 +11:00
|
|
|
elif method == 'Playlist.OnRemove':
|
|
|
|
self._playlist_onremove(data)
|
|
|
|
elif method == 'Playlist.OnClear':
|
2018-11-19 00:59:17 +11:00
|
|
|
with app.APP.lock_playqueues:
|
2018-06-22 04:43:39 +10:00
|
|
|
self._playlist_onclear(data)
|
2015-12-25 07:07:00 +11:00
|
|
|
elif method == "VideoLibrary.OnUpdate":
|
2021-09-09 21:55:20 +10:00
|
|
|
with app.APP.lock_playqueues:
|
|
|
|
_videolibrary_onupdate(data)
|
2015-12-25 07:07:00 +11:00
|
|
|
elif method == "VideoLibrary.OnRemove":
|
2016-02-17 15:13:10 +11:00
|
|
|
pass
|
2016-08-31 00:57:47 +10:00
|
|
|
elif method == "System.OnSleep":
|
|
|
|
# Connection is going to sleep
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.info("Marking the server as offline. SystemOnSleep activated.")
|
2015-12-25 07:07:00 +11:00
|
|
|
elif method == "System.OnWake":
|
|
|
|
# Allow network to wake up
|
2018-11-21 02:58:25 +11:00
|
|
|
self.waitForAbort(10)
|
2018-11-26 03:03:19 +11:00
|
|
|
app.CONN.online = False
|
2016-05-15 20:33:14 +10:00
|
|
|
elif method == "GUI.OnScreensaverDeactivated":
|
2018-06-22 03:24:37 +10:00
|
|
|
if utils.settings('dbSyncScreensaver') == "true":
|
2018-11-21 02:58:25 +11:00
|
|
|
self.waitForAbort(5)
|
2018-11-26 17:25:50 +11:00
|
|
|
app.SYNC.run_lib_scan = 'full'
|
2017-05-17 18:09:57 +10:00
|
|
|
elif method == "System.OnQuit":
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.info('Kodi OnQuit detected - shutting down')
|
2018-11-19 00:59:17 +11:00
|
|
|
app.APP.stop_pkc = True
|
2019-07-14 20:00:07 +10:00
|
|
|
elif method == 'Other.plugin.video.plexkodiconnect_play_action':
|
|
|
|
self._start_next_episode(data)
|
2017-05-17 18:09:57 +10:00
|
|
|
|
2017-12-21 19:28:06 +11:00
|
|
|
def _playlist_onadd(self, data):
|
|
|
|
"""
|
|
|
|
Called if an item is added to a Kodi playlist. Example data dict:
|
|
|
|
{
|
|
|
|
u'item': {
|
|
|
|
u'type': u'movie',
|
|
|
|
u'id': 2},
|
|
|
|
u'playlistid': 1,
|
|
|
|
u'position': 0
|
|
|
|
}
|
|
|
|
Will NOT be called if playback initiated by Kodi widgets
|
|
|
|
"""
|
2019-10-29 19:59:10 +11:00
|
|
|
pass
|
2017-12-21 19:28:06 +11:00
|
|
|
|
|
|
|
def _playlist_onremove(self, data):
|
|
|
|
"""
|
|
|
|
Called if an item is removed from a Kodi playlist. Example data dict:
|
|
|
|
{
|
|
|
|
u'playlistid': 1,
|
|
|
|
u'position': 0
|
|
|
|
}
|
|
|
|
"""
|
2018-02-04 22:06:39 +11:00
|
|
|
pass
|
2017-12-21 19:28:06 +11:00
|
|
|
|
2019-02-08 23:52:33 +11:00
|
|
|
@staticmethod
|
|
|
|
def _playlist_onclear(data):
|
2017-12-21 19:28:06 +11:00
|
|
|
"""
|
|
|
|
Called if a Kodi playlist is cleared. Example data dict:
|
|
|
|
{
|
|
|
|
u'playlistid': 1,
|
|
|
|
}
|
|
|
|
"""
|
2018-03-07 04:23:56 +11:00
|
|
|
playqueue = PQ.PLAYQUEUES[data['playlistid']]
|
|
|
|
if not playqueue.is_pkc_clear():
|
2018-04-15 21:16:58 +10:00
|
|
|
playqueue.pkc_edit = True
|
2018-03-07 04:23:56 +11:00
|
|
|
playqueue.clear(kodi=False)
|
|
|
|
else:
|
|
|
|
LOG.debug('Detected PKC clear - ignoring')
|
2017-12-21 19:28:06 +11:00
|
|
|
|
2019-02-08 23:52:33 +11:00
|
|
|
@staticmethod
|
|
|
|
def _get_ids(kodi_id, kodi_type, path):
|
2016-03-17 03:02:22 +11:00
|
|
|
"""
|
2018-03-29 16:33:07 +11:00
|
|
|
Returns the tuple (plex_id, plex_type) or (None, None)
|
2016-03-17 03:02:22 +11:00
|
|
|
"""
|
2017-12-14 06:14:27 +11:00
|
|
|
# No Kodi id returned by Kodi, even if there is one. Ex: Widgets
|
2018-03-29 16:33:07 +11:00
|
|
|
plex_id = None
|
|
|
|
plex_type = None
|
2017-12-14 06:14:27 +11:00
|
|
|
# If using direct paths and starting playback from a widget
|
2018-03-29 16:33:07 +11:00
|
|
|
if not kodi_id and kodi_type and path:
|
2018-11-09 07:22:16 +11:00
|
|
|
kodi_id, _ = kodi_db.kodiid_from_filename(path, kodi_type)
|
2018-03-29 16:33:07 +11:00
|
|
|
if kodi_id:
|
2018-10-25 02:17:02 +11:00
|
|
|
with PlexDB() as plexdb:
|
|
|
|
db_item = plexdb.item_by_kodi_id(kodi_id, kodi_type)
|
|
|
|
if db_item:
|
|
|
|
plex_id = db_item['plex_id']
|
|
|
|
plex_type = db_item['plex_type']
|
2018-03-29 16:33:07 +11:00
|
|
|
return plex_id, plex_type
|
2018-01-11 06:14:05 +11:00
|
|
|
|
2018-02-04 02:40:24 +11:00
|
|
|
@staticmethod
|
|
|
|
def _add_remaining_items_to_playlist(playqueue):
|
|
|
|
"""
|
|
|
|
Adds all but the very first item of the Kodi playlist to the Plex
|
|
|
|
playqueue
|
|
|
|
"""
|
|
|
|
items = js.playlist_get_items(playqueue.playlistid)
|
|
|
|
if not items:
|
|
|
|
LOG.error('Could not retrieve Kodi playlist items')
|
|
|
|
return
|
|
|
|
# Remove first item
|
|
|
|
items.pop(0)
|
|
|
|
try:
|
|
|
|
for i, item in enumerate(items):
|
2018-05-02 23:34:21 +10:00
|
|
|
PL.add_item_to_plex_playqueue(playqueue, i + 1, kodi_item=item)
|
2021-09-13 19:24:06 +10:00
|
|
|
except exceptions.PlaylistError:
|
2018-02-04 02:40:24 +11:00
|
|
|
LOG.info('Could not build Plex playlist for: %s', items)
|
|
|
|
|
2018-03-29 16:33:07 +11:00
|
|
|
def _json_item(self, playerid):
|
|
|
|
"""
|
|
|
|
Uses JSON RPC to get the playing item's info and returns the tuple
|
|
|
|
kodi_id, kodi_type, path
|
|
|
|
or None each time if not found.
|
|
|
|
"""
|
|
|
|
if not self._already_slept:
|
|
|
|
# SLEEP before calling this for the first time just after playback
|
|
|
|
# start as Kodi updates this info very late!! Might get previous
|
|
|
|
# element otherwise
|
|
|
|
self._already_slept = True
|
2018-11-21 02:58:25 +11:00
|
|
|
self.waitForAbort(1)
|
2018-06-17 20:35:09 +10:00
|
|
|
try:
|
|
|
|
json_item = js.get_item(playerid)
|
|
|
|
except KeyError:
|
|
|
|
LOG.debug('No playing item returned by Kodi')
|
|
|
|
return None, None, None
|
2018-03-29 16:33:07 +11:00
|
|
|
LOG.debug('Kodi playing item properties: %s', json_item)
|
|
|
|
return (json_item.get('id'),
|
|
|
|
json_item.get('type'),
|
|
|
|
json_item.get('file'))
|
|
|
|
|
2019-07-14 20:00:07 +10:00
|
|
|
@staticmethod
|
|
|
|
def _start_next_episode(data):
|
|
|
|
"""
|
|
|
|
Used for the add-on Upnext to start playback of the next episode
|
|
|
|
"""
|
|
|
|
LOG.info('Upnext: Start playback of the next episode')
|
|
|
|
play_info = binascii.unhexlify(data[0])
|
|
|
|
play_info = json.loads(play_info)
|
|
|
|
app.APP.player.stop()
|
|
|
|
handle = 'RunPlugin(%s)' % play_info.get('handle')
|
2020-12-20 06:43:08 +11:00
|
|
|
xbmc.executebuiltin(handle)
|
2019-07-14 20:00:07 +10:00
|
|
|
|
2018-01-11 06:14:05 +11:00
|
|
|
def PlayBackStart(self, data):
|
|
|
|
"""
|
|
|
|
Called whenever playback is started. Example data:
|
|
|
|
{
|
|
|
|
u'item': {u'type': u'movie', u'title': u''},
|
|
|
|
u'player': {u'playerid': 1, u'speed': 1}
|
|
|
|
}
|
|
|
|
Unfortunately when using Widgets, Kodi doesn't tell us shit
|
|
|
|
"""
|
2018-03-29 16:33:07 +11:00
|
|
|
self._already_slept = False
|
2018-01-11 06:14:05 +11:00
|
|
|
# Get the type of media we're playing
|
|
|
|
try:
|
|
|
|
playerid = data['player']['playerid']
|
|
|
|
except (TypeError, KeyError):
|
|
|
|
LOG.info('Aborting playback report - item invalid for updates %s',
|
|
|
|
data)
|
|
|
|
return
|
2018-08-23 23:16:23 +10:00
|
|
|
kodi_id = data['item'].get('id') if 'item' in data else None
|
|
|
|
kodi_type = data['item'].get('type') if 'item' in data else None
|
|
|
|
path = data['item'].get('file') if 'item' in data else None
|
2018-01-23 17:59:53 +11:00
|
|
|
if playerid == -1:
|
|
|
|
# Kodi might return -1 for "last player"
|
2018-08-23 23:16:23 +10:00
|
|
|
# Getting the playerid is really a PITA
|
2018-01-23 17:59:53 +11:00
|
|
|
try:
|
|
|
|
playerid = js.get_player_ids()[0]
|
|
|
|
except IndexError:
|
2018-08-23 23:16:23 +10:00
|
|
|
# E.g. Kodi 18 doesn't tell us anything useful
|
|
|
|
if kodi_type in v.KODI_VIDEOTYPES:
|
|
|
|
playlist_type = v.KODI_TYPE_VIDEO_PLAYLIST
|
|
|
|
elif kodi_type in v.KODI_AUDIOTYPES:
|
|
|
|
playlist_type = v.KODI_TYPE_AUDIO_PLAYLIST
|
|
|
|
else:
|
|
|
|
LOG.error('Unexpected type %s, data %s', kodi_type, data)
|
|
|
|
return
|
|
|
|
playerid = js.get_playlist_id(playlist_type)
|
|
|
|
if not playerid:
|
2019-02-08 23:52:33 +11:00
|
|
|
LOG.error('Coud not get playerid for data %s', data)
|
2018-08-23 23:16:23 +10:00
|
|
|
return
|
2018-01-07 01:19:12 +11:00
|
|
|
playqueue = PQ.PLAYQUEUES[playerid]
|
2018-01-11 06:14:05 +11:00
|
|
|
info = js.get_player_props(playerid)
|
2018-06-15 00:27:13 +10:00
|
|
|
if playqueue.kodi_playlist_playback:
|
|
|
|
# Kodi will tell us the wrong position - of the playlist, not the
|
|
|
|
# playqueue, when user starts playing from a playlist :-(
|
|
|
|
pos = 0
|
|
|
|
LOG.debug('Detected playback from a Kodi playlist')
|
|
|
|
else:
|
|
|
|
pos = info['position'] if info['position'] != -1 else 0
|
|
|
|
LOG.debug('Detected position %s for %s', pos, playqueue)
|
2018-11-19 00:59:17 +11:00
|
|
|
status = app.PLAYSTATE.player_states[playerid]
|
2017-12-21 19:28:06 +11:00
|
|
|
try:
|
2018-01-11 06:14:05 +11:00
|
|
|
item = playqueue.items[pos]
|
2018-12-02 21:11:24 +11:00
|
|
|
LOG.debug('PKC playqueue item is: %s', item)
|
2017-12-21 19:28:06 +11:00
|
|
|
except IndexError:
|
2018-03-15 18:24:56 +11:00
|
|
|
# PKC playqueue not yet initialized
|
2018-03-29 16:33:07 +11:00
|
|
|
LOG.debug('Position %s not in PKC playqueue yet', pos)
|
2018-03-15 18:24:56 +11:00
|
|
|
initialize = True
|
|
|
|
else:
|
2018-03-29 16:33:07 +11:00
|
|
|
if not kodi_id:
|
|
|
|
kodi_id, kodi_type, path = self._json_item(playerid)
|
|
|
|
if kodi_id and item.kodi_id:
|
|
|
|
if item.kodi_id != kodi_id or item.kodi_type != kodi_type:
|
|
|
|
LOG.debug('Detected different Kodi id')
|
2018-03-15 18:24:56 +11:00
|
|
|
initialize = True
|
|
|
|
else:
|
|
|
|
initialize = False
|
|
|
|
else:
|
|
|
|
# E.g. clips set-up previously with no Kodi DB entry
|
2018-03-29 16:33:07 +11:00
|
|
|
if not path:
|
|
|
|
kodi_id, kodi_type, path = self._json_item(playerid)
|
2018-12-02 21:11:24 +11:00
|
|
|
if path == '':
|
|
|
|
LOG.debug('Detected empty path: aborting playback report')
|
|
|
|
return
|
2018-03-29 16:33:07 +11:00
|
|
|
if item.file != path:
|
2018-12-02 21:11:24 +11:00
|
|
|
# Clips will get a new path
|
2018-03-29 16:33:07 +11:00
|
|
|
LOG.debug('Detected different path')
|
2018-12-02 21:11:24 +11:00
|
|
|
try:
|
|
|
|
tmp_plex_id = int(utils.REGEX_PLEX_ID.findall(path)[0])
|
2021-02-25 01:21:07 +11:00
|
|
|
except (IndexError, TypeError):
|
2018-12-02 21:11:24 +11:00
|
|
|
LOG.debug('No Plex id in path, need to init playqueue')
|
|
|
|
initialize = True
|
|
|
|
else:
|
|
|
|
if tmp_plex_id == item.plex_id:
|
|
|
|
LOG.debug('Detected different path for the same id')
|
|
|
|
initialize = False
|
|
|
|
else:
|
|
|
|
LOG.debug('Different Plex id, need to init playqueue')
|
|
|
|
initialize = True
|
2018-02-04 00:59:43 +11:00
|
|
|
else:
|
2018-03-15 18:24:56 +11:00
|
|
|
initialize = False
|
|
|
|
if initialize:
|
|
|
|
LOG.debug('Need to initialize Plex and PKC playqueue')
|
2018-03-29 16:33:07 +11:00
|
|
|
if not kodi_id or not kodi_type:
|
|
|
|
kodi_id, kodi_type, path = self._json_item(playerid)
|
|
|
|
plex_id, plex_type = self._get_ids(kodi_id, kodi_type, path)
|
2018-03-15 18:24:56 +11:00
|
|
|
if not plex_id:
|
|
|
|
LOG.debug('No Plex id obtained - aborting playback report')
|
2018-11-19 00:59:17 +11:00
|
|
|
app.PLAYSTATE.player_states[playerid] = copy.deepcopy(app.PLAYSTATE.template)
|
2018-03-15 18:24:56 +11:00
|
|
|
return
|
2019-08-10 20:01:23 +10:00
|
|
|
try:
|
|
|
|
item = PL.init_plex_playqueue(playqueue, plex_id=plex_id)
|
2021-09-13 19:24:06 +10:00
|
|
|
except exceptions.PlaylistError:
|
2019-08-10 20:01:23 +10:00
|
|
|
LOG.info('Could not initialize the Plex playlist')
|
|
|
|
return
|
2018-07-08 20:13:32 +10:00
|
|
|
item.file = path
|
2018-01-11 06:14:05 +11:00
|
|
|
# Set the Plex container key (e.g. using the Plex playqueue)
|
|
|
|
container_key = None
|
|
|
|
if info['playlistid'] != -1:
|
|
|
|
# -1 is Kodi's answer if there is no playlist
|
|
|
|
container_key = PQ.PLAYQUEUES[playerid].id
|
|
|
|
if container_key is not None:
|
|
|
|
container_key = '/playQueues/%s' % container_key
|
|
|
|
elif plex_id is not None:
|
|
|
|
container_key = '/library/metadata/%s' % plex_id
|
2018-02-07 23:32:10 +11:00
|
|
|
else:
|
2018-03-15 18:24:56 +11:00
|
|
|
LOG.debug('No need to initialize playqueues')
|
2018-02-07 23:32:10 +11:00
|
|
|
kodi_id = item.kodi_id
|
|
|
|
kodi_type = item.kodi_type
|
|
|
|
plex_id = item.plex_id
|
|
|
|
plex_type = item.plex_type
|
|
|
|
if playqueue.id:
|
|
|
|
container_key = '/playQueues/%s' % playqueue.id
|
|
|
|
else:
|
|
|
|
container_key = '/library/metadata/%s' % plex_id
|
2021-02-08 07:42:47 +11:00
|
|
|
# Mechanik for Plex skip intro feature
|
|
|
|
if utils.settings('enableSkipIntro') == 'true':
|
2021-09-13 21:26:04 +10:00
|
|
|
status['intro_markers'] = item.api.intro_markers()
|
2019-10-05 19:29:08 +10:00
|
|
|
# Remember the currently playing item
|
|
|
|
app.PLAYSTATE.item = item
|
2018-04-03 01:09:44 +10:00
|
|
|
# Remember that this player has been active
|
2018-11-19 00:59:17 +11:00
|
|
|
app.PLAYSTATE.active_players.add(playerid)
|
2018-01-22 04:31:49 +11:00
|
|
|
status.update(info)
|
2018-03-29 16:33:07 +11:00
|
|
|
LOG.debug('Set the Plex container_key to: %s', container_key)
|
2018-02-04 02:16:53 +11:00
|
|
|
status['container_key'] = container_key
|
2018-01-22 04:31:49 +11:00
|
|
|
status['file'] = path
|
|
|
|
status['kodi_id'] = kodi_id
|
|
|
|
status['kodi_type'] = kodi_type
|
|
|
|
status['plex_id'] = plex_id
|
|
|
|
status['plex_type'] = plex_type
|
|
|
|
status['playmethod'] = item.playmethod
|
|
|
|
status['playcount'] = item.playcount
|
2021-09-13 00:33:18 +10:00
|
|
|
status['external_player'] = app.APP.player.isExternalPlayer() == 1
|
2018-02-04 02:20:10 +11:00
|
|
|
LOG.debug('Set the player state: %s', status)
|
2021-09-04 01:42:59 +10:00
|
|
|
|
|
|
|
# Workaround for the Kodi add-on Up Next
|
2019-07-14 20:00:07 +10:00
|
|
|
if not app.SYNC.direct_paths:
|
|
|
|
_notify_upnext(item)
|
2021-09-13 00:33:18 +10:00
|
|
|
self._switched_to_plex_streams = False
|
2021-09-04 01:42:59 +10:00
|
|
|
|
2021-09-13 00:33:18 +10:00
|
|
|
def _on_av_change(self, data):
|
2021-09-04 01:42:59 +10:00
|
|
|
"""
|
|
|
|
Will be called when Kodi has a video, audio or subtitle stream. Also
|
|
|
|
happens when the stream changes.
|
2021-09-13 00:33:18 +10:00
|
|
|
|
|
|
|
Example data as returned by Kodi:
|
|
|
|
{'item': {'id': 5, 'type': 'movie'},
|
|
|
|
'player': {'playerid': 1, 'speed': 1}}
|
2021-09-19 21:38:25 +10:00
|
|
|
|
|
|
|
PICKING UP CHANGES ON SUBTITLES IS CURRENTLY BROKEN ON THE KODI SIDE!
|
|
|
|
Kodi subs will never change. Also see json_rpc.py
|
2021-09-04 01:42:59 +10:00
|
|
|
"""
|
2021-09-19 21:38:25 +10:00
|
|
|
playerid = data['player']['playerid']
|
|
|
|
if not playerid == v.KODI_VIDEO_PLAYER_ID:
|
|
|
|
# We're just messing with Kodi's videoplayer
|
|
|
|
return
|
2021-09-30 20:13:29 +10:00
|
|
|
item = app.PLAYSTATE.item
|
|
|
|
if item is None:
|
|
|
|
# Player might've quit
|
|
|
|
return
|
2021-09-13 00:33:18 +10:00
|
|
|
if not self._switched_to_plex_streams:
|
2021-09-19 21:38:25 +10:00
|
|
|
# We need to switch to the Plex streams ONCE upon playback start
|
|
|
|
# after onavchange has been fired
|
2021-09-30 20:13:29 +10:00
|
|
|
item.switch_to_plex_streams()
|
2021-09-13 00:33:18 +10:00
|
|
|
self._switched_to_plex_streams = True
|
2021-09-19 21:38:25 +10:00
|
|
|
else:
|
2021-09-30 20:13:29 +10:00
|
|
|
item.on_av_change(playerid)
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2018-01-22 21:20:37 +11:00
|
|
|
|
2018-04-09 16:13:54 +10:00
|
|
|
def _playback_cleanup(ended=False):
|
|
|
|
"""
|
|
|
|
PKC cleanup after playback ends/is stopped. Pass ended=True if Kodi
|
|
|
|
completely finished playing an item (because we will get and use wrong
|
|
|
|
timing data otherwise)
|
|
|
|
"""
|
|
|
|
LOG.debug('playback_cleanup called. Active players: %s',
|
2018-11-19 00:59:17 +11:00
|
|
|
app.PLAYSTATE.active_players)
|
2021-02-08 07:42:47 +11:00
|
|
|
if app.APP.skip_intro_dialog:
|
|
|
|
app.APP.skip_intro_dialog.close()
|
|
|
|
app.APP.skip_intro_dialog = None
|
2018-04-09 16:13:54 +10:00
|
|
|
# We might have saved a transient token from a user flinging media via
|
|
|
|
# Companion (if we could not use the playqueue to store the token)
|
2018-11-19 00:59:17 +11:00
|
|
|
app.CONN.plex_transient_token = None
|
|
|
|
for playerid in app.PLAYSTATE.active_players:
|
|
|
|
status = app.PLAYSTATE.player_states[playerid]
|
2018-04-09 16:13:54 +10:00
|
|
|
# Remember the last played item later
|
2018-11-19 00:59:17 +11:00
|
|
|
app.PLAYSTATE.old_player_states[playerid] = copy.deepcopy(status)
|
2018-04-09 16:13:54 +10:00
|
|
|
# Stop transcoding
|
2019-10-05 20:43:12 +10:00
|
|
|
if status['playmethod'] == v.PLAYBACK_METHOD_TRANSCODE:
|
2018-04-09 16:13:54 +10:00
|
|
|
LOG.debug('Tell the PMS to stop transcoding')
|
|
|
|
DU().downloadUrl(
|
|
|
|
'{server}/video/:/transcode/universal/stop',
|
|
|
|
parameters={'session': v.PKC_MACHINE_IDENTIFIER})
|
|
|
|
if playerid == 1:
|
|
|
|
# Bookmarks might not be pickup up correctly, so let's do them
|
|
|
|
# manually. Applies to addon paths, but direct paths might have
|
|
|
|
# started playback via PMS
|
|
|
|
_record_playstate(status, ended)
|
|
|
|
# Reset the player's status
|
2018-11-19 00:59:17 +11:00
|
|
|
app.PLAYSTATE.player_states[playerid] = copy.deepcopy(app.PLAYSTATE.template)
|
2018-04-09 16:13:54 +10:00
|
|
|
# As all playback has halted, reset the players that have been active
|
2018-11-19 00:59:17 +11:00
|
|
|
app.PLAYSTATE.active_players = set()
|
2019-10-05 19:29:08 +10:00
|
|
|
app.PLAYSTATE.item = None
|
2019-10-06 00:50:04 +10:00
|
|
|
utils.delete_temporary_subtitles()
|
2019-10-29 19:59:10 +11:00
|
|
|
LOG.debug('Finished PKC playback cleanup')
|
2018-04-09 16:13:54 +10:00
|
|
|
|
|
|
|
|
|
|
|
def _record_playstate(status, ended):
|
|
|
|
if not status['plex_id']:
|
|
|
|
LOG.debug('No Plex id found to record playstate for status %s', status)
|
|
|
|
return
|
2019-03-10 02:19:29 +11:00
|
|
|
if status['plex_type'] not in v.PLEX_VIDEOTYPES:
|
|
|
|
LOG.debug('Not messing with non-video entries')
|
|
|
|
return
|
2018-10-25 02:17:02 +11:00
|
|
|
with PlexDB() as plexdb:
|
2018-11-06 23:50:46 +11:00
|
|
|
db_item = plexdb.item_by_id(status['plex_id'], status['plex_type'])
|
|
|
|
if not db_item:
|
2018-04-09 16:13:54 +10:00
|
|
|
# Item not (yet) in Kodi library
|
|
|
|
LOG.debug('No playstate update due to Plex id not found: %s', status)
|
|
|
|
return
|
2018-11-19 00:59:17 +11:00
|
|
|
totaltime = float(timing.kodi_time_to_millis(status['totaltime'])) / 1000
|
2019-11-03 01:58:22 +11:00
|
|
|
if status['external_player']:
|
2020-05-03 17:36:01 +10:00
|
|
|
# video has either been entirely watched - or not.
|
|
|
|
# "ended" won't work, need a workaround
|
|
|
|
ended = _external_player_correct_plex_watch_count(db_item)
|
|
|
|
if ended:
|
|
|
|
progress = 0.99
|
|
|
|
time = v.IGNORE_SECONDS_AT_START + 1
|
|
|
|
else:
|
|
|
|
progress = 0.0
|
|
|
|
time = 0.0
|
2018-04-09 16:13:54 +10:00
|
|
|
else:
|
2019-11-03 01:58:22 +11:00
|
|
|
if ended:
|
|
|
|
progress = 0.99
|
|
|
|
time = v.IGNORE_SECONDS_AT_START + 1
|
|
|
|
else:
|
|
|
|
time = float(timing.kodi_time_to_millis(status['time'])) / 1000
|
|
|
|
try:
|
|
|
|
progress = time / totaltime
|
|
|
|
except ZeroDivisionError:
|
|
|
|
progress = 0.0
|
|
|
|
LOG.debug('Playback progress %s (%s of %s seconds)',
|
|
|
|
progress, time, totaltime)
|
2018-04-09 16:13:54 +10:00
|
|
|
playcount = status['playcount']
|
2018-11-26 17:50:38 +11:00
|
|
|
last_played = timing.kodi_now()
|
2018-04-09 16:13:54 +10:00
|
|
|
if playcount is None:
|
|
|
|
LOG.debug('playcount not found, looking it up in the Kodi DB')
|
2018-11-09 07:22:16 +11:00
|
|
|
with kodi_db.KodiVideoDB() as kodidb:
|
|
|
|
playcount = kodidb.get_playcount(db_item['kodi_fileid'])
|
2018-04-09 16:13:54 +10:00
|
|
|
playcount = 0 if playcount is None else playcount
|
|
|
|
if time < v.IGNORE_SECONDS_AT_START:
|
|
|
|
LOG.debug('Ignoring playback less than %s seconds',
|
|
|
|
v.IGNORE_SECONDS_AT_START)
|
|
|
|
# Annoying Plex bug - it'll reset an already watched video to unwatched
|
|
|
|
playcount = None
|
|
|
|
last_played = None
|
|
|
|
time = 0
|
|
|
|
elif progress >= v.MARK_PLAYED_AT:
|
|
|
|
LOG.debug('Recording entirely played video since progress > %s',
|
|
|
|
v.MARK_PLAYED_AT)
|
|
|
|
playcount += 1
|
|
|
|
time = 0
|
2018-11-09 07:22:16 +11:00
|
|
|
with kodi_db.KodiVideoDB() as kodidb:
|
|
|
|
kodidb.set_resume(db_item['kodi_fileid'],
|
|
|
|
time,
|
|
|
|
totaltime,
|
|
|
|
playcount,
|
2019-02-06 01:32:50 +11:00
|
|
|
last_played)
|
|
|
|
if 'kodi_fileid_2' in db_item and db_item['kodi_fileid_2']:
|
|
|
|
# Dirty hack for our episodes
|
|
|
|
kodidb.set_resume(db_item['kodi_fileid_2'],
|
|
|
|
time,
|
|
|
|
totaltime,
|
|
|
|
playcount,
|
|
|
|
last_played)
|
2018-04-09 16:13:54 +10:00
|
|
|
# Hack to force "in progress" widget to appear if it wasn't visible before
|
2018-11-19 00:59:17 +11:00
|
|
|
if (app.APP.force_reload_skin and
|
2018-04-09 16:13:54 +10:00
|
|
|
xbmc.getCondVisibility('Window.IsVisible(Home.xml)')):
|
|
|
|
LOG.debug('Refreshing skin to update widgets')
|
|
|
|
xbmc.executebuiltin('ReloadSkin()')
|
2018-11-26 17:56:27 +11:00
|
|
|
task = backgroundthread.FunctionAsTask(_clean_file_table, None)
|
2018-11-26 17:58:12 +11:00
|
|
|
backgroundthread.BGThreader.addTasksToFront([task])
|
2018-04-09 16:13:54 +10:00
|
|
|
|
|
|
|
|
2019-11-03 01:58:22 +11:00
|
|
|
def _external_player_correct_plex_watch_count(db_item):
|
|
|
|
"""
|
|
|
|
Kodi won't safe playstate at all for external players
|
|
|
|
|
|
|
|
There's currently no way to get a resumpoint if an external player is
|
|
|
|
in use We are just checking whether we should mark video as
|
|
|
|
completely watched or completely unwatched (according to
|
|
|
|
playcountminimumtime set in playercorefactory.xml)
|
|
|
|
See https://kodi.wiki/view/External_players
|
|
|
|
"""
|
|
|
|
with kodi_db.KodiVideoDB() as kodidb:
|
|
|
|
playcount = kodidb.get_playcount(db_item['kodi_fileid'])
|
|
|
|
LOG.debug('External player detected. Playcount: %s', playcount)
|
|
|
|
PF.scrobble(db_item['plex_id'], 'watched' if playcount else 'unwatched')
|
2020-05-03 17:36:01 +10:00
|
|
|
return True if playcount else False
|
2019-11-03 01:58:22 +11:00
|
|
|
|
|
|
|
|
2018-04-09 16:13:54 +10:00
|
|
|
def _clean_file_table():
|
|
|
|
"""
|
|
|
|
If we associate a playing video e.g. pointing to plugin://... to an existing
|
|
|
|
Kodi library item, Kodi will add an additional entry for this (additional)
|
|
|
|
path plugin:// in the file table. This leads to all sorts of wierd behavior.
|
|
|
|
This function tries for at most 5 seconds to clean the file table.
|
|
|
|
"""
|
|
|
|
LOG.debug('Start cleaning Kodi files table')
|
2019-11-01 23:15:34 +11:00
|
|
|
if app.APP.monitor.waitForAbort(2):
|
|
|
|
# PKC should exit
|
|
|
|
return
|
2018-11-24 19:56:30 +11:00
|
|
|
try:
|
2019-11-01 23:15:34 +11:00
|
|
|
with kodi_db.KodiVideoDB() as kodidb:
|
|
|
|
obsolete_file_ids = list(kodidb.obsolete_file_ids())
|
|
|
|
for file_id in obsolete_file_ids:
|
|
|
|
LOG.debug('Removing obsolete Kodi file_id %s', file_id)
|
|
|
|
kodidb.remove_file(file_id, remove_orphans=False)
|
2018-11-24 19:56:30 +11:00
|
|
|
except utils.OperationalError:
|
|
|
|
LOG.debug('Database was locked, unable to clean file table')
|
|
|
|
else:
|
|
|
|
LOG.debug('Done cleaning up Kodi file table')
|
2019-02-22 17:51:27 +11:00
|
|
|
|
|
|
|
|
2019-07-14 20:00:07 +10:00
|
|
|
def _next_episode(current_api):
|
|
|
|
"""
|
|
|
|
Returns the xml for the next episode after the current one
|
|
|
|
Returns None if something went wrong or there is no next episode
|
|
|
|
"""
|
|
|
|
xml = PF.show_episodes(current_api.grandparent_id())
|
|
|
|
if xml is None:
|
|
|
|
return
|
|
|
|
for counter, episode in enumerate(xml):
|
|
|
|
api = API(episode)
|
|
|
|
if api.plex_id == current_api.plex_id:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
LOG.error('Did not find the episode with Plex id %s for show %s: %s',
|
|
|
|
current_api.plex_id, current_api.grandparent_id(),
|
|
|
|
current_api.grandparent_title())
|
|
|
|
return
|
|
|
|
try:
|
2021-09-13 21:26:04 +10:00
|
|
|
return API(xml[counter + 1])
|
2019-07-14 20:00:07 +10:00
|
|
|
except IndexError:
|
|
|
|
# Was the last episode
|
2021-09-13 21:26:04 +10:00
|
|
|
pass
|
2019-07-14 20:00:07 +10:00
|
|
|
|
|
|
|
|
|
|
|
def _complete_artwork_keys(info):
|
|
|
|
"""
|
|
|
|
Make sure that the minimum set of keys is present in the info dict
|
|
|
|
"""
|
|
|
|
for key in ('tvshow.poster',
|
|
|
|
'tvshow.fanart',
|
|
|
|
'tvshow.landscape',
|
|
|
|
'tvshow.clearart',
|
|
|
|
'tvshow.clearlogo',
|
|
|
|
'thumb'):
|
|
|
|
if key not in info['art']:
|
|
|
|
info['art'][key] = ''
|
|
|
|
|
|
|
|
|
|
|
|
def _notify_upnext(item):
|
|
|
|
"""
|
|
|
|
Signals to the Kodi add-on Upnext that there is another episode after this
|
|
|
|
one.
|
|
|
|
Needed for add-on paths in order to prevent crashes when Upnext does this
|
|
|
|
by itself
|
|
|
|
"""
|
|
|
|
if not item.plex_type == v.PLEX_TYPE_EPISODE:
|
|
|
|
return
|
2021-09-13 21:26:04 +10:00
|
|
|
this_api = item.api
|
2019-07-14 20:00:07 +10:00
|
|
|
next_api = _next_episode(this_api)
|
|
|
|
if next_api is None:
|
|
|
|
return
|
|
|
|
info = {}
|
|
|
|
for key, api in (('current_episode', this_api),
|
|
|
|
('next_episode', next_api)):
|
|
|
|
info[key] = {
|
|
|
|
'episodeid': api.plex_id,
|
|
|
|
'tvshowid': api.grandparent_id(),
|
|
|
|
'title': api.title(),
|
|
|
|
'showtitle': api.grandparent_title(),
|
|
|
|
'plot': api.plot(),
|
|
|
|
'playcount': api.viewcount(),
|
|
|
|
'season': api.season_number(),
|
|
|
|
'episode': api.index(),
|
|
|
|
'firstaired': api.year(),
|
|
|
|
'rating': api.rating(),
|
|
|
|
'art': api.artwork(kodi_id=api.kodi_id,
|
|
|
|
kodi_type=api.kodi_type,
|
|
|
|
full_artwork=True)
|
|
|
|
}
|
|
|
|
_complete_artwork_keys(info[key])
|
2020-02-27 04:10:15 +11:00
|
|
|
info['play_info'] = {'handle': next_api.fullpath(force_addon=True)[0]}
|
2020-12-20 06:43:08 +11:00
|
|
|
sender = v.ADDON_ID
|
|
|
|
method = 'upnext_data'
|
2021-02-10 04:03:17 +11:00
|
|
|
data = binascii.hexlify(json.dumps(info).encode('utf-8'))
|
2019-07-14 20:00:07 +10:00
|
|
|
data = '\\"[\\"{0}\\"]\\"'.format(data)
|
2020-12-20 06:43:08 +11:00
|
|
|
xbmc.executebuiltin(f'NotifyAll({sender}, {method}, {data})')
|
2019-07-14 20:00:07 +10:00
|
|
|
|
|
|
|
|
2019-10-05 18:50:55 +10:00
|
|
|
def _videolibrary_onupdate(data):
|
|
|
|
"""
|
|
|
|
A specific Kodi library item has been updated. This seems to happen if the
|
|
|
|
user marks an item as watched/unwatched or if playback of the item just
|
|
|
|
stopped
|
2021-09-09 21:55:20 +10:00
|
|
|
|
|
|
|
2 kinds of messages possible, e.g.
|
|
|
|
Method: VideoLibrary.OnUpdate Data: ("Reset resume position" and also
|
|
|
|
fired just after stopping playback - BEFORE OnStop fires)
|
|
|
|
{'id': 1, 'type': 'movie'}
|
|
|
|
Method: VideoLibrary.OnUpdate Data: ("Mark as watched")
|
|
|
|
{'item': {'id': 1, 'type': 'movie'}, 'playcount': 1}
|
2019-10-05 18:50:55 +10:00
|
|
|
"""
|
2021-09-09 21:55:20 +10:00
|
|
|
item = data.get('item') if 'item' in data else data
|
2019-10-05 18:50:55 +10:00
|
|
|
try:
|
|
|
|
kodi_id = item['id']
|
|
|
|
kodi_type = item['type']
|
|
|
|
except (KeyError, TypeError):
|
2021-09-09 21:55:20 +10:00
|
|
|
LOG.debug("Item is invalid for a Plex playstate update")
|
2019-10-05 18:50:55 +10:00
|
|
|
return
|
2021-09-09 21:55:20 +10:00
|
|
|
playcount = data.get('playcount')
|
|
|
|
if playcount is None:
|
|
|
|
# "Reset resume position"
|
|
|
|
# Kodi might set as watched or unwatched!
|
|
|
|
with KodiVideoDB(lock=False) as kodidb:
|
|
|
|
file_id = kodidb.file_id_from_id(kodi_id, kodi_type)
|
|
|
|
if file_id is None:
|
|
|
|
return
|
|
|
|
if kodidb.get_resume(file_id):
|
|
|
|
# We do have an existing bookmark entry - not toggling to
|
|
|
|
# either watched or unwatched on the Plex side
|
|
|
|
return
|
|
|
|
playcount = kodidb.get_playcount(file_id) or 0
|
2019-10-05 18:50:55 +10:00
|
|
|
if app.PLAYSTATE.item and kodi_id == app.PLAYSTATE.item.kodi_id and \
|
|
|
|
kodi_type == app.PLAYSTATE.item.kodi_type:
|
|
|
|
# Kodi updates an item immediately after playback. Hence we do NOT
|
|
|
|
# increase or decrease the viewcount
|
|
|
|
return
|
|
|
|
# Send notification to the server.
|
|
|
|
with PlexDB(lock=False) as plexdb:
|
|
|
|
db_item = plexdb.item_by_kodi_id(kodi_id, kodi_type)
|
|
|
|
if not db_item:
|
|
|
|
LOG.error("Could not find plex_id in plex database for a "
|
|
|
|
"video library update")
|
|
|
|
return
|
|
|
|
# notify the server
|
|
|
|
if playcount > 0:
|
|
|
|
PF.scrobble(db_item['plex_id'], 'watched')
|
|
|
|
else:
|
|
|
|
PF.scrobble(db_item['plex_id'], 'unwatched')
|