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
|
|
|
|
|
|
|
import json
|
2016-02-11 20:30:29 +11:00
|
|
|
from urllib import urlencode
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
import xbmc
|
|
|
|
|
|
|
|
import embydb_functions as embydb
|
|
|
|
import utils
|
2016-05-08 21:17:04 +10:00
|
|
|
import playbackutils
|
2016-02-11 20:30:29 +11:00
|
|
|
import PlexFunctions
|
|
|
|
import PlexAPI
|
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-01-27 03:20:13 +11:00
|
|
|
@utils.logging
|
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')
|
|
|
|
"""
|
|
|
|
def __init__(self, typus=None):
|
2016-03-11 02:02:46 +11:00
|
|
|
self.userid = utils.window('currUserId')
|
|
|
|
self.server = utils.window('pms_server')
|
2016-07-21 02:36:31 +10:00
|
|
|
# Construct the Kodi playlist instance
|
2016-06-27 00:10:32 +10:00
|
|
|
if typus == 'video':
|
|
|
|
self.playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
|
2016-07-21 02:36:31 +10:00
|
|
|
self.typus = 'video'
|
2016-07-20 02:22:34 +10:00
|
|
|
self.logMsg('Initiated video playlist', 1)
|
2016-06-27 00:10:32 +10:00
|
|
|
elif typus == 'music':
|
|
|
|
self.playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
|
2016-07-21 02:36:31 +10:00
|
|
|
self.typus = 'music'
|
2016-07-20 02:22:34 +10:00
|
|
|
self.logMsg('Initiated music playlist', 1)
|
2016-06-27 00:10:32 +10:00
|
|
|
else:
|
|
|
|
self.playlist = None
|
2016-07-21 02:36:31 +10:00
|
|
|
self.typus = None
|
2016-07-20 02:22:34 +10:00
|
|
|
if self.playlist is not None:
|
|
|
|
self.playlistId = self.playlist.getPlayListId()
|
2016-07-21 02:36:31 +10:00
|
|
|
# "interal" PKC playlist
|
|
|
|
self.items = []
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-07-21 02:36:31 +10:00
|
|
|
def clear(self):
|
|
|
|
"""
|
|
|
|
Empties current Kodi playlist and internal self.items list
|
|
|
|
"""
|
|
|
|
self.logMsg('Clearing playlist', 1)
|
|
|
|
self.playlist.clear()
|
|
|
|
self.items = []
|
|
|
|
|
|
|
|
def _initiatePlaylist(self):
|
2016-07-20 02:22:34 +10:00
|
|
|
self.logMsg('Initiating playlist', 1)
|
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-07-20 02:22:34 +10:00
|
|
|
self.logMsg('Couldnt find item %s in Kodi db'
|
|
|
|
% itemid, 1)
|
2016-06-27 00:10:32 +10:00
|
|
|
item = PlexFunctions.GetPlexMetadata(itemid)
|
|
|
|
if item in (None, 401):
|
2016-07-20 02:22:34 +10:00
|
|
|
self.logMsg('Couldnt find item %s on PMS, trying next'
|
|
|
|
% itemid, 1)
|
2016-06-27 00:10:32 +10:00
|
|
|
continue
|
|
|
|
if PlexAPI.API(item[0]).getType() == 'track':
|
|
|
|
playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
|
|
|
|
self.logMsg('Music playlist initiated', 1)
|
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)
|
|
|
|
self.logMsg('Video playlist initiated', 1)
|
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)
|
|
|
|
self.logMsg('Music playlist initiated', 1)
|
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)
|
|
|
|
self.logMsg('Video playlist initiated', 1)
|
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-07-21 02:36:31 +10:00
|
|
|
def _addToPlaylist(self, startitem, startPlayer=False):
|
2016-02-04 12:06:12 +11:00
|
|
|
started = False
|
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-07-21 02:36:31 +10:00
|
|
|
self.logMsg('Couldnt find item %s in Kodi db' % plexId, 1)
|
|
|
|
xml = PlexFunctions.GetPlexMetadata(plexId)
|
|
|
|
if xml in (None, 401):
|
|
|
|
self.logMsg('Could not download plexId %s'
|
|
|
|
% plexId, -1)
|
2016-04-08 02:29:23 +10:00
|
|
|
else:
|
2016-07-21 02:36:31 +10:00
|
|
|
self.logMsg('Downloaded xml metadata, adding now', 1)
|
|
|
|
self._addtoPlaylist_xbmc(xml[0])
|
2016-02-10 23:50:04 +11:00
|
|
|
else:
|
|
|
|
# Add to playlist
|
2016-07-19 23:08:28 +10:00
|
|
|
self.logMsg("Adding %s PlexId %s, KodiId %s to playlist."
|
2016-07-21 02:36:31 +10:00
|
|
|
% (mediatype, plexId, kodiId), 1)
|
|
|
|
self.addtoPlaylist(kodiId, mediatype)
|
|
|
|
# Add the kodiId
|
|
|
|
if kodiId is not None:
|
|
|
|
item['kodiId'] = str(kodiId)
|
|
|
|
if (started is False and
|
|
|
|
startPlayer is True and
|
|
|
|
startitem[1] == item[startitem[0]]):
|
2016-06-27 00:10:32 +10:00
|
|
|
started = True
|
2016-07-21 02:36:31 +10:00
|
|
|
xbmc.Player().play(self.playlist, startpos=pos)
|
|
|
|
if (started is False and
|
|
|
|
startPlayer is True and
|
|
|
|
len(self.playlist) > 0):
|
|
|
|
self.logMsg('Never received a starting item for playlist, '
|
|
|
|
'starting with the first entry', 1)
|
|
|
|
xbmc.Player().play(self.playlist)
|
|
|
|
|
|
|
|
def playAll(self, items, startitem, offset):
|
|
|
|
"""
|
|
|
|
items: list of dicts of the form
|
|
|
|
{
|
|
|
|
'queueId': Plex playQueueItemID, e.g. '29175'
|
|
|
|
'plexId': Plex ratingKey, e.g. '125'
|
|
|
|
'kodiId': Kodi's db id of the same item
|
|
|
|
}
|
2016-02-10 23:50:04 +11:00
|
|
|
|
2016-07-21 02:36:31 +10:00
|
|
|
startitem: tuple (typus, id), where typus is either 'queueId' or
|
|
|
|
'plexId' and id is the corresponding id as a string
|
|
|
|
offset: First item's time offset to play in Kodi time (an int)
|
|
|
|
"""
|
2016-06-27 00:10:32 +10:00
|
|
|
self.logMsg("---*** PLAY ALL ***---", 1)
|
2016-07-21 02:36:31 +10:00
|
|
|
self.logMsg('Startitem: %s, offset: %s, items: %s'
|
|
|
|
% (startitem, offset, items), 1)
|
|
|
|
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:
|
|
|
|
self.logMsg('Could not create playlist, abort', -1)
|
|
|
|
return
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-06-27 00:10:32 +10:00
|
|
|
utils.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-07-21 02:36:31 +10:00
|
|
|
utils.window('plex_customplaylist.seektime', str(offset))
|
|
|
|
self._addToPlaylist(startitem, startPlayer=True)
|
|
|
|
# Log playlist
|
2015-12-25 06:51:47 +11:00
|
|
|
self.verifyPlaylist()
|
2016-07-21 02:36:31 +10:00
|
|
|
self.logMsg('Internal playlist: %s' % self.items, 2)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def modifyPlaylist(self, itemids):
|
2016-04-26 21:53:19 +10:00
|
|
|
self.logMsg("---*** ADD TO PLAYLIST ***---", 1)
|
|
|
|
self.logMsg("Items: %s" % itemids, 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-07-20 02:22:34 +10:00
|
|
|
self._initiatePlaylist(itemids)
|
2016-07-19 23:08:28 +10:00
|
|
|
self._addToPlaylist(itemids, startPlayer=True)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
self.verifyPlaylist()
|
2016-04-26 22:41:58 +10:00
|
|
|
|
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'
|
|
|
|
"""
|
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-06-27 00:10:32 +10:00
|
|
|
pl['params']['item'] = {'%sid' % utils.tryEncode(mediatype):
|
|
|
|
int(dbid)}
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
pl['params']['item'] = {'file': url}
|
2016-04-26 21:53:19 +10:00
|
|
|
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2)
|
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-05-08 21:17:04 +10:00
|
|
|
'dbid': 999999999,
|
|
|
|
'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
|
|
|
|
|
|
|
def insertintoPlaylist(self, position, dbid=None, mediatype=None, url=None):
|
|
|
|
|
|
|
|
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-06-27 00:10:32 +10:00
|
|
|
pl['params']['item'] = {'%sid' % utils.tryEncode(mediatype):
|
|
|
|
int(dbid)}
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
pl['params']['item'] = {'file': url}
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def verifyPlaylist(self):
|
|
|
|
|
|
|
|
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-04-26 21:53:19 +10:00
|
|
|
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2)
|
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-04-26 21:53:19 +10:00
|
|
|
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2)
|