PlexKodiConnect/resources/lib/playlist.py

204 lines
6.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2016-02-20 06:03:06 +11:00
###############################################################################
import json
2016-02-11 20:30:29 +11:00
from urllib import urlencode
import xbmc
import embydb_functions as embydb
import utils
import playbackutils
2016-02-11 20:30:29 +11:00
import PlexFunctions
import PlexAPI
2016-02-20 06:03:06 +11:00
###############################################################################
2016-01-27 03:20:13 +11:00
@utils.logging
class Playlist():
2016-06-27 00:10:32 +10:00
"""
Initiate with Playlist(typus='video' or 'music')
"""
2016-06-27 00:10:32 +10:00
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-06-27 00:10:32 +10:00
if typus == 'video':
self.playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
elif typus == 'music':
self.playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
else:
self.playlist = None
2016-06-27 00:10:32 +10:00
def __initiatePlaylist(self, itemids):
playlist = None
with embydb.GetEmbyDB() as emby_db:
for itemid in itemids:
embydb_item = emby_db.getItem_byId(itemid)
try:
mediatype = embydb_item[4]
except TypeError:
self.logMsg('Couldnt find item %s in Kodi db' % itemid, 1)
item = PlexFunctions.GetPlexMetadata(itemid)
if item in (None, 401):
continue
if PlexAPI.API(item[0]).getType() == 'track':
playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
self.logMsg('Music playlist initiated', 1)
else:
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
self.logMsg('Video playlist initiated', 1)
else:
if mediatype == 'song':
playlist = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
self.logMsg('Music playlist initiated', 1)
else:
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
self.logMsg('Video playlist initiated', 1)
break
if playlist is not None:
playlist.clear()
self.playlist = playlist
def __addToPlaylist(self, itemids, startPlayer=False):
2016-02-04 12:06:12 +11:00
started = False
with embydb.GetEmbyDB() as emby_db:
for itemid in itemids:
2016-06-27 00:10:32 +10:00
self.logMsg("Adding %s to playlist." % itemid, 1)
embydb_item = emby_db.getItem_byId(itemid)
try:
dbid = embydb_item[0]
mediatype = embydb_item[4]
except TypeError:
2016-06-27 00:10:32 +10:00
self.logMsg('Couldnt find item %s in Kodi db' % itemid, 1)
2016-02-11 20:30:29 +11:00
item = PlexFunctions.GetPlexMetadata(itemid)
2016-06-27 00:10:32 +10:00
if item in (None, 401):
2016-04-26 22:41:58 +10:00
self.logMsg('Could not download itemid %s'
% itemid, -1)
else:
2016-06-27 00:10:32 +10:00
self.addtoPlaylist_xbmc(self.playlist, item)
else:
# Add to playlist
self.addtoPlaylist(dbid, mediatype)
2016-06-27 00:10:32 +10:00
if started is False and startPlayer is True:
started = True
xbmc.Player().play(self.playlist)
2016-06-27 00:10:32 +10:00
def playAll(self, itemids, startat):
self.logMsg("---*** PLAY ALL ***---", 1)
self.logMsg("Items: %s and start at: %s" % (itemids, startat), 1)
2016-06-27 00:10:32 +10:00
if self.playlist is None:
self.__initiatePlaylist(itemids)
if self.playlist is None:
self.logMsg('Could not create playlist, abort', -1)
return
2016-06-27 00:10:32 +10:00
utils.window('plex_customplaylist', value="true")
if startat != 0:
# Seek to the starting position
utils.window('plex_customplaylist.seektime', str(startat))
self.__addToPlaylist(itemids, startPlayer=True)
self.verifyPlaylist()
def modifyPlaylist(self, itemids):
self.logMsg("---*** ADD TO PLAYLIST ***---", 1)
self.logMsg("Items: %s" % itemids, 1)
2016-06-27 00:10:32 +10:00
self.__initiatePlaylist(itemids)
self.__addToPlaylist(itemids, startPlayer=True)
self.verifyPlaylist()
2016-04-26 22:41:58 +10: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'
"""
pl = {
'jsonrpc': "2.0",
'id': 1,
'method': "Playlist.Add",
'params': {
2016-06-27 00:10:32 +10:00
'playlistid': self.playlist.getPlayListId()
}
}
if dbid is not None:
2016-06-27 00:10:32 +10:00
pl['params']['item'] = {'%sid' % utils.tryEncode(mediatype):
int(dbid)}
else:
pl['params']['item'] = {'file': url}
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2)
2016-06-27 00:10:32 +10:00
def addtoPlaylist_xbmc(self, item):
API = PlexAPI.API(item[0])
2016-02-11 20:30:29 +11:00
params = {
'mode': "play",
'dbid': 999999999,
'id': API.getRatingKey(),
'filename': API.getKey()
2016-02-11 20:30:29 +11:00
}
playurl = "plugin://plugin.video.plexkodiconnect.movies/?%s" \
% urlencode(params)
listitem = API.CreateListItemFromPlexItem()
playbackutils.PlaybackUtils(item[0]).setArtwork(listitem)
2016-06-27 00:10:32 +10:00
self.playlist.add(playurl, listitem)
def insertintoPlaylist(self, position, dbid=None, mediatype=None, url=None):
pl = {
'jsonrpc': "2.0",
'id': 1,
'method': "Playlist.Insert",
'params': {
2016-06-27 00:10:32 +10:00
'playlistid': self.playlist.getPlayListId(),
'position': position
}
}
if dbid is not None:
2016-06-27 00:10:32 +10:00
pl['params']['item'] = {'%sid' % utils.tryEncode(mediatype):
int(dbid)}
else:
pl['params']['item'] = {'file': url}
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2)
def verifyPlaylist(self):
pl = {
'jsonrpc': "2.0",
'id': 1,
'method': "Playlist.GetItems",
'params': {
2016-06-27 00:10:32 +10:00
'playlistid': self.playlist.getPlayListId(),
2016-02-07 23:26:28 +11:00
'properties': ['title', 'file']
}
}
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2)
def removefromPlaylist(self, position):
pl = {
'jsonrpc': "2.0",
'id': 1,
'method': "Playlist.Remove",
'params': {
2016-06-27 00:10:32 +10:00
'playlistid': self.playlist.getPlayListId(),
'position': position
}
}
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2)