2015-12-25 06:51:47 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
import clientinfo
|
|
|
|
import utils
|
|
|
|
|
|
|
|
##################################################################################################
|
2015-08-14 19:03:12 +10:00
|
|
|
|
2015-03-14 08:24:59 +11:00
|
|
|
|
|
|
|
class API():
|
2015-12-25 06:51:47 +11:00
|
|
|
|
|
|
|
def __init__(self, item):
|
|
|
|
|
|
|
|
self.item = item
|
|
|
|
self.clientinfo = clientinfo.ClientInfo()
|
|
|
|
self.addonName = self.clientinfo.getAddonName()
|
|
|
|
|
|
|
|
def logMsg(self, msg, lvl=1):
|
|
|
|
|
|
|
|
className = self.__class__.__name__
|
|
|
|
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
|
|
|
|
|
|
|
|
|
|
|
|
def getUserData(self):
|
|
|
|
# Default
|
|
|
|
favorite = False
|
|
|
|
playcount = None
|
|
|
|
played = False
|
|
|
|
lastPlayedDate = None
|
|
|
|
resume = 0
|
|
|
|
rating = 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
userdata = self.item['UserData']
|
|
|
|
|
|
|
|
except KeyError: # No userdata found.
|
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
|
|
|
favorite = userdata['IsFavorite']
|
|
|
|
likes = userdata.get('Likes')
|
|
|
|
# Rating for album and songs
|
|
|
|
if favorite:
|
|
|
|
rating = 5
|
|
|
|
elif likes:
|
|
|
|
rating = 3
|
|
|
|
elif likes == False:
|
|
|
|
rating = 1
|
|
|
|
else:
|
|
|
|
rating = 0
|
|
|
|
|
|
|
|
lastPlayedDate = userdata.get('LastPlayedDate')
|
|
|
|
if lastPlayedDate:
|
|
|
|
lastPlayedDate = lastPlayedDate.split('.')[0].replace('T', " ")
|
|
|
|
|
|
|
|
if userdata['Played']:
|
|
|
|
# Playcount is tied to the watch status
|
|
|
|
played = True
|
|
|
|
playcount = userdata['PlayCount']
|
|
|
|
if playcount == 0:
|
|
|
|
playcount = 1
|
|
|
|
|
|
|
|
if lastPlayedDate is None:
|
|
|
|
lastPlayedDate = self.getDateCreated()
|
|
|
|
|
|
|
|
playbackPosition = userdata.get('PlaybackPositionTicks')
|
|
|
|
if playbackPosition:
|
|
|
|
resume = playbackPosition / 10000000.0
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
'Favorite': favorite,
|
|
|
|
'PlayCount': playcount,
|
|
|
|
'Played': played,
|
|
|
|
'LastPlayedDate': lastPlayedDate,
|
|
|
|
'Resume': resume,
|
|
|
|
'Rating': rating
|
|
|
|
}
|
|
|
|
|
|
|
|
def getPeople(self):
|
2015-03-14 08:24:59 +11:00
|
|
|
# Process People
|
2015-07-18 18:08:05 +10:00
|
|
|
director = []
|
|
|
|
writer = []
|
|
|
|
cast = []
|
|
|
|
|
|
|
|
try:
|
2015-12-25 06:51:47 +11:00
|
|
|
people = self.item['People']
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2015-07-18 18:08:05 +10:00
|
|
|
else:
|
2015-03-14 08:24:59 +11:00
|
|
|
for person in people:
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
type = person['Type']
|
2015-12-25 06:51:47 +11:00
|
|
|
name = person['Name']
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
if "Director" in type:
|
2015-12-25 06:51:47 +11:00
|
|
|
director.append(name)
|
2015-07-18 18:08:05 +10:00
|
|
|
elif "Actor" in type:
|
2015-12-25 06:51:47 +11:00
|
|
|
cast.append(name)
|
|
|
|
elif type in ("Writing", "Writer"):
|
|
|
|
writer.append(name)
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
'Director': director,
|
2015-12-25 06:51:47 +11:00
|
|
|
'Writer': writer,
|
|
|
|
'Cast': cast
|
2015-07-18 18:08:05 +10:00
|
|
|
}
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getMediaStreams(self):
|
|
|
|
item = self.item
|
|
|
|
videotracks = []
|
|
|
|
audiotracks = []
|
|
|
|
subtitlelanguages = []
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
|
|
|
media_streams = item['MediaSources'][0]['MediaStreams']
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
media_streams = item['MediaStreams']
|
|
|
|
|
|
|
|
for media_stream in media_streams:
|
|
|
|
# Sort through Video, Audio, Subtitle
|
|
|
|
stream_type = media_stream['Type']
|
|
|
|
codec = media_stream.get('Codec', "").lower()
|
|
|
|
profile = media_stream.get('Profile', "").lower()
|
|
|
|
|
|
|
|
if stream_type == "Video":
|
|
|
|
# Height, Width, Codec, AspectRatio, AspectFloat, 3D
|
|
|
|
track = {
|
|
|
|
|
|
|
|
'videocodec': codec,
|
|
|
|
'height': media_stream.get('Height'),
|
|
|
|
'width': media_stream.get('Width'),
|
|
|
|
'video3DFormat': item.get('Video3DFormat'),
|
|
|
|
'aspectratio': 1.85
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
container = item['MediaSources'][0]['Container'].lower()
|
|
|
|
except:
|
|
|
|
container = ""
|
|
|
|
|
|
|
|
# Sort codec vs container/profile
|
|
|
|
if "msmpeg4" in codec:
|
|
|
|
track['videocodec'] = "divx"
|
|
|
|
elif "mpeg4" in codec:
|
|
|
|
if "simple profile" in profile or not profile:
|
|
|
|
track['videocodec'] = "xvid"
|
|
|
|
elif "h264" in codec:
|
|
|
|
if container in ("mp4", "mov", "m4v"):
|
|
|
|
track['videocodec'] = "avc1"
|
|
|
|
|
|
|
|
# Aspect ratio
|
|
|
|
if item.get('AspectRatio'):
|
|
|
|
# Metadata AR
|
|
|
|
aspectratio = item['AspectRatio']
|
|
|
|
else: # File AR
|
|
|
|
aspectratio = media_stream.get('AspectRatio', "0")
|
|
|
|
|
|
|
|
try:
|
|
|
|
aspectwidth, aspectheight = aspectratio.split(':')
|
|
|
|
track['aspectratio'] = round(float(aspectwidth) / float(aspectheight), 6)
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
width = track['width']
|
|
|
|
height = track['height']
|
|
|
|
|
|
|
|
if width and height:
|
|
|
|
track['aspectratio'] = round(float(width / height), 6)
|
|
|
|
|
|
|
|
videotracks.append(track)
|
|
|
|
|
|
|
|
elif stream_type == "Audio":
|
|
|
|
# Codec, Channels, language
|
|
|
|
track = {
|
|
|
|
|
|
|
|
'audiocodec': codec,
|
|
|
|
'channels': media_stream.get('Channels'),
|
|
|
|
'audiolanguage': media_stream.get('Language')
|
|
|
|
}
|
|
|
|
|
|
|
|
if "dca" in codec and "dts-hd ma" in profile:
|
|
|
|
track['audiocodec'] = "dtshd_ma"
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
audiotracks.append(track)
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
elif stream_type == "Subtitle":
|
|
|
|
# Language
|
|
|
|
subtitlelanguages.append(media_stream.get('Language', "Unknown"))
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
return {
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
'video': videotracks,
|
|
|
|
'audio': audiotracks,
|
|
|
|
'subtitle': subtitlelanguages
|
2015-07-18 18:08:05 +10:00
|
|
|
}
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getRuntime(self):
|
|
|
|
item = self.item
|
|
|
|
try:
|
|
|
|
runtime = item['RunTimeTicks'] / 10000000.0
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
runtime = item.get('CumulativeRunTimeTicks', 0) / 10000000.0
|
|
|
|
|
|
|
|
return runtime
|
|
|
|
|
|
|
|
def adjustResume(self, resume_seconds):
|
|
|
|
|
|
|
|
resume = 0
|
|
|
|
if resume_seconds:
|
|
|
|
resume = round(float(resume_seconds), 6)
|
|
|
|
jumpback = int(utils.settings('resumeJumpBack'))
|
|
|
|
if resume > jumpback:
|
|
|
|
# To avoid negative bookmark
|
|
|
|
resume = resume - jumpback
|
|
|
|
|
|
|
|
return resume
|
|
|
|
|
|
|
|
def getStudios(self):
|
|
|
|
# Process Studios
|
|
|
|
item = self.item
|
2015-07-18 18:08:05 +10:00
|
|
|
studios = []
|
|
|
|
|
|
|
|
try:
|
|
|
|
studio = item['SeriesStudio']
|
2015-12-25 06:51:47 +11:00
|
|
|
studios.append(self.verifyStudio(studio))
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
studioList = item['Studios']
|
|
|
|
for studio in studioList:
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
name = studio['Name']
|
|
|
|
studios.append(self.verifyStudio(name))
|
|
|
|
|
2015-03-15 04:23:45 +11:00
|
|
|
return studios
|
2015-03-14 08:24:59 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def verifyStudio(self, studioName):
|
2015-10-01 18:13:39 +10:00
|
|
|
# Convert studio for Kodi to properly detect them
|
|
|
|
studios = {
|
|
|
|
|
|
|
|
'abc (us)': "ABC",
|
|
|
|
'fox (us)': "FOX",
|
|
|
|
'mtv (us)': "MTV",
|
|
|
|
'showcase (ca)': "Showcase",
|
|
|
|
'wgn america': "WGN"
|
|
|
|
}
|
|
|
|
|
|
|
|
return studios.get(studioName.lower(), studioName)
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getChecksum(self):
|
|
|
|
# Use the etags checksum and userdata
|
|
|
|
item = self.item
|
|
|
|
userdata = item['UserData']
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
checksum = "%s%s%s%s%s%s" % (
|
2015-05-02 10:26:06 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
item['Etag'],
|
|
|
|
userdata['Played'],
|
|
|
|
userdata['IsFavorite'],
|
|
|
|
userdata['PlaybackPositionTicks'],
|
|
|
|
userdata.get('UnplayedItemCount', ""),
|
|
|
|
userdata.get('LastPlayedDate', "")
|
|
|
|
)
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
return checksum
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getGenres(self):
|
|
|
|
item = self.item
|
|
|
|
all_genres = ""
|
|
|
|
genres = item.get('Genres', item.get('SeriesGenres'))
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
if genres:
|
|
|
|
all_genres = " / ".join(genres)
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
return all_genres
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getDateCreated(self):
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
try:
|
2015-12-25 06:51:47 +11:00
|
|
|
dateadded = self.item['DateCreated']
|
2015-07-18 18:08:05 +10:00
|
|
|
dateadded = dateadded.split('.')[0].replace('T', " ")
|
2015-12-25 06:51:47 +11:00
|
|
|
except KeyError:
|
2015-08-08 22:16:51 +10:00
|
|
|
dateadded = None
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
return dateadded
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getPremiereDate(self):
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
try:
|
2015-12-25 06:51:47 +11:00
|
|
|
premiere = self.item['PremiereDate']
|
2015-07-18 18:08:05 +10:00
|
|
|
premiere = premiere.split('.')[0].replace('T', " ")
|
2015-12-25 06:51:47 +11:00
|
|
|
except KeyError:
|
2015-08-08 22:16:51 +10:00
|
|
|
premiere = None
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
return premiere
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getOverview(self):
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
try:
|
2015-12-25 06:51:47 +11:00
|
|
|
overview = self.item['Overview']
|
|
|
|
overview = overview.replace("\"", "\'")
|
|
|
|
overview = overview.replace("\n", " ")
|
|
|
|
overview = overview.replace("\r", " ")
|
|
|
|
except KeyError:
|
|
|
|
overview = ""
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
return overview
|
|
|
|
|
|
|
|
def getTagline(self):
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
try:
|
2015-12-25 06:51:47 +11:00
|
|
|
tagline = self.item['Taglines'][0]
|
|
|
|
except IndexError:
|
|
|
|
tagline = None
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
return tagline
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getProvider(self, providername):
|
2015-07-18 18:08:05 +10:00
|
|
|
|
|
|
|
try:
|
2015-12-25 06:51:47 +11:00
|
|
|
provider = self.item['ProviderIds'][providername]
|
|
|
|
except KeyError:
|
|
|
|
provider = None
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
return provider
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getMpaa(self):
|
2015-08-08 22:16:51 +10:00
|
|
|
# Convert more complex cases
|
2015-12-25 06:51:47 +11:00
|
|
|
mpaa = self.item.get('OfficialRating', "")
|
|
|
|
|
2015-09-01 13:13:42 +10:00
|
|
|
if mpaa in ("NR", "UR"):
|
2015-12-25 06:51:47 +11:00
|
|
|
# Kodi seems to not like NR, but will accept Not Rated
|
|
|
|
mpaa = "Not Rated"
|
2015-08-08 22:16:51 +10:00
|
|
|
|
|
|
|
return mpaa
|
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getCountry(self):
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
try:
|
|
|
|
country = self.item['ProductionLocations'][0]
|
|
|
|
except IndexError:
|
|
|
|
country = None
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
return country
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
def getFilePath(self):
|
2015-10-21 18:40:02 +11:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
item = self.item
|
|
|
|
try:
|
|
|
|
filepath = item['Path']
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
except KeyError:
|
|
|
|
filepath = ""
|
2015-07-18 18:08:05 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
else:
|
|
|
|
if "\\\\" in filepath:
|
|
|
|
# append smb protocol
|
|
|
|
filepath = filepath.replace("\\\\", "smb://")
|
|
|
|
filepath = filepath.replace("\\", "/")
|
|
|
|
|
|
|
|
if item.get('VideoType'):
|
|
|
|
videotype = item['VideoType']
|
|
|
|
# Specific format modification
|
|
|
|
if 'Dvd'in videotype:
|
|
|
|
filepath = "%s/VIDEO_TS/VIDEO_TS.IFO" % filepath
|
|
|
|
elif 'Bluray' in videotype:
|
|
|
|
filepath = "%s/BDMV/index.bdmv" % filepath
|
2015-06-17 16:13:21 +10:00
|
|
|
|
2015-12-25 06:51:47 +11:00
|
|
|
if "\\" in filepath:
|
|
|
|
# Local path scenario, with special videotype
|
|
|
|
filepath = filepath.replace("/", "\\")
|
|
|
|
|
|
|
|
return filepath
|