2015-03-14 08:24:59 +11:00
# API.py
# This class helps translate more complex cases from the MediaBrowser API to the XBMC API
from datetime import datetime
2015-03-15 05:10:27 +11:00
import xbmc
import xbmcgui
import xbmcaddon
2015-03-14 08:24:59 +11:00
class API ( ) :
def getPeople ( self , item ) :
# Process People
2015-03-15 04:23:45 +11:00
director = [ ]
writer = [ ]
2015-03-14 08:24:59 +11:00
cast = [ ]
people = item . get ( " People " )
if ( people != None ) :
for person in people :
if ( person . get ( " Type " ) == " Director " ) :
2015-03-15 04:23:45 +11:00
director . append ( person . get ( " Name " ) )
2015-03-14 08:24:59 +11:00
if ( person . get ( " Type " ) == " Writing " ) :
2015-03-15 04:23:45 +11:00
writer . append ( person . get ( " Name " ) )
2015-03-14 08:24:59 +11:00
if ( person . get ( " Type " ) == " Writer " ) :
2015-03-15 04:23:45 +11:00
writer . append ( person . get ( " Name " ) )
if ( person . get ( " Type " ) == " Actor " ) :
Name = person . get ( " Name " )
Role = person . get ( " Role " )
if Role == None :
Role = ' '
cast . append ( Name )
2015-03-14 08:24:59 +11:00
return { ' Director ' : director ,
' Writer ' : writer ,
' Cast ' : cast
}
def getTimeInfo ( self , item ) :
resumeTime = ' '
userData = item . get ( " UserData " )
PlaybackPositionTicks = ' 100 '
if userData . get ( " PlaybackPositionTicks " ) != None :
PlaybackPositionTicks = str ( userData . get ( " PlaybackPositionTicks " ) )
reasonableTicks = int ( userData . get ( " PlaybackPositionTicks " ) ) / 1000
resumeTime = reasonableTicks / 10000
try :
tempDuration = str ( int ( item . get ( " RunTimeTicks " , " 0 " ) ) / ( 10000000 * 60 ) )
except TypeError :
try :
tempDuration = str ( int ( item . get ( " CumulativeRunTimeTicks " ) ) / ( 10000000 * 60 ) )
except TypeError :
tempDuration = " 0 "
cappedPercentage = None
resume = 0
percentage = 0
if ( resumeTime != " " and int ( resumeTime ) > 0 ) :
duration = float ( tempDuration )
if ( duration > 0 ) :
resume = float ( resumeTime ) / 60
percentage = int ( ( resume / duration ) * 100.0 )
return { ' Duration ' : tempDuration ,
' TotalTime ' : tempDuration ,
' Percent ' : str ( percentage ) ,
' ResumeTime ' : str ( resume )
}
2015-03-15 04:23:45 +11:00
def getStudios ( self , item ) :
2015-03-14 08:24:59 +11:00
# Process Studio
2015-03-15 04:23:45 +11:00
studios = [ ]
2015-03-14 08:24:59 +11:00
if item . get ( " SeriesStudio " ) != None and item . get ( " SeriesStudio " ) != ' ' :
2015-03-15 04:23:45 +11:00
studios . append ( item . get ( " SeriesStudio " ) )
else :
2015-03-19 07:49:49 +11:00
if ( item . get ( " Studios " ) != [ ] ) :
2015-03-15 04:23:45 +11:00
for studio_string in item . get ( " Studios " ) :
2015-03-17 10:04:29 +11:00
temp = studio_string . get ( " Name " )
2015-03-15 04:23:45 +11:00
studios . append ( temp )
return studios
2015-03-14 08:24:59 +11:00
def getMediaStreams ( self , item , mediaSources = False ) :
# Process MediaStreams
channels = ' '
videocodec = ' '
audiocodec = ' '
height = ' '
width = ' '
aspectratio = ' 1:1 '
aspectfloat = 1.85
2015-05-03 08:44:35 +10:00
Video3DFormat = ' '
2015-03-14 08:24:59 +11:00
if mediaSources == True :
mediaSources = item . get ( " MediaSources " )
if ( mediaSources != None ) :
MediaStreams = mediaSources [ 0 ] . get ( " MediaStreams " )
else :
MediaStreams = None
else :
MediaStreams = item . get ( " MediaStreams " )
if ( MediaStreams != None ) :
#mediaStreams = MediaStreams[0].get("MediaStreams")
if ( MediaStreams != None ) :
for mediaStream in MediaStreams :
if ( mediaStream . get ( " Type " ) == " Video " ) :
videocodec = mediaStream . get ( " Codec " )
2015-05-02 10:57:58 +10:00
height = int ( mediaStream . get ( " Height " ) )
width = int ( mediaStream . get ( " Width " ) )
2015-03-14 08:24:59 +11:00
aspectratio = mediaStream . get ( " AspectRatio " )
2015-05-03 08:44:35 +10:00
Video3DFormat = item . get ( " Video3DFormat " )
2015-03-14 08:24:59 +11:00
if aspectratio != None and len ( aspectratio ) > = 3 :
try :
aspectwidth , aspectheight = aspectratio . split ( ' : ' )
aspectfloat = float ( aspectwidth ) / float ( aspectheight )
except :
aspectfloat = 1.85
if ( mediaStream . get ( " Type " ) == " Audio " ) :
audiocodec = mediaStream . get ( " Codec " )
channels = mediaStream . get ( " Channels " )
return { ' channels ' : str ( channels ) ,
' videocodec ' : videocodec ,
' audiocodec ' : audiocodec ,
' height ' : height ,
' width ' : width ,
2015-05-03 08:44:35 +10:00
' aspectratio ' : aspectfloat ,
' 3dformat ' : Video3DFormat
2015-03-14 08:24:59 +11:00
}
2015-05-02 10:26:06 +10:00
def getChecksum ( self , item ) :
2015-05-03 09:46:11 +10:00
# use the etags checksum for this if available
# AND the userdata
2015-05-02 10:26:06 +10:00
checksum = " "
2015-05-03 00:34:59 +10:00
2015-05-03 07:14:04 +10:00
if item . get ( " Etag " ) != None :
checksum = item . get ( " Etag " )
userData = item . get ( " UserData " )
2015-05-03 09:46:11 +10:00
if ( userData != None ) :
checksum + = str ( userData . get ( " Played " ) )
checksum + = str ( userData . get ( " IsFavorite " ) )
if userData . get ( ' UnplayedItemCount ' ) != None :
checksum + = str ( userData . get ( " UnplayedItemCount " ) )
if userData . get ( ' LastPlayedDate ' ) != None :
checksum + = str ( userData . get ( " LastPlayedDate " ) )
if userData . get ( ' PlaybackPositionTicks ' ) != None :
checksum + = str ( userData . get ( " PlaybackPositionTicks " ) )
2015-05-02 10:26:06 +10:00
return checksum
2015-03-14 08:24:59 +11:00
def getUserData ( self , item ) :
userData = item . get ( " UserData " )
resumeTime = 0
if ( userData != None ) :
if userData . get ( " Played " ) != True :
watched = " True "
else :
watched = " False "
if userData . get ( " IsFavorite " ) == True :
2015-05-03 09:46:11 +10:00
favorite = True
2015-03-14 08:24:59 +11:00
else :
2015-05-03 09:46:11 +10:00
favorite = False
2015-04-30 14:10:37 +10:00
if ( userData . get ( " Played " ) == True ) :
playcount = " 1 "
2015-03-14 08:24:59 +11:00
else :
2015-04-30 14:10:37 +10:00
playcount = " 0 "
2015-03-14 08:24:59 +11:00
if userData . get ( ' UnplayedItemCount ' ) != None :
2015-04-30 14:10:37 +10:00
UnplayedItemCount = userData . get ( ' UnplayedItemCount ' )
2015-03-14 08:24:59 +11:00
else :
UnplayedItemCount = " 0 "
2015-03-25 07:03:07 +11:00
if userData . get ( ' LastPlayedDate ' ) != None :
#TODO--> is there some other way to do this ?
datestring = userData . get ( ' LastPlayedDate ' ) . split ( ' T ' ) [ 0 ]
timestring = userData . get ( ' LastPlayedDate ' ) . split ( ' T ' ) [ 1 ]
timestring = timestring . split ( ' . ' ) [ 0 ]
LastPlayedDate = datestring + " " + timestring
else :
LastPlayedDate = None
2015-03-14 08:24:59 +11:00
if userData . get ( ' PlaybackPositionTicks ' ) != None :
PlaybackPositionTicks = userData . get ( ' PlaybackPositionTicks ' )
else :
PlaybackPositionTicks = ' '
2015-04-03 10:41:39 +11:00
userKey = userData . get ( " Key " , " " )
2015-03-14 08:24:59 +11:00
return { ' Watched ' : watched ,
' Favorite ' : favorite ,
' PlayCount ' : playcount ,
2015-03-25 07:03:07 +11:00
' LastPlayedDate ' : LastPlayedDate ,
2015-03-14 08:24:59 +11:00
' UnplayedItemCount ' : UnplayedItemCount ,
2015-04-03 10:41:39 +11:00
' PlaybackPositionTicks ' : str ( PlaybackPositionTicks ) ,
' Key ' : userKey
2015-03-14 08:24:59 +11:00
}
def getGenre ( self , item ) :
genre = " "
genres = item . get ( " Genres " )
if genres != None and genres != [ ] :
for genre_string in genres :
if genre == " " : #Just take the first genre
genre = genre_string
else :
genre = genre + " / " + genre_string
elif item . get ( " SeriesGenres " ) != None and item . get ( " SeriesGenres " ) != ' ' :
genres = item . get ( " SeriesGenres " )
if genres != None and genres != [ ] :
for genre_string in genres :
if genre == " " : #Just take the first genre
genre = genre_string
else :
genre = genre + " / " + genre_string
return genre
def getName ( self , item ) :
Temp = item . get ( " Name " )
if Temp == None :
Temp = " "
Name = Temp . encode ( ' utf-8 ' )
return Name
def getRecursiveItemCount ( self , item ) :
if item . get ( " RecursiveItemCount " ) != None :
return str ( item . get ( " RecursiveItemCount " ) )
else :
return " 0 "
def getSeriesName ( self , item ) :
Temp = item . get ( " SeriesName " )
if Temp == None :
Temp = " "
Name = Temp . encode ( ' utf-8 ' )
return Name
def getOverview ( self , item ) :
Temp = item . get ( " Overview " )
if Temp == None :
Temp = ' '
Overview1 = Temp . encode ( ' utf-8 ' )
Overview = str ( Overview1 )
2015-04-12 03:45:35 +10:00
Overview = Overview . replace ( " \" " , " \' " )
2015-04-12 03:34:20 +10:00
Overview = Overview . replace ( " \n " , " " )
Overview = Overview . replace ( " \r " , " " )
2015-03-14 08:24:59 +11:00
return Overview
def getPremiereDate ( self , item ) :
if ( item . get ( " PremiereDate " ) != None ) :
premieredatelist = ( item . get ( " PremiereDate " ) ) . split ( " T " )
premieredate = premieredatelist [ 0 ]
else :
premieredate = " "
Temp = premieredate
premieredate = Temp . encode ( ' utf-8 ' )
return premieredate
def getTVInfo ( self , item , userData ) :
TotalSeasons = 0 if item . get ( " ChildCount " ) == None else item . get ( " ChildCount " )
TotalEpisodes = 0 if item . get ( " RecursiveItemCount " ) == None else item . get ( " RecursiveItemCount " )
WatchedEpisodes = 0 if userData . get ( " UnplayedItemCount " ) == None else TotalEpisodes - int ( userData . get ( " UnplayedItemCount " ) )
UnWatchedEpisodes = 0 if userData . get ( " UnplayedItemCount " ) == None else int ( userData . get ( " UnplayedItemCount " ) )
NumEpisodes = TotalEpisodes
tempEpisode = " "
if ( item . get ( " IndexNumber " ) != None ) :
episodeNum = item . get ( " IndexNumber " )
if episodeNum < 10 :
tempEpisode = " 0 " + str ( episodeNum )
else :
tempEpisode = str ( episodeNum )
tempSeason = " "
if ( str ( item . get ( " ParentIndexNumber " ) ) != None ) :
tempSeason = str ( item . get ( " ParentIndexNumber " ) )
if item . get ( " ParentIndexNumber " ) < 10 :
tempSeason = " 0 " + tempSeason
if item . get ( " SeriesName " ) != None :
temp = item . get ( " SeriesName " )
SeriesName = temp . encode ( ' utf-8 ' )
else :
SeriesName = ' '
return { ' TotalSeasons ' : str ( TotalSeasons ) ,
' TotalEpisodes ' : str ( TotalEpisodes ) ,
' WatchedEpisodes ' : str ( WatchedEpisodes ) ,
' UnWatchedEpisodes ' : str ( UnWatchedEpisodes ) ,
' NumEpisodes ' : str ( NumEpisodes ) ,
' Season ' : tempSeason ,
' Episode ' : tempEpisode ,
' SeriesName ' : SeriesName
}
2015-03-15 05:10:27 +11:00
def getDateCreated ( self , item ) :
2015-03-14 08:24:59 +11:00
tempDate = item . get ( " DateCreated " )
if tempDate != None :
tempDate = tempDate . split ( " T " ) [ 0 ]
date = tempDate . split ( " - " )
tempDate = date [ 2 ] + " . " + date [ 1 ] + " . " + date [ 0 ]
else :
tempDate = " 01.01.2000 "
2015-03-15 05:10:27 +11:00
return tempDate
def getArtwork ( self , data , type , index = " 0 " , userParentInfo = False ) :
2015-03-26 04:37:21 +11:00
addonSettings = xbmcaddon . Addon ( id = ' plugin.video.emby ' )
2015-03-15 05:10:27 +11:00
id = data . get ( " Id " )
getSeriesData = False
userData = data . get ( " UserData " )
if type == " tvshow.poster " : # Change the Id to the series to get the overall series poster
if data . get ( " Type " ) == " Season " or data . get ( " Type " ) == " Episode " :
id = data . get ( " SeriesId " )
getSeriesData = True
2015-03-20 11:58:58 +11:00
elif type == " poster " and data . get ( " Type " ) == " Episode " and addonSettings . getSetting ( ' useSeasonPoster ' ) == ' true ' : # Change the Id to the Season to get the season poster
2015-03-15 05:10:27 +11:00
id = data . get ( " SeasonId " )
if type == " poster " or type == " tvshow.poster " : # Now that the Ids are right, change type to MB3 name
type = " Primary "
if data . get ( " Type " ) == " Season " : # For seasons: primary (poster), thumb and banner get season art, rest series art
if type != " Primary " and type != " Primary2 " and type != " Primary3 " and type != " Primary4 " and type != " Thumb " and type != " Banner " and type != " Thumb3 " :
id = data . get ( " SeriesId " )
getSeriesData = True
if data . get ( " Type " ) == " Episode " : # For episodes: primary (episode thumb) gets episode art, rest series art.
if type != " Primary " and type != " Primary2 " and type != " Primary3 " and type != " Primary4 " :
id = data . get ( " SeriesId " )
getSeriesData = True
if type == " Primary2 " or type == " Primary3 " or type == " Primary4 " :
id = data . get ( " SeasonId " )
getSeriesData = True
if data . get ( " SeasonUserData " ) != None :
userData = data . get ( " SeasonUserData " )
if id == None :
id = data . get ( " Id " )
imageTag = " e3ab56fe27d389446754d0fb04910a34 " # a place holder tag, needs to be in this format
originalType = type
if type == " Primary2 " or type == " Primary3 " or type == " Primary4 " or type == " SeriesPrimary " :
type = " Primary "
if type == " Backdrop2 " or type == " Backdrop3 " or type == " BackdropNoIndicators " :
type = " Backdrop "
if type == " Thumb2 " or type == " Thumb3 " :
type = " Thumb "
if ( data . get ( " ImageTags " ) != None and data . get ( " ImageTags " ) . get ( type ) != None ) :
imageTag = data . get ( " ImageTags " ) . get ( type )
if ( data . get ( " Type " ) == " Episode " or data . get ( " Type " ) == " Season " ) and type == " Logo " :
imageTag = data . get ( " ParentLogoImageTag " )
if ( data . get ( " Type " ) == " Episode " or data . get ( " Type " ) == " Season " ) and type == " Art " :
imageTag = data . get ( " ParentArtImageTag " )
if ( data . get ( " Type " ) == " Episode " ) and originalType == " Thumb3 " :
imageTag = data . get ( " SeriesThumbImageTag " )
if ( data . get ( " Type " ) == " Season " ) and originalType == " Thumb3 " and imageTag == " e3ab56fe27d389446754d0fb04910a34 " :
imageTag = data . get ( " ParentThumbImageTag " )
id = data . get ( " SeriesId " )
query = " "
height = " 10000 "
width = " 10000 "
played = " 0 "
totalbackdrops = 0
if originalType == " BackdropNoIndicators " and index == " 0 " and data . get ( " BackdropImageTags " ) != None :
totalbackdrops = len ( data . get ( " BackdropImageTags " ) )
if totalbackdrops != 0 :
index = str ( randrange ( 0 , totalbackdrops ) )
# use the local image proxy server that is made available by this addons service
2015-04-14 04:56:36 +10:00
# Load user information set by UserClient
WINDOW = xbmcgui . Window ( 10000 )
username = WINDOW . getProperty ( ' currUser ' )
server = WINDOW . getProperty ( ' server %s ' % username )
2015-03-20 11:58:58 +11:00
if addonSettings . getSetting ( ' compressArt ' ) == ' true ' :
2015-03-15 05:10:27 +11:00
query = query + " &Quality=90 "
if imageTag == None :
imageTag = " e3ab56fe27d389446754d0fb04910a34 "
2015-04-18 09:59:12 +10:00
artwork = " %s /mediabrowser/Items/ %s /Images/ %s / %s ?MaxWidth= %s &MaxHeight= %s &Format=original&Tag= %s %s " % ( server , id , type , index , width , height , imageTag , query )
#artwork = "%s/mediabrowser/Items/%s/Images/%s/%s/%s/original/%s/%s/%s?%s" % (server, id, type, index, imageTag, width, height, played, query) <- broken
2015-03-20 11:58:58 +11:00
if addonSettings . getSetting ( ' disableCoverArt ' ) == ' true ' :
2015-03-15 05:10:27 +11:00
artwork = artwork + " &EnableImageEnhancers=false "
# do not return non-existing images
if ( ( type != " Backdrop " and imageTag == " e3ab56fe27d389446754d0fb04910a34 " ) | #Remember, this is the placeholder tag, meaning we didn't find a valid tag
( type == " Backdrop " and data . get ( " BackdropImageTags " ) != None and len ( data . get ( " BackdropImageTags " ) ) == 0 ) |
( type == " Backdrop " and data . get ( " BackdropImageTag " ) != None and len ( data . get ( " BackdropImageTag " ) ) == 0 )
) :
if type != " Backdrop " or ( type == " Backdrop " and getSeriesData == True and data . get ( " ParentBackdropImageTags " ) == None ) or ( type == " Backdrop " and getSeriesData != True ) :
artwork = ' '
return artwork
def getUserArtwork ( self , data , type , index = " 0 " ) :
2015-04-14 04:56:36 +10:00
# Load user information set by UserClient
WINDOW = xbmcgui . Window ( 10000 )
username = WINDOW . getProperty ( ' currUser ' )
server = WINDOW . getProperty ( ' server %s ' % username )
2015-03-15 05:10:27 +11:00
id = data . get ( " Id " )
2015-04-14 04:56:36 +10:00
artwork = " %s /mediabrowser/Users/ %s /Images/ %s ?Format=original " % ( server , id , type )
2015-03-15 05:10:27 +11:00
2015-04-14 04:56:36 +10:00
return artwork
2015-03-15 05:10:27 +11:00