2015-12-25 06:51:47 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-02 03:53:04 +10:00
|
|
|
import logging
|
2015-12-25 06:51:47 +11:00
|
|
|
import json
|
2016-02-11 20:30:29 +11:00
|
|
|
from urllib import urlencode
|
2016-08-07 23:33:36 +10:00
|
|
|
from threading import Lock
|
|
|
|
from functools import wraps
|
2016-12-03 21:50:05 +11:00
|
|
|
from urllib import quote, urlencode
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
import xbmc
|
|
|
|
|
|
|
|
import embydb_functions as embydb
|
2016-12-03 21:50:05 +11:00
|
|
|
import kodidb_functions as kodidb
|
|
|
|
from utils import window, tryEncode, JSONRPC
|
2016-05-08 21:17:04 +10:00
|
|
|
import playbackutils
|
2016-12-03 21:50:05 +11:00
|
|
|
import PlexFunctions as PF
|
2016-02-11 20:30:29 +11:00
|
|
|
import PlexAPI
|
2016-12-03 21:50:05 +11:00
|
|
|
from downloadutils import DownloadUtils
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-02 03:53:04 +10:00
|
|
|
log = logging.getLogger("PLEX."+__name__)
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2016-12-03 21:50:05 +11:00
|
|
|
PLEX_PLAYQUEUE_ARGS = (
|
|
|
|
'playQueueID',
|
|
|
|
'playQueueVersion',
|
|
|
|
'playQueueSelectedItemID',
|
|
|
|
'playQueueSelectedItemOffset'
|
|
|
|
)
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
class lockMethod:
|
2016-08-09 02:40:35 +10:00
|
|
|
"""
|
|
|
|
Decorator for class methods to lock hem completely. Same lock is used for
|
|
|
|
every single decorator and instance used!
|
|
|
|
|
|
|
|
Here only used for Playlist()
|
|
|
|
"""
|
2016-08-07 23:33:36 +10:00
|
|
|
lock = Lock()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def decorate(cls, func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
with cls.lock:
|
|
|
|
result = func(*args, **kwargs)
|
|
|
|
return result
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
class Playlist():
|
2016-06-27 00:10:32 +10:00
|
|
|
"""
|
|
|
|
Initiate with Playlist(typus='video' or 'music')
|
2016-12-03 21:50:05 +11:00
|
|
|
|
|
|
|
ATTRIBUTES:
|
|
|
|
id: integer
|
|
|
|
position: integer, default -1
|
|
|
|
type: string, default "unknown"
|
|
|
|
"unknown",
|
|
|
|
"video",
|
|
|
|
"audio",
|
|
|
|
"picture",
|
|
|
|
"mixed"
|
|
|
|
size: integer
|
2016-06-27 00:10:32 +10:00
|
|
|
"""
|
2016-08-07 23:33:36 +10:00
|
|
|
# Borg - multiple instances, shared state
|
|
|
|
_shared_state = {}
|
|
|
|
|
2016-08-12 06:11:00 +10:00
|
|
|
player = xbmc.Player()
|
2016-12-03 21:50:05 +11:00
|
|
|
|
|
|
|
playlists = None
|
2016-08-12 06:11:00 +10:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
@lockMethod.decorate
|
|
|
|
def __init__(self, typus=None):
|
|
|
|
# Borg
|
|
|
|
self.__dict__ = self._shared_state
|
|
|
|
|
2016-12-03 21:50:05 +11:00
|
|
|
# If already initiated, return
|
|
|
|
if self.playlists is not None:
|
2016-08-12 06:11:00 +10:00
|
|
|
return
|
2016-12-03 21:50:05 +11:00
|
|
|
|
|
|
|
self.doUtils = DownloadUtils().downloadUrl
|
|
|
|
# Get all playlists from Kodi
|
|
|
|
self.playlists = JSONRPC('Playlist.GetPlaylists').execute()
|
|
|
|
try:
|
|
|
|
self.playlists = self.playlists['result']
|
|
|
|
except KeyError:
|
|
|
|
log.error('Could not get Kodi playlists. JSON Result was: %s'
|
|
|
|
% self.playlists)
|
|
|
|
self.playlists = None
|
|
|
|
return
|
|
|
|
# Example return: [{u'playlistid': 0, u'type': u'audio'},
|
|
|
|
# {u'playlistid': 1, u'type': u'video'},
|
|
|
|
# {u'playlistid': 2, u'type': u'picture'}]
|
|
|
|
# Initiate the Kodi playlists
|
|
|
|
for playlist in self.playlists:
|
|
|
|
# Initialize each Kodi playlist
|
|
|
|
if playlist['type'] == 'audio':
|
|
|
|
playlist['kodi_pl'] = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
|
|
|
|
elif playlist['type'] == 'video':
|
|
|
|
playlist['kodi_pl'] = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
|
|
|
|
else:
|
|
|
|
# Currently, only video or audio playlists available
|
|
|
|
playlist['kodi_pl'] = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
|
|
|
|
|
|
|
|
# Initialize Plex info on the playQueue
|
|
|
|
for arg in PLEX_PLAYQUEUE_ARGS:
|
|
|
|
playlist[arg] = None
|
|
|
|
|
|
|
|
# Build a list of all items within each playlist
|
|
|
|
playlist['items'] = []
|
|
|
|
for item in self._get_kodi_items(playlist['playlistid']):
|
|
|
|
playlist['items'].append({
|
|
|
|
'kodi_id': item.get('id'),
|
|
|
|
'type': item['type'],
|
|
|
|
'file': item['file'],
|
|
|
|
'playQueueItemID': None,
|
|
|
|
'plex_id': self._get_plexid(item)
|
|
|
|
})
|
|
|
|
log.debug('self.playlist: %s' % playlist)
|
|
|
|
|
|
|
|
def _init_pl_item(self):
|
|
|
|
return {
|
|
|
|
'plex_id': None,
|
|
|
|
'kodi_id': None,
|
|
|
|
'file': None,
|
|
|
|
'type': None, # 'audio' or 'video'
|
|
|
|
'playQueueItemID': None,
|
|
|
|
'uri': None,
|
|
|
|
# To be able to drag Kodi JSON data along:
|
|
|
|
'playlistid': None,
|
|
|
|
'position': None,
|
|
|
|
'item': None,
|
|
|
|
}
|
|
|
|
|
|
|
|
def _get_plexid(self, item):
|
|
|
|
"""
|
|
|
|
Supply with data['item'] as returned from Kodi JSON-RPC interface
|
|
|
|
"""
|
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
|
|
|
emby_dbitem = emby_db.getItem_byKodiId(item.get('id'),
|
|
|
|
item.get('type'))
|
|
|
|
try:
|
|
|
|
plex_id = emby_dbitem[0]
|
|
|
|
except TypeError:
|
|
|
|
plex_id = None
|
|
|
|
return plex_id
|
|
|
|
|
|
|
|
def _get_kodi_items(self, playlistid):
|
|
|
|
params = {
|
|
|
|
'playlistid': playlistid,
|
|
|
|
'properties': ["title", "file"]
|
|
|
|
}
|
|
|
|
answ = JSONRPC('Playlist.GetItems').execute(params)
|
|
|
|
# returns e.g. [{u'title': u'3 Idiots', u'type': u'movie', u'id': 3,
|
|
|
|
# u'file': u'smb://nas/PlexMovies/3 Idiots 2009 pt1.mkv', u'label':
|
|
|
|
# u'3 Idiots'}]
|
|
|
|
try:
|
|
|
|
answ = answ['result']['items']
|
|
|
|
except KeyError:
|
|
|
|
answ = []
|
|
|
|
return answ
|
2016-08-12 06:11:00 +10:00
|
|
|
|
|
|
|
@lockMethod.decorate
|
|
|
|
def getQueueIdFromPosition(self, playlistPosition):
|
|
|
|
return self.items[playlistPosition]['playQueueItemID']
|
|
|
|
|
|
|
|
@lockMethod.decorate
|
|
|
|
def Typus(self, value=None):
|
|
|
|
if value:
|
|
|
|
self.typus = value
|
|
|
|
else:
|
|
|
|
return self.typus
|
|
|
|
|
|
|
|
@lockMethod.decorate
|
|
|
|
def PlayQueueVersion(self, value=None):
|
|
|
|
if value:
|
|
|
|
self.playQueueVersion = value
|
|
|
|
else:
|
|
|
|
return self.playQueueVersion
|
|
|
|
|
|
|
|
@lockMethod.decorate
|
|
|
|
def QueueId(self, value=None):
|
|
|
|
if value:
|
|
|
|
self.queueId = value
|
|
|
|
else:
|
|
|
|
return self.queueId
|
|
|
|
|
|
|
|
@lockMethod.decorate
|
|
|
|
def Guid(self, value=None):
|
|
|
|
if value:
|
|
|
|
self.guid = value
|
|
|
|
else:
|
|
|
|
return self.guid
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
@lockMethod.decorate
|
2016-07-21 02:36:31 +10:00
|
|
|
def clear(self):
|
|
|
|
"""
|
2016-08-12 06:11:00 +10:00
|
|
|
Empties current Kodi playlist and associated variables
|
2016-07-21 02:36:31 +10:00
|
|
|
"""
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info('Clearing playlist')
|
2016-07-21 02:36:31 +10:00
|
|
|
self.playlist.clear()
|
|
|
|
self.items = []
|
2016-08-12 06:11:00 +10:00
|
|
|
self.queueId = None
|
|
|
|
self.playQueueVersion = None
|
|
|
|
self.guid = None
|
2016-07-21 02:36:31 +10:00
|
|
|
|
|
|
|
def _initiatePlaylist(self):
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info('Initiating playlist')
|
2016-06-27 00:10:32 +10:00
|
|
|
playlist = None
|
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
2016-07-21 02:36:31 +10:00
|
|
|
for item in self.items:
|
|
|
|
itemid = item['plexId']
|
2016-06-27 00:10:32 +10:00
|
|
|
embydb_item = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
mediatype = embydb_item[4]
|
|
|
|
except TypeError:
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info('Couldnt find item %s in Kodi db' % itemid)
|
2016-12-03 21:50:05 +11:00
|
|
|
item = PF.GetPlexMetadata(itemid)
|
2016-06-27 00:10:32 +10:00
|
|
|
if item in (None, 401):
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info('Couldnt find item %s on PMS, trying next'
|
|
|
|
% itemid)
|
2016-06-27 00:10:32 +10:00
|
|
|
continue
|
|
|
|
if PlexAPI.API(item[0]).getType() == 'track':
|
|
|
|
playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info('Music playlist initiated')
|
2016-07-21 02:36:31 +10:00
|
|
|
self.typus = 'music'
|
2016-06-27 00:10:32 +10:00
|
|
|
else:
|
|
|
|
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info('Video playlist initiated')
|
2016-07-21 02:36:31 +10:00
|
|
|
self.typus = 'video'
|
2016-06-27 00:10:32 +10:00
|
|
|
else:
|
|
|
|
if mediatype == 'song':
|
|
|
|
playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info('Music playlist initiated')
|
2016-07-21 02:36:31 +10:00
|
|
|
self.typus = 'music'
|
2016-06-27 00:10:32 +10:00
|
|
|
else:
|
|
|
|
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info('Video playlist initiated')
|
2016-07-21 02:36:31 +10:00
|
|
|
self.typus = 'video'
|
2016-06-27 00:10:32 +10:00
|
|
|
break
|
|
|
|
self.playlist = playlist
|
2016-07-20 02:22:34 +10:00
|
|
|
if self.playlist is not None:
|
|
|
|
self.playlistId = self.playlist.getPlayListId()
|
2016-06-27 00:10:32 +10:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
def _processItems(self, startitem, startPlayer=False):
|
2016-07-24 01:32:57 +10:00
|
|
|
startpos = None
|
2016-02-10 23:50:04 +11:00
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
2016-07-21 02:36:31 +10:00
|
|
|
for pos, item in enumerate(self.items):
|
|
|
|
kodiId = None
|
|
|
|
plexId = item['plexId']
|
|
|
|
embydb_item = emby_db.getItem_byId(plexId)
|
2016-02-10 23:50:04 +11:00
|
|
|
try:
|
2016-07-21 02:36:31 +10:00
|
|
|
kodiId = embydb_item[0]
|
2016-02-10 23:50:04 +11:00
|
|
|
mediatype = embydb_item[4]
|
|
|
|
except TypeError:
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info('Couldnt find item %s in Kodi db' % plexId)
|
2016-12-03 21:50:05 +11:00
|
|
|
xml = PF.GetPlexMetadata(plexId)
|
2016-07-21 02:36:31 +10:00
|
|
|
if xml in (None, 401):
|
2016-09-02 03:53:04 +10:00
|
|
|
log.error('Could not download plexId %s' % plexId)
|
2016-04-08 02:29:23 +10:00
|
|
|
else:
|
2016-09-02 03:53:04 +10:00
|
|
|
log.debug('Downloaded xml metadata, adding now')
|
2016-07-21 02:36:31 +10:00
|
|
|
self._addtoPlaylist_xbmc(xml[0])
|
2016-02-10 23:50:04 +11:00
|
|
|
else:
|
|
|
|
# Add to playlist
|
2016-09-02 03:53:04 +10:00
|
|
|
log.debug("Adding %s PlexId %s, KodiId %s to playlist."
|
|
|
|
% (mediatype, plexId, kodiId))
|
2016-08-07 23:33:36 +10:00
|
|
|
self._addtoPlaylist(kodiId, mediatype)
|
2016-07-21 02:36:31 +10:00
|
|
|
# Add the kodiId
|
|
|
|
if kodiId is not None:
|
|
|
|
item['kodiId'] = str(kodiId)
|
2016-07-24 01:32:57 +10:00
|
|
|
if (startpos is None and startitem[1] == item[startitem[0]]):
|
|
|
|
startpos = pos
|
|
|
|
|
|
|
|
if startPlayer is True and len(self.playlist) > 0:
|
|
|
|
if startpos is not None:
|
2016-07-24 01:38:45 +10:00
|
|
|
self.player.play(self.playlist, startpos=startpos)
|
2016-07-24 01:32:57 +10:00
|
|
|
else:
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info('Never received a starting item for playlist, '
|
|
|
|
'starting with the first entry')
|
2016-07-24 01:38:45 +10:00
|
|
|
self.player.play(self.playlist)
|
2016-07-21 02:36:31 +10:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
@lockMethod.decorate
|
2016-07-21 02:36:31 +10:00
|
|
|
def playAll(self, items, startitem, offset):
|
|
|
|
"""
|
|
|
|
items: list of dicts of the form
|
|
|
|
{
|
2016-08-12 06:11:00 +10:00
|
|
|
'playQueueItemID': Plex playQueueItemID, e.g. '29175'
|
2016-07-21 02:36:31 +10:00
|
|
|
'plexId': Plex ratingKey, e.g. '125'
|
|
|
|
'kodiId': Kodi's db id of the same item
|
|
|
|
}
|
2016-02-10 23:50:04 +11:00
|
|
|
|
2016-08-12 06:11:00 +10:00
|
|
|
startitem: tuple (typus, id), where typus is either
|
|
|
|
'playQueueItemID' or 'plexId' and id is the corresponding
|
|
|
|
id as a string
|
2016-07-21 02:36:31 +10:00
|
|
|
offset: First item's time offset to play in Kodi time (an int)
|
|
|
|
"""
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info("---*** PLAY ALL ***---")
|
|
|
|
log.debug('Startitem: %s, offset: %s, items: %s'
|
|
|
|
% (startitem, offset, items))
|
2016-07-21 02:36:31 +10:00
|
|
|
self.items = items
|
2016-06-27 00:10:32 +10:00
|
|
|
if self.playlist is None:
|
2016-07-21 02:36:31 +10:00
|
|
|
self._initiatePlaylist()
|
2016-06-27 00:10:32 +10:00
|
|
|
if self.playlist is None:
|
2016-09-02 03:53:04 +10:00
|
|
|
log.error('Could not create playlist, abort')
|
2016-06-27 00:10:32 +10:00
|
|
|
return
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-09-02 03:53:04 +10:00
|
|
|
window('plex_customplaylist', value="true")
|
2016-07-21 02:36:31 +10:00
|
|
|
if offset != 0:
|
2016-06-27 00:10:32 +10:00
|
|
|
# Seek to the starting position
|
2016-09-02 03:53:04 +10:00
|
|
|
window('plex_customplaylist.seektime', str(offset))
|
2016-08-07 23:33:36 +10:00
|
|
|
self._processItems(startitem, startPlayer=True)
|
2016-07-21 02:36:31 +10:00
|
|
|
# Log playlist
|
2016-08-07 23:33:36 +10:00
|
|
|
self._verifyPlaylist()
|
2016-09-02 03:53:04 +10:00
|
|
|
log.debug('Internal playlist: %s' % self.items)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
@lockMethod.decorate
|
2015-12-25 06:51:47 +11:00
|
|
|
def modifyPlaylist(self, itemids):
|
2016-09-02 03:53:04 +10:00
|
|
|
log.info("---*** MODIFY PLAYLIST ***---")
|
|
|
|
log.debug("Items: %s" % itemids)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-07-20 02:22:34 +10:00
|
|
|
self._initiatePlaylist(itemids)
|
2016-08-07 23:33:36 +10:00
|
|
|
self._processItems(itemids, startPlayer=True)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
self._verifyPlaylist()
|
2016-04-26 22:41:58 +10:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
@lockMethod.decorate
|
2015-12-25 06:51:47 +11:00
|
|
|
def addtoPlaylist(self, dbid=None, mediatype=None, url=None):
|
2016-06-27 00:10:32 +10:00
|
|
|
"""
|
|
|
|
mediatype: Kodi type: 'movie', 'episode', 'musicvideo', 'artist',
|
|
|
|
'album', 'song', 'genre'
|
|
|
|
"""
|
2016-09-06 05:29:23 +10:00
|
|
|
self._addtoPlaylist(dbid, mediatype, url)
|
2016-08-12 06:11:00 +10:00
|
|
|
|
|
|
|
def _addtoPlaylist(self, dbid=None, mediatype=None, url=None):
|
2015-12-25 06:51:47 +11:00
|
|
|
pl = {
|
|
|
|
'jsonrpc': "2.0",
|
|
|
|
'id': 1,
|
|
|
|
'method': "Playlist.Add",
|
|
|
|
'params': {
|
2016-07-20 02:22:34 +10:00
|
|
|
'playlistid': self.playlistId
|
2015-12-25 06:51:47 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if dbid is not None:
|
2016-09-02 03:53:04 +10:00
|
|
|
pl['params']['item'] = {'%sid' % tryEncode(mediatype): int(dbid)}
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
pl['params']['item'] = {'file': url}
|
2016-09-02 03:53:04 +10:00
|
|
|
log.debug(xbmc.executeJSONRPC(json.dumps(pl)))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-07-20 02:22:34 +10:00
|
|
|
def _addtoPlaylist_xbmc(self, item):
|
|
|
|
API = PlexAPI.API(item)
|
2016-02-11 20:30:29 +11:00
|
|
|
params = {
|
|
|
|
'mode': "play",
|
2016-11-06 22:21:11 +11:00
|
|
|
'dbid': 'plextrailer',
|
2016-05-08 21:17:04 +10:00
|
|
|
'id': API.getRatingKey(),
|
|
|
|
'filename': API.getKey()
|
2016-02-11 20:30:29 +11:00
|
|
|
}
|
2016-05-08 21:17:04 +10:00
|
|
|
playurl = "plugin://plugin.video.plexkodiconnect.movies/?%s" \
|
|
|
|
% urlencode(params)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-05-08 21:17:04 +10:00
|
|
|
listitem = API.CreateListItemFromPlexItem()
|
2016-07-20 02:22:34 +10:00
|
|
|
playbackutils.PlaybackUtils(item).setArtwork(listitem)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-06-27 00:10:32 +10:00
|
|
|
self.playlist.add(playurl, listitem)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
@lockMethod.decorate
|
2016-09-02 03:53:04 +10:00
|
|
|
def insertintoPlaylist(self,
|
|
|
|
position,
|
|
|
|
dbid=None,
|
|
|
|
mediatype=None,
|
|
|
|
url=None):
|
2015-12-25 06:51:47 +11:00
|
|
|
pl = {
|
|
|
|
'jsonrpc': "2.0",
|
|
|
|
'id': 1,
|
|
|
|
'method': "Playlist.Insert",
|
|
|
|
'params': {
|
2016-07-20 02:22:34 +10:00
|
|
|
'playlistid': self.playlistId,
|
2015-12-25 06:51:47 +11:00
|
|
|
'position': position
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dbid is not None:
|
2016-09-02 03:53:04 +10:00
|
|
|
pl['params']['item'] = {'%sid' % tryEncode(mediatype): int(dbid)}
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
pl['params']['item'] = {'file': url}
|
|
|
|
|
2016-09-02 03:53:04 +10:00
|
|
|
log.debug(xbmc.executeJSONRPC(json.dumps(pl)))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
@lockMethod.decorate
|
2015-12-25 06:51:47 +11:00
|
|
|
def verifyPlaylist(self):
|
2016-08-07 23:33:36 +10:00
|
|
|
self._verifyPlaylist()
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
def _verifyPlaylist(self):
|
2015-12-25 06:51:47 +11:00
|
|
|
pl = {
|
|
|
|
'jsonrpc': "2.0",
|
|
|
|
'id': 1,
|
|
|
|
'method': "Playlist.GetItems",
|
|
|
|
'params': {
|
2016-07-20 02:22:34 +10:00
|
|
|
'playlistid': self.playlistId,
|
2016-02-07 23:26:28 +11:00
|
|
|
'properties': ['title', 'file']
|
2015-12-25 06:51:47 +11:00
|
|
|
}
|
|
|
|
}
|
2016-09-02 03:53:04 +10:00
|
|
|
log.debug(xbmc.executeJSONRPC(json.dumps(pl)))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
@lockMethod.decorate
|
2015-12-25 06:51:47 +11:00
|
|
|
def removefromPlaylist(self, position):
|
|
|
|
pl = {
|
|
|
|
'jsonrpc': "2.0",
|
|
|
|
'id': 1,
|
|
|
|
'method': "Playlist.Remove",
|
|
|
|
'params': {
|
2016-07-20 02:22:34 +10:00
|
|
|
'playlistid': self.playlistId,
|
2015-12-25 06:51:47 +11:00
|
|
|
'position': position
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 03:53:04 +10:00
|
|
|
log.debug(xbmc.executeJSONRPC(json.dumps(pl)))
|
2016-12-03 21:50:05 +11:00
|
|
|
|
|
|
|
def _get_uri(self, plex_id=None, item=None):
|
|
|
|
"""
|
|
|
|
Supply with either plex_id or data['item'] as received from Kodi JSON-
|
|
|
|
RPC
|
|
|
|
"""
|
|
|
|
uri = None
|
|
|
|
if plex_id is None:
|
|
|
|
plex_id = self._get_plexid(item)
|
|
|
|
self._cur_item['plex_id'] = plex_id
|
|
|
|
if plex_id is not None:
|
|
|
|
xml = PF.GetPlexMetadata(plex_id)
|
|
|
|
try:
|
|
|
|
uri = ('library://%s/item/%s%s' %
|
|
|
|
(xml.attrib.get('librarySectionUUID'),
|
|
|
|
quote('library/metadata/', safe=''), plex_id))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if uri is None:
|
|
|
|
try:
|
|
|
|
uri = 'library://whatever/item/%s' % quote(item['file'],
|
|
|
|
safe='')
|
|
|
|
except:
|
|
|
|
raise KeyError('Could not get file/url with item: %s' % item)
|
|
|
|
self._cur_item['uri'] = uri
|
|
|
|
return uri
|
|
|
|
|
|
|
|
def _init_plex_playQueue(self, plex_id=None, data=None):
|
|
|
|
"""
|
|
|
|
Supply either plex_id or the data supplied by Kodi JSON-RPC
|
|
|
|
"""
|
|
|
|
if plex_id is None:
|
|
|
|
plex_id = self._get_plexid(data['item'])
|
|
|
|
self._cur_item['plex_id'] = plex_id
|
|
|
|
|
|
|
|
if data is not None:
|
|
|
|
playlistid = data['playlistid']
|
|
|
|
plex_type = self.playlists[playlistid]['type']
|
|
|
|
else:
|
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
|
|
|
plex_type = emby_db.getItem_byId(plex_id)
|
|
|
|
try:
|
|
|
|
plex_type = PF.KODIAUDIOVIDEO_FROM_MEDIA_TYPE[plex_type[4]]
|
|
|
|
except TypeError:
|
|
|
|
raise KeyError('Unknown plex_type %s' % plex_type)
|
|
|
|
for playlist in self.playlists:
|
|
|
|
if playlist['type'] == plex_type:
|
|
|
|
playlistid = playlist['playlistid']
|
|
|
|
self._cur_item['playlistid'] = playlistid
|
|
|
|
self._cur_item['type'] = plex_type
|
|
|
|
|
|
|
|
params = {
|
|
|
|
'next': 0,
|
|
|
|
'type': plex_type,
|
|
|
|
'uri': self._get_uri(plex_id=plex_id, item=data['item'])
|
|
|
|
}
|
|
|
|
log.debug('params: %s' % urlencode(params))
|
|
|
|
xml = self.doUtils(url="{server}/playQueues",
|
|
|
|
action_type="POST",
|
|
|
|
parameters=params)
|
|
|
|
try:
|
|
|
|
xml.attrib
|
|
|
|
except (TypeError, AttributeError):
|
|
|
|
raise KeyError('Could not post to PMS, received: %s' % xml)
|
|
|
|
self._Plex_item_updated(xml)
|
|
|
|
|
|
|
|
def _Plex_item_updated(self, xml):
|
|
|
|
"""
|
|
|
|
Called if a new item has just been added/updated @ Plex playQueue
|
|
|
|
|
|
|
|
Call with the PMS' xml reply
|
|
|
|
"""
|
|
|
|
# Update the ITEM
|
|
|
|
log.debug('xml.attrib: %s' % xml.attrib)
|
|
|
|
args = {
|
|
|
|
'playQueueItemID': 'playQueueLastAddedItemID', # for playlist PUT
|
|
|
|
'playQueueItemID': 'playQueueSelectedItemID' # for playlist INIT
|
|
|
|
}
|
|
|
|
for old, new in args.items():
|
|
|
|
if new in xml.attrib:
|
|
|
|
self._cur_item[old] = xml.attrib[new]
|
|
|
|
# Update the PLAYLIST
|
|
|
|
for arg in PLEX_PLAYQUEUE_ARGS:
|
|
|
|
if arg in xml.attrib:
|
|
|
|
self.playlists[self._cur_item['playlistid']][arg] = xml.attrib[arg]
|
|
|
|
|
|
|
|
def _init_Kodi_item(self, item):
|
|
|
|
"""
|
|
|
|
Call with Kodi's JSON-RPC data['item']
|
|
|
|
"""
|
|
|
|
self._cur_item['kodi_id'] = item.get('id')
|
|
|
|
try:
|
|
|
|
self._cur_item['type'] = PF.KODIAUDIOVIDEO_FROM_MEDIA_TYPE[
|
|
|
|
item.get('type')]
|
|
|
|
except KeyError:
|
|
|
|
log.error('Could not get media_type for %s' % item)
|
|
|
|
|
|
|
|
def _add_curr_item(self):
|
|
|
|
self.playlists[self._cur_item['playlistid']]['items'].insert(
|
|
|
|
self._cur_item['position'],
|
|
|
|
self._cur_item)
|
|
|
|
|
|
|
|
@lockMethod.decorate
|
|
|
|
def kodi_onadd(self, data):
|
|
|
|
"""
|
|
|
|
Called if Kodi playlist is modified. Data is Kodi JSON-RPC output, e.g.
|
|
|
|
{
|
|
|
|
u'item': {u'type': u'movie', u'id': 3},
|
|
|
|
u'playlistid': 1,
|
|
|
|
u'position': 0
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
self._cur_item = self._init_pl_item()
|
|
|
|
self._cur_item.update(data)
|
|
|
|
self._init_Kodi_item(data['item'])
|
|
|
|
|
|
|
|
pl = self.playlists[data['playlistid']]
|
|
|
|
if pl['playQueueID'] is None:
|
|
|
|
# Playlist needs to be initialized!
|
|
|
|
try:
|
|
|
|
self._init_plex_playQueue(data=data)
|
|
|
|
except KeyError as e:
|
|
|
|
log.error('Error encountered while init playQueue: %s' % e)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
next_item = data['position']
|
|
|
|
if next_item != 0:
|
|
|
|
next_item = pl['items'][data['position']-1]['playQueueItemID']
|
|
|
|
params = {
|
|
|
|
'next': next_item,
|
|
|
|
'type': pl['type'],
|
|
|
|
'uri': self._get_uri(item=data['item'])
|
|
|
|
}
|
|
|
|
xml = self.doUtils(url="{server}/playQueues/%s"
|
|
|
|
% pl['playQueueID'],
|
|
|
|
action_type="PUT",
|
|
|
|
parameters=params)
|
|
|
|
try:
|
|
|
|
xml.attrib
|
|
|
|
except AttributeError:
|
|
|
|
log.error('Could not add item %s to playQueue' % data)
|
|
|
|
return
|
|
|
|
self._Plex_item_updated(xml)
|
|
|
|
# Add the new item to our playlist
|
|
|
|
self._add_curr_item()
|
|
|
|
log.debug('self.playlists are now: %s' % self.playlists)
|