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 read_embyserver as embyserver
|
|
|
|
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():
|
|
|
|
|
|
|
|
def __init__(self):
|
2016-03-11 02:02:46 +11:00
|
|
|
self.userid = utils.window('currUserId')
|
|
|
|
self.server = utils.window('pms_server')
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
self.emby = embyserver.Read_EmbyServer()
|
|
|
|
|
|
|
|
def playAll(self, itemids, startat):
|
2016-02-20 06:03:06 +11:00
|
|
|
window = utils.window
|
2016-02-17 19:13:37 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
embyconn = utils.kodiSQL('emby')
|
|
|
|
embycursor = embyconn.cursor()
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
|
|
|
|
|
|
|
player = xbmc.Player()
|
|
|
|
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
|
|
|
|
playlist.clear()
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.logMsg("---*** PLAY ALL ***---", 1)
|
|
|
|
self.logMsg("Items: %s and start at: %s" % (itemids, startat), 1)
|
2016-02-04 12:06:12 +11:00
|
|
|
|
|
|
|
started = False
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_customplaylist', value="true")
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-04 12:06:12 +11:00
|
|
|
if startat != 0:
|
|
|
|
# Seek to the starting position
|
2016-05-31 16:06:42 +10:00
|
|
|
window('plex_customplaylist.seektime', str(startat))
|
2015-12-25 06:51:47 +11:00
|
|
|
|
2016-02-10 23:50:04 +11:00
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
|
|
|
for itemid in itemids:
|
|
|
|
embydb_item = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
dbid = embydb_item[0]
|
|
|
|
mediatype = embydb_item[4]
|
|
|
|
except TypeError:
|
|
|
|
# Item is not found in our database, add item manually
|
2016-04-26 22:41:58 +10:00
|
|
|
self.logMsg("Item was not found in the database, manually "
|
|
|
|
"adding item.", 1)
|
2016-02-11 20:30:29 +11:00
|
|
|
item = PlexFunctions.GetPlexMetadata(itemid)
|
2016-04-08 02:29:23 +10:00
|
|
|
if item is None or item == 401:
|
2016-04-26 22:41:58 +10:00
|
|
|
self.logMsg('Could not download itemid %s'
|
|
|
|
% itemid, -1)
|
2016-04-08 02:29:23 +10:00
|
|
|
else:
|
|
|
|
self.addtoPlaylist_xbmc(playlist, item)
|
2016-02-10 23:50:04 +11:00
|
|
|
else:
|
|
|
|
# Add to playlist
|
|
|
|
self.addtoPlaylist(dbid, mediatype)
|
|
|
|
|
2016-04-26 22:41:58 +10:00
|
|
|
self.logMsg("Adding %s to playlist." % itemid, 1)
|
2016-02-10 23:50:04 +11:00
|
|
|
|
|
|
|
if not started:
|
|
|
|
started = True
|
|
|
|
player.play(playlist)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
self.verifyPlaylist()
|
|
|
|
|
|
|
|
def modifyPlaylist(self, itemids):
|
|
|
|
|
|
|
|
embyconn = utils.kodiSQL('emby')
|
|
|
|
embycursor = embyconn.cursor()
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
|
|
|
|
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-02-20 06:03:06 +11:00
|
|
|
# player = xbmc.Player()
|
2015-12-25 06:51:47 +11:00
|
|
|
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
|
|
|
|
|
|
|
|
for itemid in itemids:
|
|
|
|
embydb_item = emby_db.getItem_byId(itemid)
|
|
|
|
try:
|
|
|
|
dbid = embydb_item[0]
|
|
|
|
mediatype = embydb_item[4]
|
|
|
|
except TypeError:
|
2016-05-08 21:17:04 +10:00
|
|
|
self.logMsg('item %s not found in Kodi DB, add manually'
|
|
|
|
% itemid, 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
item = self.emby.getItem(itemid)
|
|
|
|
self.addtoPlaylist_xbmc(playlist, item)
|
|
|
|
else:
|
|
|
|
# Add to playlist
|
|
|
|
self.addtoPlaylist(dbid, mediatype)
|
|
|
|
|
2016-04-26 21:53:19 +10:00
|
|
|
self.logMsg("Adding %s to playlist." % itemid, 1)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
self.verifyPlaylist()
|
|
|
|
embycursor.close()
|
|
|
|
return playlist
|
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):
|
|
|
|
|
|
|
|
pl = {
|
|
|
|
|
|
|
|
'jsonrpc': "2.0",
|
|
|
|
'id': 1,
|
|
|
|
'method': "Playlist.Add",
|
|
|
|
'params': {
|
|
|
|
|
|
|
|
'playlistid': 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dbid is not None:
|
|
|
|
pl['params']['item'] = {'%sid' % mediatype: int(dbid)}
|
|
|
|
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 addtoPlaylist_xbmc(self, playlist, item):
|
2016-05-08 21:17:04 +10:00
|
|
|
API = PlexAPI.API(item[0])
|
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()
|
|
|
|
playbackutils.PlaybackUtils(item[0]).setArtwork(listitem)
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
playlist.add(playurl, listitem)
|
|
|
|
|
|
|
|
def insertintoPlaylist(self, position, dbid=None, mediatype=None, url=None):
|
|
|
|
|
|
|
|
pl = {
|
|
|
|
|
|
|
|
'jsonrpc': "2.0",
|
|
|
|
'id': 1,
|
|
|
|
'method': "Playlist.Insert",
|
|
|
|
'params': {
|
|
|
|
|
|
|
|
'playlistid': 1,
|
|
|
|
'position': position
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dbid is not None:
|
|
|
|
pl['params']['item'] = {'%sid' % mediatype: int(dbid)}
|
|
|
|
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-02-07 23:26:28 +11:00
|
|
|
'playlistid': 1,
|
|
|
|
'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': {
|
|
|
|
|
|
|
|
'playlistid': 1,
|
|
|
|
'position': position
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 21:53:19 +10:00
|
|
|
self.logMsg(xbmc.executeJSONRPC(json.dumps(pl)), 2)
|