2018-07-12 05:24:27 +10:00
|
|
|
#!/usr/bin/env python
|
2018-03-07 06:40:30 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-12-09 05:43:06 +11:00
|
|
|
"""
|
|
|
|
Collection of functions associated with Kodi and Plex playlists and playqueues
|
|
|
|
"""
|
2017-12-10 00:35:08 +11:00
|
|
|
from logging import getLogger
|
2016-12-28 03:33:52 +11:00
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
from .plex_api import API
|
2018-10-25 02:17:02 +11:00
|
|
|
from .plex_db import PlexDB
|
2018-06-22 03:24:37 +10:00
|
|
|
from . import plex_functions as PF
|
2018-11-09 07:22:16 +11:00
|
|
|
from .kodi_db import kodiid_from_filename
|
2018-06-22 03:24:37 +10:00
|
|
|
from .downloadutils import DownloadUtils as DU
|
|
|
|
from . import utils
|
2021-09-19 21:38:25 +10:00
|
|
|
from .utils import cast
|
2018-06-22 03:24:37 +10:00
|
|
|
from . import json_rpc as js
|
|
|
|
from . import variables as v
|
2018-11-19 00:59:17 +11:00
|
|
|
from . import app
|
2021-09-13 19:24:06 +10:00
|
|
|
from .exceptions import PlaylistError
|
2021-09-04 01:42:59 +10:00
|
|
|
from .subtitles import accessible_plex_subtitles
|
2016-12-28 03:33:52 +11:00
|
|
|
|
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
LOG = getLogger('PLEX.playlist_func')
|
2016-12-28 03:33:52 +11:00
|
|
|
|
2018-01-30 17:50:44 +11:00
|
|
|
|
2019-08-09 23:40:39 +10:00
|
|
|
class PlaylistItem(object):
|
2017-12-06 21:40:27 +11:00
|
|
|
"""
|
|
|
|
Object to fill our playqueues and playlists with.
|
|
|
|
|
2018-11-07 05:01:56 +11:00
|
|
|
id = None [int] Plex playlist/playqueue id, e.g. playQueueItemID
|
|
|
|
plex_id = None [int] Plex unique item id, "ratingKey"
|
2017-12-07 04:05:01 +11:00
|
|
|
plex_type = None [str] Plex type, e.g. 'movie', 'clip'
|
2018-11-07 05:01:56 +11:00
|
|
|
kodi_id = None [int] Kodi unique kodi id (unique only within type!)
|
2017-12-07 04:05:01 +11:00
|
|
|
kodi_type = None [str] Kodi type: 'movie'
|
|
|
|
file = None [str] Path to the item's file. STRING!!
|
2019-08-09 21:40:18 +10:00
|
|
|
uri = None [str] PMS path to item; will be auto-set with plex_id
|
2017-12-07 04:05:01 +11:00
|
|
|
guid = None [str] Weird Plex guid
|
2021-09-13 21:26:04 +10:00
|
|
|
api = None [API] API of xml 1 lvl below <MediaContainer>
|
2019-08-17 21:00:02 +10:00
|
|
|
playmethod = None [str] either 'DirectPath', 'DirectStream', 'Transcode'
|
2018-01-22 04:31:49 +11:00
|
|
|
playcount = None [int] how many times the item has already been played
|
|
|
|
offset = None [int] the item's view offset UPON START in Plex time
|
2018-01-11 06:14:05 +11:00
|
|
|
part = 0 [int] part number if Plex video consists of mult. parts
|
2018-02-03 22:45:48 +11:00
|
|
|
force_transcode [bool] defaults to False
|
2017-12-06 21:40:27 +11:00
|
|
|
"""
|
2021-09-13 21:26:04 +10:00
|
|
|
|
2017-12-21 19:28:06 +11:00
|
|
|
def __init__(self):
|
2019-08-10 04:39:04 +10:00
|
|
|
self.id = None
|
2018-11-07 05:01:56 +11:00
|
|
|
self._plex_id = None
|
2017-12-21 19:28:06 +11:00
|
|
|
self.plex_type = None
|
2019-08-10 04:39:04 +10:00
|
|
|
self.kodi_id = None
|
2017-12-21 19:28:06 +11:00
|
|
|
self.kodi_type = None
|
|
|
|
self.file = None
|
2019-08-09 21:40:18 +10:00
|
|
|
self._uri = None
|
2017-12-21 19:28:06 +11:00
|
|
|
self.guid = None
|
2021-09-13 21:26:04 +10:00
|
|
|
self.api = None
|
2018-01-08 03:50:30 +11:00
|
|
|
self.playmethod = None
|
2019-08-10 04:39:04 +10:00
|
|
|
self.playcount = None
|
|
|
|
self.offset = None
|
2021-11-06 03:38:37 +11:00
|
|
|
self.playerid = None
|
2019-08-25 21:46:47 +10:00
|
|
|
# Transcoding quality, if needed
|
|
|
|
self.quality = None
|
2018-01-11 06:14:05 +11:00
|
|
|
# If Plex video consists of several parts; part number
|
2019-08-10 04:39:04 +10:00
|
|
|
self.part = 0
|
2018-02-03 22:45:48 +11:00
|
|
|
self.force_transcode = False
|
2019-06-02 00:00:47 +10:00
|
|
|
# Shall we ask user to resume this item?
|
|
|
|
# None: ask user to resume
|
|
|
|
# False: do NOT resume, don't ask user
|
|
|
|
# True: do resume, don't ask user
|
|
|
|
self.resume = None
|
2021-09-19 21:38:25 +10:00
|
|
|
# Get the Plex audio and subtitle streams in the same order as Kodi
|
|
|
|
# uses them (Kodi uses indexes to activate them, not ids like Plex)
|
|
|
|
self._streams_have_been_processed = False
|
2021-10-20 23:55:39 +11:00
|
|
|
self._video_streams = None
|
2021-09-19 21:38:25 +10:00
|
|
|
self._audio_streams = None
|
|
|
|
self._subtitle_streams = None
|
|
|
|
# Which Kodi streams are active?
|
2021-11-06 03:38:37 +11:00
|
|
|
self._current_kodi_video_stream = None
|
|
|
|
self._current_kodi_audio_stream = None
|
|
|
|
# Kodi subs can be turned on/off additionally!
|
|
|
|
self._current_kodi_sub_stream = None
|
|
|
|
self._current_kodi_sub_stream_enabled = None
|
|
|
|
self.streams_initialized = False
|
2017-12-08 03:25:24 +11:00
|
|
|
|
2018-11-07 05:01:56 +11:00
|
|
|
@property
|
|
|
|
def plex_id(self):
|
|
|
|
return self._plex_id
|
|
|
|
|
|
|
|
@plex_id.setter
|
|
|
|
def plex_id(self, value):
|
|
|
|
self._plex_id = value
|
2019-08-09 21:40:18 +10:00
|
|
|
self._uri = ('server://%s/com.plexapp.plugins.library/library/metadata/%s' %
|
|
|
|
(app.CONN.machine_identifier, value))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def uri(self):
|
|
|
|
return self._uri
|
2018-11-07 05:01:56 +11:00
|
|
|
|
2021-10-20 23:55:39 +11:00
|
|
|
@property
|
|
|
|
def video_streams(self):
|
|
|
|
if not self._streams_have_been_processed:
|
|
|
|
self._process_streams()
|
|
|
|
return self._video_streams
|
|
|
|
|
2021-09-19 21:38:25 +10:00
|
|
|
@property
|
|
|
|
def audio_streams(self):
|
|
|
|
if not self._streams_have_been_processed:
|
|
|
|
self._process_streams()
|
|
|
|
return self._audio_streams
|
|
|
|
|
|
|
|
@property
|
|
|
|
def subtitle_streams(self):
|
|
|
|
if not self._streams_have_been_processed:
|
|
|
|
self._process_streams()
|
|
|
|
return self._subtitle_streams
|
|
|
|
|
2021-11-06 03:38:37 +11:00
|
|
|
@property
|
|
|
|
def current_kodi_video_stream(self):
|
|
|
|
return self._current_kodi_video_stream
|
|
|
|
|
|
|
|
@current_kodi_video_stream.setter
|
|
|
|
def current_kodi_video_stream(self, value):
|
|
|
|
if value != self._current_kodi_video_stream:
|
|
|
|
self.on_kodi_video_stream_change(value)
|
|
|
|
self._current_kodi_video_stream = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_kodi_audio_stream(self):
|
|
|
|
return self._current_kodi_audio_stream
|
|
|
|
|
|
|
|
@current_kodi_audio_stream.setter
|
|
|
|
def current_kodi_audio_stream(self, value):
|
|
|
|
if value != self._current_kodi_audio_stream:
|
|
|
|
self.on_kodi_audio_stream_change(value)
|
|
|
|
self._current_kodi_audio_stream = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_kodi_sub_stream_enabled(self):
|
|
|
|
return self._current_kodi_sub_stream_enabled
|
|
|
|
|
|
|
|
@current_kodi_sub_stream_enabled.setter
|
|
|
|
def current_kodi_sub_stream_enabled(self, value):
|
|
|
|
if value != self._current_kodi_sub_stream_enabled:
|
|
|
|
self.on_kodi_subtitle_stream_change(self.current_kodi_sub_stream,
|
|
|
|
value)
|
|
|
|
self._current_kodi_sub_stream_enabled = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_kodi_sub_stream(self):
|
|
|
|
return self._current_kodi_sub_stream
|
|
|
|
|
|
|
|
@current_kodi_sub_stream.setter
|
|
|
|
def current_kodi_sub_stream(self, value):
|
|
|
|
if value != self._current_kodi_sub_stream:
|
|
|
|
self.on_kodi_subtitle_stream_change(value,
|
|
|
|
self.current_kodi_sub_stream_enabled)
|
|
|
|
self._current_kodi_sub_stream = value
|
|
|
|
|
2021-10-20 23:55:39 +11:00
|
|
|
@property
|
|
|
|
def current_plex_video_stream(self):
|
|
|
|
return self.plex_stream_index(self.current_kodi_video_stream, 'video')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_plex_audio_stream(self):
|
|
|
|
return self.plex_stream_index(self.current_kodi_audio_stream, 'audio')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_plex_sub_stream(self):
|
|
|
|
return self.plex_stream_index(self.current_kodi_sub_stream, 'subtitle')
|
|
|
|
|
2020-12-19 05:59:33 +11:00
|
|
|
def __repr__(self):
|
2019-08-10 04:39:04 +10:00
|
|
|
return ("{{"
|
2018-08-05 22:44:24 +10:00
|
|
|
"'id': {self.id}, "
|
|
|
|
"'plex_id': {self.plex_id}, "
|
|
|
|
"'plex_type': '{self.plex_type}', "
|
|
|
|
"'kodi_id': {self.kodi_id}, "
|
|
|
|
"'kodi_type': '{self.kodi_type}', "
|
|
|
|
"'file': '{self.file}', "
|
|
|
|
"'guid': '{self.guid}', "
|
|
|
|
"'playmethod': '{self.playmethod}', "
|
|
|
|
"'playcount': {self.playcount}, "
|
2019-10-26 02:06:50 +11:00
|
|
|
"'resume': {self.resume},"
|
2018-08-05 22:44:24 +10:00
|
|
|
"'offset': {self.offset}, "
|
|
|
|
"'force_transcode': {self.force_transcode}, "
|
2021-12-05 21:54:40 +11:00
|
|
|
"'part': {self.part}}}".format(self=self))
|
2018-08-05 22:44:24 +10:00
|
|
|
|
2021-09-19 21:38:25 +10:00
|
|
|
def _process_streams(self):
|
|
|
|
"""
|
|
|
|
Builds audio and subtitle streams and enables matching between Plex
|
|
|
|
and Kodi using self.audio_streams and self.subtitle_streams
|
|
|
|
"""
|
|
|
|
# The playqueue response from the PMS does not contain a stream filename
|
|
|
|
# thanks Plex
|
|
|
|
self._subtitle_streams = accessible_plex_subtitles(
|
|
|
|
self.playmethod,
|
|
|
|
self.file,
|
|
|
|
self.api.plex_media_streams())
|
|
|
|
# Audio streams are much easier - they're always available and sorted
|
|
|
|
# the same in Kodi and Plex
|
|
|
|
self._audio_streams = [x for x in self.api.plex_media_streams()
|
|
|
|
if x.get('streamType') == '2']
|
2021-10-20 23:55:39 +11:00
|
|
|
# Same for video streams
|
|
|
|
self._video_streams = [x for x in self.api.plex_media_streams()
|
|
|
|
if x.get('streamType') == '1']
|
|
|
|
if len(self._video_streams) == 1:
|
|
|
|
# Add a selected = "1" attribute to let our logic stand!
|
|
|
|
# Missing if there is only 1 video stream present
|
|
|
|
self._video_streams[0].set('selected', '1')
|
2021-09-19 21:38:25 +10:00
|
|
|
self._streams_have_been_processed = True
|
|
|
|
|
|
|
|
def _get_iterator(self, stream_type):
|
|
|
|
if stream_type == 'audio':
|
|
|
|
return self.audio_streams
|
|
|
|
elif stream_type == 'subtitle':
|
|
|
|
return self.subtitle_streams
|
2021-10-20 23:55:39 +11:00
|
|
|
elif stream_type == 'video':
|
|
|
|
return self.video_streams
|
|
|
|
|
2021-11-06 03:38:37 +11:00
|
|
|
def _current_index(self, stream_type):
|
2021-10-31 21:04:31 +11:00
|
|
|
"""
|
|
|
|
Kodi might tell us the wrong index for any stream after playback start
|
|
|
|
Get the correct one!
|
|
|
|
"""
|
|
|
|
function = {
|
|
|
|
'audio': js.get_current_audio_stream_index,
|
|
|
|
'video': js.get_current_video_stream_index,
|
|
|
|
'subtitle': js.get_current_subtitle_stream_index
|
|
|
|
}[stream_type]
|
|
|
|
i = 0
|
|
|
|
while i < 30:
|
|
|
|
# Really annoying: Kodi might return wrong results directly after
|
|
|
|
# playback startup, e.g. a Kodi audio index of 1953718901 (!)
|
|
|
|
try:
|
2021-11-06 03:38:37 +11:00
|
|
|
index = function(self.playerid)
|
2021-11-19 18:47:32 +11:00
|
|
|
except (TypeError, IndexError, KeyError):
|
2021-10-31 21:04:31 +11:00
|
|
|
# No sensible reply yet
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if index != 1953718901:
|
|
|
|
# Correct result!
|
|
|
|
return index
|
|
|
|
i += 1
|
|
|
|
app.APP.monitor.waitForAbort(0.1)
|
|
|
|
else:
|
|
|
|
raise RuntimeError('Kodi did not tell us the correct index for %s'
|
|
|
|
% stream_type)
|
|
|
|
|
2021-11-06 03:38:37 +11:00
|
|
|
def init_streams(self):
|
2021-10-20 23:55:39 +11:00
|
|
|
"""
|
|
|
|
Initializes all streams after Kodi has started playing this video
|
2021-11-25 18:34:23 +11:00
|
|
|
WARNING: KODI TAKES FOREVER TO INITIALIZE STREAMS AFTER PLAYBACK
|
|
|
|
STARTUP. YOU WONT GET THE CORRECT NUMBER OFAUDIO AND SUB STREAMS RIGHT
|
|
|
|
AFTER STARTUP. Seems like you need to wait a couple of seconds
|
2021-10-20 23:55:39 +11:00
|
|
|
"""
|
2021-11-06 05:12:41 +11:00
|
|
|
if not app.PLAYSTATE.item == self:
|
|
|
|
# Already stopped playback or skipped to the next one
|
2021-11-25 18:34:23 +11:00
|
|
|
LOG.warn('Skipping init_streams!')
|
2021-11-06 05:12:41 +11:00
|
|
|
return
|
2021-11-06 03:38:37 +11:00
|
|
|
self.init_kodi_streams()
|
|
|
|
self.switch_to_plex_stream('video')
|
|
|
|
if utils.settings('audioStreamPick') == '0':
|
|
|
|
self.switch_to_plex_stream('audio')
|
|
|
|
if utils.settings('subtitleStreamPick') == '0':
|
|
|
|
self.switch_to_plex_stream('subtitle')
|
|
|
|
self.streams_initialized = True
|
|
|
|
|
|
|
|
def init_kodi_streams(self):
|
|
|
|
self._current_kodi_video_stream = self._current_index('video')
|
|
|
|
self._current_kodi_audio_stream = self._current_index('audio')
|
|
|
|
self._current_kodi_sub_stream_enabled = js.get_subtitle_enabled(self.playerid)
|
|
|
|
self._current_kodi_sub_stream = self._current_index('subtitle')
|
2021-09-19 21:38:25 +10:00
|
|
|
|
2017-12-14 20:22:48 +11:00
|
|
|
def plex_stream_index(self, kodi_stream_index, stream_type):
|
|
|
|
"""
|
|
|
|
Pass in the kodi_stream_index [int] in order to receive the Plex stream
|
2021-09-19 21:38:25 +10:00
|
|
|
index [int].
|
2017-12-14 20:22:48 +11:00
|
|
|
stream_type: 'video', 'audio', 'subtitle'
|
|
|
|
"""
|
2021-09-19 21:38:25 +10:00
|
|
|
if stream_type == 'audio':
|
|
|
|
return int(self.audio_streams[kodi_stream_index].get('id'))
|
2021-10-20 23:55:39 +11:00
|
|
|
elif stream_type == 'video':
|
|
|
|
return int(self.video_streams[kodi_stream_index].get('id'))
|
2021-09-19 21:38:25 +10:00
|
|
|
elif stream_type == 'subtitle':
|
2021-11-06 03:38:37 +11:00
|
|
|
if self.current_kodi_sub_stream_enabled:
|
|
|
|
try:
|
|
|
|
return int(self.subtitle_streams[kodi_stream_index].get('id'))
|
|
|
|
except (IndexError, TypeError):
|
|
|
|
# A subtitle that is not available on the Plex side
|
|
|
|
# deactivating subs
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return 0
|
2017-12-14 20:22:48 +11:00
|
|
|
|
2017-12-16 02:11:19 +11:00
|
|
|
def kodi_stream_index(self, plex_stream_index, stream_type):
|
|
|
|
"""
|
2021-09-19 21:38:25 +10:00
|
|
|
Pass in the plex_stream_index [int] in order to receive the Kodi stream
|
|
|
|
index [int].
|
2017-12-16 02:11:19 +11:00
|
|
|
stream_type: 'video', 'audio', 'subtitle'
|
2021-10-22 17:40:25 +11:00
|
|
|
Raises ValueError if unsuccessful
|
2017-12-16 02:11:19 +11:00
|
|
|
"""
|
2021-10-22 17:40:25 +11:00
|
|
|
if not isinstance(plex_stream_index, int):
|
|
|
|
raise ValueError('%s plex_stream_index %s of type %s received' %
|
|
|
|
(stream_type, plex_stream_index, type(plex_stream_index)))
|
2021-09-19 21:38:25 +10:00
|
|
|
for i, stream in enumerate(self._get_iterator(stream_type)):
|
|
|
|
if cast(int, stream.get('id')) == plex_stream_index:
|
|
|
|
return i
|
2021-10-22 17:40:25 +11:00
|
|
|
raise ValueError('No %s kodi_stream_index for plex_stream_index %s' %
|
|
|
|
(stream_type, plex_stream_index))
|
2021-09-04 01:42:59 +10:00
|
|
|
|
|
|
|
def active_plex_stream_index(self, stream_type):
|
|
|
|
"""
|
|
|
|
Returns the following tuple for the active stream on the Plex side:
|
2021-09-19 21:38:25 +10:00
|
|
|
(Plex stream id [int], languageTag [str] or None)
|
2021-09-04 01:42:59 +10:00
|
|
|
Returns None if no stream has been selected
|
|
|
|
"""
|
2021-09-19 21:38:25 +10:00
|
|
|
for i, stream in enumerate(self._get_iterator(stream_type)):
|
|
|
|
if stream.get('selected') == '1':
|
|
|
|
return (int(stream.get('id')), stream.get('languageTag'))
|
|
|
|
|
|
|
|
def on_kodi_subtitle_stream_change(self, kodi_stream_index, subs_enabled):
|
2021-09-04 01:42:59 +10:00
|
|
|
"""
|
2021-09-19 21:38:25 +10:00
|
|
|
Call this method if Kodi changed its subtitle and you want Plex to
|
|
|
|
know.
|
2021-09-04 01:42:59 +10:00
|
|
|
"""
|
2021-09-19 21:38:25 +10:00
|
|
|
if subs_enabled:
|
|
|
|
try:
|
|
|
|
plex_stream_index = int(self.subtitle_streams[kodi_stream_index].get('id'))
|
|
|
|
except (IndexError, TypeError):
|
|
|
|
LOG.debug('Kodi subtitle change detected to a sub %s that is '
|
|
|
|
'NOT available on the Plex side', kodi_stream_index)
|
2021-11-06 03:38:37 +11:00
|
|
|
plex_stream_index = 0
|
|
|
|
else:
|
|
|
|
LOG.debug('Kodi subtitle change detected: telling Plex about '
|
|
|
|
'switch to index %s, Plex stream id %s',
|
|
|
|
kodi_stream_index, plex_stream_index)
|
2021-09-04 01:42:59 +10:00
|
|
|
else:
|
2021-09-19 21:38:25 +10:00
|
|
|
plex_stream_index = 0
|
|
|
|
LOG.debug('Kodi subtitle has been deactivated, telling Plex')
|
|
|
|
PF.change_subtitle(plex_stream_index, self.api.part_id())
|
|
|
|
|
|
|
|
def on_kodi_audio_stream_change(self, kodi_stream_index):
|
|
|
|
"""
|
|
|
|
Call this method if Kodi changed its audio stream and you want Plex to
|
|
|
|
know. kodi_stream_index [int]
|
|
|
|
"""
|
|
|
|
plex_stream_index = int(self.audio_streams[kodi_stream_index].get('id'))
|
|
|
|
LOG.debug('Changing Plex audio stream to %s, Kodi index %s',
|
|
|
|
plex_stream_index, kodi_stream_index)
|
|
|
|
PF.change_audio_stream(plex_stream_index, self.api.part_id())
|
2017-12-16 02:11:19 +11:00
|
|
|
|
2021-10-20 23:55:39 +11:00
|
|
|
def on_kodi_video_stream_change(self, kodi_stream_index):
|
|
|
|
"""
|
|
|
|
Call this method if Kodi changed its video stream and you want Plex to
|
|
|
|
know. kodi_stream_index [int]
|
|
|
|
"""
|
|
|
|
plex_stream_index = int(self.video_streams[kodi_stream_index].get('id'))
|
|
|
|
LOG.debug('Changing Plex video stream to %s, Kodi index %s',
|
|
|
|
plex_stream_index, kodi_stream_index)
|
|
|
|
PF.change_video_stream(plex_stream_index, self.api.part_id())
|
|
|
|
|
2021-11-06 03:38:37 +11:00
|
|
|
def _set_kodi_stream_if_different(self, kodi_index, typus):
|
2021-10-20 23:55:39 +11:00
|
|
|
if typus == 'video':
|
2021-11-06 03:38:37 +11:00
|
|
|
current = js.get_current_video_stream_index(self.playerid)
|
2021-10-20 23:55:39 +11:00
|
|
|
if current != kodi_index:
|
|
|
|
LOG.debug('Switching video stream')
|
|
|
|
app.APP.player.setVideoStream(kodi_index)
|
|
|
|
else:
|
|
|
|
LOG.debug('Not switching video stream (no change)')
|
|
|
|
elif typus == 'audio':
|
2021-11-06 03:38:37 +11:00
|
|
|
current = js.get_current_audio_stream_index(self.playerid)
|
2021-10-20 23:55:39 +11:00
|
|
|
if current != kodi_index:
|
|
|
|
LOG.debug('Switching audio stream')
|
|
|
|
app.APP.player.setAudioStream(kodi_index)
|
|
|
|
else:
|
|
|
|
LOG.debug('Not switching audio stream (no change)')
|
|
|
|
|
2021-09-30 20:13:29 +10:00
|
|
|
def switch_to_plex_stream(self, typus):
|
|
|
|
try:
|
|
|
|
plex_index, language_tag = self.active_plex_stream_index(typus)
|
|
|
|
except TypeError:
|
|
|
|
LOG.debug('Deactivating Kodi subtitles because the PMS '
|
|
|
|
'told us to not show any subtitles')
|
|
|
|
app.APP.player.showSubtitles(False)
|
2021-11-06 03:38:37 +11:00
|
|
|
self._current_kodi_sub_stream_enabled = False
|
2021-09-30 20:13:29 +10:00
|
|
|
return
|
|
|
|
LOG.debug('The PMS wants to display %s stream with Plex id %s and '
|
|
|
|
'languageTag %s', typus, plex_index, language_tag)
|
2021-10-31 21:04:31 +11:00
|
|
|
try:
|
|
|
|
kodi_index = self.kodi_stream_index(plex_index, typus)
|
|
|
|
except ValueError:
|
|
|
|
kodi_index = None
|
2021-09-30 20:13:29 +10:00
|
|
|
LOG.debug('Leaving Kodi %s stream settings untouched since we '
|
|
|
|
'could not parse Plex %s stream with id %s to a Kodi'
|
|
|
|
' index', typus, typus, plex_index)
|
|
|
|
else:
|
|
|
|
LOG.debug('Switching to Kodi %s stream number %s because the '
|
|
|
|
'PMS told us to show stream with Plex id %s',
|
|
|
|
typus, kodi_index, plex_index)
|
|
|
|
# If we're choosing an "illegal" index, this function does
|
|
|
|
# need seem to fail nor log any errors
|
|
|
|
if typus == 'audio':
|
2021-10-20 23:55:39 +11:00
|
|
|
self._set_kodi_stream_if_different(kodi_index, 'audio')
|
|
|
|
elif typus == 'subtitle':
|
2021-09-30 20:13:29 +10:00
|
|
|
app.APP.player.setSubtitleStream(kodi_index)
|
|
|
|
app.APP.player.showSubtitles(True)
|
2021-10-20 23:55:39 +11:00
|
|
|
elif typus == 'video':
|
|
|
|
self._set_kodi_stream_if_different(kodi_index, 'video')
|
2021-09-30 20:13:29 +10:00
|
|
|
if typus == 'audio':
|
2021-11-06 03:38:37 +11:00
|
|
|
self._current_kodi_audio_stream = kodi_index
|
2021-10-20 23:55:39 +11:00
|
|
|
elif typus == 'subtitle':
|
2021-11-06 03:38:37 +11:00
|
|
|
self._current_kodi_sub_stream_enabled = True
|
|
|
|
self._current_kodi_sub_stream = kodi_index
|
2021-10-20 23:55:39 +11:00
|
|
|
elif typus == 'video':
|
2021-11-06 03:38:37 +11:00
|
|
|
self._current_kodi_video_stream = kodi_index
|
2021-09-30 20:13:29 +10:00
|
|
|
|
2021-10-22 17:40:25 +11:00
|
|
|
def on_plex_stream_change(self, video_stream_id=None, audio_stream_id=None,
|
|
|
|
subtitle_stream_id=None):
|
2021-10-19 17:36:05 +11:00
|
|
|
"""
|
2021-10-22 17:40:25 +11:00
|
|
|
Call this method if Plex Companion wants to change streams [ints]
|
2021-10-19 17:36:05 +11:00
|
|
|
"""
|
2021-10-22 17:40:25 +11:00
|
|
|
if video_stream_id is not None:
|
2021-10-31 21:04:31 +11:00
|
|
|
try:
|
|
|
|
kodi_index = self.kodi_stream_index(video_stream_id, 'video')
|
|
|
|
except ValueError:
|
|
|
|
LOG.error('Unexpected Plex video_stream_id %s, not changing '
|
|
|
|
'the video stream!', video_stream_id)
|
|
|
|
return
|
2021-10-20 23:55:39 +11:00
|
|
|
self._set_kodi_stream_if_different(kodi_index, 'video')
|
2021-11-06 03:38:37 +11:00
|
|
|
self._current_kodi_video_stream = kodi_index
|
2021-10-22 17:40:25 +11:00
|
|
|
if audio_stream_id is not None:
|
2021-10-31 21:04:31 +11:00
|
|
|
try:
|
|
|
|
kodi_index = self.kodi_stream_index(audio_stream_id, 'audio')
|
|
|
|
except ValueError:
|
|
|
|
LOG.error('Unexpected Plex audio_stream_id %s, not changing '
|
|
|
|
'the video stream!', audio_stream_id)
|
|
|
|
return
|
2021-10-22 17:40:25 +11:00
|
|
|
self._set_kodi_stream_if_different(kodi_index, 'audio')
|
2021-11-06 03:38:37 +11:00
|
|
|
self._current_kodi_audio_stream = kodi_index
|
2021-10-22 17:40:25 +11:00
|
|
|
if subtitle_stream_id is not None:
|
|
|
|
if subtitle_stream_id == 0:
|
2021-10-19 17:36:05 +11:00
|
|
|
app.APP.player.showSubtitles(False)
|
2021-11-06 03:38:37 +11:00
|
|
|
self._current_kodi_sub_stream_enabled = False
|
2021-10-19 17:36:05 +11:00
|
|
|
else:
|
2021-10-31 21:04:31 +11:00
|
|
|
try:
|
|
|
|
kodi_index = self.kodi_stream_index(subtitle_stream_id,
|
|
|
|
'subtitle')
|
|
|
|
except ValueError:
|
|
|
|
LOG.debug('The PMS wanted to change subs, but we could not'
|
|
|
|
' match the sub with id %s to a Kodi sub',
|
|
|
|
subtitle_stream_id)
|
|
|
|
else:
|
2021-10-19 17:36:05 +11:00
|
|
|
app.APP.player.setSubtitleStream(kodi_index)
|
|
|
|
app.APP.player.showSubtitles(True)
|
2021-11-06 03:38:37 +11:00
|
|
|
self._current_kodi_sub_stream_enabled = True
|
|
|
|
self._current_kodi_sub_stream = kodi_index
|
2021-10-19 17:36:05 +11:00
|
|
|
|
2016-12-28 03:33:52 +11:00
|
|
|
|
2017-01-03 00:07:24 +11:00
|
|
|
def playlist_item_from_kodi(kodi_item):
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
|
|
|
Turns the JSON answer from Kodi into a playlist element
|
|
|
|
|
|
|
|
Supply with data['item'] as returned from Kodi JSON-RPC interface.
|
|
|
|
kodi_item dict contains keys 'id', 'type', 'file' (if applicable)
|
|
|
|
"""
|
2019-08-09 23:40:39 +10:00
|
|
|
item = PlaylistItem()
|
2016-12-29 00:48:23 +11:00
|
|
|
item.kodi_id = kodi_item.get('id')
|
2017-05-07 23:02:45 +10:00
|
|
|
item.kodi_type = kodi_item.get('type')
|
2016-12-29 00:48:23 +11:00
|
|
|
if item.kodi_id:
|
2019-01-29 04:05:40 +11:00
|
|
|
with PlexDB(lock=False) as plexdb:
|
2018-10-25 02:17:02 +11:00
|
|
|
db_item = plexdb.item_by_kodi_id(kodi_item['id'], kodi_item['type'])
|
|
|
|
if db_item:
|
|
|
|
item.plex_id = db_item['plex_id']
|
|
|
|
item.plex_type = db_item['plex_type']
|
2017-01-03 00:07:24 +11:00
|
|
|
item.file = kodi_item.get('file')
|
2017-05-07 23:02:45 +10:00
|
|
|
if item.plex_id is None and item.file is not None:
|
2019-03-30 20:32:56 +11:00
|
|
|
try:
|
|
|
|
query = item.file.split('?', 1)[1]
|
|
|
|
except IndexError:
|
|
|
|
query = ''
|
|
|
|
query = dict(utils.parse_qsl(query))
|
2021-09-19 21:38:25 +10:00
|
|
|
item.plex_id = cast(int, query.get('plex_id'))
|
2017-05-07 23:02:45 +10:00
|
|
|
item.plex_type = query.get('itemType')
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('Made playlist item from Kodi: %s', item)
|
2016-12-28 03:33:52 +11:00
|
|
|
return item
|
|
|
|
|
|
|
|
|
2018-02-15 18:09:57 +11:00
|
|
|
def verify_kodi_item(plex_id, kodi_item):
|
|
|
|
"""
|
|
|
|
Tries to lookup kodi_id and kodi_type for kodi_item (with kodi_item['file']
|
|
|
|
supplied) - if and only if plex_id is None.
|
|
|
|
|
|
|
|
Returns the kodi_item with kodi_item['id'] and kodi_item['type'] possibly
|
|
|
|
set to None if unsuccessful.
|
|
|
|
|
|
|
|
Will raise a PlaylistError if plex_id is None and kodi_item['file'] starts
|
2019-08-10 20:01:23 +10:00
|
|
|
with either 'plugin' or 'http'.
|
|
|
|
Will raise KeyError if neither plex_id nor kodi_id are found
|
2018-02-15 18:09:57 +11:00
|
|
|
"""
|
|
|
|
if plex_id is not None or kodi_item.get('id') is not None:
|
|
|
|
# Got all the info we need
|
|
|
|
return kodi_item
|
2018-07-08 19:52:59 +10:00
|
|
|
# Special case playlist startup - got type but no id
|
2018-11-19 00:59:17 +11:00
|
|
|
if (not app.SYNC.direct_paths and app.SYNC.enable_music and
|
2018-07-08 19:52:59 +10:00
|
|
|
kodi_item.get('type') == v.KODI_TYPE_SONG and
|
|
|
|
kodi_item['file'].startswith('http')):
|
2018-11-09 07:22:16 +11:00
|
|
|
kodi_item['id'], _ = kodiid_from_filename(kodi_item['file'],
|
|
|
|
v.KODI_TYPE_SONG)
|
2018-07-08 19:52:59 +10:00
|
|
|
LOG.debug('Detected song. Research results: %s', kodi_item)
|
|
|
|
return kodi_item
|
2018-02-15 18:09:57 +11:00
|
|
|
# Need more info since we don't have kodi_id nor type. Use file path.
|
2018-07-25 05:04:31 +10:00
|
|
|
if ((kodi_item['file'].startswith('plugin') and
|
|
|
|
not kodi_item['file'].startswith('plugin://%s' % v.ADDON_ID)) or
|
2018-02-15 18:09:57 +11:00
|
|
|
kodi_item['file'].startswith('http')):
|
2019-08-09 23:42:33 +10:00
|
|
|
LOG.debug('kodi_item cannot be used for Plex playback: %s', kodi_item)
|
|
|
|
raise PlaylistError('kodi_item cannot be used for Plex playback')
|
2018-02-15 18:09:57 +11:00
|
|
|
LOG.debug('Starting research for Kodi id since we didnt get one: %s',
|
|
|
|
kodi_item)
|
2019-03-17 21:41:43 +11:00
|
|
|
# Try the VIDEO DB first - will find both movies and episodes
|
|
|
|
kodi_id, kodi_type = kodiid_from_filename(kodi_item['file'],
|
|
|
|
db_type='video')
|
|
|
|
if not kodi_id:
|
|
|
|
# No movie or episode found - try MUSIC DB now for songs
|
|
|
|
kodi_id, kodi_type = kodiid_from_filename(kodi_item['file'],
|
|
|
|
db_type='music')
|
2018-02-15 18:09:57 +11:00
|
|
|
kodi_item['id'] = kodi_id
|
2019-03-17 21:41:43 +11:00
|
|
|
kodi_item['type'] = None if kodi_id is None else kodi_type
|
2019-08-10 20:01:23 +10:00
|
|
|
if plex_id is None and kodi_id is None:
|
|
|
|
raise KeyError('Neither Plex nor Kodi id found for %s' % kodi_item)
|
2018-02-15 18:09:57 +11:00
|
|
|
LOG.debug('Research results for kodi_item: %s', kodi_item)
|
|
|
|
return kodi_item
|
|
|
|
|
|
|
|
|
2016-12-28 03:33:52 +11:00
|
|
|
def playlist_item_from_plex(plex_id):
|
|
|
|
"""
|
|
|
|
Returns a playlist element providing the plex_id ("ratingKey")
|
2017-01-03 00:07:24 +11:00
|
|
|
|
|
|
|
Returns a Playlist_Item
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2019-08-09 23:40:39 +10:00
|
|
|
item = PlaylistItem()
|
2016-12-28 03:33:52 +11:00
|
|
|
item.plex_id = plex_id
|
2019-01-29 04:05:40 +11:00
|
|
|
with PlexDB(lock=False) as plexdb:
|
2018-10-25 02:17:02 +11:00
|
|
|
db_item = plexdb.item_by_id(plex_id)
|
|
|
|
if db_item:
|
|
|
|
item.plex_type = db_item['plex_type']
|
|
|
|
item.kodi_id = db_item['kodi_id']
|
|
|
|
item.kodi_type = db_item['kodi_type']
|
|
|
|
else:
|
2016-12-28 03:33:52 +11:00
|
|
|
raise KeyError('Could not find plex_id %s in database' % plex_id)
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('Made playlist item from plex: %s', item)
|
2016-12-28 03:33:52 +11:00
|
|
|
return item
|
|
|
|
|
|
|
|
|
2018-02-23 23:06:18 +11:00
|
|
|
def playlist_item_from_xml(xml_video_element, kodi_id=None, kodi_type=None):
|
2017-01-03 00:07:24 +11:00
|
|
|
"""
|
|
|
|
Returns a playlist element for the playqueue using the Plex xml
|
2017-12-08 03:25:24 +11:00
|
|
|
|
|
|
|
xml_video_element: etree xml piece 1 level underneath <MediaContainer>
|
2017-01-03 00:07:24 +11:00
|
|
|
"""
|
2019-08-09 23:40:39 +10:00
|
|
|
item = PlaylistItem()
|
2017-01-03 00:07:24 +11:00
|
|
|
api = API(xml_video_element)
|
2019-06-11 05:29:42 +10:00
|
|
|
item.plex_id = api.plex_id
|
|
|
|
item.plex_type = api.plex_type
|
2018-02-23 23:06:18 +11:00
|
|
|
# item.id will only be set if you passed in an xml_video_element from e.g.
|
|
|
|
# a playQueue
|
|
|
|
item.id = api.item_id()
|
2018-01-21 23:42:22 +11:00
|
|
|
if kodi_id is not None:
|
|
|
|
item.kodi_id = kodi_id
|
|
|
|
item.kodi_type = kodi_type
|
2019-08-09 23:02:12 +10:00
|
|
|
elif item.plex_type != v.PLEX_TYPE_CLIP:
|
2019-01-29 04:05:40 +11:00
|
|
|
with PlexDB(lock=False) as plexdb:
|
2019-08-09 23:02:12 +10:00
|
|
|
db_element = plexdb.item_by_id(item.plex_id,
|
|
|
|
plex_type=item.plex_type)
|
2018-10-25 02:17:02 +11:00
|
|
|
if db_element:
|
|
|
|
item.kodi_id = db_element['kodi_id']
|
|
|
|
item.kodi_type = db_element['kodi_type']
|
2018-02-23 23:06:18 +11:00
|
|
|
item.guid = api.guid_html_escaped()
|
|
|
|
item.playcount = api.viewcount()
|
|
|
|
item.offset = api.resume_point()
|
2021-09-13 21:26:04 +10:00
|
|
|
item.api = api
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('Created new playlist item from xml: %s', item)
|
2017-01-03 00:07:24 +11:00
|
|
|
return item
|
|
|
|
|
|
|
|
|
2021-09-13 21:26:04 +10:00
|
|
|
def _update_playlist_version(playlist, xml):
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2021-09-13 21:26:04 +10:00
|
|
|
Takes a PMS xml (one level above the xml-depth where we're usually applying
|
|
|
|
API()) as input to overwrite the playlist version (e.g. Plex
|
2018-01-30 17:50:44 +11:00
|
|
|
playQueueVersion).
|
|
|
|
|
|
|
|
Raises PlaylistError if unsuccessful
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2021-04-30 18:22:11 +10:00
|
|
|
try:
|
|
|
|
playlist.version = int(xml.get('%sVersion' % playlist.kind))
|
|
|
|
except (AttributeError, TypeError):
|
2018-01-30 17:50:44 +11:00
|
|
|
raise PlaylistError('Could not get new playlist Version for playlist '
|
2018-02-08 00:28:54 +11:00
|
|
|
'%s' % playlist)
|
2016-12-28 03:33:52 +11:00
|
|
|
|
|
|
|
|
2017-01-03 00:07:24 +11:00
|
|
|
def get_playlist_details_from_xml(playlist, xml):
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
|
|
|
Takes a PMS xml as input and overwrites all the playlist's details, e.g.
|
2017-12-29 04:29:51 +11:00
|
|
|
playlist.id with the XML's playQueueID
|
2018-01-30 17:50:44 +11:00
|
|
|
|
|
|
|
Raises PlaylistError if something went wrong.
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2021-04-30 18:22:11 +10:00
|
|
|
if xml is None:
|
|
|
|
raise PlaylistError('No playlist received for playlist %s' % playlist)
|
2021-09-19 21:38:25 +10:00
|
|
|
playlist.id = cast(int, xml.get('%sID' % playlist.kind))
|
|
|
|
playlist.version = cast(int, xml.get('%sVersion' % playlist.kind))
|
|
|
|
playlist.shuffled = cast(int, xml.get('%sShuffled' % playlist.kind))
|
|
|
|
playlist.selectedItemID = cast(int, xml.get('%sSelectedItemID'
|
|
|
|
% playlist.kind))
|
|
|
|
playlist.selectedItemOffset = cast(int, xml.get('%sSelectedItemOffset'
|
|
|
|
% playlist.kind))
|
2018-05-02 00:41:10 +10:00
|
|
|
LOG.debug('Updated playlist from xml: %s', playlist)
|
2017-01-03 00:07:24 +11:00
|
|
|
|
|
|
|
|
|
|
|
def update_playlist_from_PMS(playlist, playlist_id=None, xml=None):
|
|
|
|
"""
|
|
|
|
Updates Kodi playlist using a new PMS playlist. Pass in playlist_id if we
|
|
|
|
need to fetch a new playqueue
|
|
|
|
|
|
|
|
If an xml is passed in, the playlist will be overwritten with its info
|
2019-08-09 03:54:12 +10:00
|
|
|
|
|
|
|
Raises PlaylistError if something went wront
|
2017-01-03 00:07:24 +11:00
|
|
|
"""
|
|
|
|
if xml is None:
|
|
|
|
xml = get_PMS_playlist(playlist, playlist_id)
|
|
|
|
# Clear our existing playlist and the associated Kodi playlist
|
|
|
|
playlist.clear()
|
|
|
|
# Set new values
|
2018-01-30 17:50:44 +11:00
|
|
|
get_playlist_details_from_xml(playlist, xml)
|
2017-01-03 00:07:24 +11:00
|
|
|
for plex_item in xml:
|
2017-01-22 03:20:42 +11:00
|
|
|
playlist_item = add_to_Kodi_playlist(playlist, plex_item)
|
|
|
|
if playlist_item is not None:
|
|
|
|
playlist.items.append(playlist_item)
|
2016-12-28 03:33:52 +11:00
|
|
|
|
|
|
|
|
2018-05-02 02:08:31 +10:00
|
|
|
def init_plex_playqueue(playlist, plex_id=None, kodi_item=None):
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2017-01-03 00:07:24 +11:00
|
|
|
Initializes the Plex side without changing the Kodi playlists
|
2018-05-02 23:44:54 +10:00
|
|
|
WILL ALSO UPDATE OUR PLAYLISTS.
|
2017-01-03 00:07:24 +11:00
|
|
|
|
2018-01-30 17:50:44 +11:00
|
|
|
Returns the first PKC playlist item or raises PlaylistError
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2018-05-02 17:33:37 +10:00
|
|
|
LOG.debug('Initializing the playqueue on the Plex side: %s', playlist)
|
2018-02-15 18:09:57 +11:00
|
|
|
verify_kodi_item(plex_id, kodi_item)
|
2019-08-10 20:01:23 +10:00
|
|
|
playlist.clear(kodi=False)
|
2017-02-27 01:16:03 +11:00
|
|
|
try:
|
|
|
|
if plex_id:
|
|
|
|
item = playlist_item_from_plex(plex_id)
|
|
|
|
else:
|
|
|
|
item = playlist_item_from_kodi(kodi_item)
|
|
|
|
params = {
|
|
|
|
'next': 0,
|
|
|
|
'type': playlist.type,
|
2021-02-08 07:42:47 +11:00
|
|
|
'uri': item.uri,
|
|
|
|
'includeMarkers': 1, # e.g. start + stop of intros
|
2017-02-27 01:16:03 +11:00
|
|
|
}
|
|
|
|
xml = DU().downloadUrl(url="{server}/%ss" % playlist.kind,
|
|
|
|
action_type="POST",
|
|
|
|
parameters=params)
|
2019-08-09 03:54:12 +10:00
|
|
|
if xml in (None, 401):
|
|
|
|
raise PlaylistError('Did not receive a valid xml from the PMS')
|
2017-02-27 01:16:03 +11:00
|
|
|
get_playlist_details_from_xml(playlist, xml)
|
2017-12-21 19:28:06 +11:00
|
|
|
# Need to get the details for the playlist item
|
2018-02-23 23:06:18 +11:00
|
|
|
item = playlist_item_from_xml(xml[0])
|
2017-12-08 03:25:24 +11:00
|
|
|
except (KeyError, IndexError, TypeError):
|
2018-06-22 03:24:37 +10:00
|
|
|
LOG.error('Could not init Plex playlist: plex_id %s, kodi_item %s',
|
|
|
|
plex_id, kodi_item)
|
|
|
|
raise PlaylistError
|
2016-12-28 03:33:52 +11:00
|
|
|
playlist.items.append(item)
|
2018-05-02 17:33:37 +10:00
|
|
|
LOG.debug('Initialized the playqueue on the Plex side: %s', playlist)
|
2018-01-30 17:50:44 +11:00
|
|
|
return item
|
2017-01-03 00:07:24 +11:00
|
|
|
|
|
|
|
|
|
|
|
def add_listitem_to_playlist(playlist, pos, listitem, kodi_id=None,
|
|
|
|
kodi_type=None, plex_id=None, file=None):
|
|
|
|
"""
|
|
|
|
Adds a listitem to both the Kodi and Plex playlist at position pos [int].
|
|
|
|
|
|
|
|
If file is not None, file will overrule kodi_id!
|
2017-03-06 01:06:54 +11:00
|
|
|
|
|
|
|
file: str!!
|
2017-01-03 00:07:24 +11:00
|
|
|
"""
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('add_listitem_to_playlist at position %s. Playlist before add: '
|
|
|
|
'%s', pos, playlist)
|
2017-01-03 00:07:24 +11:00
|
|
|
kodi_item = {'id': kodi_id, 'type': kodi_type, 'file': file}
|
2017-12-09 05:43:06 +11:00
|
|
|
if playlist.id is None:
|
2018-05-02 02:08:31 +10:00
|
|
|
init_plex_playqueue(playlist, plex_id, kodi_item)
|
2017-01-03 00:07:24 +11:00
|
|
|
else:
|
2018-05-02 23:34:21 +10:00
|
|
|
add_item_to_plex_playqueue(playlist, pos, plex_id, kodi_item)
|
2017-01-03 00:07:24 +11:00
|
|
|
if kodi_id is None and playlist.items[pos].kodi_id:
|
|
|
|
kodi_id = playlist.items[pos].kodi_id
|
|
|
|
kodi_type = playlist.items[pos].kodi_type
|
|
|
|
if file is None:
|
|
|
|
file = playlist.items[pos].file
|
|
|
|
# Otherwise we double the item!
|
|
|
|
del playlist.items[pos]
|
|
|
|
kodi_item = {'id': kodi_id, 'type': kodi_type, 'file': file}
|
|
|
|
add_listitem_to_Kodi_playlist(playlist,
|
|
|
|
pos,
|
|
|
|
listitem,
|
|
|
|
file,
|
|
|
|
kodi_item=kodi_item)
|
|
|
|
|
|
|
|
|
|
|
|
def add_item_to_playlist(playlist, pos, kodi_id=None, kodi_type=None,
|
|
|
|
plex_id=None, file=None):
|
|
|
|
"""
|
|
|
|
Adds an item to BOTH the Kodi and Plex playlist at position pos [int]
|
2018-01-30 17:50:44 +11:00
|
|
|
file: str!
|
2017-03-06 01:06:54 +11:00
|
|
|
|
2018-01-30 17:50:44 +11:00
|
|
|
Raises PlaylistError if something went wrong
|
2017-01-03 00:07:24 +11:00
|
|
|
"""
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('add_item_to_playlist. Playlist before adding: %s', playlist)
|
2017-01-03 00:07:24 +11:00
|
|
|
kodi_item = {'id': kodi_id, 'type': kodi_type, 'file': file}
|
2017-12-09 05:43:06 +11:00
|
|
|
if playlist.id is None:
|
2018-05-02 02:08:31 +10:00
|
|
|
item = init_plex_playqueue(playlist, plex_id, kodi_item)
|
2017-01-03 00:07:24 +11:00
|
|
|
else:
|
2018-05-02 23:34:21 +10:00
|
|
|
item = add_item_to_plex_playqueue(playlist, pos, plex_id, kodi_item)
|
2017-12-08 03:25:24 +11:00
|
|
|
params = {
|
|
|
|
'playlistid': playlist.playlistid,
|
|
|
|
'position': pos
|
|
|
|
}
|
|
|
|
if item.kodi_id is not None:
|
|
|
|
params['item'] = {'%sid' % item.kodi_type: int(item.kodi_id)}
|
|
|
|
else:
|
|
|
|
params['item'] = {'file': item.file}
|
2017-12-09 05:43:06 +11:00
|
|
|
reply = js.playlist_insert(params)
|
2017-12-08 03:25:24 +11:00
|
|
|
if reply.get('error') is not None:
|
2018-02-23 23:06:18 +11:00
|
|
|
raise PlaylistError('Could not add item to playlist. Kodi reply. %s'
|
|
|
|
% reply)
|
2018-01-30 17:50:44 +11:00
|
|
|
return item
|
2016-12-28 03:33:52 +11:00
|
|
|
|
|
|
|
|
2018-05-02 23:34:21 +10:00
|
|
|
def add_item_to_plex_playqueue(playlist, pos, plex_id=None, kodi_item=None):
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2017-01-03 00:07:24 +11:00
|
|
|
Adds a new item to the playlist at position pos [int] only on the Plex
|
|
|
|
side of things (e.g. because the user changed the Kodi side)
|
|
|
|
WILL ALSO UPDATE OUR PLAYLISTS
|
2017-12-08 03:25:24 +11:00
|
|
|
|
2018-01-30 17:50:44 +11:00
|
|
|
Returns the PKC PlayList item or raises PlaylistError
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2018-02-15 18:09:57 +11:00
|
|
|
verify_kodi_item(plex_id, kodi_item)
|
2017-01-03 00:07:24 +11:00
|
|
|
if plex_id:
|
2018-01-30 17:50:44 +11:00
|
|
|
item = playlist_item_from_plex(plex_id)
|
2017-01-03 00:07:24 +11:00
|
|
|
else:
|
|
|
|
item = playlist_item_from_kodi(kodi_item)
|
2021-02-08 07:42:47 +11:00
|
|
|
url = "{server}/%ss/%s" % (playlist.kind, playlist.id)
|
|
|
|
parameters = {
|
|
|
|
'uri': item.uri,
|
|
|
|
'includeMarkers': 1, # e.g. start + stop of intros
|
|
|
|
}
|
2017-01-03 00:07:24 +11:00
|
|
|
# Will always put the new item at the end of the Plex playlist
|
2021-02-08 07:42:47 +11:00
|
|
|
xml = DU().downloadUrl(url,
|
|
|
|
action_type="PUT",
|
|
|
|
parameters=parameters)
|
2016-12-28 03:33:52 +11:00
|
|
|
try:
|
2018-02-23 23:06:18 +11:00
|
|
|
xml[-1].attrib
|
|
|
|
except (TypeError, AttributeError, KeyError, IndexError):
|
|
|
|
raise PlaylistError('Could not add item %s to playlist %s'
|
|
|
|
% (kodi_item, playlist))
|
|
|
|
api = API(xml[-1])
|
2021-09-13 21:26:04 +10:00
|
|
|
item.api = api
|
2018-02-23 23:06:18 +11:00
|
|
|
item.id = api.item_id()
|
|
|
|
item.guid = api.guid_html_escaped()
|
|
|
|
item.offset = api.resume_point()
|
|
|
|
item.playcount = api.viewcount()
|
2016-12-28 03:33:52 +11:00
|
|
|
playlist.items.append(item)
|
2017-01-03 00:07:24 +11:00
|
|
|
if pos == len(playlist.items) - 1:
|
2016-12-28 03:33:52 +11:00
|
|
|
# Item was added at the end
|
2021-09-13 21:26:04 +10:00
|
|
|
_update_playlist_version(playlist, xml)
|
2016-12-28 03:33:52 +11:00
|
|
|
else:
|
|
|
|
# Move the new item to the correct position
|
|
|
|
move_playlist_item(playlist,
|
|
|
|
len(playlist.items) - 1,
|
2017-01-03 00:07:24 +11:00
|
|
|
pos)
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('Successfully added item on the Plex side: %s', playlist)
|
2018-01-30 17:50:44 +11:00
|
|
|
return item
|
2017-01-03 00:07:24 +11:00
|
|
|
|
|
|
|
|
|
|
|
def add_item_to_kodi_playlist(playlist, pos, kodi_id=None, kodi_type=None,
|
2018-01-11 06:14:05 +11:00
|
|
|
file=None, xml_video_element=None):
|
2017-01-03 00:07:24 +11:00
|
|
|
"""
|
2017-01-22 00:47:37 +11:00
|
|
|
Adds an item to the KODI playlist only. WILL ALSO UPDATE OUR PLAYLISTS
|
2017-01-03 00:07:24 +11:00
|
|
|
|
2018-01-30 17:50:44 +11:00
|
|
|
Returns the playlist item that was just added or raises PlaylistError
|
2017-03-06 01:06:54 +11:00
|
|
|
|
|
|
|
file: str!
|
2017-01-03 00:07:24 +11:00
|
|
|
"""
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('Adding new item kodi_id: %s, kodi_type: %s, file: %s to Kodi '
|
|
|
|
'only at position %s for %s',
|
|
|
|
kodi_id, kodi_type, file, pos, playlist)
|
2017-01-03 00:07:24 +11:00
|
|
|
params = {
|
|
|
|
'playlistid': playlist.playlistid,
|
|
|
|
'position': pos
|
|
|
|
}
|
|
|
|
if kodi_id is not None:
|
|
|
|
params['item'] = {'%sid' % kodi_type: int(kodi_id)}
|
|
|
|
else:
|
|
|
|
params['item'] = {'file': file}
|
2017-12-09 05:43:06 +11:00
|
|
|
reply = js.playlist_insert(params)
|
2017-01-22 00:47:37 +11:00
|
|
|
if reply.get('error') is not None:
|
2018-01-30 17:50:44 +11:00
|
|
|
raise PlaylistError('Could not add item to playlist. Kodi reply. %s',
|
|
|
|
reply)
|
2018-01-11 06:14:05 +11:00
|
|
|
if xml_video_element is not None:
|
2018-02-23 23:06:18 +11:00
|
|
|
item = playlist_item_from_xml(xml_video_element)
|
2018-01-11 06:14:05 +11:00
|
|
|
item.kodi_id = kodi_id
|
|
|
|
item.kodi_type = kodi_type
|
|
|
|
item.file = file
|
|
|
|
elif kodi_id is not None:
|
|
|
|
item = playlist_item_from_kodi(
|
|
|
|
{'id': kodi_id, 'type': kodi_type, 'file': file})
|
|
|
|
if item.plex_id is not None:
|
2018-06-22 03:24:37 +10:00
|
|
|
xml = PF.GetPlexMetadata(item.plex_id)
|
2021-09-13 21:26:04 +10:00
|
|
|
item.api = API(xml[-1])
|
2017-12-08 03:25:24 +11:00
|
|
|
playlist.items.insert(pos, item)
|
2018-01-11 06:14:05 +11:00
|
|
|
return item
|
2016-12-28 03:33:52 +11:00
|
|
|
|
|
|
|
|
|
|
|
def move_playlist_item(playlist, before_pos, after_pos):
|
|
|
|
"""
|
2017-01-03 00:07:24 +11:00
|
|
|
Moves playlist item from before_pos [int] to after_pos [int] for Plex only.
|
|
|
|
|
2018-01-30 17:50:44 +11:00
|
|
|
WILL ALSO CHANGE OUR PLAYLISTS.
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('Moving item from %s to %s on the Plex side for %s',
|
|
|
|
before_pos, after_pos, playlist)
|
2016-12-28 03:33:52 +11:00
|
|
|
if after_pos == 0:
|
|
|
|
url = "{server}/%ss/%s/items/%s/move?after=0" % \
|
|
|
|
(playlist.kind,
|
2017-12-09 05:43:06 +11:00
|
|
|
playlist.id,
|
|
|
|
playlist.items[before_pos].id)
|
2016-12-28 03:33:52 +11:00
|
|
|
else:
|
|
|
|
url = "{server}/%ss/%s/items/%s/move?after=%s" % \
|
|
|
|
(playlist.kind,
|
2017-12-09 05:43:06 +11:00
|
|
|
playlist.id,
|
|
|
|
playlist.items[before_pos].id,
|
|
|
|
playlist.items[after_pos - 1].id)
|
2021-09-13 21:26:04 +10:00
|
|
|
# Tell the PMS that we're moving items around
|
|
|
|
xml = DU().downloadUrl(url, action_type="PUT")
|
|
|
|
# We need to increment the playlist version for communicating with the PMS
|
|
|
|
_update_playlist_version(playlist, xml)
|
2016-12-28 03:33:52 +11:00
|
|
|
# Move our item's position in our internal playlist
|
|
|
|
playlist.items.insert(after_pos, playlist.items.pop(before_pos))
|
2018-01-30 17:50:44 +11:00
|
|
|
LOG.debug('Done moving for %s', playlist)
|
2016-12-28 03:33:52 +11:00
|
|
|
|
|
|
|
|
2016-12-29 05:38:43 +11:00
|
|
|
def get_PMS_playlist(playlist, playlist_id=None):
|
|
|
|
"""
|
|
|
|
Fetches the PMS playlist/playqueue as an XML. Pass in playlist_id if we
|
|
|
|
need to fetch a new playlist
|
|
|
|
|
2019-08-09 03:54:12 +10:00
|
|
|
Raises PlaylistError if something went wrong
|
2016-12-29 05:38:43 +11:00
|
|
|
"""
|
2017-12-09 05:43:06 +11:00
|
|
|
playlist_id = playlist_id if playlist_id else playlist.id
|
2021-02-08 07:42:47 +11:00
|
|
|
parameters = {'includeMarkers': 1}
|
2018-05-02 00:27:18 +10:00
|
|
|
if playlist.kind == 'playList':
|
2021-02-08 07:42:47 +11:00
|
|
|
xml = DU().downloadUrl("{server}/playlists/%s/items" % playlist_id,
|
|
|
|
parameters=parameters)
|
2018-05-02 00:27:18 +10:00
|
|
|
else:
|
2021-02-08 07:42:47 +11:00
|
|
|
xml = DU().downloadUrl("{server}/playQueues/%s" % playlist_id,
|
|
|
|
parameters=parameters)
|
2016-12-29 05:38:43 +11:00
|
|
|
try:
|
2018-05-02 00:04:26 +10:00
|
|
|
xml.attrib
|
|
|
|
except AttributeError:
|
2019-08-09 03:54:12 +10:00
|
|
|
raise PlaylistError('Did not get a valid xml')
|
2016-12-29 05:38:43 +11:00
|
|
|
return xml
|
|
|
|
|
|
|
|
|
|
|
|
def refresh_playlist_from_PMS(playlist):
|
|
|
|
"""
|
|
|
|
Only updates the selected item from the PMS side (e.g.
|
|
|
|
playQueueSelectedItemID). Will NOT check whether items still make sense.
|
|
|
|
"""
|
2018-01-30 17:50:44 +11:00
|
|
|
get_playlist_details_from_xml(playlist, get_PMS_playlist(playlist))
|
2016-12-29 05:38:43 +11:00
|
|
|
|
|
|
|
|
2017-01-03 00:07:24 +11:00
|
|
|
def delete_playlist_item_from_PMS(playlist, pos):
|
2016-12-29 05:38:43 +11:00
|
|
|
"""
|
2017-01-03 00:07:24 +11:00
|
|
|
Delete the item at position pos [int] on the Plex side and our playlists
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('Deleting position %s for %s on the Plex side', pos, playlist)
|
2021-11-13 23:49:41 +11:00
|
|
|
try:
|
|
|
|
xml = DU().downloadUrl("{server}/%ss/%s/items/%s?repeat=%s" %
|
|
|
|
(playlist.kind,
|
|
|
|
playlist.id,
|
|
|
|
playlist.items[pos].id,
|
|
|
|
playlist.repeat),
|
|
|
|
action_type="DELETE")
|
|
|
|
except IndexError:
|
|
|
|
raise PlaylistError('Position %s out of bound for %s' % (pos, playlist))
|
|
|
|
else:
|
|
|
|
del playlist.items[pos]
|
|
|
|
_update_playlist_version(playlist, xml)
|
2016-12-28 03:33:52 +11:00
|
|
|
|
|
|
|
|
|
|
|
# Functions operating on the Kodi playlist objects ##########
|
|
|
|
|
2016-12-28 23:14:21 +11:00
|
|
|
def add_to_Kodi_playlist(playlist, xml_video_element):
|
|
|
|
"""
|
|
|
|
Adds a new item to the Kodi playlist via JSON (at the end of the playlist).
|
|
|
|
Pass in the PMS xml's video element (one level underneath MediaContainer).
|
|
|
|
|
2018-01-30 17:50:44 +11:00
|
|
|
Returns a Playlist_Item or raises PlaylistError
|
2016-12-28 23:14:21 +11:00
|
|
|
"""
|
2018-02-23 23:06:18 +11:00
|
|
|
item = playlist_item_from_xml(xml_video_element)
|
2016-12-28 23:14:21 +11:00
|
|
|
if item.kodi_id:
|
2017-12-09 05:43:06 +11:00
|
|
|
json_item = {'%sid' % item.kodi_type: item.kodi_id}
|
2016-12-28 23:14:21 +11:00
|
|
|
else:
|
2017-12-09 05:43:06 +11:00
|
|
|
json_item = {'file': item.file}
|
|
|
|
reply = js.playlist_add(playlist.playlistid, json_item)
|
2017-01-22 03:20:42 +11:00
|
|
|
if reply.get('error') is not None:
|
2018-01-30 17:50:44 +11:00
|
|
|
raise PlaylistError('Could not add item %s to Kodi playlist. Error: '
|
|
|
|
'%s', xml_video_element, reply)
|
2017-12-08 03:25:24 +11:00
|
|
|
return item
|
2016-12-28 23:14:21 +11:00
|
|
|
|
|
|
|
|
2017-01-03 00:07:24 +11:00
|
|
|
def add_listitem_to_Kodi_playlist(playlist, pos, listitem, file,
|
|
|
|
xml_video_element=None, kodi_item=None):
|
2016-12-29 05:38:43 +11:00
|
|
|
"""
|
2017-01-03 00:07:24 +11:00
|
|
|
Adds an xbmc listitem to the Kodi playlist.xml_video_element
|
2016-12-29 05:38:43 +11:00
|
|
|
|
2017-01-03 00:07:24 +11:00
|
|
|
WILL NOT UPDATE THE PLEX SIDE, BUT WILL UPDATE OUR PLAYLISTS
|
2017-03-06 01:06:54 +11:00
|
|
|
|
|
|
|
file: string!
|
2017-01-03 00:07:24 +11:00
|
|
|
"""
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('Insert listitem at position %s for Kodi only for %s',
|
|
|
|
pos, playlist)
|
2017-01-03 00:07:24 +11:00
|
|
|
# Add the item into Kodi playlist
|
2018-01-21 23:42:22 +11:00
|
|
|
playlist.kodi_pl.add(url=file, listitem=listitem, index=pos)
|
2017-01-03 00:07:24 +11:00
|
|
|
# We need to add this to our internal queue as well
|
|
|
|
if xml_video_element is not None:
|
2018-02-23 23:06:18 +11:00
|
|
|
item = playlist_item_from_xml(xml_video_element)
|
2016-12-28 03:33:52 +11:00
|
|
|
else:
|
2017-01-03 00:07:24 +11:00
|
|
|
item = playlist_item_from_kodi(kodi_item)
|
2017-05-08 00:58:12 +10:00
|
|
|
if file is not None:
|
|
|
|
item.file = file
|
2017-01-03 00:07:24 +11:00
|
|
|
playlist.items.insert(pos, item)
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('Done inserting for %s', playlist)
|
2018-01-11 06:14:05 +11:00
|
|
|
return item
|
2016-12-28 03:33:52 +11:00
|
|
|
|
|
|
|
|
2017-12-09 05:43:06 +11:00
|
|
|
def remove_from_kodi_playlist(playlist, pos):
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2017-01-03 00:07:24 +11:00
|
|
|
Removes the item at position pos from the Kodi playlist using JSON.
|
|
|
|
|
|
|
|
WILL NOT UPDATE THE PLEX SIDE, BUT WILL UPDATE OUR PLAYLISTS
|
2016-12-28 03:33:52 +11:00
|
|
|
"""
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.debug('Removing position %s from Kodi only from %s', pos, playlist)
|
|
|
|
reply = js.playlist_remove(playlist.playlistid, pos)
|
2017-01-22 03:20:42 +11:00
|
|
|
if reply.get('error') is not None:
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.error('Could not delete the item from the playlist. Error: %s',
|
|
|
|
reply)
|
2017-01-22 03:20:42 +11:00
|
|
|
return
|
2017-12-09 05:43:06 +11:00
|
|
|
try:
|
|
|
|
del playlist.items[pos]
|
|
|
|
except IndexError:
|
|
|
|
LOG.error('Cannot delete position %s for %s', pos, playlist)
|
2017-05-31 21:44:04 +10:00
|
|
|
|
|
|
|
|
|
|
|
def get_pms_playqueue(playqueue_id):
|
|
|
|
"""
|
|
|
|
Returns the Plex playqueue as an etree XML or None if unsuccessful
|
|
|
|
"""
|
2021-02-08 07:42:47 +11:00
|
|
|
parameters = {'includeMarkers': 1}
|
|
|
|
xml = DU().downloadUrl("{server}/playQueues/%s" % playqueue_id,
|
|
|
|
parameters=parameters,
|
|
|
|
headerOptions={'Accept': 'application/xml'})
|
2017-05-31 21:44:04 +10:00
|
|
|
try:
|
|
|
|
xml.attrib
|
|
|
|
except AttributeError:
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.error('Could not download Plex playqueue %s', playqueue_id)
|
2017-05-31 21:44:04 +10:00
|
|
|
xml = None
|
|
|
|
return xml
|
|
|
|
|
|
|
|
|
|
|
|
def get_plextype_from_xml(xml):
|
|
|
|
"""
|
|
|
|
Needed if PMS returns an empty playqueue. Will get the Plex type from the
|
|
|
|
empty playlist playQueueSourceURI. Feed with (empty) etree xml
|
|
|
|
|
|
|
|
returns None if unsuccessful
|
|
|
|
"""
|
|
|
|
try:
|
2018-06-24 02:25:18 +10:00
|
|
|
plex_id = utils.REGEX_PLEX_ID_FROM_URL.findall(
|
|
|
|
xml.attrib['playQueueSourceURI'])[0]
|
2017-05-31 21:44:04 +10:00
|
|
|
except IndexError:
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.error('Could not get plex_id from xml: %s', xml.attrib)
|
2017-05-31 21:44:04 +10:00
|
|
|
return
|
2018-06-22 03:24:37 +10:00
|
|
|
new_xml = PF.GetPlexMetadata(plex_id)
|
2017-05-31 21:44:04 +10:00
|
|
|
try:
|
|
|
|
new_xml[0].attrib
|
|
|
|
except (TypeError, IndexError, AttributeError):
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.error('Could not get plex metadata for plex id %s', plex_id)
|
2017-05-31 21:44:04 +10:00
|
|
|
return
|
2020-12-20 06:43:08 +11:00
|
|
|
return new_xml[0].attrib.get('type')
|