2015-10-01 13:08:34 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#################################################################################################
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
import json
|
2015-03-13 21:24:59 +00:00
|
|
|
import sys
|
|
|
|
|
2015-10-01 13:08:34 +00:00
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
2015-12-24 19:54:34 +00:00
|
|
|
import xbmcplugin
|
2015-10-01 13:08:34 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
import api
|
|
|
|
import artwork
|
|
|
|
import clientinfo
|
|
|
|
import downloadutils
|
|
|
|
import playutils as putils
|
|
|
|
import playlist
|
|
|
|
import read_embyserver as embyserver
|
|
|
|
import utils
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-10-01 13:08:34 +00:00
|
|
|
#################################################################################################
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
class PlaybackUtils():
|
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
|
|
|
|
def __init__(self, item):
|
|
|
|
|
|
|
|
self.item = item
|
|
|
|
self.API = api.API(self.item)
|
|
|
|
|
|
|
|
self.clientInfo = clientinfo.ClientInfo()
|
|
|
|
self.addonName = self.clientInfo.getAddonName()
|
|
|
|
self.doUtils = downloadutils.DownloadUtils()
|
2015-07-23 08:18:17 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
self.userid = utils.window('emby_currUser')
|
|
|
|
self.server = utils.window('emby_server%s' % self.userid)
|
|
|
|
|
|
|
|
self.artwork = artwork.Artwork()
|
|
|
|
self.emby = embyserver.Read_EmbyServer()
|
|
|
|
self.pl = playlist.Playlist()
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-10-01 13:08:34 +00:00
|
|
|
def logMsg(self, msg, lvl=1):
|
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
self.className = self.__class__.__name__
|
|
|
|
utils.logMsg("%s %s" % (self.addonName, self.className), msg, lvl)
|
2015-10-01 13:08:34 +00:00
|
|
|
|
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
def play(self, itemid, dbid=None):
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
self.logMsg("Play called.", 1)
|
2015-10-01 13:08:34 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
doUtils = self.doUtils
|
|
|
|
item = self.item
|
|
|
|
API = self.API
|
|
|
|
listitem = xbmcgui.ListItem()
|
|
|
|
playutils = putils.PlayUtils(item)
|
2015-10-01 13:08:34 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
playurl = playutils.getPlayUrl()
|
|
|
|
if not playurl:
|
|
|
|
return xbmcplugin.setResolvedUrl(int(sys.argv[1]), False, listitem)
|
|
|
|
|
|
|
|
if dbid is None:
|
|
|
|
# Item is not in Kodi database
|
|
|
|
listitem.setPath(playurl)
|
|
|
|
self.setProperties(playurl, listitem)
|
|
|
|
return xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, listitem)
|
2015-10-12 16:19:44 +00:00
|
|
|
|
|
|
|
############### ORGANIZE CURRENT PLAYLIST ################
|
|
|
|
|
|
|
|
homeScreen = xbmc.getCondVisibility('Window.IsActive(home)')
|
|
|
|
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
|
|
|
|
startPos = max(playlist.getposition(), 0) # Can return -1
|
|
|
|
sizePlaylist = playlist.size()
|
2015-12-24 19:54:34 +00:00
|
|
|
currentPosition = startPos
|
2015-10-12 16:19:44 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
propertiesPlayback = utils.window('emby_playbackProps', windowid=10101) == "true"
|
2015-10-12 16:19:44 +00:00
|
|
|
introsPlaylist = False
|
2015-10-13 08:00:32 +00:00
|
|
|
dummyPlaylist = False
|
2015-10-12 16:19:44 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
self.logMsg("Playlist start position: %s" % startPos, 1)
|
|
|
|
self.logMsg("Playlist plugin position: %s" % currentPosition, 1)
|
|
|
|
self.logMsg("Playlist size: %s" % sizePlaylist, 1)
|
2015-10-12 16:19:44 +00:00
|
|
|
|
2015-10-11 14:25:10 +00:00
|
|
|
############### RESUME POINT ################
|
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
userdata = API.getUserData()
|
|
|
|
seektime = API.adjustResume(userdata['Resume'])
|
2015-10-01 13:08:34 +00:00
|
|
|
|
2015-10-12 16:19:44 +00:00
|
|
|
# We need to ensure we add the intro and additional parts only once.
|
|
|
|
# Otherwise we get a loop.
|
|
|
|
if not propertiesPlayback:
|
2015-10-01 13:08:34 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
utils.window('emby_playbackProps', value="true", windowid=10101)
|
|
|
|
self.logMsg("Setting up properties in playlist.", 1)
|
|
|
|
|
|
|
|
if (not homeScreen and not seektime and
|
|
|
|
utils.window('emby_customPlaylist', windowid=10101) != "true"):
|
|
|
|
|
|
|
|
self.logMsg("Adding dummy file to playlist.", 2)
|
|
|
|
dummyPlaylist = True
|
|
|
|
playlist.add(playurl, listitem, index=startPos)
|
|
|
|
# Remove the original item from playlist
|
|
|
|
self.pl.removefromPlaylist(startPos+1)
|
|
|
|
# Readd the original item to playlist - via jsonrpc so we have full metadata
|
|
|
|
self.pl.insertintoPlaylist(currentPosition+1, dbid, item['Type'].lower())
|
|
|
|
currentPosition += 1
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-10-12 16:19:44 +00:00
|
|
|
############### -- CHECK FOR INTROS ################
|
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
if utils.settings('enableCinema') == "true" and not seektime:
|
2015-10-12 16:19:44 +00:00
|
|
|
# if we have any play them when the movie/show is not being resumed
|
2015-12-24 19:54:34 +00:00
|
|
|
url = "{server}/emby/Users/{UserId}/Items/%s/Intros?format=json" % itemid
|
2015-10-26 13:49:09 +00:00
|
|
|
intros = doUtils.downloadUrl(url)
|
|
|
|
|
|
|
|
if intros['TotalRecordCount'] != 0:
|
|
|
|
getTrailers = True
|
|
|
|
|
|
|
|
if utils.settings('askCinema') == "true":
|
|
|
|
resp = xbmcgui.Dialog().yesno("Emby Cinema Mode", "Play trailers?")
|
|
|
|
if not resp:
|
|
|
|
# User selected to not play trailers
|
|
|
|
getTrailers = False
|
|
|
|
self.logMsg("Skip trailers.", 1)
|
|
|
|
|
|
|
|
if getTrailers:
|
2015-10-23 04:09:01 +00:00
|
|
|
for intro in intros['Items']:
|
|
|
|
# The server randomly returns intros, process them.
|
|
|
|
introListItem = xbmcgui.ListItem()
|
2015-12-24 19:54:34 +00:00
|
|
|
introPlayurl = putils.PlayUtils(intro).getPlayUrl()
|
2015-10-23 04:09:01 +00:00
|
|
|
self.logMsg("Adding Intro: %s" % introPlayurl, 1)
|
|
|
|
|
|
|
|
# Set listitem and properties for intros
|
2015-12-24 19:54:34 +00:00
|
|
|
pbutils = PlaybackUtils(intro)
|
|
|
|
pbutils.setProperties(introPlayurl, introListItem)
|
|
|
|
|
|
|
|
self.pl.insertintoPlaylist(currentPosition, url=introPlayurl)
|
2015-10-23 04:09:01 +00:00
|
|
|
introsPlaylist = True
|
|
|
|
currentPosition += 1
|
2015-10-12 16:19:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
############### -- ADD MAIN ITEM ONLY FOR HOMESCREEN ###############
|
|
|
|
|
|
|
|
if homeScreen and not sizePlaylist:
|
2015-12-24 19:54:34 +00:00
|
|
|
# Extend our current playlist with the actual item to play
|
|
|
|
# only if there's no playlist first
|
2015-10-12 16:19:44 +00:00
|
|
|
self.logMsg("Adding main item to playlist.", 1)
|
2015-12-24 19:54:34 +00:00
|
|
|
self.pl.addtoPlaylist(dbid, item['Type'].lower())
|
|
|
|
|
2015-10-12 16:19:44 +00:00
|
|
|
# Ensure that additional parts are played after the main item
|
2015-10-11 14:25:10 +00:00
|
|
|
currentPosition += 1
|
|
|
|
|
2015-10-12 16:19:44 +00:00
|
|
|
############### -- CHECK FOR ADDITIONAL PARTS ################
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
if item.get('PartCount'):
|
2015-10-12 16:19:44 +00:00
|
|
|
# Only add to the playlist after intros have played
|
2015-12-24 19:54:34 +00:00
|
|
|
partcount = item['PartCount']
|
|
|
|
url = "{server}/emby/Videos/%s/AdditionalParts?format=json" % itemid
|
2015-10-12 16:19:44 +00:00
|
|
|
parts = doUtils.downloadUrl(url)
|
|
|
|
for part in parts['Items']:
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-10-12 16:19:44 +00:00
|
|
|
additionalListItem = xbmcgui.ListItem()
|
2015-12-24 19:54:34 +00:00
|
|
|
additionalPlayurl = putils.PlayUtils(part).getPlayUrl()
|
2015-10-12 16:19:44 +00:00
|
|
|
self.logMsg("Adding additional part: %s" % partcount, 1)
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-10-12 16:19:44 +00:00
|
|
|
# Set listitem and properties for each additional parts
|
2015-12-24 19:54:34 +00:00
|
|
|
pbutils = PlaybackUtils(part)
|
|
|
|
pbutils.setProperties(additionalPlayurl, additionalListItem)
|
|
|
|
pbutils.setArtwork(additionalListItem)
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-10-12 16:19:44 +00:00
|
|
|
playlist.add(additionalPlayurl, additionalListItem, index=currentPosition)
|
2015-12-24 19:54:34 +00:00
|
|
|
self.pl.verifyPlaylist()
|
2015-10-12 16:19:44 +00:00
|
|
|
currentPosition += 1
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
if dummyPlaylist:
|
|
|
|
# Added a dummy file to the playlist,
|
|
|
|
# because the first item is going to fail automatically.
|
|
|
|
self.logMsg("Processed as a playlist. First item is skipped.", 1)
|
|
|
|
return xbmcplugin.setResolvedUrl(int(sys.argv[1]), False, listitem)
|
2015-10-12 16:19:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
# We just skipped adding properties. Reset flag for next time.
|
|
|
|
elif propertiesPlayback:
|
|
|
|
self.logMsg("Resetting properties playback flag.", 2)
|
2015-12-24 19:54:34 +00:00
|
|
|
utils.window('emby_playbackProps', clear=True, windowid=10101)
|
|
|
|
|
|
|
|
#self.pl.verifyPlaylist()
|
|
|
|
########## SETUP MAIN ITEM ##########
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
# For transcoding only, ask for audio/subs pref
|
|
|
|
if utils.window('emby_%s.playmethod' % playurl) == "Transcode":
|
|
|
|
playurl = playutils.audioSubsPref(playurl)
|
|
|
|
utils.window('emby_%s.playmethod' % playurl, value="Transcode")
|
2015-10-13 08:00:32 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
listitem.setPath(playurl)
|
|
|
|
self.setProperties(playurl, listitem)
|
2015-10-12 16:19:44 +00:00
|
|
|
|
|
|
|
############### PLAYBACK ################
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
if homeScreen and seektime:
|
|
|
|
self.logMsg("Play as a widget item.", 1)
|
|
|
|
self.setListItem(listitem)
|
|
|
|
xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, listitem)
|
|
|
|
|
|
|
|
elif ((introsPlaylist and utils.window('emby_customPlaylist', windowid=10101) == "true") or
|
|
|
|
(homeScreen and not sizePlaylist)):
|
|
|
|
# Playlist was created just now, play it.
|
|
|
|
self.logMsg("Play playlist.", 1)
|
|
|
|
xbmc.Player().play(playlist, startpos=startPos)
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-10-12 16:19:44 +00:00
|
|
|
else:
|
|
|
|
self.logMsg("Play as a regular item.", 1)
|
2015-12-24 19:54:34 +00:00
|
|
|
xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, listitem)
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
def setProperties(self, playurl, listitem):
|
|
|
|
# Set all properties necessary for plugin path playback
|
|
|
|
item = self.item
|
|
|
|
itemid = item['Id']
|
|
|
|
itemtype = item['Type']
|
|
|
|
|
|
|
|
embyitem = "emby_%s" % playurl
|
|
|
|
utils.window('%s.runtime' % embyitem, value=str(item.get('RunTimeTicks')))
|
|
|
|
utils.window('%s.type' % embyitem, value=itemtype)
|
|
|
|
utils.window('%s.itemid' % embyitem, value=itemid)
|
2015-08-24 10:55:37 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
if itemtype == "Episode":
|
|
|
|
utils.window('%s.refreshid' % embyitem, value=item.get('SeriesId'))
|
|
|
|
else:
|
|
|
|
utils.window('%s.refreshid' % embyitem, value=itemid)
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
# Append external subtitles to stream
|
|
|
|
playmethod = utils.window('%s.playmethod' % embyitem)
|
|
|
|
# Only for direct play and direct stream
|
|
|
|
subtitles = self.externalSubs(playurl)
|
|
|
|
if playmethod in ("DirectStream", "Transcode"):
|
|
|
|
# Direct play automatically appends external
|
|
|
|
listitem.setSubtitles(subtitles)
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
self.setArtwork(listitem)
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
def externalSubs(self, playurl):
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-08-24 10:55:37 +00:00
|
|
|
externalsubs = []
|
|
|
|
mapping = {}
|
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
item = self.item
|
|
|
|
itemid = item['Id']
|
|
|
|
try:
|
|
|
|
mediastreams = item['MediaSources'][0]['MediaStreams']
|
|
|
|
except (TypeError, KeyError, IndexError):
|
|
|
|
return
|
|
|
|
|
2015-08-24 10:55:37 +00:00
|
|
|
kodiindex = 0
|
2015-12-24 19:54:34 +00:00
|
|
|
for stream in mediastreams:
|
|
|
|
|
2015-08-24 10:55:37 +00:00
|
|
|
index = stream['Index']
|
|
|
|
# Since Emby returns all possible tracks together, have to pull only external subtitles.
|
|
|
|
# IsTextSubtitleStream if true, is available to download from emby.
|
2015-12-24 19:54:34 +00:00
|
|
|
if (stream['Type'] == "Subtitle" and
|
|
|
|
stream['IsExternal'] and stream['IsTextSubtitleStream']):
|
2015-08-24 10:55:37 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
# Direct stream
|
|
|
|
url = ("%s/Videos/%s/%s/Subtitles/%s/Stream.srt"
|
|
|
|
% (self.server, itemid, itemid, index))
|
2015-08-24 10:55:37 +00:00
|
|
|
|
|
|
|
# map external subtitles for mapping
|
|
|
|
mapping[kodiindex] = index
|
|
|
|
externalsubs.append(url)
|
|
|
|
kodiindex += 1
|
|
|
|
|
|
|
|
mapping = json.dumps(mapping)
|
2015-12-24 19:54:34 +00:00
|
|
|
utils.window('emby_%s.indexMapping' % playurl, value=mapping)
|
2015-08-24 10:55:37 +00:00
|
|
|
|
|
|
|
return externalsubs
|
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
def setArtwork(self, listItem):
|
|
|
|
# Set up item and item info
|
|
|
|
item = self.item
|
|
|
|
artwork = self.artwork
|
2015-10-12 16:19:44 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
allartwork = artwork.getAllArtwork(item, parentInfo=True)
|
|
|
|
# Set artwork for listitem
|
|
|
|
arttypes = {
|
|
|
|
|
|
|
|
'poster': "Primary",
|
|
|
|
'tvshow.poster': "Primary",
|
|
|
|
'clearart': "Art",
|
|
|
|
'tvshow.clearart': "Art",
|
|
|
|
'clearlogo': "Logo",
|
|
|
|
'tvshow.clearlogo': "Logo",
|
|
|
|
'discart': "Disc",
|
|
|
|
'fanart_image': "Backdrop",
|
|
|
|
'landscape': "Thumb"
|
|
|
|
}
|
|
|
|
for arttype in arttypes:
|
2015-08-06 08:09:45 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
art = arttypes[arttype]
|
|
|
|
if art == "Backdrop":
|
|
|
|
try: # Backdrop is a list, grab the first backdrop
|
|
|
|
self.setArtProp(listItem, arttype, allartwork[art][0])
|
|
|
|
except: pass
|
|
|
|
else:
|
|
|
|
self.setArtProp(listItem, arttype, allartwork[art])
|
2015-10-11 14:25:10 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
def setArtProp(self, listItem, arttype, path):
|
2015-10-01 13:08:34 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
if arttype in (
|
|
|
|
'thumb', 'fanart_image', 'small_poster', 'tiny_poster',
|
|
|
|
'medium_landscape', 'medium_poster', 'small_fanartimage',
|
|
|
|
'medium_fanartimage', 'fanart_noindicators'):
|
|
|
|
|
|
|
|
listItem.setProperty(arttype, path)
|
2015-03-13 21:24:59 +00:00
|
|
|
else:
|
2015-12-24 19:54:34 +00:00
|
|
|
listItem.setArt({arttype: path})
|
|
|
|
|
|
|
|
def setListItem(self, listItem):
|
2015-10-01 13:08:34 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
item = self.item
|
|
|
|
type = item['Type']
|
|
|
|
API = self.API
|
|
|
|
people = API.getPeople()
|
|
|
|
studios = API.getStudios()
|
2015-10-01 13:08:34 +00:00
|
|
|
|
|
|
|
metadata = {
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-12-24 19:54:34 +00:00
|
|
|
'title': item.get('Name', "Missing name"),
|
|
|
|
'year': item.get('ProductionYear'),
|
|
|
|
'plot': API.getOverview(),
|
2015-10-01 13:08:34 +00:00
|
|
|
'director': people.get('Director'),
|
|
|
|
'writer': people.get('Writer'),
|
2015-12-24 19:54:34 +00:00
|
|
|
'mpaa': API.getMpaa(),
|
|
|
|
'genre': " / ".join(item['Genres']),
|
2015-10-01 13:08:34 +00:00
|
|
|
'studio': " / ".join(studios),
|
2015-12-24 19:54:34 +00:00
|
|
|
'aired': API.getPremiereDate(),
|
|
|
|
'rating': item.get('CommunityRating'),
|
|
|
|
'votes': item.get('VoteCount')
|
2015-10-01 13:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if "Episode" in type:
|
|
|
|
# Only for tv shows
|
2015-12-24 19:54:34 +00:00
|
|
|
thumbId = item.get('SeriesId')
|
|
|
|
season = item.get('ParentIndexNumber', -1)
|
|
|
|
episode = item.get('IndexNumber', -1)
|
|
|
|
show = item.get('SeriesName', "")
|
2015-10-01 13:08:34 +00:00
|
|
|
|
|
|
|
metadata['TVShowTitle'] = show
|
|
|
|
metadata['season'] = season
|
|
|
|
metadata['episode'] = episode
|
|
|
|
|
|
|
|
listItem.setProperty('IsPlayable', 'true')
|
|
|
|
listItem.setProperty('IsFolder', 'false')
|
2015-10-12 16:19:44 +00:00
|
|
|
listItem.setLabel(metadata['title'])
|
2015-12-24 19:54:34 +00:00
|
|
|
listItem.setInfo('video', infoLabels=metadata)
|