2018-07-13 02:46:02 +10:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2017-12-09 05:43:06 +11:00
|
|
|
"""
|
|
|
|
Monitors the Kodi playqueue and adjusts the Plex playqueue accordingly
|
|
|
|
"""
|
2018-07-13 02:46:02 +10:00
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2017-12-10 00:35:08 +11:00
|
|
|
from logging import getLogger
|
2019-02-23 18:53:37 +11:00
|
|
|
import copy
|
|
|
|
|
2019-05-26 19:26:14 +10:00
|
|
|
from .common import PlayqueueError, PlaylistItem, PLAYQUEUES
|
2019-05-26 04:49:29 +10:00
|
|
|
from .. import backgroundthread, json_rpc as js, utils, app
|
2016-12-28 03:33:52 +11:00
|
|
|
|
|
|
|
|
2019-05-26 04:49:29 +10:00
|
|
|
LOG = getLogger('PLEX.playqueue_monitor')
|
2016-12-29 05:38:43 +11:00
|
|
|
|
2018-01-07 01:19:12 +11:00
|
|
|
|
2018-11-19 00:59:17 +11:00
|
|
|
class PlayqueueMonitor(backgroundthread.KillableThread):
|
2018-02-04 22:06:39 +11:00
|
|
|
"""
|
|
|
|
Unfortunately, Kodi does not tell if items within a Kodi playqueue
|
|
|
|
(playlist) are swapped. This is what this monitor is for. Don't replace
|
|
|
|
this mechanism till Kodi's implementation of playlists has improved
|
|
|
|
"""
|
2019-02-23 18:53:37 +11:00
|
|
|
def _compare_playqueues(self, playqueue, new_kodi_playqueue):
|
2018-02-04 22:06:39 +11:00
|
|
|
"""
|
|
|
|
Used to poll the Kodi playqueue and update the Plex playqueue if needed
|
|
|
|
"""
|
|
|
|
old = list(playqueue.items)
|
2019-02-23 18:53:37 +11:00
|
|
|
# We might append to new_kodi_playqueue but will need the original
|
|
|
|
# still back in the main loop
|
|
|
|
new = copy.deepcopy(new_kodi_playqueue)
|
2018-02-04 22:06:39 +11:00
|
|
|
index = list(range(0, len(old)))
|
|
|
|
LOG.debug('Comparing new Kodi playqueue %s with our play queue %s',
|
|
|
|
new, old)
|
|
|
|
for i, new_item in enumerate(new):
|
|
|
|
if (new_item['file'].startswith('plugin://') and
|
|
|
|
not new_item['file'].startswith(PLUGIN)):
|
|
|
|
# Ignore new media added by other addons
|
|
|
|
continue
|
|
|
|
for j, old_item in enumerate(old):
|
2018-11-19 00:59:17 +11:00
|
|
|
if self.isCanceled():
|
2018-02-04 22:06:39 +11:00
|
|
|
# Chances are that we got an empty Kodi playlist due to
|
|
|
|
# Kodi exit
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
if (old_item.file.startswith('plugin://') and
|
|
|
|
not old_item.file.startswith(PLUGIN)):
|
|
|
|
# Ignore media by other addons
|
|
|
|
continue
|
|
|
|
except AttributeError:
|
|
|
|
# were not passed a filename; ignore
|
|
|
|
pass
|
2018-02-08 00:57:36 +11:00
|
|
|
if 'id' in new_item:
|
2018-02-04 22:06:39 +11:00
|
|
|
identical = (old_item.kodi_id == new_item['id'] and
|
|
|
|
old_item.kodi_type == new_item['type'])
|
2018-02-08 00:57:36 +11:00
|
|
|
else:
|
|
|
|
try:
|
2018-11-23 19:05:51 +11:00
|
|
|
plex_id = int(utils.REGEX_PLEX_ID.findall(new_item['file'])[0])
|
2018-02-08 00:57:36 +11:00
|
|
|
except IndexError:
|
|
|
|
LOG.debug('Comparing paths directly as a fallback')
|
|
|
|
identical = old_item.file == new_item['file']
|
|
|
|
else:
|
|
|
|
identical = plex_id == old_item.plex_id
|
2018-02-04 22:06:39 +11:00
|
|
|
if j == 0 and identical:
|
|
|
|
del old[j], index[j]
|
|
|
|
break
|
|
|
|
elif identical:
|
2018-07-08 19:08:30 +10:00
|
|
|
LOG.debug('Playqueue item %s moved to position %s',
|
2018-02-04 22:06:39 +11:00
|
|
|
i + j, i)
|
2018-07-08 19:08:30 +10:00
|
|
|
try:
|
2019-05-26 04:49:29 +10:00
|
|
|
playqueue.plex_move_item(i + j, i)
|
2019-05-26 19:26:14 +10:00
|
|
|
except PlayqueueError:
|
2018-07-08 19:08:30 +10:00
|
|
|
LOG.error('Could not modify playqueue positions')
|
|
|
|
LOG.error('This is likely caused by mixing audio and '
|
|
|
|
'video tracks in the Kodi playqueue')
|
2018-02-04 22:06:39 +11:00
|
|
|
del old[j], index[j]
|
|
|
|
break
|
|
|
|
else:
|
2019-05-26 04:49:29 +10:00
|
|
|
playlistitem = PlaylistItem(kodi_item=new_item)
|
2018-02-04 22:06:39 +11:00
|
|
|
LOG.debug('Detected new Kodi element at position %s: %s ',
|
2019-05-26 04:49:29 +10:00
|
|
|
i, playlistitem)
|
2018-06-03 01:31:57 +10:00
|
|
|
try:
|
|
|
|
if playqueue.id is None:
|
2019-05-26 04:49:29 +10:00
|
|
|
playqueue.init(playlistitem)
|
2018-02-21 17:59:19 +11:00
|
|
|
else:
|
2019-05-26 04:49:29 +10:00
|
|
|
playqueue.plex_add_item(playlistitem, i)
|
2019-05-26 19:26:14 +10:00
|
|
|
except PlayqueueError:
|
2019-05-26 04:49:29 +10:00
|
|
|
LOG.warn('Couldnt add new item to Plex: %s', playlistitem)
|
2018-06-03 01:31:57 +10:00
|
|
|
except IndexError:
|
|
|
|
# This is really a hack - happens when using Addon Paths
|
|
|
|
# and repeatedly starting the same element. Kodi will then
|
|
|
|
# not pass kodi id nor file path AND will also not
|
|
|
|
# start-up playback. Hence kodimonitor kicks off playback.
|
|
|
|
# Also see kodimonitor.py - _playlist_onadd()
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
for j in range(i, len(index)):
|
|
|
|
index[j] += 1
|
2018-02-04 22:06:39 +11:00
|
|
|
for i in reversed(index):
|
2018-11-19 00:59:17 +11:00
|
|
|
if self.isCanceled():
|
2018-02-04 22:17:05 +11:00
|
|
|
# Chances are that we got an empty Kodi playlist due to
|
|
|
|
# Kodi exit
|
|
|
|
return
|
2018-02-04 22:06:39 +11:00
|
|
|
LOG.debug('Detected deletion of playqueue element at pos %s', i)
|
2018-07-08 19:08:30 +10:00
|
|
|
try:
|
2019-05-26 04:49:29 +10:00
|
|
|
playqueue.plex_remove_item(i)
|
2019-05-26 19:26:14 +10:00
|
|
|
except PlayqueueError:
|
2018-07-08 19:08:30 +10:00
|
|
|
LOG.error('Could not delete PMS element from position %s', i)
|
|
|
|
LOG.error('This is likely caused by mixing audio and '
|
|
|
|
'video tracks in the Kodi playqueue')
|
2018-02-04 22:06:39 +11:00
|
|
|
LOG.debug('Done comparing playqueues')
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
LOG.info("----===## Starting PlayqueueMonitor ##===----")
|
2019-01-31 06:36:52 +11:00
|
|
|
app.APP.register_thread(self)
|
|
|
|
try:
|
|
|
|
self._run()
|
|
|
|
finally:
|
|
|
|
app.APP.deregister_thread(self)
|
|
|
|
LOG.info("----===## PlayqueueMonitor stopped ##===----")
|
|
|
|
|
|
|
|
def _run(self):
|
2018-11-19 00:59:17 +11:00
|
|
|
while not self.isCanceled():
|
2019-01-31 06:36:52 +11:00
|
|
|
if self.wait_while_suspended():
|
|
|
|
return
|
2018-11-19 00:59:17 +11:00
|
|
|
with app.APP.lock_playqueues:
|
2018-04-11 03:23:37 +10:00
|
|
|
for playqueue in PLAYQUEUES:
|
|
|
|
kodi_pl = js.playlist_get_items(playqueue.playlistid)
|
2019-04-29 02:03:20 +10:00
|
|
|
playqueue.old_kodi_pl = list(kodi_pl)
|
|
|
|
continue
|
2018-04-11 03:23:37 +10:00
|
|
|
if playqueue.old_kodi_pl != kodi_pl:
|
2018-11-21 04:39:18 +11:00
|
|
|
if playqueue.id is None and (not app.SYNC.direct_paths or
|
2018-11-19 00:59:17 +11:00
|
|
|
app.PLAYSTATE.context_menu_play):
|
2018-04-11 03:23:37 +10:00
|
|
|
# Only initialize if directly fired up using direct
|
|
|
|
# paths. Otherwise let default.py do its magic
|
|
|
|
LOG.debug('Not yet initiating playback')
|
|
|
|
else:
|
2018-06-03 01:31:57 +10:00
|
|
|
# compare old and new playqueue
|
|
|
|
self._compare_playqueues(playqueue, kodi_pl)
|
2018-11-21 02:58:25 +11:00
|
|
|
app.APP.monitor.waitForAbort(0.2)
|