2016-09-03 00:53:16 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
|
|
|
Taken from iBaa, https://github.com/iBaa/PlexConnect
|
|
|
|
Point of time: December 22, 2015
|
|
|
|
|
|
|
|
|
|
|
|
Collection of "connector functions" to Plex Media Server/MyPlex
|
|
|
|
|
|
|
|
|
|
|
|
PlexGDM:
|
|
|
|
loosely based on hippojay's plexGDM:
|
|
|
|
https://github.com/hippojay/script.plexbmc.helper... /resources/lib/plexgdm.py
|
|
|
|
|
|
|
|
|
|
|
|
Plex Media Server communication:
|
|
|
|
source (somewhat): https://github.com/hippojay/plugin.video.plexbmc
|
|
|
|
later converted from httplib to urllib2
|
|
|
|
|
|
|
|
|
|
|
|
Transcoder support:
|
|
|
|
PlexAPI_getTranscodePath() based on getTranscodeURL from pyplex/plexAPI
|
|
|
|
https://github.com/megawubs/pyplex/blob/master/plexAPI/info.py
|
|
|
|
|
|
|
|
|
|
|
|
MyPlex - Basic Authentication:
|
|
|
|
http://www.voidspace.org.uk/python/articles/urllib2.shtml
|
|
|
|
http://www.voidspace.org.uk/python/articles/authentication.shtml
|
|
|
|
http://stackoverflow.com/questions/2407126/python-urllib2-basic-auth-problem
|
|
|
|
http://stackoverflow.com/questions/111945/is-there-any-way-to-do-http-put-in-python
|
|
|
|
(and others...)
|
|
|
|
"""
|
2016-09-03 00:53:16 +10:00
|
|
|
|
|
|
|
import logging
|
2015-12-27 22:09:07 +11:00
|
|
|
import time
|
|
|
|
import urllib2
|
|
|
|
import socket
|
|
|
|
from threading import Thread
|
2016-03-24 02:57:49 +11:00
|
|
|
import xml.etree.ElementTree as etree
|
2015-12-29 04:47:16 +11:00
|
|
|
import re
|
2016-01-02 00:40:40 +11:00
|
|
|
import json
|
2016-03-16 06:26:45 +11:00
|
|
|
from urllib import urlencode, quote_plus, unquote
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-03-24 20:24:11 +11:00
|
|
|
import xbmcaddon
|
|
|
|
import xbmcgui
|
|
|
|
import xbmc
|
|
|
|
import xbmcvfs
|
2016-01-30 06:07:21 +11:00
|
|
|
|
2016-04-07 02:23:51 +10:00
|
|
|
import clientinfo
|
|
|
|
import downloadutils
|
2016-09-03 00:53:16 +10:00
|
|
|
from utils import window, settings, language as lang, tryDecode, tryEncode, \
|
|
|
|
DateToKodi
|
2016-10-30 00:23:48 +11:00
|
|
|
from PlexFunctions import PLEX_TO_KODI_TIMEFACTOR, PMSHttpsEnabled
|
2016-04-22 18:26:40 +10:00
|
|
|
import embydb_functions as embydb
|
2016-03-24 02:57:49 +11:00
|
|
|
|
2016-09-03 00:53:16 +10:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
log = logging.getLogger("PLEX."+__name__)
|
|
|
|
|
|
|
|
addonName = 'PlexKodiConnect'
|
2016-09-26 03:21:12 +10:00
|
|
|
REGEX_IMDB = re.compile(r'''/(tt\d+)''')
|
|
|
|
REGEX_TVDB = re.compile(r'''tvdb://(\d+)''')
|
2016-09-03 00:53:16 +10:00
|
|
|
###############################################################################
|
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
|
|
|
|
class PlexAPI():
|
|
|
|
# CONSTANTS
|
|
|
|
# Timeout for POST/GET commands, I guess in seconds
|
2016-03-11 02:02:46 +11:00
|
|
|
timeout = 10
|
2015-12-27 22:09:07 +11:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.g_PMS = {}
|
2016-04-06 04:10:29 +10:00
|
|
|
self.doUtils = downloadutils.DownloadUtils().downloadUrl
|
2016-09-03 00:53:16 +10:00
|
|
|
self.client = clientinfo.ClientInfo()
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-01-13 03:23:55 +11:00
|
|
|
def GetPlexLoginFromSettings(self):
|
|
|
|
"""
|
2016-03-04 23:34:30 +11:00
|
|
|
Returns a dict:
|
2016-09-03 00:53:16 +10:00
|
|
|
'plexLogin': settings('plexLogin'),
|
|
|
|
'plexToken': settings('plexToken'),
|
|
|
|
'plexhome': settings('plexhome'),
|
|
|
|
'plexid': settings('plexid'),
|
|
|
|
'myplexlogin': settings('myplexlogin'),
|
|
|
|
'plexAvatar': settings('plexAvatar'),
|
|
|
|
'plexHomeSize': settings('plexHomeSize')
|
2016-03-04 23:34:30 +11:00
|
|
|
|
2016-03-10 01:37:27 +11:00
|
|
|
Returns strings or unicode
|
2016-03-07 23:01:45 +11:00
|
|
|
|
2016-03-04 23:34:30 +11:00
|
|
|
Returns empty strings '' for a setting if not found.
|
2016-01-13 03:23:55 +11:00
|
|
|
|
2016-01-14 20:20:19 +11:00
|
|
|
myplexlogin is 'true' if user opted to log into plex.tv (the default)
|
2016-01-15 00:47:34 +11:00
|
|
|
plexhome is 'true' if plex home is used (the default)
|
2016-01-13 03:23:55 +11:00
|
|
|
"""
|
2016-03-04 23:34:30 +11:00
|
|
|
return {
|
2016-09-03 00:53:16 +10:00
|
|
|
'plexLogin': settings('plexLogin'),
|
|
|
|
'plexToken': settings('plexToken'),
|
|
|
|
'plexhome': settings('plexhome'),
|
|
|
|
'plexid': settings('plexid'),
|
|
|
|
'myplexlogin': settings('myplexlogin'),
|
|
|
|
'plexAvatar': settings('plexAvatar'),
|
|
|
|
'plexHomeSize': settings('plexHomeSize')
|
2016-03-04 23:34:30 +11:00
|
|
|
}
|
2016-01-13 03:23:55 +11:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
def GetPlexLoginAndPassword(self):
|
|
|
|
"""
|
|
|
|
Signs in to plex.tv.
|
|
|
|
|
|
|
|
plexLogin, authtoken = GetPlexLoginAndPassword()
|
|
|
|
|
|
|
|
Input: nothing
|
|
|
|
Output:
|
|
|
|
plexLogin plex.tv username
|
|
|
|
authtoken token for plex.tv
|
|
|
|
|
|
|
|
Also writes 'plexLogin' and 'token_plex.tv' to Kodi settings file
|
|
|
|
If not logged in, empty strings are returned for both.
|
|
|
|
"""
|
|
|
|
retrievedPlexLogin = ''
|
|
|
|
plexLogin = 'dummy'
|
|
|
|
authtoken = ''
|
2016-03-07 23:01:45 +11:00
|
|
|
dialog = xbmcgui.Dialog()
|
2015-12-27 22:09:07 +11:00
|
|
|
while retrievedPlexLogin == '' and plexLogin != '':
|
2016-03-04 00:00:48 +11:00
|
|
|
# Enter plex.tv username. Or nothing to cancel.
|
2016-09-03 00:53:16 +10:00
|
|
|
plexLogin = dialog.input(addonName + lang(39300),
|
2016-03-08 23:00:03 +11:00
|
|
|
type=xbmcgui.INPUT_ALPHANUM)
|
2015-12-27 22:09:07 +11:00
|
|
|
if plexLogin != "":
|
2016-03-04 00:00:48 +11:00
|
|
|
# Enter password for plex.tv user
|
2015-12-27 22:09:07 +11:00
|
|
|
plexPassword = dialog.input(
|
2016-09-03 00:53:16 +10:00
|
|
|
lang(39301) + plexLogin,
|
2015-12-27 22:09:07 +11:00
|
|
|
type=xbmcgui.INPUT_ALPHANUM,
|
2016-03-08 23:00:03 +11:00
|
|
|
option=xbmcgui.ALPHANUM_HIDE_INPUT)
|
2015-12-27 22:09:07 +11:00
|
|
|
retrievedPlexLogin, authtoken = self.MyPlexSignIn(
|
|
|
|
plexLogin,
|
|
|
|
plexPassword,
|
2016-09-03 00:53:16 +10:00
|
|
|
{'X-Plex-Client-Identifier': self.client.getDeviceId()})
|
|
|
|
log.debug("plex.tv username and token: %s, %s"
|
|
|
|
% (plexLogin, authtoken))
|
2015-12-27 22:09:07 +11:00
|
|
|
if plexLogin == '':
|
2016-03-04 00:00:48 +11:00
|
|
|
# Could not sign in user
|
2016-09-03 00:53:16 +10:00
|
|
|
dialog.ok(addonName,
|
|
|
|
lang(39302) + plexLogin)
|
2015-12-27 22:09:07 +11:00
|
|
|
# Write to Kodi settings file
|
2016-09-03 00:53:16 +10:00
|
|
|
settings('plexLogin', value=retrievedPlexLogin)
|
|
|
|
settings('plexToken', value=authtoken)
|
2015-12-27 22:09:07 +11:00
|
|
|
return (retrievedPlexLogin, authtoken)
|
|
|
|
|
2016-01-14 20:20:19 +11:00
|
|
|
def PlexTvSignInWithPin(self):
|
|
|
|
"""
|
|
|
|
Prompts user to sign in by visiting https://plex.tv/pin
|
|
|
|
|
2016-03-10 01:37:27 +11:00
|
|
|
Writes to Kodi settings file. Also returns:
|
2016-01-14 20:20:19 +11:00
|
|
|
{
|
2016-01-15 00:47:34 +11:00
|
|
|
'plexhome': 'true' if Plex Home, 'false' otherwise
|
2016-01-14 20:20:19 +11:00
|
|
|
'username':
|
2016-03-10 01:37:27 +11:00
|
|
|
'avatar': URL to user avator
|
2016-01-14 20:20:19 +11:00
|
|
|
'token':
|
2016-03-10 01:37:27 +11:00
|
|
|
'plexid': Plex user ID
|
|
|
|
'homesize': Number of Plex home users (defaults to '1')
|
2016-01-14 20:20:19 +11:00
|
|
|
}
|
|
|
|
Returns False if authentication did not work.
|
|
|
|
"""
|
|
|
|
code, identifier = self.GetPlexPin()
|
|
|
|
dialog = xbmcgui.Dialog()
|
|
|
|
if not code:
|
2016-03-04 00:00:48 +11:00
|
|
|
# Problems trying to contact plex.tv. Try again later
|
2016-09-03 00:53:16 +10:00
|
|
|
dialog.ok(addonName, lang(39303))
|
2016-01-14 20:20:19 +11:00
|
|
|
return False
|
2016-03-04 00:00:48 +11:00
|
|
|
# Go to https://plex.tv/pin and enter the code:
|
2016-03-11 02:02:46 +11:00
|
|
|
# Or press No to cancel the sign in.
|
2016-09-03 00:53:16 +10:00
|
|
|
answer = dialog.yesno(addonName,
|
|
|
|
lang(39304) + "\n\n",
|
2016-03-11 02:02:46 +11:00
|
|
|
code + "\n\n",
|
2016-09-03 00:53:16 +10:00
|
|
|
lang(39311))
|
2016-01-14 20:20:19 +11:00
|
|
|
if not answer:
|
|
|
|
return False
|
|
|
|
count = 0
|
|
|
|
# Wait for approx 30 seconds (since the PIN is not visible anymore :-))
|
2016-03-04 23:34:30 +11:00
|
|
|
while count < 30:
|
2016-01-14 20:20:19 +11:00
|
|
|
xml = self.CheckPlexTvSignin(identifier)
|
2016-03-24 02:57:49 +11:00
|
|
|
if xml is not False:
|
2016-01-14 20:20:19 +11:00
|
|
|
break
|
2016-03-04 23:34:30 +11:00
|
|
|
# Wait for 1 seconds
|
|
|
|
xbmc.sleep(1000)
|
2016-01-14 20:20:19 +11:00
|
|
|
count += 1
|
2016-03-24 02:57:49 +11:00
|
|
|
if xml is False:
|
2016-03-04 00:00:48 +11:00
|
|
|
# Could not sign in to plex.tv Try again later
|
2016-09-03 00:53:16 +10:00
|
|
|
dialog.ok(addonName, lang(39305))
|
2016-01-14 20:20:19 +11:00
|
|
|
return False
|
|
|
|
# Parse xml
|
2016-03-04 23:34:30 +11:00
|
|
|
userid = xml.attrib.get('id')
|
2016-01-14 20:20:19 +11:00
|
|
|
home = xml.get('home', '0')
|
2016-01-15 00:47:34 +11:00
|
|
|
if home == '1':
|
|
|
|
home = 'true'
|
|
|
|
else:
|
|
|
|
home = 'false'
|
2016-01-14 20:20:19 +11:00
|
|
|
username = xml.get('username', '')
|
2016-03-10 01:37:27 +11:00
|
|
|
avatar = xml.get('thumb', '')
|
2016-01-14 20:20:19 +11:00
|
|
|
token = xml.findtext('authentication-token')
|
2016-03-10 01:37:27 +11:00
|
|
|
homeSize = xml.get('homeSize', '1')
|
2016-01-14 20:20:19 +11:00
|
|
|
result = {
|
2016-01-15 00:47:34 +11:00
|
|
|
'plexhome': home,
|
2016-01-14 20:20:19 +11:00
|
|
|
'username': username,
|
|
|
|
'avatar': avatar,
|
2016-03-04 23:34:30 +11:00
|
|
|
'token': token,
|
2016-03-10 01:37:27 +11:00
|
|
|
'plexid': userid,
|
|
|
|
'homesize': homeSize
|
2016-01-14 20:20:19 +11:00
|
|
|
}
|
2016-09-03 00:53:16 +10:00
|
|
|
settings('plexLogin', username)
|
|
|
|
settings('plexToken', token)
|
|
|
|
settings('plexhome', home)
|
|
|
|
settings('plexid', userid)
|
|
|
|
settings('plexAvatar', avatar)
|
|
|
|
settings('plexHomeSize', homeSize)
|
2016-03-04 23:34:30 +11:00
|
|
|
# Let Kodi log into plex.tv on startup from now on
|
2016-09-03 00:53:16 +10:00
|
|
|
settings('myplexlogin', 'true')
|
|
|
|
settings('plex_status', value='Logged in to plex.tv')
|
2016-01-14 20:20:19 +11:00
|
|
|
return result
|
|
|
|
|
|
|
|
def CheckPlexTvSignin(self, identifier):
|
|
|
|
"""
|
|
|
|
Checks with plex.tv whether user entered the correct PIN on plex.tv/pin
|
|
|
|
|
|
|
|
Returns False if not yet done so, or the XML response file as etree
|
|
|
|
"""
|
|
|
|
# Try to get a temporary token
|
2016-04-06 04:10:29 +10:00
|
|
|
xml = self.doUtils('https://plex.tv/pins/%s.xml' % identifier,
|
2016-04-26 22:02:19 +10:00
|
|
|
authenticate=False)
|
2016-01-14 20:20:19 +11:00
|
|
|
try:
|
|
|
|
temp_token = xml.find('auth_token').text
|
|
|
|
except:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error("Could not find token in plex.tv answer")
|
2016-01-14 20:20:19 +11:00
|
|
|
return False
|
|
|
|
if not temp_token:
|
|
|
|
return False
|
|
|
|
# Use temp token to get the final plex credentials
|
2016-04-07 00:24:03 +10:00
|
|
|
xml = self.doUtils('https://plex.tv/users/account',
|
2016-04-06 04:10:29 +10:00
|
|
|
authenticate=False,
|
2016-04-26 22:02:19 +10:00
|
|
|
parameters={'X-Plex-Token': temp_token})
|
2016-01-14 20:20:19 +11:00
|
|
|
return xml
|
|
|
|
|
|
|
|
def GetPlexPin(self):
|
|
|
|
"""
|
|
|
|
For plex.tv sign-in: returns 4-digit code and identifier as 2 str
|
|
|
|
"""
|
|
|
|
code = None
|
|
|
|
identifier = None
|
|
|
|
# Download
|
2016-04-06 04:10:29 +10:00
|
|
|
xml = self.doUtils('https://plex.tv/pins.xml',
|
|
|
|
authenticate=False,
|
2016-04-26 22:02:19 +10:00
|
|
|
action_type="POST")
|
2016-01-14 20:20:19 +11:00
|
|
|
try:
|
2016-04-06 04:10:29 +10:00
|
|
|
xml.attrib
|
2016-01-14 20:20:19 +11:00
|
|
|
except:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error("Error, no PIN from plex.tv provided")
|
2016-03-07 23:01:45 +11:00
|
|
|
return None, None
|
2016-04-06 04:10:29 +10:00
|
|
|
code = xml.find('code').text
|
|
|
|
identifier = xml.find('id').text
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Successfully retrieved code and id from plex.tv')
|
2016-04-06 04:10:29 +10:00
|
|
|
return code, identifier
|
2016-01-14 20:20:19 +11:00
|
|
|
|
2016-04-07 00:24:03 +10:00
|
|
|
def CheckConnection(self, url, token=None, verifySSL=None):
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
|
|
|
Checks connection to a Plex server, available at url. Can also be used
|
2016-01-15 00:47:34 +11:00
|
|
|
to check for connection with plex.tv.
|
2016-04-05 19:20:39 +10:00
|
|
|
Will check up to 3x until reply with False
|
2015-12-27 22:09:07 +11:00
|
|
|
|
2016-04-07 00:24:03 +10:00
|
|
|
Override SSL to skip the check by setting verifySSL=False
|
|
|
|
if 'None', SSL will be checked (standard requests setting)
|
|
|
|
if 'True', SSL settings from file settings are used (False/True)
|
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
Input:
|
|
|
|
url URL to Plex server (e.g. https://192.168.1.1:32400)
|
2016-01-15 00:47:34 +11:00
|
|
|
token appropriate token to access server. If None is passed,
|
|
|
|
the current token is used
|
2015-12-27 22:09:07 +11:00
|
|
|
Output:
|
2016-01-15 00:47:34 +11:00
|
|
|
False if server could not be reached or timeout occured
|
2016-04-07 00:24:03 +10:00
|
|
|
200 if connection was successfull
|
2016-01-15 00:47:34 +11:00
|
|
|
int or other HTML status codes as received from the server
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
|
|
|
# Add '/clients' to URL because then an authentication is necessary
|
|
|
|
# If a plex.tv URL was passed, this does not work.
|
2016-04-07 00:24:03 +10:00
|
|
|
headerOptions = None
|
|
|
|
if token is not None:
|
|
|
|
headerOptions = {'X-Plex-Token': token}
|
|
|
|
if verifySSL is True:
|
2016-09-03 00:53:16 +10:00
|
|
|
verifySSL = None if settings('sslverify') == 'true' \
|
2016-04-07 00:24:03 +10:00
|
|
|
else False
|
2015-12-27 22:09:07 +11:00
|
|
|
if 'plex.tv' in url:
|
|
|
|
url = 'https://plex.tv/api/home/users'
|
|
|
|
else:
|
2016-01-14 00:56:44 +11:00
|
|
|
url = url + '/library/onDeck'
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info("Checking connection to server %s with verifySSL=%s"
|
|
|
|
% (url, verifySSL))
|
2016-04-07 00:24:03 +10:00
|
|
|
# Check up to 3 times before giving up
|
2016-04-05 19:20:39 +10:00
|
|
|
count = 0
|
|
|
|
while count < 3:
|
2016-04-07 00:24:03 +10:00
|
|
|
answer = self.doUtils(url,
|
|
|
|
authenticate=False,
|
|
|
|
headerOptions=headerOptions,
|
2016-05-30 00:52:00 +10:00
|
|
|
verifySSL=verifySSL,
|
|
|
|
timeout=4)
|
2016-05-19 04:10:20 +10:00
|
|
|
if answer is None:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info("Could not connect to %s" % url)
|
2016-04-05 19:20:39 +10:00
|
|
|
count += 1
|
2016-04-07 00:24:03 +10:00
|
|
|
xbmc.sleep(500)
|
2016-04-05 19:20:39 +10:00
|
|
|
continue
|
2016-04-07 00:24:03 +10:00
|
|
|
try:
|
2016-05-19 04:10:20 +10:00
|
|
|
# xml received?
|
2016-04-07 00:24:03 +10:00
|
|
|
answer.attrib
|
|
|
|
except:
|
2016-05-19 04:10:20 +10:00
|
|
|
if answer is True:
|
|
|
|
# Maybe no xml but connection was successful nevertheless
|
|
|
|
answer = 200
|
2016-04-05 19:20:39 +10:00
|
|
|
else:
|
2016-04-07 00:24:03 +10:00
|
|
|
# Success - we downloaded an xml!
|
|
|
|
answer = 200
|
2016-05-19 04:10:20 +10:00
|
|
|
# We could connect but maybe were not authenticated. No worries
|
2016-09-03 00:53:16 +10:00
|
|
|
log.debug("Checking connection successfull. Answer: %s" % answer)
|
2016-04-07 00:24:03 +10:00
|
|
|
return answer
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Failed to connect to %s too many times. PMS is dead' % url)
|
2016-04-05 19:20:39 +10:00
|
|
|
return False
|
2015-12-27 22:09:07 +11:00
|
|
|
|
|
|
|
def GetgPMSKeylist(self):
|
|
|
|
"""
|
|
|
|
Returns a list of all keys that are saved for every entry in the
|
|
|
|
g_PMS variable.
|
|
|
|
"""
|
|
|
|
keylist = [
|
|
|
|
'address',
|
|
|
|
'baseURL',
|
|
|
|
'enableGzip',
|
|
|
|
'ip',
|
|
|
|
'local',
|
|
|
|
'name',
|
|
|
|
'owned',
|
|
|
|
'port',
|
|
|
|
'scheme'
|
|
|
|
]
|
|
|
|
return keylist
|
|
|
|
|
|
|
|
def setgPMSToSettings(self, g_PMS):
|
|
|
|
"""
|
|
|
|
PlexDB: takes an g_PMS list of Plex servers and saves them all to
|
|
|
|
the Kodi settings file. It does NOT save the ATV_udid as that id
|
|
|
|
seems to change with reboot. Settings are set using the Plex server
|
|
|
|
machineIdentifier.
|
|
|
|
|
|
|
|
Input:
|
|
|
|
g_PMS
|
|
|
|
|
|
|
|
Output:
|
|
|
|
|
|
|
|
Assumptions:
|
|
|
|
There is only one ATV_udid in g_PMS
|
|
|
|
|
|
|
|
Existing entries for servers with the same ID get overwritten.
|
|
|
|
New entries get added. Serverinfo already set in file are set to ''.
|
|
|
|
|
|
|
|
NOTE: it is currently not possible to delete entries in Kodi settings
|
|
|
|
file!
|
|
|
|
"""
|
|
|
|
addon = xbmcaddon.Addon()
|
|
|
|
# Get rid of uppermost level ATV_udid in g_PMS
|
|
|
|
ATV_udid = list(g_PMS.keys())[0]
|
|
|
|
g_PMS = g_PMS[ATV_udid]
|
|
|
|
|
|
|
|
serverlist = []
|
|
|
|
keylist = self.getgPMSKeylist()
|
|
|
|
for serverid, servervalues in g_PMS.items():
|
|
|
|
serverlist.append(serverid)
|
|
|
|
# Set values in Kodi settings file
|
|
|
|
for item in keylist:
|
|
|
|
# Append the server's ID first, then immediatelly the setting
|
|
|
|
addon.setSetting(
|
|
|
|
str(serverid) + str(item), # the key
|
|
|
|
str(g_PMS[serverid][item]) # the value
|
|
|
|
)
|
|
|
|
# Write a new or updated 'serverlist' string to settings
|
|
|
|
oldserverlist = addon.getSetting('serverlist')
|
|
|
|
# If no server has been saved yet, return
|
|
|
|
if oldserverlist == '':
|
|
|
|
serverlist = ','.join(serverlist)
|
|
|
|
addon.setSetting('serverlist', serverlist)
|
|
|
|
return
|
|
|
|
oldserverlist = oldserverlist.split(',')
|
|
|
|
for server in oldserverlist:
|
|
|
|
# Delete serverinfo that has NOT been passed in serverlist
|
|
|
|
if server not in serverlist:
|
|
|
|
# Set old value to '', because deleting is not possible
|
|
|
|
for item in keylist:
|
|
|
|
addon.setSetting(str(server) + str(item), '')
|
|
|
|
serverlist = ','.join(serverlist)
|
|
|
|
addon.setSetting('serverlist', serverlist)
|
|
|
|
return
|
|
|
|
|
2016-04-07 00:24:03 +10:00
|
|
|
def declarePMS(self, uuid, name, scheme, ip, port):
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
|
|
|
Plex Media Server handling
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
uuid - PMS ID
|
|
|
|
name, scheme, ip, port, type, owned, token
|
|
|
|
"""
|
|
|
|
address = ip + ':' + port
|
2016-04-13 01:18:32 +10:00
|
|
|
baseURL = scheme + '://' + ip + ':' + port
|
2016-04-07 00:24:03 +10:00
|
|
|
self.g_PMS[uuid] = {
|
|
|
|
'name': name,
|
|
|
|
'scheme': scheme,
|
|
|
|
'ip': ip,
|
|
|
|
'port': port,
|
|
|
|
'address': address,
|
|
|
|
'baseURL': baseURL,
|
|
|
|
'local': '1',
|
|
|
|
'owned': '1',
|
|
|
|
'accesstoken': '',
|
|
|
|
'enableGzip': False
|
|
|
|
}
|
|
|
|
|
|
|
|
def updatePMSProperty(self, uuid, tag, value):
|
2015-12-27 22:09:07 +11:00
|
|
|
# set property element of PMS by UUID
|
2016-04-07 00:24:03 +10:00
|
|
|
try:
|
|
|
|
self.g_PMS[uuid][tag] = value
|
|
|
|
except:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('%s has not yet been declared ' % uuid)
|
2016-04-07 00:24:03 +10:00
|
|
|
return False
|
2015-12-27 22:09:07 +11:00
|
|
|
|
2016-04-07 00:24:03 +10:00
|
|
|
def getPMSProperty(self, uuid, tag):
|
2015-12-27 22:09:07 +11:00
|
|
|
# get name of PMS by UUID
|
2016-04-07 00:24:03 +10:00
|
|
|
try:
|
|
|
|
answ = self.g_PMS[uuid].get(tag, '')
|
|
|
|
except:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('%s not found in PMS catalogue' % uuid)
|
2016-04-07 00:24:03 +10:00
|
|
|
answ = False
|
|
|
|
return answ
|
2015-12-27 22:09:07 +11:00
|
|
|
|
|
|
|
def PlexGDM(self):
|
|
|
|
"""
|
|
|
|
PlexGDM
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
none
|
|
|
|
result:
|
|
|
|
PMS_list - dict() of PMSs found
|
|
|
|
"""
|
2016-04-07 00:24:03 +10:00
|
|
|
import struct
|
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
IP_PlexGDM = '239.0.0.250' # multicast to PMS
|
|
|
|
Port_PlexGDM = 32414
|
|
|
|
Msg_PlexGDM = 'M-SEARCH * HTTP/1.0'
|
2016-04-07 00:24:03 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
# setup socket for discovery -> multicast message
|
|
|
|
GDM = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
2016-04-07 00:24:03 +10:00
|
|
|
GDM.settimeout(2.0)
|
|
|
|
|
|
|
|
# Set the time-to-live for messages to 2 for local network
|
|
|
|
ttl = struct.pack('b', 2)
|
2015-12-27 22:09:07 +11:00
|
|
|
GDM.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
|
2016-04-07 00:24:03 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
returnData = []
|
|
|
|
try:
|
|
|
|
# Send data to the multicast group
|
|
|
|
GDM.sendto(Msg_PlexGDM, (IP_PlexGDM, Port_PlexGDM))
|
|
|
|
|
|
|
|
# Look for responses from all recipients
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data, server = GDM.recvfrom(1024)
|
2016-04-07 00:24:03 +10:00
|
|
|
returnData.append({'from': server,
|
|
|
|
'data': data})
|
2015-12-27 22:09:07 +11:00
|
|
|
except socket.timeout:
|
|
|
|
break
|
|
|
|
finally:
|
|
|
|
GDM.close()
|
|
|
|
|
2016-04-07 00:24:03 +10:00
|
|
|
pmsList = {}
|
|
|
|
for response in returnData:
|
|
|
|
update = {'ip': response.get('from')[0]}
|
|
|
|
# Check if we had a positive HTTP response
|
2016-05-30 00:52:00 +10:00
|
|
|
if "200 OK" not in response.get('data'):
|
|
|
|
continue
|
|
|
|
for each in response.get('data').split('\n'):
|
|
|
|
# decode response data
|
|
|
|
update['discovery'] = "auto"
|
|
|
|
# update['owned']='1'
|
|
|
|
# update['master']= 1
|
|
|
|
# update['role']='master'
|
|
|
|
|
|
|
|
if "Content-Type:" in each:
|
|
|
|
update['content-type'] = each.split(':')[1].strip()
|
|
|
|
elif "Resource-Identifier:" in each:
|
|
|
|
update['uuid'] = each.split(':')[1].strip()
|
|
|
|
elif "Name:" in each:
|
2016-09-03 00:53:16 +10:00
|
|
|
update['serverName'] = tryDecode(
|
|
|
|
each.split(':')[1].strip())
|
2016-05-30 00:52:00 +10:00
|
|
|
elif "Port:" in each:
|
|
|
|
update['port'] = each.split(':')[1].strip()
|
|
|
|
elif "Updated-At:" in each:
|
|
|
|
update['updated'] = each.split(':')[1].strip()
|
|
|
|
elif "Version:" in each:
|
|
|
|
update['version'] = each.split(':')[1].strip()
|
2016-04-07 00:24:03 +10:00
|
|
|
pmsList[update['uuid']] = update
|
|
|
|
return pmsList
|
|
|
|
|
|
|
|
def discoverPMS(self, IP_self, plexToken=None):
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
|
|
|
parameters:
|
2016-04-07 00:24:03 +10:00
|
|
|
IP_self Own IP
|
2015-12-27 22:09:07 +11:00
|
|
|
optional:
|
2016-04-07 00:24:03 +10:00
|
|
|
plexToken token for plex.tv
|
2015-12-27 22:09:07 +11:00
|
|
|
result:
|
2016-04-07 00:24:03 +10:00
|
|
|
self.g_PMS dict set
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
2016-04-07 00:24:03 +10:00
|
|
|
self.g_PMS = {}
|
2016-05-25 03:00:39 +10:00
|
|
|
# "Searching for Plex Server"
|
2016-04-07 00:24:03 +10:00
|
|
|
xbmcgui.Dialog().notification(
|
2016-09-03 00:53:16 +10:00
|
|
|
heading=addonName,
|
|
|
|
message=lang(39055),
|
2016-04-07 00:24:03 +10:00
|
|
|
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
|
2016-05-25 03:00:39 +10:00
|
|
|
time=4000,
|
2016-04-07 00:24:03 +10:00
|
|
|
sound=False)
|
|
|
|
|
|
|
|
# Look first for local PMS in the LAN
|
|
|
|
pmsList = self.PlexGDM()
|
2016-09-03 00:53:16 +10:00
|
|
|
log.debug('PMS found in the local LAN via GDM: %s' % pmsList)
|
2016-05-25 03:00:39 +10:00
|
|
|
|
|
|
|
# Get PMS from plex.tv
|
|
|
|
if plexToken:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Checking with plex.tv for more PMS to connect to')
|
2016-05-25 03:00:39 +10:00
|
|
|
self.getPMSListFromMyPlex(plexToken)
|
|
|
|
else:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('No plex token supplied, only checked LAN for PMS')
|
2016-05-25 03:00:39 +10:00
|
|
|
|
2016-04-07 00:24:03 +10:00
|
|
|
for uuid in pmsList:
|
|
|
|
PMS = pmsList[uuid]
|
2016-05-25 03:00:39 +10:00
|
|
|
if PMS['uuid'] in self.g_PMS:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.debug('We already know of PMS %s from plex.tv'
|
|
|
|
% PMS['serverName'])
|
2016-05-25 03:00:39 +10:00
|
|
|
continue
|
2016-04-07 00:24:03 +10:00
|
|
|
self.declarePMS(PMS['uuid'], PMS['serverName'], 'http',
|
|
|
|
PMS['ip'], PMS['port'])
|
2016-03-09 03:41:07 +11:00
|
|
|
# Ping to check whether we need HTTPs or HTTP
|
2016-05-25 03:00:39 +10:00
|
|
|
https = PMSHttpsEnabled('%s:%s' % (PMS['ip'], PMS['port']))
|
2016-03-24 02:07:09 +11:00
|
|
|
if https is None:
|
2016-04-07 00:24:03 +10:00
|
|
|
# Error contacting url. Skip for now
|
2016-03-24 02:07:09 +11:00
|
|
|
continue
|
2016-04-07 00:24:03 +10:00
|
|
|
elif https is True:
|
|
|
|
self.updatePMSProperty(PMS['uuid'], 'scheme', 'https')
|
|
|
|
self.updatePMSProperty(
|
|
|
|
PMS['uuid'],
|
|
|
|
'baseURL',
|
|
|
|
'https://%s:%s' % (PMS['ip'], PMS['port']))
|
2016-03-11 02:02:46 +11:00
|
|
|
else:
|
2016-04-07 00:24:03 +10:00
|
|
|
# Already declared with http
|
|
|
|
pass
|
2015-12-27 22:09:07 +11:00
|
|
|
|
2016-04-07 00:24:03 +10:00
|
|
|
# install plex.tv "virtual" PMS - for myPlex, PlexHome
|
|
|
|
# self.declarePMS('plex.tv', 'plex.tv', 'https', 'plex.tv', '443')
|
|
|
|
# self.updatePMSProperty('plex.tv', 'local', '-')
|
|
|
|
# self.updatePMSProperty('plex.tv', 'owned', '-')
|
|
|
|
# self.updatePMSProperty(
|
|
|
|
# 'plex.tv', 'accesstoken', plexToken)
|
|
|
|
# (remote and local) servers from plex.tv
|
|
|
|
|
|
|
|
def getPMSListFromMyPlex(self, token):
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
|
|
|
getPMSListFromMyPlex
|
|
|
|
|
|
|
|
get Plex media Server List from plex.tv/pms/resources
|
|
|
|
"""
|
2016-04-07 00:24:03 +10:00
|
|
|
xml = self.doUtils('https://plex.tv/api/resources',
|
2016-04-06 04:10:29 +10:00
|
|
|
authenticate=False,
|
2016-04-07 00:24:03 +10:00
|
|
|
parameters={'includeHttps': 1},
|
|
|
|
headerOptions={'X-Plex-Token': token})
|
2016-04-06 04:10:29 +10:00
|
|
|
try:
|
|
|
|
xml.attrib
|
2016-05-25 03:00:39 +10:00
|
|
|
except AttributeError:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('Could not get list of PMS from plex.tv')
|
2016-04-06 04:10:29 +10:00
|
|
|
return
|
2015-12-27 22:09:07 +11:00
|
|
|
|
2016-04-06 04:10:29 +10:00
|
|
|
import Queue
|
|
|
|
queue = Queue.Queue()
|
2016-05-30 00:52:00 +10:00
|
|
|
threadQueue = []
|
2015-12-27 22:09:07 +11:00
|
|
|
|
2016-05-25 03:00:39 +10:00
|
|
|
maxAgeSeconds = 2*60*60*24
|
2016-04-10 00:26:08 +10:00
|
|
|
for Dir in xml.findall('Device'):
|
2016-05-25 03:00:39 +10:00
|
|
|
if 'server' not in Dir.get('provides'):
|
|
|
|
# No PMS - skip
|
|
|
|
continue
|
|
|
|
if Dir.find('Connection') is None:
|
|
|
|
# no valid connection - skip
|
|
|
|
continue
|
|
|
|
|
|
|
|
# check MyPlex data age - skip if >2 days
|
|
|
|
PMS = {}
|
|
|
|
PMS['name'] = Dir.get('name')
|
|
|
|
infoAge = time.time() - int(Dir.get('lastSeenAt'))
|
|
|
|
if infoAge > maxAgeSeconds:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.debug("Server %s not seen for 2 days - skipping."
|
|
|
|
% PMS['name'])
|
2016-05-25 03:00:39 +10:00
|
|
|
continue
|
|
|
|
|
|
|
|
PMS['uuid'] = Dir.get('clientIdentifier')
|
|
|
|
PMS['token'] = Dir.get('accessToken', token)
|
|
|
|
PMS['owned'] = Dir.get('owned', '1')
|
|
|
|
PMS['local'] = Dir.get('publicAddressMatches')
|
|
|
|
PMS['ownername'] = Dir.get('sourceTitle', '')
|
|
|
|
PMS['path'] = '/'
|
|
|
|
PMS['options'] = None
|
|
|
|
|
|
|
|
# Try a local connection first
|
|
|
|
# Backup to remote connection, if that failes
|
|
|
|
PMS['connections'] = []
|
|
|
|
for Con in Dir.findall('Connection'):
|
|
|
|
if Con.get('local') == '1':
|
|
|
|
PMS['connections'].append(Con)
|
|
|
|
# Append non-local
|
|
|
|
for Con in Dir.findall('Connection'):
|
|
|
|
if Con.get('local') != '1':
|
|
|
|
PMS['connections'].append(Con)
|
|
|
|
|
|
|
|
t = Thread(target=self.pokePMS,
|
|
|
|
args=(PMS, queue))
|
2016-05-30 00:52:00 +10:00
|
|
|
threadQueue.append(t)
|
|
|
|
|
2016-09-17 23:20:43 +10:00
|
|
|
maxThreads = 5
|
2016-05-30 00:52:00 +10:00
|
|
|
threads = []
|
|
|
|
# poke PMS, own thread for each PMS
|
2016-09-17 23:20:43 +10:00
|
|
|
while True:
|
2016-05-30 00:52:00 +10:00
|
|
|
# Remove finished threads
|
|
|
|
for t in threads:
|
|
|
|
if not t.isAlive():
|
|
|
|
threads.remove(t)
|
|
|
|
if len(threads) < maxThreads:
|
|
|
|
try:
|
|
|
|
t = threadQueue.pop()
|
|
|
|
except IndexError:
|
|
|
|
# We have done our work
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
t.start()
|
|
|
|
threads.append(t)
|
|
|
|
else:
|
|
|
|
xbmc.sleep(50)
|
2016-05-25 03:00:39 +10:00
|
|
|
|
|
|
|
# wait for requests being answered
|
|
|
|
for t in threads:
|
|
|
|
t.join()
|
|
|
|
|
|
|
|
# declare new PMSs
|
|
|
|
while not queue.empty():
|
|
|
|
PMS = queue.get()
|
|
|
|
self.declarePMS(PMS['uuid'], PMS['name'],
|
|
|
|
PMS['protocol'], PMS['ip'], PMS['port'])
|
|
|
|
self.updatePMSProperty(
|
|
|
|
PMS['uuid'], 'accesstoken', PMS['token'])
|
|
|
|
self.updatePMSProperty(
|
|
|
|
PMS['uuid'], 'owned', PMS['owned'])
|
|
|
|
self.updatePMSProperty(
|
|
|
|
PMS['uuid'], 'local', PMS['local'])
|
|
|
|
# set in declarePMS, overwrite for https encryption
|
|
|
|
self.updatePMSProperty(
|
|
|
|
PMS['uuid'], 'baseURL', PMS['baseURL'])
|
|
|
|
self.updatePMSProperty(
|
|
|
|
PMS['uuid'], 'ownername', PMS['ownername'])
|
|
|
|
queue.task_done()
|
2016-04-06 04:10:29 +10:00
|
|
|
|
2016-04-07 00:24:03 +10:00
|
|
|
def pokePMS(self, PMS, queue):
|
2016-05-30 00:52:00 +10:00
|
|
|
data = PMS['connections'][0].attrib
|
|
|
|
if data['local'] == '1':
|
|
|
|
protocol = data['protocol']
|
|
|
|
address = data['address']
|
|
|
|
port = data['port']
|
2016-05-25 03:00:39 +10:00
|
|
|
url = '%s://%s:%s' % (protocol, address, port)
|
|
|
|
else:
|
2016-05-30 00:52:00 +10:00
|
|
|
url = data['uri']
|
2016-05-25 03:00:39 +10:00
|
|
|
protocol, address, port = url.split(':')
|
|
|
|
address = address.replace('/', '')
|
|
|
|
|
|
|
|
xml = self.doUtils('%s/identity' % url,
|
2016-04-06 04:10:29 +10:00
|
|
|
authenticate=False,
|
2016-04-07 00:24:03 +10:00
|
|
|
headerOptions={'X-Plex-Token': PMS['token']},
|
2016-05-25 03:00:39 +10:00
|
|
|
verifySSL=False,
|
|
|
|
timeout=3)
|
2015-12-27 22:09:07 +11:00
|
|
|
try:
|
2016-05-30 00:52:00 +10:00
|
|
|
xml.attrib['machineIdentifier']
|
|
|
|
except (AttributeError, KeyError):
|
2016-05-25 03:00:39 +10:00
|
|
|
# No connection, delete the one we just tested
|
|
|
|
del PMS['connections'][0]
|
|
|
|
if len(PMS['connections']) > 0:
|
|
|
|
# Still got connections left, try them
|
|
|
|
return self.pokePMS(PMS, queue)
|
|
|
|
return
|
2015-12-27 22:09:07 +11:00
|
|
|
else:
|
2016-05-25 03:00:39 +10:00
|
|
|
# Connection successful - correct PMS?
|
|
|
|
if xml.get('machineIdentifier') == PMS['uuid']:
|
|
|
|
# process later
|
|
|
|
PMS['baseURL'] = url
|
|
|
|
PMS['protocol'] = protocol
|
|
|
|
PMS['ip'] = address
|
|
|
|
PMS['port'] = port
|
|
|
|
queue.put(PMS)
|
|
|
|
return
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Found a PMS at %s, but the expected machineIdentifier of '
|
|
|
|
'%s did not match the one we found: %s'
|
|
|
|
% (url, PMS['uuid'], xml.get('machineIdentifier')))
|
2015-12-27 22:09:07 +11:00
|
|
|
|
|
|
|
def MyPlexSignIn(self, username, password, options):
|
|
|
|
"""
|
|
|
|
MyPlex Sign In, Sign Out
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
username - Plex forum name, MyPlex login, or email address
|
|
|
|
password
|
|
|
|
options - dict() of PlexConnect-options as received from aTV - necessary: PlexConnectUDID
|
|
|
|
result:
|
|
|
|
username
|
|
|
|
authtoken - token for subsequent communication with MyPlex
|
|
|
|
"""
|
|
|
|
# MyPlex web address
|
|
|
|
MyPlexHost = 'plex.tv'
|
|
|
|
MyPlexSignInPath = '/users/sign_in.xml'
|
|
|
|
MyPlexURL = 'https://' + MyPlexHost + MyPlexSignInPath
|
|
|
|
|
|
|
|
# create POST request
|
2016-09-03 00:53:16 +10:00
|
|
|
xargs = self.client.getXArgsDeviceInfo(options)
|
2015-12-27 22:09:07 +11:00
|
|
|
request = urllib2.Request(MyPlexURL, None, xargs)
|
|
|
|
request.get_method = lambda: 'POST'
|
|
|
|
|
|
|
|
passmanager = urllib2.HTTPPasswordMgr()
|
|
|
|
passmanager.add_password(MyPlexHost, MyPlexURL, username, password)
|
|
|
|
authhandler = urllib2.HTTPBasicAuthHandler(passmanager)
|
|
|
|
urlopener = urllib2.build_opener(authhandler)
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
# sign in, get MyPlex response
|
|
|
|
try:
|
|
|
|
response = urlopener.open(request).read()
|
|
|
|
except urllib2.HTTPError as e:
|
|
|
|
if e.code == 401:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info("Authentication failed")
|
2015-12-27 22:09:07 +11:00
|
|
|
return ('', '')
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
# analyse response
|
|
|
|
XMLTree = etree.ElementTree(etree.fromstring(response))
|
|
|
|
|
|
|
|
el_username = XMLTree.find('username')
|
|
|
|
el_authtoken = XMLTree.find('authentication-token')
|
|
|
|
if el_username is None or \
|
|
|
|
el_authtoken is None:
|
|
|
|
username = ''
|
|
|
|
authtoken = ''
|
|
|
|
else:
|
|
|
|
username = el_username.text
|
|
|
|
authtoken = el_authtoken.text
|
|
|
|
return (username, authtoken)
|
|
|
|
|
|
|
|
def MyPlexSignOut(self, authtoken):
|
2016-09-03 00:53:16 +10:00
|
|
|
"""
|
|
|
|
TO BE DONE!
|
|
|
|
"""
|
2015-12-27 22:09:07 +11:00
|
|
|
# MyPlex web address
|
|
|
|
MyPlexHost = 'plex.tv'
|
|
|
|
MyPlexSignOutPath = '/users/sign_out.xml'
|
|
|
|
MyPlexURL = 'http://' + MyPlexHost + MyPlexSignOutPath
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
# create POST request
|
2016-04-13 01:18:32 +10:00
|
|
|
xargs = {'X-Plex-Token': authtoken}
|
2015-12-27 22:09:07 +11:00
|
|
|
request = urllib2.Request(MyPlexURL, None, xargs)
|
2016-04-13 01:18:32 +10:00
|
|
|
# turn into 'POST' - done automatically with data!=None. But we don't
|
|
|
|
# have data.
|
|
|
|
request.get_method = lambda: 'POST'
|
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
response = urllib2.urlopen(request).read()
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-28 20:35:27 +11:00
|
|
|
def GetUserArtworkURL(self, username):
|
|
|
|
"""
|
|
|
|
Returns the URL for the user's Avatar. Or False if something went
|
|
|
|
wrong.
|
|
|
|
"""
|
2016-09-03 00:53:16 +10:00
|
|
|
plexToken = settings('plexToken')
|
2015-12-28 20:35:27 +11:00
|
|
|
users = self.MyPlexListHomeUsers(plexToken)
|
2016-01-08 03:27:48 +11:00
|
|
|
url = ''
|
2015-12-28 20:35:27 +11:00
|
|
|
# If an error is encountered, set to False
|
|
|
|
if not users:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info("Couldnt get user from plex.tv. No URL for user avatar")
|
2015-12-28 20:35:27 +11:00
|
|
|
return False
|
|
|
|
for user in users:
|
|
|
|
if username in user['title']:
|
|
|
|
url = user['thumb']
|
2016-09-03 00:53:16 +10:00
|
|
|
log.debug("Avatar url for user %s is: %s" % (username, url))
|
2015-12-28 20:35:27 +11:00
|
|
|
return url
|
|
|
|
|
2016-03-10 01:37:27 +11:00
|
|
|
def ChoosePlexHomeUser(self, plexToken):
|
2015-12-28 02:27:49 +11:00
|
|
|
"""
|
|
|
|
Let's user choose from a list of Plex home users. Will switch to that
|
|
|
|
user accordingly.
|
|
|
|
|
2016-03-10 01:37:27 +11:00
|
|
|
Returns a dict:
|
|
|
|
{
|
|
|
|
'username': Unicode
|
|
|
|
'userid': '' Plex ID of the user
|
|
|
|
'token': '' User's token
|
|
|
|
'protected': True if PIN is needed, else False
|
|
|
|
}
|
2015-12-28 20:35:27 +11:00
|
|
|
|
2016-03-10 01:37:27 +11:00
|
|
|
Will return False if something went wrong (wrong PIN, no connection)
|
2015-12-28 02:27:49 +11:00
|
|
|
"""
|
2016-03-07 23:01:45 +11:00
|
|
|
dialog = xbmcgui.Dialog()
|
2016-03-04 00:00:48 +11:00
|
|
|
|
2015-12-28 19:00:43 +11:00
|
|
|
# Get list of Plex home users
|
2015-12-28 02:27:49 +11:00
|
|
|
users = self.MyPlexListHomeUsers(plexToken)
|
|
|
|
if not users:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error("User download failed.")
|
2016-03-10 01:37:27 +11:00
|
|
|
return False
|
2015-12-28 02:27:49 +11:00
|
|
|
|
|
|
|
userlist = []
|
2016-03-07 23:01:45 +11:00
|
|
|
userlistCoded = []
|
2015-12-28 02:27:49 +11:00
|
|
|
for user in users:
|
|
|
|
username = user['title']
|
|
|
|
userlist.append(username)
|
2016-03-10 01:37:27 +11:00
|
|
|
# To take care of non-ASCII usernames
|
2016-09-03 00:53:16 +10:00
|
|
|
userlistCoded.append(tryEncode(username))
|
2015-12-28 19:00:43 +11:00
|
|
|
usernumber = len(userlist)
|
2016-03-10 01:37:27 +11:00
|
|
|
|
|
|
|
username = ''
|
2015-12-28 02:27:49 +11:00
|
|
|
usertoken = ''
|
2016-01-13 03:23:55 +11:00
|
|
|
trials = 0
|
|
|
|
while trials < 3:
|
2015-12-28 19:00:43 +11:00
|
|
|
if usernumber > 1:
|
2016-03-04 00:00:48 +11:00
|
|
|
# Select user
|
2016-03-07 23:01:45 +11:00
|
|
|
user_select = dialog.select(
|
2016-09-03 00:53:16 +10:00
|
|
|
addonName + lang(39306),
|
2016-03-07 23:01:45 +11:00
|
|
|
userlistCoded)
|
2015-12-28 19:17:17 +11:00
|
|
|
if user_select == -1:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info("No user selected.")
|
|
|
|
settings('username', value='')
|
2016-01-15 00:47:34 +11:00
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)'
|
2016-09-03 00:53:16 +10:00
|
|
|
% self.client.getAddonId())
|
2016-03-10 01:37:27 +11:00
|
|
|
return False
|
|
|
|
# Only 1 user received, choose that one
|
2015-12-28 19:00:43 +11:00
|
|
|
else:
|
|
|
|
user_select = 0
|
2015-12-28 19:17:17 +11:00
|
|
|
selected_user = userlist[user_select]
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info("Selected user: %s" % selected_user)
|
2015-12-28 19:17:17 +11:00
|
|
|
user = users[user_select]
|
2015-12-28 02:27:49 +11:00
|
|
|
# Ask for PIN, if protected:
|
2016-03-10 01:37:27 +11:00
|
|
|
pin = None
|
2015-12-28 02:27:49 +11:00
|
|
|
if user['protected'] == '1':
|
2016-09-03 00:53:16 +10:00
|
|
|
log.debug('Asking for users PIN')
|
2015-12-28 02:27:49 +11:00
|
|
|
pin = dialog.input(
|
2016-09-03 00:53:16 +10:00
|
|
|
lang(39307) + selected_user,
|
2016-05-30 01:45:19 +10:00
|
|
|
'',
|
|
|
|
xbmcgui.INPUT_NUMERIC,
|
|
|
|
xbmcgui.ALPHANUM_HIDE_INPUT)
|
2016-03-04 23:34:30 +11:00
|
|
|
# User chose to cancel
|
2016-03-10 01:37:27 +11:00
|
|
|
# Plex bug: don't call url for protected user with empty PIN
|
|
|
|
if not pin:
|
|
|
|
trials += 1
|
|
|
|
continue
|
2015-12-28 02:27:49 +11:00
|
|
|
# Switch to this Plex Home user, if applicable
|
2016-03-11 02:02:46 +11:00
|
|
|
result = self.PlexSwitchHomeUser(
|
2015-12-28 03:12:46 +11:00
|
|
|
user['id'],
|
2015-12-28 02:27:49 +11:00
|
|
|
pin,
|
2016-01-15 00:47:34 +11:00
|
|
|
plexToken,
|
2016-09-03 00:53:16 +10:00
|
|
|
settings('plex_machineIdentifier'))
|
2016-03-11 02:02:46 +11:00
|
|
|
if result:
|
|
|
|
# Successfully retrieved username: break out of while loop
|
|
|
|
username = result['username']
|
|
|
|
usertoken = result['usertoken']
|
|
|
|
break
|
2015-12-28 19:00:43 +11:00
|
|
|
# Couldn't get user auth
|
2016-03-11 02:02:46 +11:00
|
|
|
else:
|
2016-03-10 01:37:27 +11:00
|
|
|
trials += 1
|
2016-03-04 00:00:48 +11:00
|
|
|
# Could not login user, please try again
|
2016-09-03 00:53:16 +10:00
|
|
|
if not dialog.yesno(addonName,
|
|
|
|
lang(39308) + selected_user,
|
|
|
|
lang(39309)):
|
2016-03-04 23:34:30 +11:00
|
|
|
# User chose to cancel
|
|
|
|
break
|
2015-12-28 19:00:43 +11:00
|
|
|
if not username:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('Failed signing in a user to plex.tv')
|
2016-04-06 02:23:00 +10:00
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)'
|
2016-09-03 00:53:16 +10:00
|
|
|
% self.client.getAddonId())
|
2016-03-10 01:37:27 +11:00
|
|
|
return False
|
|
|
|
return {
|
|
|
|
'username': username,
|
|
|
|
'userid': user['id'],
|
|
|
|
'protected': True if user['protected'] == '1' else False,
|
|
|
|
'token': usertoken
|
|
|
|
}
|
|
|
|
|
|
|
|
def PlexSwitchHomeUser(self, userId, pin, token, machineIdentifier):
|
2015-12-28 02:27:49 +11:00
|
|
|
"""
|
|
|
|
Retrieves Plex home token for a Plex home user.
|
2016-03-11 02:02:46 +11:00
|
|
|
Returns False if unsuccessful
|
2015-12-28 02:27:49 +11:00
|
|
|
|
|
|
|
Input:
|
2016-01-15 00:47:34 +11:00
|
|
|
userId id of the Plex home user
|
2015-12-28 02:27:49 +11:00
|
|
|
pin PIN of the Plex home user, if protected
|
2016-01-15 00:47:34 +11:00
|
|
|
token token for plex.tv
|
2015-12-28 02:27:49 +11:00
|
|
|
|
|
|
|
Output:
|
2016-03-11 02:02:46 +11:00
|
|
|
{
|
|
|
|
'username'
|
|
|
|
'usertoken' Might be empty strings if no token found
|
|
|
|
for the machineIdentifier that was chosen
|
|
|
|
}
|
2015-12-28 02:27:49 +11:00
|
|
|
|
2016-03-11 02:02:46 +11:00
|
|
|
settings('userid') and settings('username') with new plex token
|
2015-12-28 02:27:49 +11:00
|
|
|
"""
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Switching to user %s' % userId)
|
2016-01-15 00:47:34 +11:00
|
|
|
url = 'https://plex.tv/api/home/users/' + userId + '/switch'
|
2015-12-27 22:09:07 +11:00
|
|
|
if pin:
|
2016-01-15 00:47:34 +11:00
|
|
|
url += '?pin=' + pin
|
2016-04-06 04:10:29 +10:00
|
|
|
answer = self.doUtils(url,
|
|
|
|
authenticate=False,
|
2016-04-26 22:02:19 +10:00
|
|
|
action_type="POST",
|
2016-04-06 04:10:29 +10:00
|
|
|
headerOptions={'X-Plex-Token': token})
|
2016-03-10 01:37:27 +11:00
|
|
|
try:
|
|
|
|
answer.attrib
|
|
|
|
except:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('Error: plex.tv switch HomeUser change failed')
|
2016-03-11 02:02:46 +11:00
|
|
|
return False
|
2015-12-28 02:27:49 +11:00
|
|
|
|
2016-01-15 00:47:34 +11:00
|
|
|
username = answer.attrib.get('title', '')
|
|
|
|
token = answer.attrib.get('authenticationToken', '')
|
2015-12-28 02:27:49 +11:00
|
|
|
|
2016-03-11 02:02:46 +11:00
|
|
|
# Write to settings file
|
2016-09-03 00:53:16 +10:00
|
|
|
settings('username', username)
|
|
|
|
settings('accessToken', token)
|
|
|
|
settings('userid', answer.attrib.get('id', ''))
|
|
|
|
settings('plex_restricteduser',
|
|
|
|
'true' if answer.attrib.get('restricted', '0') == '1'
|
|
|
|
else 'false')
|
2016-03-11 02:02:46 +11:00
|
|
|
|
|
|
|
# Get final token to the PMS we've chosen
|
2016-03-10 01:37:27 +11:00
|
|
|
url = 'https://plex.tv/api/resources?includeHttps=1'
|
2016-04-06 04:10:29 +10:00
|
|
|
xml = self.doUtils(url,
|
|
|
|
authenticate=False,
|
|
|
|
headerOptions={'X-Plex-Token': token})
|
2016-03-10 01:37:27 +11:00
|
|
|
try:
|
|
|
|
xml.attrib
|
|
|
|
except:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('Answer from plex.tv not as excepted')
|
2016-03-11 02:02:46 +11:00
|
|
|
# Set to empty iterable list for loop
|
|
|
|
xml = []
|
2016-01-15 00:47:34 +11:00
|
|
|
|
|
|
|
found = 0
|
2016-09-03 00:53:16 +10:00
|
|
|
log.debug('Our machineIdentifier is %s' % machineIdentifier)
|
2016-03-10 01:37:27 +11:00
|
|
|
for device in xml:
|
2016-03-11 02:02:46 +11:00
|
|
|
identifier = device.attrib.get('clientIdentifier')
|
2016-09-03 00:53:16 +10:00
|
|
|
log.debug('Found a Plex machineIdentifier: %s' % identifier)
|
2016-03-11 02:02:46 +11:00
|
|
|
if (identifier in machineIdentifier or
|
|
|
|
machineIdentifier in identifier):
|
2016-01-15 00:47:34 +11:00
|
|
|
found += 1
|
2016-03-10 01:37:27 +11:00
|
|
|
token = device.attrib.get('accessToken')
|
2016-03-11 02:02:46 +11:00
|
|
|
|
|
|
|
result = {
|
|
|
|
'username': username,
|
|
|
|
}
|
2016-01-15 00:47:34 +11:00
|
|
|
if found == 0:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('No tokens found for your server! Using empty string')
|
2016-03-11 02:02:46 +11:00
|
|
|
result['usertoken'] = ''
|
|
|
|
else:
|
|
|
|
result['usertoken'] = token
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Plex.tv switch HomeUser change successfull for user %s'
|
|
|
|
% username)
|
2016-03-11 02:02:46 +11:00
|
|
|
return result
|
2015-12-27 22:09:07 +11:00
|
|
|
|
2016-04-06 04:10:29 +10:00
|
|
|
def MyPlexListHomeUsers(self, token):
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
2016-01-15 00:47:34 +11:00
|
|
|
Returns a list for myPlex home users for the current plex.tv account.
|
2015-12-27 22:09:07 +11:00
|
|
|
|
|
|
|
Input:
|
2016-04-06 04:10:29 +10:00
|
|
|
token for plex.tv
|
2015-12-27 22:09:07 +11:00
|
|
|
Output:
|
|
|
|
List of users, where one entry is of the form:
|
2016-01-15 00:47:34 +11:00
|
|
|
"id": userId,
|
|
|
|
"admin": '1'/'0',
|
|
|
|
"guest": '1'/'0',
|
|
|
|
"restricted": '1'/'0',
|
|
|
|
"protected": '1'/'0',
|
|
|
|
"email": email,
|
|
|
|
"title": title,
|
|
|
|
"username": username,
|
2015-12-27 22:09:07 +11:00
|
|
|
"thumb": thumb_url
|
|
|
|
}
|
2015-12-28 20:35:27 +11:00
|
|
|
If any value is missing, None is returned instead (or "" from plex.tv)
|
|
|
|
If an error is encountered, False is returned
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
2016-04-06 04:10:29 +10:00
|
|
|
xml = self.doUtils('https://plex.tv/api/home/users/',
|
|
|
|
authenticate=False,
|
|
|
|
headerOptions={'X-Plex-Token': token})
|
|
|
|
try:
|
|
|
|
xml.attrib
|
|
|
|
except:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('Download of Plex home users failed.')
|
2015-12-27 22:09:07 +11:00
|
|
|
return False
|
|
|
|
users = []
|
2016-04-06 04:10:29 +10:00
|
|
|
for user in xml:
|
2015-12-27 22:09:07 +11:00
|
|
|
users.append(user.attrib)
|
|
|
|
return users
|
|
|
|
|
|
|
|
def getDirectVideoPath(self, key, AuthToken):
|
|
|
|
"""
|
|
|
|
Direct Video Play support
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
path
|
|
|
|
AuthToken
|
|
|
|
Indirect - media indirect specified, grab child XML to gain real path
|
|
|
|
options
|
|
|
|
result:
|
|
|
|
final path to media file
|
|
|
|
"""
|
|
|
|
if key.startswith('http://') or key.startswith('https://'): # external address - keep
|
|
|
|
path = key
|
|
|
|
else:
|
2016-04-13 01:18:32 +10:00
|
|
|
if AuthToken == '':
|
2015-12-27 22:09:07 +11:00
|
|
|
path = key
|
|
|
|
else:
|
|
|
|
xargs = dict()
|
|
|
|
xargs['X-Plex-Token'] = AuthToken
|
2016-04-13 01:18:32 +10:00
|
|
|
if key.find('?') == -1:
|
2015-12-27 22:09:07 +11:00
|
|
|
path = key + '?' + urlencode(xargs)
|
|
|
|
else:
|
|
|
|
path = key + '&' + urlencode(xargs)
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
return path
|
|
|
|
|
|
|
|
def getTranscodeImagePath(self, key, AuthToken, path, width, height):
|
|
|
|
"""
|
|
|
|
Transcode Image support
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
key
|
|
|
|
AuthToken
|
|
|
|
path - source path of current XML: path[srcXML]
|
|
|
|
width
|
|
|
|
height
|
|
|
|
result:
|
|
|
|
final path to image file
|
|
|
|
"""
|
|
|
|
if key.startswith('http://') or key.startswith('https://'): # external address - can we get a transcoding request for external images?
|
|
|
|
path = key
|
|
|
|
elif key.startswith('/'): # internal full path.
|
|
|
|
path = 'http://127.0.0.1:32400' + key
|
|
|
|
else: # internal path, add-on
|
|
|
|
path = 'http://127.0.0.1:32400' + path + '/' + key
|
2016-09-03 00:53:16 +10:00
|
|
|
path = tryEncode(path)
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
# This is bogus (note the extra path component) but ATV is stupid when it comes to caching images, it doesn't use querystrings.
|
|
|
|
# Fortunately PMS is lenient...
|
2016-04-13 01:18:32 +10:00
|
|
|
transcodePath = '/photo/:/transcode/' + \
|
|
|
|
str(width) + 'x' + str(height) + '/' + quote_plus(path)
|
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
args = dict()
|
|
|
|
args['width'] = width
|
|
|
|
args['height'] = height
|
|
|
|
args['url'] = path
|
2016-04-13 01:18:32 +10:00
|
|
|
|
|
|
|
if not AuthToken == '':
|
2015-12-27 22:09:07 +11:00
|
|
|
args['X-Plex-Token'] = AuthToken
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
return transcodePath + '?' + urlencode(args)
|
|
|
|
|
|
|
|
def getDirectImagePath(self, path, AuthToken):
|
|
|
|
"""
|
|
|
|
Direct Image support
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
path
|
|
|
|
AuthToken
|
|
|
|
result:
|
|
|
|
final path to image file
|
|
|
|
"""
|
2016-04-13 01:18:32 +10:00
|
|
|
if not AuthToken == '':
|
2015-12-27 22:09:07 +11:00
|
|
|
xargs = dict()
|
|
|
|
xargs['X-Plex-Token'] = AuthToken
|
2016-04-13 01:18:32 +10:00
|
|
|
if path.find('?') == -1:
|
2015-12-27 22:09:07 +11:00
|
|
|
path = path + '?' + urlencode(xargs)
|
|
|
|
else:
|
|
|
|
path = path + '&' + urlencode(xargs)
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
return path
|
|
|
|
|
|
|
|
def getTranscodeAudioPath(self, path, AuthToken, options, maxAudioBitrate):
|
|
|
|
"""
|
|
|
|
Transcode Audio support
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
path
|
|
|
|
AuthToken
|
|
|
|
options - dict() of PlexConnect-options as received from aTV
|
|
|
|
maxAudioBitrate - [kbps]
|
|
|
|
result:
|
|
|
|
final path to pull in PMS transcoder
|
|
|
|
"""
|
|
|
|
UDID = options['PlexConnectUDID']
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
transcodePath = '/music/:/transcode/universal/start.mp3?'
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
args = dict()
|
|
|
|
args['path'] = path
|
|
|
|
args['session'] = UDID
|
|
|
|
args['protocol'] = 'http'
|
|
|
|
args['maxAudioBitrate'] = maxAudioBitrate
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2016-09-03 00:53:16 +10:00
|
|
|
xargs = self.client.getXArgsDeviceInfo(options)
|
2016-04-13 01:18:32 +10:00
|
|
|
if not AuthToken == '':
|
2015-12-27 22:09:07 +11:00
|
|
|
xargs['X-Plex-Token'] = AuthToken
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
return transcodePath + urlencode(args) + '&' + urlencode(xargs)
|
|
|
|
|
|
|
|
def getDirectAudioPath(self, path, AuthToken):
|
|
|
|
"""
|
|
|
|
Direct Audio support
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
path
|
|
|
|
AuthToken
|
|
|
|
result:
|
|
|
|
final path to audio file
|
|
|
|
"""
|
2016-04-13 01:18:32 +10:00
|
|
|
if not AuthToken == '':
|
2015-12-27 22:09:07 +11:00
|
|
|
xargs = dict()
|
|
|
|
xargs['X-Plex-Token'] = AuthToken
|
2016-04-13 01:18:32 +10:00
|
|
|
if path.find('?') == -1:
|
2015-12-27 22:09:07 +11:00
|
|
|
path = path + '?' + urlencode(xargs)
|
|
|
|
else:
|
|
|
|
path = path + '&' + urlencode(xargs)
|
2016-04-13 01:18:32 +10:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
return path
|
|
|
|
|
2016-04-07 00:24:03 +10:00
|
|
|
def returnServerList(self, data):
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
|
|
|
Returns a nicer list of all servers found in data, where data is in
|
|
|
|
g_PMS format, for the client device with unique ID ATV_udid
|
|
|
|
|
|
|
|
Input:
|
|
|
|
data e.g. self.g_PMS
|
|
|
|
|
|
|
|
Output: List of all servers, with an entry of the form:
|
|
|
|
{
|
|
|
|
'name': friendlyName, the Plex server's name
|
|
|
|
'address': ip:port
|
|
|
|
'ip': ip, without http/https
|
|
|
|
'port': port
|
|
|
|
'scheme': 'http'/'https', nice for checking for secure connections
|
|
|
|
'local': '1'/'0', Is the server a local server?
|
|
|
|
'owned': '1'/'0', Is the server owned by the user?
|
|
|
|
'machineIdentifier': id, Plex server machine identifier
|
|
|
|
'accesstoken': token Access token to this server
|
|
|
|
'baseURL': baseURL scheme://ip:port
|
2016-04-07 00:24:03 +10:00
|
|
|
'ownername' Plex username of PMS owner
|
2015-12-27 22:09:07 +11:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
serverlist = []
|
2016-04-07 00:24:03 +10:00
|
|
|
for key, value in data.items():
|
2015-12-27 22:09:07 +11:00
|
|
|
serverlist.append({
|
2016-04-07 00:24:03 +10:00
|
|
|
'name': value.get('name'),
|
|
|
|
'address': value.get('address'),
|
|
|
|
'ip': value.get('ip'),
|
|
|
|
'port': value.get('port'),
|
|
|
|
'scheme': value.get('scheme'),
|
|
|
|
'local': value.get('local'),
|
|
|
|
'owned': value.get('owned'),
|
2015-12-27 22:09:07 +11:00
|
|
|
'machineIdentifier': key,
|
2016-04-07 00:24:03 +10:00
|
|
|
'accesstoken': value.get('accesstoken'),
|
|
|
|
'baseURL': value.get('baseURL'),
|
|
|
|
'ownername': value.get('ownername')
|
2015-12-27 22:09:07 +11:00
|
|
|
})
|
|
|
|
return serverlist
|
|
|
|
|
2016-01-02 06:36:02 +11:00
|
|
|
|
2016-01-03 23:36:00 +11:00
|
|
|
class API():
|
|
|
|
"""
|
|
|
|
API(item)
|
2016-01-02 06:36:02 +11:00
|
|
|
|
2016-01-03 23:36:00 +11:00
|
|
|
Processes a Plex media server's XML response
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-01-03 23:36:00 +11:00
|
|
|
item: xml.etree.ElementTree element
|
|
|
|
"""
|
2015-12-29 04:47:16 +11:00
|
|
|
|
|
|
|
def __init__(self, item):
|
|
|
|
self.item = item
|
2016-01-04 05:17:59 +11:00
|
|
|
# which media part in the XML response shall we look at?
|
|
|
|
self.part = 0
|
2016-09-08 23:55:49 +10:00
|
|
|
self.mediastream = None
|
2016-09-03 00:53:16 +10:00
|
|
|
self.server = window('pms_server')
|
|
|
|
self.client = clientinfo.ClientInfo()
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-02-08 04:05:59 +11:00
|
|
|
def setPartNumber(self, number=None):
|
2016-01-04 05:17:59 +11:00
|
|
|
"""
|
|
|
|
Sets the part number to work with (used to deal with Movie with several
|
|
|
|
parts).
|
|
|
|
"""
|
2016-02-08 04:05:59 +11:00
|
|
|
self.part = number or 0
|
2016-01-04 05:17:59 +11:00
|
|
|
|
|
|
|
def getPartNumber(self):
|
|
|
|
"""
|
|
|
|
Returns the current media part number we're dealing with.
|
|
|
|
"""
|
|
|
|
return self.part
|
|
|
|
|
2016-01-02 19:28:31 +11:00
|
|
|
def getType(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-01-30 06:07:21 +11:00
|
|
|
Returns the type of media, e.g. 'movie' or 'clip' for trailers
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-03-16 06:26:45 +11:00
|
|
|
return self.item.attrib.get('type')
|
2016-01-02 19:28:31 +11:00
|
|
|
|
2015-12-29 04:47:16 +11:00
|
|
|
def getChecksum(self):
|
|
|
|
"""
|
2016-03-28 01:57:35 +11:00
|
|
|
Returns a string, not int.
|
|
|
|
|
|
|
|
WATCH OUT - time in Plex, not Kodi ;-)
|
2015-12-29 04:47:16 +11:00
|
|
|
"""
|
2016-01-03 23:36:00 +11:00
|
|
|
# Include a letter to prohibit saving as an int!
|
2016-01-30 06:07:21 +11:00
|
|
|
checksum = "K%s%s" % (self.getRatingKey(),
|
2016-02-01 20:33:33 +11:00
|
|
|
self.item.attrib.get('updatedAt', ''))
|
2016-01-02 06:36:02 +11:00
|
|
|
return checksum
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-01-30 06:07:21 +11:00
|
|
|
def getRatingKey(self):
|
2015-12-30 23:25:37 +11:00
|
|
|
"""
|
2016-01-30 06:07:21 +11:00
|
|
|
Returns the Plex key such as '246922' as a string
|
2015-12-30 23:25:37 +11:00
|
|
|
"""
|
2016-03-16 06:26:45 +11:00
|
|
|
return self.item.attrib.get('ratingKey')
|
2015-12-30 00:13:32 +11:00
|
|
|
|
2016-01-30 06:07:21 +11:00
|
|
|
def getKey(self):
|
|
|
|
"""
|
|
|
|
Returns the Plex key such as '/library/metadata/246922'
|
|
|
|
"""
|
2016-03-16 06:26:45 +11:00
|
|
|
return self.item.attrib.get('key')
|
|
|
|
|
2016-07-13 04:27:59 +10:00
|
|
|
def getFilePath(self, forceFirstMediaStream=False):
|
2016-03-16 06:26:45 +11:00
|
|
|
"""
|
|
|
|
Returns the direct path to this item, e.g. '\\NAS\movies\movie.mkv'
|
|
|
|
or None
|
2016-07-13 04:27:59 +10:00
|
|
|
|
|
|
|
forceFirstMediaStream=True:
|
|
|
|
will always use 1st media stream, e.g. when several different
|
|
|
|
files are present for the same PMS item
|
2016-03-16 06:26:45 +11:00
|
|
|
"""
|
2016-09-08 23:55:49 +10:00
|
|
|
if self.mediastream is None and forceFirstMediaStream is False:
|
|
|
|
self.getMediastreamNumber()
|
2016-03-16 06:26:45 +11:00
|
|
|
try:
|
2016-07-13 04:27:59 +10:00
|
|
|
if forceFirstMediaStream is False:
|
2016-09-08 23:55:49 +10:00
|
|
|
ans = self.item[self.mediastream][self.part].attrib['file']
|
2016-07-13 04:27:59 +10:00
|
|
|
else:
|
|
|
|
ans = self.item[0][self.part].attrib['file']
|
2016-03-16 06:26:45 +11:00
|
|
|
except:
|
2016-07-13 04:27:59 +10:00
|
|
|
ans = None
|
|
|
|
if ans is not None:
|
2016-04-27 03:19:52 +10:00
|
|
|
try:
|
2016-09-03 00:53:16 +10:00
|
|
|
ans = tryDecode(unquote(ans))
|
2016-05-07 06:16:56 +10:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
# Sometimes, Plex seems to have encoded in latin1
|
2016-07-13 04:27:59 +10:00
|
|
|
ans = unquote(ans).decode('latin1')
|
|
|
|
return ans
|
2016-03-16 06:26:45 +11:00
|
|
|
|
|
|
|
def getTVShowPath(self):
|
|
|
|
"""
|
|
|
|
Returns the direct path to the TV show, e.g. '\\NAS\tv\series'
|
|
|
|
or None
|
|
|
|
"""
|
|
|
|
res = None
|
|
|
|
for child in self.item:
|
|
|
|
if child.tag == 'Location':
|
|
|
|
res = child.attrib.get('path')
|
|
|
|
return res
|
2016-01-30 06:07:21 +11:00
|
|
|
|
2016-01-10 02:14:02 +11:00
|
|
|
def getIndex(self):
|
|
|
|
"""
|
|
|
|
Returns the 'index' of an PMS XML reply. Depicts e.g. season number.
|
|
|
|
"""
|
2016-03-16 06:26:45 +11:00
|
|
|
return self.item.attrib.get('index')
|
2016-01-10 02:14:02 +11:00
|
|
|
|
2015-12-29 04:47:16 +11:00
|
|
|
def getDateCreated(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-04-15 17:32:04 +10:00
|
|
|
Returns the date when this library item was created.
|
|
|
|
|
|
|
|
If not found, returns 2000-01-01 10:00:00
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-04-02 19:43:50 +11:00
|
|
|
res = self.item.attrib.get('addedAt')
|
|
|
|
if res is not None:
|
2016-09-03 00:53:16 +10:00
|
|
|
res = DateToKodi(res)
|
2016-04-15 17:32:04 +10:00
|
|
|
else:
|
|
|
|
res = '2000-01-01 10:00:00'
|
2016-04-02 19:43:50 +11:00
|
|
|
return res
|
2015-12-29 04:47:16 +11:00
|
|
|
|
|
|
|
def getUserData(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
|
|
|
Returns a dict with None if a value is missing
|
|
|
|
{
|
|
|
|
'Favorite': favorite, # False, because n/a in Plex
|
|
|
|
'PlayCount': playcount,
|
|
|
|
'Played': played, # True/False
|
|
|
|
'LastPlayedDate': lastPlayedDate,
|
|
|
|
'Resume': resume, # Resume time in seconds
|
2016-01-15 02:17:24 +11:00
|
|
|
'Runtime': runtime,
|
2016-01-03 23:36:00 +11:00
|
|
|
'Rating': rating
|
|
|
|
}
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
item = self.item.attrib
|
2016-04-02 19:31:21 +11:00
|
|
|
# Default - attributes not found with Plex
|
2015-12-29 04:47:16 +11:00
|
|
|
favorite = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
playcount = int(item['viewCount'])
|
2016-02-13 02:53:49 +11:00
|
|
|
except:
|
2016-04-07 23:13:05 +10:00
|
|
|
playcount = None
|
2015-12-29 04:47:16 +11:00
|
|
|
|
|
|
|
if playcount:
|
|
|
|
played = True
|
2016-04-02 19:31:21 +11:00
|
|
|
else:
|
|
|
|
played = False
|
2015-12-29 04:47:16 +11:00
|
|
|
|
|
|
|
try:
|
2016-09-03 00:53:16 +10:00
|
|
|
lastPlayedDate = DateToKodi(int(item['lastViewedAt']))
|
2016-02-13 02:53:49 +11:00
|
|
|
except:
|
2015-12-29 04:47:16 +11:00
|
|
|
lastPlayedDate = None
|
|
|
|
|
2016-04-02 19:31:21 +11:00
|
|
|
try:
|
|
|
|
userrating = float(item['userRating'])
|
|
|
|
except:
|
|
|
|
userrating = 0.0
|
|
|
|
|
|
|
|
try:
|
|
|
|
rating = float(item['audienceRating'])
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
rating = float(item['rating'])
|
|
|
|
except:
|
|
|
|
rating = 0.0
|
2016-02-13 02:53:49 +11:00
|
|
|
|
2016-01-15 02:17:24 +11:00
|
|
|
resume, runtime = self.getRuntime()
|
2015-12-29 04:47:16 +11:00
|
|
|
return {
|
|
|
|
'Favorite': favorite,
|
|
|
|
'PlayCount': playcount,
|
|
|
|
'Played': played,
|
|
|
|
'LastPlayedDate': lastPlayedDate,
|
|
|
|
'Resume': resume,
|
2016-01-15 02:17:24 +11:00
|
|
|
'Runtime': runtime,
|
2016-02-13 02:53:49 +11:00
|
|
|
'Rating': rating,
|
|
|
|
'UserRating': userrating
|
2015-12-29 04:47:16 +11:00
|
|
|
}
|
|
|
|
|
2016-03-02 02:18:12 +11:00
|
|
|
def getCollections(self):
|
|
|
|
"""
|
2016-03-02 02:28:48 +11:00
|
|
|
Returns a list of PMS collection tags or an empty list
|
2016-03-02 02:18:12 +11:00
|
|
|
"""
|
|
|
|
collections = []
|
|
|
|
for child in self.item:
|
|
|
|
if child.tag == 'Collection':
|
|
|
|
collections.append(child.attrib['tag'])
|
|
|
|
return collections
|
|
|
|
|
2015-12-29 04:47:16 +11:00
|
|
|
def getPeople(self):
|
|
|
|
"""
|
2016-01-03 23:36:00 +11:00
|
|
|
Returns a dict of lists of people found.
|
2015-12-29 04:47:16 +11:00
|
|
|
{
|
2016-01-03 23:36:00 +11:00
|
|
|
'Director': list,
|
|
|
|
'Writer': list,
|
|
|
|
'Cast': list,
|
|
|
|
'Producer': list
|
2015-12-29 04:47:16 +11:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
director = []
|
|
|
|
writer = []
|
|
|
|
cast = []
|
|
|
|
producer = []
|
2016-02-01 20:33:33 +11:00
|
|
|
for child in self.item:
|
2015-12-31 20:53:22 +11:00
|
|
|
if child.tag == 'Director':
|
|
|
|
director.append(child.attrib['tag'])
|
|
|
|
elif child.tag == 'Writer':
|
|
|
|
writer.append(child.attrib['tag'])
|
|
|
|
elif child.tag == 'Role':
|
|
|
|
cast.append(child.attrib['tag'])
|
|
|
|
elif child.tag == 'Producer':
|
|
|
|
producer.append(child.attrib['tag'])
|
2015-12-29 04:47:16 +11:00
|
|
|
return {
|
|
|
|
'Director': director,
|
|
|
|
'Writer': writer,
|
|
|
|
'Cast': cast,
|
|
|
|
'Producer': producer
|
|
|
|
}
|
|
|
|
|
|
|
|
def getPeopleList(self):
|
|
|
|
"""
|
|
|
|
Returns a list of people from item, with a list item of the form
|
|
|
|
{
|
2016-01-03 23:36:00 +11:00
|
|
|
'Name': xxx,
|
|
|
|
'Type': xxx,
|
|
|
|
'Id': xxx
|
|
|
|
'imageurl': url to picture, None otherwise
|
|
|
|
('Role': xxx for cast/actors only, None if not found)
|
2015-12-29 04:47:16 +11:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
people = []
|
|
|
|
# Key of library: Plex-identifier. Value represents the Kodi/emby side
|
|
|
|
people_of_interest = {
|
|
|
|
'Director': 'Director',
|
|
|
|
'Writer': 'Writer',
|
|
|
|
'Role': 'Actor',
|
|
|
|
'Producer': 'Producer'
|
|
|
|
}
|
2016-02-01 20:33:33 +11:00
|
|
|
for child in self.item:
|
2015-12-31 20:53:22 +11:00
|
|
|
if child.tag in people_of_interest.keys():
|
|
|
|
name = child.attrib['tag']
|
|
|
|
name_id = child.attrib['id']
|
|
|
|
Type = child.tag
|
2015-12-29 04:47:16 +11:00
|
|
|
Type = people_of_interest[Type]
|
2016-03-02 02:18:12 +11:00
|
|
|
|
|
|
|
url = child.attrib.get('thumb')
|
|
|
|
Role = child.attrib.get('role')
|
|
|
|
|
2015-12-31 20:53:22 +11:00
|
|
|
people.append({
|
|
|
|
'Name': name,
|
|
|
|
'Type': Type,
|
|
|
|
'Id': name_id,
|
|
|
|
'imageurl': url
|
|
|
|
})
|
|
|
|
if url:
|
|
|
|
people[-1].update({'imageurl': url})
|
|
|
|
if Role:
|
|
|
|
people[-1].update({'Role': Role})
|
2015-12-29 04:47:16 +11:00
|
|
|
return people
|
|
|
|
|
|
|
|
def getGenres(self):
|
|
|
|
"""
|
2016-01-03 23:36:00 +11:00
|
|
|
Returns a list of genres found. (Not a string)
|
2015-12-29 04:47:16 +11:00
|
|
|
"""
|
|
|
|
genre = []
|
2016-02-01 20:33:33 +11:00
|
|
|
for child in self.item:
|
2015-12-31 20:53:22 +11:00
|
|
|
if child.tag == 'Genre':
|
|
|
|
genre.append(child.attrib['tag'])
|
2015-12-29 04:47:16 +11:00
|
|
|
return genre
|
|
|
|
|
2016-02-07 22:38:50 +11:00
|
|
|
def getGuid(self):
|
|
|
|
return self.item.attrib.get('guid')
|
|
|
|
|
2016-01-10 02:14:02 +11:00
|
|
|
def getProvider(self, providername=None):
|
2015-12-29 04:47:16 +11:00
|
|
|
"""
|
2016-03-17 04:47:57 +11:00
|
|
|
providername: e.g. 'imdb', 'tvdb'
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-02-03 23:01:13 +11:00
|
|
|
Return IMDB, e.g. "tt0903624". Returns None if not found
|
2015-12-29 04:47:16 +11:00
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
item = self.item.attrib
|
2016-01-11 19:57:45 +11:00
|
|
|
try:
|
|
|
|
item = item['guid']
|
|
|
|
except KeyError:
|
|
|
|
return None
|
2016-01-10 02:14:02 +11:00
|
|
|
|
2016-02-03 23:01:13 +11:00
|
|
|
if providername == 'imdb':
|
2016-09-26 03:21:12 +10:00
|
|
|
regex = REGEX_IMDB
|
2016-03-17 04:47:57 +11:00
|
|
|
elif providername == 'tvdb':
|
|
|
|
# originally e.g. com.plexapp.agents.thetvdb://276564?lang=en
|
2016-09-26 03:21:12 +10:00
|
|
|
regex = REGEX_TVDB
|
2016-02-03 23:01:13 +11:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2016-01-11 19:57:45 +11:00
|
|
|
provider = regex.findall(item)
|
2015-12-29 04:47:16 +11:00
|
|
|
try:
|
2016-01-10 02:14:02 +11:00
|
|
|
provider = provider[0]
|
2016-01-11 19:57:45 +11:00
|
|
|
except IndexError:
|
2015-12-29 04:47:16 +11:00
|
|
|
provider = None
|
|
|
|
return provider
|
|
|
|
|
2015-12-31 20:53:22 +11:00
|
|
|
def getTitle(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
Returns an item's name/title or "Missing Title Name".
|
2016-01-03 23:36:00 +11:00
|
|
|
Output:
|
|
|
|
title, sorttitle
|
|
|
|
|
|
|
|
sorttitle = title, if no sorttitle is found
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
title = self.item.attrib.get('title', 'Missing Title Name')
|
|
|
|
sorttitle = self.item.attrib.get('titleSort', title)
|
2015-12-29 04:47:16 +11:00
|
|
|
return title, sorttitle
|
|
|
|
|
2015-12-31 20:53:22 +11:00
|
|
|
def getPlot(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
|
|
|
Returns the plot or None.
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
return self.item.attrib.get('summary', None)
|
2015-12-31 20:53:22 +11:00
|
|
|
|
|
|
|
def getTagline(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
|
|
|
Returns a shorter tagline or None
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
return self.item.attrib.get('tagline', None)
|
2015-12-31 20:53:22 +11:00
|
|
|
|
|
|
|
def getAudienceRating(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-04-02 19:31:21 +11:00
|
|
|
Returns the audience rating, 'rating' itself or 0.0
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-03-18 02:15:48 +11:00
|
|
|
res = self.item.attrib.get('audienceRating')
|
|
|
|
if res is None:
|
2016-04-17 21:36:41 +10:00
|
|
|
res = self.item.attrib.get('rating')
|
|
|
|
try:
|
|
|
|
res = float(res)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
res = 0.0
|
2016-03-18 02:15:48 +11:00
|
|
|
return res
|
2015-12-31 20:53:22 +11:00
|
|
|
|
|
|
|
def getYear(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
|
|
|
Returns the production(?) year ("year") or None
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
return self.item.attrib.get('year', None)
|
2015-12-31 20:53:22 +11:00
|
|
|
|
2015-12-29 04:47:16 +11:00
|
|
|
def getRuntime(self):
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
Resume point of time and runtime/totaltime in rounded to seconds.
|
2016-01-02 06:36:02 +11:00
|
|
|
Time from Plex server is measured in milliseconds.
|
2016-01-03 23:36:00 +11:00
|
|
|
Kodi: seconds
|
|
|
|
|
|
|
|
Output:
|
2016-02-01 20:33:33 +11:00
|
|
|
resume, runtime as ints. 0 if not found
|
2015-12-29 04:47:16 +11:00
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
item = self.item.attrib
|
2016-01-04 05:17:59 +11:00
|
|
|
|
2015-12-31 20:53:22 +11:00
|
|
|
try:
|
2016-01-03 23:36:00 +11:00
|
|
|
runtime = float(item['duration'])
|
2016-04-17 21:36:41 +10:00
|
|
|
except (KeyError, ValueError):
|
2016-01-03 23:36:00 +11:00
|
|
|
runtime = 0.0
|
2015-12-29 04:47:16 +11:00
|
|
|
try:
|
2016-01-02 06:36:02 +11:00
|
|
|
resume = float(item['viewOffset'])
|
2016-04-17 21:36:41 +10:00
|
|
|
except (KeyError, ValueError):
|
2016-01-02 06:36:02 +11:00
|
|
|
resume = 0.0
|
2016-01-15 02:17:24 +11:00
|
|
|
|
2016-10-30 00:23:48 +11:00
|
|
|
runtime = int(runtime * PLEX_TO_KODI_TIMEFACTOR)
|
|
|
|
resume = int(resume * PLEX_TO_KODI_TIMEFACTOR)
|
2015-12-29 04:47:16 +11:00
|
|
|
return resume, runtime
|
|
|
|
|
|
|
|
def getMpaa(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
|
|
|
Get the content rating or None
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
mpaa = self.item.attrib.get('contentRating', None)
|
2015-12-29 04:47:16 +11:00
|
|
|
# Convert more complex cases
|
|
|
|
if mpaa in ("NR", "UR"):
|
|
|
|
# Kodi seems to not like NR, but will accept Rated Not Rated
|
|
|
|
mpaa = "Rated Not Rated"
|
|
|
|
return mpaa
|
|
|
|
|
|
|
|
def getCountry(self):
|
|
|
|
"""
|
|
|
|
Returns a list of all countries found in item.
|
|
|
|
"""
|
|
|
|
country = []
|
2016-02-01 20:33:33 +11:00
|
|
|
for child in self.item:
|
2015-12-31 20:53:22 +11:00
|
|
|
if child.tag == 'Country':
|
|
|
|
country.append(child.attrib['tag'])
|
2015-12-29 04:47:16 +11:00
|
|
|
return country
|
|
|
|
|
2016-01-02 19:28:31 +11:00
|
|
|
def getPremiereDate(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
|
|
|
Returns the "originallyAvailableAt" or None
|
|
|
|
"""
|
2016-09-03 00:53:16 +10:00
|
|
|
return self.item.attrib.get('originallyAvailableAt')
|
2016-01-02 19:28:31 +11:00
|
|
|
|
2016-03-28 18:56:22 +11:00
|
|
|
def getMusicStudio(self):
|
|
|
|
return self.item.attrib.get('studio', '')
|
|
|
|
|
2015-12-29 04:47:16 +11:00
|
|
|
def getStudios(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
|
|
|
Returns a list with a single entry for the studio, or an empty list
|
|
|
|
"""
|
2015-12-29 04:47:16 +11:00
|
|
|
studio = []
|
|
|
|
try:
|
2016-02-01 20:33:33 +11:00
|
|
|
studio.append(self.getStudio(self.item.attrib['studio']))
|
2015-12-29 04:47:16 +11:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
return studio
|
|
|
|
|
|
|
|
def getStudio(self, studioName):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
|
|
|
Convert studio for Kodi to properly detect them
|
|
|
|
"""
|
2015-12-29 04:47:16 +11:00
|
|
|
studios = {
|
|
|
|
'abc (us)': "ABC",
|
|
|
|
'fox (us)': "FOX",
|
|
|
|
'mtv (us)': "MTV",
|
|
|
|
'showcase (ca)': "Showcase",
|
|
|
|
'wgn america': "WGN"
|
|
|
|
}
|
|
|
|
return studios.get(studioName.lower(), studioName)
|
|
|
|
|
|
|
|
def joinList(self, listobject):
|
|
|
|
"""
|
2016-01-03 23:36:00 +11:00
|
|
|
Smart-joins the listobject into a single string using a " / "
|
|
|
|
separator.
|
2015-12-29 04:47:16 +11:00
|
|
|
If the list is empty, smart_join returns an empty string.
|
|
|
|
"""
|
|
|
|
string = " / ".join(listobject)
|
|
|
|
return string
|
|
|
|
|
2016-02-03 23:01:13 +11:00
|
|
|
def getParentRatingKey(self):
|
|
|
|
return self.item.attrib.get('parentRatingKey', '')
|
|
|
|
|
2016-01-10 02:14:02 +11:00
|
|
|
def getEpisodeDetails(self):
|
|
|
|
"""
|
|
|
|
Call on a single episode.
|
|
|
|
|
|
|
|
Output: for the corresponding the TV show and season:
|
|
|
|
[
|
|
|
|
TV show key, Plex: 'grandparentRatingKey'
|
|
|
|
TV show title, Plex: 'grandparentTitle'
|
|
|
|
TV show season, Plex: 'parentIndex'
|
|
|
|
Episode number, Plex: 'index'
|
|
|
|
]
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
item = self.item.attrib
|
2016-03-12 00:47:41 +11:00
|
|
|
key = item.get('grandparentRatingKey')
|
|
|
|
title = item.get('grandparentTitle')
|
|
|
|
season = item.get('parentIndex')
|
|
|
|
episode = item.get('index')
|
2016-02-03 23:01:13 +11:00
|
|
|
return key, title, season, episode
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-02-20 02:10:19 +11:00
|
|
|
def addPlexHeadersToUrl(self, url, arguments={}):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
|
|
|
Takes an URL and optional arguments (also to be URL-encoded); returns
|
|
|
|
an extended URL with e.g. the Plex token included.
|
2016-01-30 06:07:21 +11:00
|
|
|
|
|
|
|
arguments overrule everything
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-09-03 00:53:16 +10:00
|
|
|
xargs = self.client.getXArgsDeviceInfo()
|
2015-12-31 02:37:30 +11:00
|
|
|
xargs.update(arguments)
|
2016-01-30 06:07:21 +11:00
|
|
|
if '?' not in url:
|
|
|
|
url = "%s?%s" % (url, urlencode(xargs))
|
|
|
|
else:
|
|
|
|
url = "%s&%s" % (url, urlencode(xargs))
|
2015-12-31 02:37:30 +11:00
|
|
|
return url
|
|
|
|
|
2016-02-20 02:10:19 +11:00
|
|
|
def addPlexCredentialsToUrl(self, url):
|
|
|
|
"""
|
|
|
|
Returns an extended URL with the Plex token included as 'X-Plex-Token='
|
|
|
|
|
|
|
|
url may or may not already contain a '?'
|
|
|
|
"""
|
2016-09-03 00:53:16 +10:00
|
|
|
if window('pms_token') == '':
|
2016-04-06 02:23:00 +10:00
|
|
|
return url
|
2016-02-20 02:10:19 +11:00
|
|
|
if '?' not in url:
|
2016-09-03 00:53:16 +10:00
|
|
|
url = "%s?X-Plex-Token=%s" % (url, window('pms_token'))
|
2016-02-20 02:10:19 +11:00
|
|
|
else:
|
2016-09-03 00:53:16 +10:00
|
|
|
url = "%s&X-Plex-Token=%s" % (url, window('pms_token'))
|
2016-02-20 02:10:19 +11:00
|
|
|
return url
|
|
|
|
|
2016-01-30 06:07:21 +11:00
|
|
|
def GetPlayQueueItemID(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-01-30 06:07:21 +11:00
|
|
|
Returns current playQueueItemID for the item.
|
|
|
|
|
|
|
|
If not found, empty str is returned
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-02-03 23:01:13 +11:00
|
|
|
return self.item.attrib.get('playQueueItemID', '')
|
2016-01-03 23:36:00 +11:00
|
|
|
|
2016-01-06 02:05:20 +11:00
|
|
|
def getDataFromPartOrMedia(self, key):
|
|
|
|
"""
|
|
|
|
Retrieves XML data 'key' first from the active part. If unsuccessful,
|
|
|
|
tries to retrieve the data from the Media response part.
|
|
|
|
|
|
|
|
If all fails, None is returned.
|
|
|
|
"""
|
2016-02-03 23:01:13 +11:00
|
|
|
media = self.item[0].attrib
|
|
|
|
part = self.item[0][self.part].attrib
|
2016-02-01 02:13:40 +11:00
|
|
|
|
2016-01-06 02:05:20 +11:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
value = part[key]
|
|
|
|
except KeyError:
|
|
|
|
value = media[key]
|
|
|
|
except KeyError:
|
|
|
|
value = None
|
|
|
|
return value
|
|
|
|
|
|
|
|
def getVideoCodec(self):
|
|
|
|
"""
|
|
|
|
Returns the video codec and resolution for the child and part selected.
|
|
|
|
If any data is not found on a part-level, the Media-level data is
|
|
|
|
returned.
|
|
|
|
If that also fails (e.g. for old trailers, None is returned)
|
|
|
|
|
|
|
|
Output:
|
|
|
|
{
|
|
|
|
'videocodec': xxx, e.g. 'h264'
|
|
|
|
'resolution': xxx, e.g. '720' or '1080'
|
|
|
|
'height': xxx, e.g. '816'
|
|
|
|
'width': xxx, e.g. '1920'
|
|
|
|
'aspectratio': xxx, e.g. '1.78'
|
2016-05-16 00:30:09 +10:00
|
|
|
'bitrate': xxx, e.g. '10642'
|
2016-05-16 00:25:38 +10:00
|
|
|
'container': xxx e.g. 'mkv',
|
|
|
|
'bitDepth': xxx e.g. '8', '10'
|
2016-01-06 02:05:20 +11:00
|
|
|
}
|
|
|
|
"""
|
2016-10-27 06:14:58 +11:00
|
|
|
answ = {
|
2016-05-16 00:25:38 +10:00
|
|
|
'videocodec': self.getDataFromPartOrMedia('videoCodec'),
|
|
|
|
'resolution': self.getDataFromPartOrMedia('videoResolution'),
|
|
|
|
'height': self.getDataFromPartOrMedia('height'),
|
|
|
|
'width': self.getDataFromPartOrMedia('width'),
|
|
|
|
'aspectratio': self.getDataFromPartOrMedia('aspectratio'),
|
|
|
|
'bitrate': self.getDataFromPartOrMedia('bitrate'),
|
|
|
|
'container': self.getDataFromPartOrMedia('container'),
|
2016-01-06 02:05:20 +11:00
|
|
|
}
|
2016-10-27 06:14:58 +11:00
|
|
|
try:
|
|
|
|
answ['bitDepth'] = self.item[0][self.part][self.mediastream].attrib.get('bitDepth')
|
|
|
|
except:
|
|
|
|
answ['bitDepth'] = None
|
|
|
|
return answ
|
2016-01-06 02:05:20 +11:00
|
|
|
|
2016-01-12 21:47:48 +11:00
|
|
|
def getExtras(self):
|
|
|
|
"""
|
2016-10-27 06:34:58 +11:00
|
|
|
Currently ONLY returns the very first trailer found!
|
|
|
|
|
2016-01-12 23:57:26 +11:00
|
|
|
Returns a list of trailer and extras from PMS XML. Returns [] if
|
2016-01-12 21:47:48 +11:00
|
|
|
no extras are found.
|
|
|
|
Extratypes:
|
2016-10-27 06:34:58 +11:00
|
|
|
1: Trailer
|
|
|
|
5: Behind the scenes
|
2016-01-12 21:47:48 +11:00
|
|
|
|
|
|
|
Output: list of dicts with one entry of the form:
|
|
|
|
'key': e.g. /library/metadata/xxxx
|
|
|
|
'title':
|
|
|
|
'thumb': artwork
|
|
|
|
'duration':
|
|
|
|
'extraType':
|
|
|
|
'originallyAvailableAt':
|
|
|
|
'year':
|
|
|
|
"""
|
|
|
|
elements = []
|
2016-10-27 06:34:58 +11:00
|
|
|
extras = self.item.find('Extras')
|
|
|
|
if extras is None:
|
|
|
|
return elements
|
|
|
|
for extra in extras:
|
|
|
|
try:
|
|
|
|
extraType = int(extra.attrib['extraType'])
|
|
|
|
except:
|
|
|
|
extraType = None
|
|
|
|
if extraType != 1:
|
|
|
|
continue
|
2016-02-01 20:33:33 +11:00
|
|
|
key = extra.attrib.get('key', None)
|
|
|
|
title = extra.attrib.get('title', None)
|
|
|
|
thumb = extra.attrib.get('thumb', None)
|
|
|
|
duration = float(extra.attrib.get('duration', 0.0))
|
|
|
|
year = extra.attrib.get('year', None)
|
|
|
|
originallyAvailableAt = extra.attrib.get(
|
|
|
|
'originallyAvailableAt', None)
|
|
|
|
elements.append(
|
2016-09-03 00:53:16 +10:00
|
|
|
{
|
|
|
|
'key': key,
|
|
|
|
'title': title,
|
|
|
|
'thumb': thumb,
|
2016-10-30 00:23:48 +11:00
|
|
|
'duration': int(duration * PLEX_TO_KODI_TIMEFACTOR),
|
2016-09-03 00:53:16 +10:00
|
|
|
'extraType': extraType,
|
|
|
|
'originallyAvailableAt': originallyAvailableAt,
|
|
|
|
'year': year
|
|
|
|
})
|
2016-10-27 06:34:58 +11:00
|
|
|
break
|
2016-01-12 21:47:48 +11:00
|
|
|
return elements
|
|
|
|
|
2015-12-29 04:47:16 +11:00
|
|
|
def getMediaStreams(self):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
2016-03-17 03:02:22 +11:00
|
|
|
Returns the media streams for metadata purposes
|
2016-01-03 23:36:00 +11:00
|
|
|
|
|
|
|
Output: each track contains a dictionaries
|
|
|
|
{
|
2016-01-15 20:04:47 +11:00
|
|
|
'video': videotrack-list, 'codec', 'height', 'width',
|
|
|
|
'aspect', 'video3DFormat'
|
|
|
|
'audio': audiotrack-list, 'codec', 'channels',
|
|
|
|
'language'
|
2016-01-03 23:36:00 +11:00
|
|
|
'subtitle': list of subtitle languages (or "Unknown")
|
|
|
|
}
|
|
|
|
"""
|
2015-12-29 04:47:16 +11:00
|
|
|
videotracks = []
|
|
|
|
audiotracks = []
|
|
|
|
subtitlelanguages = []
|
2016-02-01 20:33:33 +11:00
|
|
|
# Sometimes, aspectratio is on the "toplevel"
|
|
|
|
aspectratio = self.item[0].attrib.get('aspectRatio', None)
|
2015-12-29 04:47:16 +11:00
|
|
|
# TODO: what if several Media tags exist?!?
|
2015-12-31 20:53:22 +11:00
|
|
|
# Loop over parts
|
2016-02-01 20:33:33 +11:00
|
|
|
for child in self.item[0]:
|
|
|
|
container = child.attrib.get('container', None)
|
2015-12-31 20:53:22 +11:00
|
|
|
# Loop over Streams
|
|
|
|
for grandchild in child:
|
|
|
|
mediaStream = grandchild.attrib
|
2016-02-01 20:33:33 +11:00
|
|
|
mediaType = int(mediaStream.get('streamType', 999))
|
|
|
|
if mediaType == 1: # Video streams
|
2015-12-29 04:47:16 +11:00
|
|
|
videotrack = {}
|
2016-01-15 20:04:47 +11:00
|
|
|
videotrack['codec'] = mediaStream['codec'].lower()
|
|
|
|
if "msmpeg4" in videotrack['codec']:
|
|
|
|
videotrack['codec'] = "divx"
|
|
|
|
elif "mpeg4" in videotrack['codec']:
|
2015-12-29 04:47:16 +11:00
|
|
|
# if "simple profile" in profile or profile == "":
|
2016-01-15 20:04:47 +11:00
|
|
|
# videotrack['codec'] = "xvid"
|
2015-12-29 04:47:16 +11:00
|
|
|
pass
|
2016-01-15 20:04:47 +11:00
|
|
|
elif "h264" in videotrack['codec']:
|
2015-12-29 04:47:16 +11:00
|
|
|
if container in ("mp4", "mov", "m4v"):
|
2016-01-15 20:04:47 +11:00
|
|
|
videotrack['codec'] = "avc1"
|
2016-02-01 20:33:33 +11:00
|
|
|
videotrack['height'] = mediaStream.get('height', None)
|
|
|
|
videotrack['width'] = mediaStream.get('width', None)
|
2015-12-29 04:47:16 +11:00
|
|
|
# TODO: 3d Movies?!?
|
|
|
|
# videotrack['Video3DFormat'] = item.get('Video3DFormat')
|
2016-02-01 20:33:33 +11:00
|
|
|
aspectratio = mediaStream.get('aspectRatio', aspectratio)
|
2016-01-15 20:04:47 +11:00
|
|
|
videotrack['aspect'] = aspectratio
|
2015-12-29 04:47:16 +11:00
|
|
|
# TODO: Video 3d format
|
|
|
|
videotrack['video3DFormat'] = None
|
|
|
|
videotracks.append(videotrack)
|
|
|
|
|
2016-02-01 20:33:33 +11:00
|
|
|
elif mediaType == 2: # Audio streams
|
2015-12-29 04:47:16 +11:00
|
|
|
audiotrack = {}
|
2016-01-15 20:04:47 +11:00
|
|
|
audiotrack['codec'] = mediaStream['codec'].lower()
|
2016-05-30 04:03:31 +10:00
|
|
|
if ("dca" in audiotrack['codec'] and
|
|
|
|
"ma" in mediaStream.get('profile', '').lower()):
|
2016-01-15 20:04:47 +11:00
|
|
|
audiotrack['codec'] = "dtshd_ma"
|
2015-12-29 04:47:16 +11:00
|
|
|
audiotrack['channels'] = mediaStream.get('channels')
|
2016-03-10 22:47:30 +11:00
|
|
|
# 'unknown' if we cannot get language
|
|
|
|
audiotrack['language'] = mediaStream.get(
|
2016-09-03 00:53:16 +10:00
|
|
|
'languageCode', lang(39310)).lower()
|
2015-12-29 04:47:16 +11:00
|
|
|
audiotracks.append(audiotrack)
|
|
|
|
|
2016-02-01 20:33:33 +11:00
|
|
|
elif mediaType == 3: # Subtitle streams
|
2016-03-10 22:47:30 +11:00
|
|
|
# 'unknown' if we cannot get language
|
|
|
|
subtitlelanguages.append(
|
2016-06-19 23:53:36 +10:00
|
|
|
mediaStream.get('languageCode',
|
2016-09-03 00:53:16 +10:00
|
|
|
lang(39310)).lower())
|
2016-02-01 20:33:33 +11:00
|
|
|
return {
|
2015-12-29 04:47:16 +11:00
|
|
|
'video': videotracks,
|
|
|
|
'audio': audiotracks,
|
|
|
|
'subtitle': subtitlelanguages
|
|
|
|
}
|
2015-12-31 01:57:55 +11:00
|
|
|
|
2016-04-22 18:26:40 +10:00
|
|
|
def __getOneArtwork(self, entry):
|
|
|
|
try:
|
|
|
|
artwork = self.item.attrib[entry]
|
|
|
|
if artwork.startswith('http'):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
artwork = "%s%s" % (self.server, artwork)
|
|
|
|
artwork = self.addPlexCredentialsToUrl(artwork)
|
|
|
|
except KeyError:
|
|
|
|
artwork = ""
|
|
|
|
return artwork
|
|
|
|
|
2015-12-31 02:37:30 +11:00
|
|
|
def getAllArtwork(self, parentInfo=False):
|
2016-01-03 23:36:00 +11:00
|
|
|
"""
|
|
|
|
Gets the URLs to the Plex artwork, or empty string if not found.
|
2016-04-22 18:26:40 +10:00
|
|
|
parentInfo=True will check for parent's artwork if None is found
|
2015-12-31 01:57:55 +11:00
|
|
|
|
2016-01-03 23:36:00 +11:00
|
|
|
Output:
|
|
|
|
{
|
2016-04-29 20:44:56 +10:00
|
|
|
'Primary'
|
|
|
|
'Art'
|
|
|
|
'Banner'
|
|
|
|
'Logo'
|
|
|
|
'Thumb'
|
|
|
|
'Disc'
|
|
|
|
'Backdrop' : LIST with the first entry xml key "art"
|
2016-01-03 23:36:00 +11:00
|
|
|
}
|
2016-04-22 18:26:40 +10:00
|
|
|
"""
|
2015-12-31 01:57:55 +11:00
|
|
|
allartworks = {
|
2016-09-11 03:49:03 +10:00
|
|
|
'Primary': "", # corresponds to Plex poster ('thumb')
|
2015-12-31 01:57:55 +11:00
|
|
|
'Art': "",
|
2016-09-11 03:49:03 +10:00
|
|
|
'Banner': "", # corresponds to Plex banner ('banner') for series
|
2015-12-31 01:57:55 +11:00
|
|
|
'Logo': "",
|
2016-09-11 03:49:03 +10:00
|
|
|
'Thumb': "", # corresponds to Plex (grand)parent posters (thumb)
|
2015-12-31 01:57:55 +11:00
|
|
|
'Disc': "",
|
2016-09-11 03:49:03 +10:00
|
|
|
'Backdrop': [] # Corresponds to Plex fanart ('art')
|
2015-12-31 01:57:55 +11:00
|
|
|
}
|
|
|
|
# Process backdrops
|
|
|
|
# Get background artwork URL
|
2016-04-22 18:26:40 +10:00
|
|
|
allartworks['Backdrop'].append(self.__getOneArtwork('art'))
|
2015-12-31 01:57:55 +11:00
|
|
|
# Get primary "thumb" pictures:
|
2016-04-22 18:26:40 +10:00
|
|
|
allartworks['Primary'] = self.__getOneArtwork('thumb')
|
|
|
|
# Banner (usually only on tv series level)
|
|
|
|
allartworks['Banner'] = self.__getOneArtwork('banner')
|
|
|
|
# For e.g. TV shows, get series thumb
|
|
|
|
allartworks['Thumb'] = self.__getOneArtwork('grandparentThumb')
|
2015-12-31 01:57:55 +11:00
|
|
|
|
|
|
|
# Process parent items if the main item is missing artwork
|
|
|
|
if parentInfo:
|
|
|
|
# Process parent backdrops
|
|
|
|
if not allartworks['Backdrop']:
|
2016-04-22 18:26:40 +10:00
|
|
|
allartworks['Backdrop'].append(
|
|
|
|
self.__getOneArtwork('parentArt'))
|
2015-12-31 01:57:55 +11:00
|
|
|
if not allartworks['Primary']:
|
2016-04-22 18:26:40 +10:00
|
|
|
allartworks['Primary'] = self.__getOneArtwork('parentThumb')
|
2016-09-11 03:49:03 +10:00
|
|
|
return allartworks
|
2015-12-31 01:57:55 +11:00
|
|
|
|
2016-09-11 03:49:03 +10:00
|
|
|
def getFanartArtwork(self, allartworks, parentInfo=False):
|
|
|
|
"""
|
|
|
|
Downloads additional fanart from third party sources (well, link to
|
|
|
|
fanart only).
|
|
|
|
|
|
|
|
allartworks = {
|
|
|
|
'Primary': "",
|
|
|
|
'Art': "",
|
|
|
|
'Banner': "",
|
|
|
|
'Logo': "",
|
|
|
|
'Thumb': "",
|
|
|
|
'Disc': "",
|
|
|
|
'Backdrop': []
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
externalId = self.getExternalItemId()
|
|
|
|
if externalId is not None:
|
|
|
|
allartworks = self.getFanartTVArt(externalId, allartworks)
|
2016-04-29 20:44:56 +10:00
|
|
|
return allartworks
|
2016-04-16 05:44:54 +10:00
|
|
|
|
2016-06-09 03:50:24 +10:00
|
|
|
def getExternalItemId(self, collection=False):
|
2016-04-29 20:44:56 +10:00
|
|
|
"""
|
|
|
|
Returns the item's IMDB id for movies or tvdb id for TV shows
|
2016-04-18 19:34:01 +10:00
|
|
|
|
2016-04-29 20:44:56 +10:00
|
|
|
If not found in item's Plex metadata, check themovidedb.org
|
2016-06-09 03:50:24 +10:00
|
|
|
|
|
|
|
collection=True will try to return the collection's ID
|
|
|
|
|
|
|
|
None is returned if unsuccessful
|
2016-04-29 20:44:56 +10:00
|
|
|
"""
|
2016-04-16 05:44:54 +10:00
|
|
|
item = self.item.attrib
|
2016-04-29 20:44:56 +10:00
|
|
|
media_type = item.get('type')
|
2016-06-09 03:50:24 +10:00
|
|
|
mediaId = None
|
|
|
|
# Return the saved Plex id's, if applicable
|
|
|
|
# Always seek collection's ids since not provided by PMS
|
|
|
|
if collection is False:
|
|
|
|
if media_type == 'movie':
|
|
|
|
mediaId = self.getProvider('imdb')
|
|
|
|
elif media_type == 'show':
|
|
|
|
mediaId = self.getProvider('tvdb')
|
|
|
|
if mediaId is not None:
|
|
|
|
return mediaId
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Plex did not provide ID for IMDB or TVDB. Start '
|
|
|
|
'lookup process')
|
2016-06-09 03:50:24 +10:00
|
|
|
else:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Start movie set/collection lookup on themoviedb')
|
2016-04-29 20:44:56 +10:00
|
|
|
|
2016-04-16 05:44:54 +10:00
|
|
|
KODILANGUAGE = xbmc.getLanguage(xbmc.ISO_639_1)
|
2016-09-03 00:53:16 +10:00
|
|
|
apiKey = settings('themoviedbAPIKey')
|
2016-04-29 20:44:56 +10:00
|
|
|
if media_type == 'show':
|
|
|
|
media_type = 'tv'
|
|
|
|
title = item.get('title', '')
|
|
|
|
# if the title has the year in remove it as tmdb cannot deal with it...
|
|
|
|
# replace e.g. 'The Americans (2015)' with 'The Americans'
|
|
|
|
title = re.sub(r'\s*\(\d{4}\)$', '', title, count=1)
|
|
|
|
url = 'http://api.themoviedb.org/3/search/%s' % media_type
|
|
|
|
parameters = {
|
|
|
|
'api_key': apiKey,
|
2016-05-07 21:15:02 +10:00
|
|
|
'language': KODILANGUAGE,
|
2016-09-03 00:53:16 +10:00
|
|
|
'query': tryEncode(title)
|
2016-04-29 20:44:56 +10:00
|
|
|
}
|
|
|
|
data = downloadutils.DownloadUtils().downloadUrl(
|
|
|
|
url,
|
|
|
|
authenticate=False,
|
|
|
|
parameters=parameters,
|
|
|
|
timeout=7)
|
|
|
|
try:
|
|
|
|
data.get('test')
|
|
|
|
except:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('Could not download data from FanartTV')
|
2016-04-29 20:44:56 +10:00
|
|
|
return
|
|
|
|
if data.get('results') is None:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('No match found on themoviedb for type: %s, title: %s'
|
|
|
|
% (media_type, title))
|
2016-04-29 20:44:56 +10:00
|
|
|
return
|
|
|
|
|
2016-04-16 05:44:54 +10:00
|
|
|
year = item.get('year')
|
2016-04-29 20:44:56 +10:00
|
|
|
matchFound = None
|
|
|
|
# find year match
|
|
|
|
if year is not None:
|
|
|
|
for entry in data["results"]:
|
|
|
|
if year in entry.get("first_air_date", ""):
|
|
|
|
matchFound = entry
|
|
|
|
break
|
|
|
|
elif year in entry.get("release_date", ""):
|
|
|
|
matchFound = entry
|
|
|
|
break
|
|
|
|
# find exact match based on title, if we haven't found a year match
|
|
|
|
if matchFound is None:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('No themoviedb match found using year %s' % year)
|
2016-04-29 20:44:56 +10:00
|
|
|
replacements = (
|
|
|
|
' ',
|
|
|
|
'-',
|
|
|
|
'&',
|
|
|
|
',',
|
|
|
|
':',
|
|
|
|
';'
|
|
|
|
)
|
|
|
|
for entry in data["results"]:
|
|
|
|
name = entry.get("name", entry.get("title", ""))
|
|
|
|
original_name = entry.get("original_name", "")
|
|
|
|
title_alt = title.lower()
|
|
|
|
name_alt = name.lower()
|
|
|
|
org_name_alt = original_name.lower()
|
|
|
|
for replaceString in replacements:
|
|
|
|
title_alt = title_alt.replace(replaceString, '')
|
|
|
|
name_alt = name_alt.replace(replaceString, '')
|
|
|
|
org_name_alt = org_name_alt.replace(replaceString, '')
|
|
|
|
if name == title or original_name == title:
|
|
|
|
# match found for exact title name
|
|
|
|
matchFound = entry
|
|
|
|
break
|
|
|
|
elif (name.split(" (")[0] == title or title_alt == name_alt
|
|
|
|
or title_alt == org_name_alt):
|
|
|
|
# match found with substituting some stuff
|
|
|
|
matchFound = entry
|
|
|
|
break
|
|
|
|
|
|
|
|
# if a match was not found, we accept the closest match from TMDB
|
|
|
|
if matchFound is None and len(data.get("results")) > 0:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Using very first match from themoviedb')
|
2016-04-29 20:44:56 +10:00
|
|
|
matchFound = entry = data.get("results")[0]
|
|
|
|
|
|
|
|
if matchFound is None:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Still no themoviedb match for type: %s, title: %s, '
|
|
|
|
'year: %s' % (media_type, title, year))
|
|
|
|
log.debug('themoviedb answer was %s' % data['results'])
|
2016-04-29 20:44:56 +10:00
|
|
|
return
|
|
|
|
|
2016-09-03 00:53:16 +10:00
|
|
|
log.info('Found themoviedb match for %s: %s'
|
|
|
|
% (item.get('title'), matchFound))
|
2016-04-29 20:44:56 +10:00
|
|
|
|
|
|
|
tmdbId = str(entry.get("id", ""))
|
|
|
|
if tmdbId == '':
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('No themoviedb ID found, aborting')
|
2016-04-29 20:44:56 +10:00
|
|
|
return
|
|
|
|
|
|
|
|
if media_type == "multi" and entry.get("media_type"):
|
|
|
|
media_type = entry.get("media_type")
|
|
|
|
name = entry.get("name", entry.get("title"))
|
|
|
|
# lookup external tmdbId and perform artwork lookup on fanart.tv
|
|
|
|
parameters = {
|
|
|
|
'api_key': apiKey
|
|
|
|
}
|
|
|
|
for language in [KODILANGUAGE, "en"]:
|
|
|
|
parameters['language'] = language
|
|
|
|
if media_type == "movie":
|
|
|
|
url = 'http://api.themoviedb.org/3/movie/%s' % tmdbId
|
|
|
|
parameters['append_to_response'] = 'videos'
|
|
|
|
elif media_type == "tv":
|
|
|
|
url = 'http://api.themoviedb.org/3/tv/%s' % tmdbId
|
|
|
|
parameters['append_to_response'] = 'external_ids,videos'
|
|
|
|
data = downloadutils.DownloadUtils().downloadUrl(
|
|
|
|
url,
|
|
|
|
authenticate=False,
|
|
|
|
parameters=parameters,
|
|
|
|
timeout=7)
|
|
|
|
try:
|
|
|
|
data.get('test')
|
|
|
|
except:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('Could not download %s with parameters %s'
|
|
|
|
% (url, parameters))
|
2016-04-29 20:44:56 +10:00
|
|
|
continue
|
2016-06-09 04:34:49 +10:00
|
|
|
if collection is False:
|
2016-06-09 03:50:24 +10:00
|
|
|
if data.get("imdb_id") is not None:
|
|
|
|
mediaId = str(data.get("imdb_id"))
|
2016-06-07 06:10:58 +10:00
|
|
|
break
|
2016-06-09 03:50:24 +10:00
|
|
|
if data.get("external_ids") is not None:
|
|
|
|
mediaId = str(data["external_ids"].get("tvdb_id"))
|
2016-06-07 06:10:58 +10:00
|
|
|
break
|
2016-06-09 03:50:24 +10:00
|
|
|
else:
|
|
|
|
if data.get("belongs_to_collection") is not None:
|
|
|
|
mediaId = str(data.get("belongs_to_collection").get("id"))
|
2016-09-03 00:53:16 +10:00
|
|
|
log.debug('Retrieved collections tmdb id %s' % mediaId)
|
2016-06-07 06:10:58 +10:00
|
|
|
return mediaId
|
|
|
|
|
|
|
|
def getFanartTVArt(self, mediaId, allartworks, setInfo=False):
|
2016-04-29 20:44:56 +10:00
|
|
|
"""
|
|
|
|
perform artwork lookup on fanart.tv
|
|
|
|
|
|
|
|
mediaId: IMDB id for movies, tvdb id for TV shows
|
|
|
|
"""
|
|
|
|
item = self.item.attrib
|
|
|
|
KODILANGUAGE = xbmc.getLanguage(xbmc.ISO_639_1)
|
2016-09-03 00:53:16 +10:00
|
|
|
api_key = settings('FanArtTVAPIKey')
|
2016-04-29 20:44:56 +10:00
|
|
|
typus = item.get('type')
|
|
|
|
if typus == 'show':
|
|
|
|
typus = 'tv'
|
|
|
|
|
|
|
|
if typus == "movie":
|
|
|
|
url = 'http://webservice.fanart.tv/v3/movies/%s?api_key=%s' \
|
|
|
|
% (mediaId, api_key)
|
|
|
|
elif typus == 'tv':
|
|
|
|
url = 'http://webservice.fanart.tv/v3/tv/%s?api_key=%s' \
|
|
|
|
% (mediaId, api_key)
|
|
|
|
else:
|
|
|
|
# Not supported artwork
|
|
|
|
return allartworks
|
|
|
|
data = downloadutils.DownloadUtils().downloadUrl(
|
|
|
|
url,
|
|
|
|
authenticate=False,
|
|
|
|
timeout=15)
|
2016-04-16 05:44:54 +10:00
|
|
|
try:
|
2016-04-29 20:44:56 +10:00
|
|
|
data.get('test')
|
|
|
|
except:
|
2016-09-03 00:53:16 +10:00
|
|
|
log.error('Could not download data from FanartTV')
|
2016-04-29 20:44:56 +10:00
|
|
|
return allartworks
|
|
|
|
|
|
|
|
# we need to use a little mapping between fanart.tv arttypes and kodi
|
|
|
|
# artttypes
|
|
|
|
fanartTVTypes = [
|
|
|
|
("logo", "Logo"),
|
|
|
|
("musiclogo", "clearlogo"),
|
|
|
|
("disc", "Disc"),
|
|
|
|
("clearart", "Art"),
|
|
|
|
("banner", "Banner"),
|
|
|
|
("clearlogo", "Logo"),
|
|
|
|
("background", "fanart"),
|
|
|
|
("showbackground", "fanart"),
|
|
|
|
("characterart", "characterart")
|
|
|
|
]
|
|
|
|
if typus == "artist":
|
|
|
|
fanartTVTypes.append(("thumb", "folder"))
|
|
|
|
else:
|
|
|
|
fanartTVTypes.append(("thumb", "Thumb"))
|
2016-06-07 06:10:58 +10:00
|
|
|
|
|
|
|
if setInfo:
|
|
|
|
fanartTVTypes.append(("poster", "Primary"))
|
|
|
|
|
2016-04-29 20:44:56 +10:00
|
|
|
prefixes = (
|
|
|
|
"hd" + typus,
|
|
|
|
"hd",
|
|
|
|
typus,
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
for fanarttype in fanartTVTypes:
|
|
|
|
# Skip the ones we already have
|
|
|
|
if allartworks.get(fanarttype[1]):
|
|
|
|
continue
|
|
|
|
for prefix in prefixes:
|
|
|
|
fanarttvimage = prefix + fanarttype[0]
|
|
|
|
if fanarttvimage not in data:
|
|
|
|
continue
|
|
|
|
# select image in preferred language
|
|
|
|
for entry in data[fanarttvimage]:
|
|
|
|
if entry.get("lang") == KODILANGUAGE:
|
|
|
|
allartworks[fanarttype[1]] = entry.get("url")
|
|
|
|
break
|
|
|
|
# just grab the first english OR undefinded one as fallback
|
2016-06-19 23:34:27 +10:00
|
|
|
# (so we're actually grabbing the more popular one)
|
|
|
|
if not allartworks.get(fanarttype[1]):
|
2016-04-29 20:44:56 +10:00
|
|
|
for entry in data[fanarttvimage]:
|
|
|
|
if entry.get("lang") in ("en", "00"):
|
|
|
|
allartworks[fanarttype[1]] = entry.get("url")
|
2016-04-16 05:44:54 +10:00
|
|
|
break
|
|
|
|
|
2016-04-29 20:44:56 +10:00
|
|
|
# grab extrafanarts in list
|
|
|
|
maxfanarts = 10
|
|
|
|
fanartcount = 0
|
|
|
|
for prefix in prefixes:
|
|
|
|
fanarttvimage = prefix + 'background'
|
|
|
|
if fanarttvimage not in data:
|
|
|
|
continue
|
|
|
|
for entry in data[fanarttvimage]:
|
|
|
|
if fanartcount < maxfanarts:
|
|
|
|
if xbmcvfs.exists(entry.get("url")):
|
|
|
|
allartworks['Backdrop'].append(entry.get("url"))
|
|
|
|
fanartcount += 1
|
|
|
|
return allartworks
|
2016-01-02 00:40:40 +11:00
|
|
|
|
2016-06-07 06:10:58 +10:00
|
|
|
def getSetArtwork(self, parentInfo=False):
|
|
|
|
"""
|
|
|
|
Gets the URLs to the Plex artwork, or empty string if not found.
|
|
|
|
parentInfo=True will check for parent's artwork if None is found
|
|
|
|
|
2016-06-09 03:50:24 +10:00
|
|
|
Only call on movies
|
|
|
|
|
2016-06-07 06:10:58 +10:00
|
|
|
Output:
|
|
|
|
{
|
|
|
|
'Primary'
|
|
|
|
'Art'
|
|
|
|
'Banner'
|
|
|
|
'Logo'
|
|
|
|
'Thumb'
|
|
|
|
'Disc'
|
|
|
|
'Backdrop' : LIST with the first entry xml key "art"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
allartworks = {
|
|
|
|
'Primary': "",
|
|
|
|
'Art': "",
|
|
|
|
'Banner': "",
|
|
|
|
'Logo': "",
|
|
|
|
'Thumb': "",
|
|
|
|
'Disc': "",
|
|
|
|
'Backdrop': []
|
|
|
|
}
|
|
|
|
|
|
|
|
# Plex does not get much artwork - go ahead and get the rest from
|
|
|
|
# fanart tv only for movie or tv show
|
2016-06-09 03:50:24 +10:00
|
|
|
externalId = self.getExternalItemId(collection=True)
|
|
|
|
if externalId is not None:
|
|
|
|
allartworks = self.getFanartTVArt(externalId, allartworks, True)
|
2016-06-07 06:10:58 +10:00
|
|
|
return allartworks
|
|
|
|
|
2016-04-17 21:36:41 +10:00
|
|
|
def shouldStream(self):
|
|
|
|
"""
|
|
|
|
Returns True if the item's 'optimizedForStreaming' is set, False other-
|
|
|
|
wise
|
|
|
|
"""
|
2016-04-29 20:44:56 +10:00
|
|
|
return self.item[0].attrib.get('optimizedForStreaming') == '1'
|
2016-04-17 21:36:41 +10:00
|
|
|
|
2016-09-08 23:55:49 +10:00
|
|
|
def getMediastreamNumber(self):
|
2016-07-13 04:16:45 +10:00
|
|
|
"""
|
|
|
|
Returns the Media stream as an int (mostly 0). Will let the user choose
|
|
|
|
if several media streams are present for a PMS item (if settings are
|
|
|
|
set accordingly)
|
|
|
|
"""
|
|
|
|
# How many streams do we have?
|
|
|
|
count = 0
|
|
|
|
for entry in self.item.findall('./Media'):
|
|
|
|
count += 1
|
|
|
|
if (count > 1 and (
|
|
|
|
(self.getType() != 'clip' and
|
2016-09-03 00:53:16 +10:00
|
|
|
settings('bestQuality') == 'false')
|
2016-07-13 04:16:45 +10:00
|
|
|
or
|
|
|
|
(self.getType() == 'clip' and
|
2016-09-03 00:53:16 +10:00
|
|
|
settings('bestTrailer') == 'false'))):
|
2016-07-13 04:16:45 +10:00
|
|
|
# Several streams/files available.
|
|
|
|
dialoglist = []
|
|
|
|
for entry in self.item.findall('./Media'):
|
|
|
|
dialoglist.append(
|
|
|
|
"%sp %s - %s (%s)"
|
|
|
|
% (entry.attrib.get('videoResolution', 'unknown'),
|
|
|
|
entry.attrib.get('videoCodec', 'unknown'),
|
|
|
|
entry.attrib.get('audioProfile', 'unknown'),
|
|
|
|
entry.attrib.get('audioCodec', 'unknown'))
|
|
|
|
)
|
|
|
|
media = xbmcgui.Dialog().select('Select stream', dialoglist)
|
|
|
|
else:
|
|
|
|
media = 0
|
2016-09-08 23:55:49 +10:00
|
|
|
self.mediastream = media
|
2016-07-13 04:16:45 +10:00
|
|
|
return media
|
|
|
|
|
2016-07-13 04:03:10 +10:00
|
|
|
def getTranscodeVideoPath(self, action, quality=None):
|
2016-01-02 00:40:40 +11:00
|
|
|
"""
|
2016-02-03 23:01:13 +11:00
|
|
|
|
|
|
|
To be called on a VIDEO level of PMS xml response!
|
|
|
|
|
2016-01-03 23:36:00 +11:00
|
|
|
Transcode Video support; returns the URL to get a media started
|
2016-01-02 00:40:40 +11:00
|
|
|
|
|
|
|
Input:
|
2016-04-12 02:57:20 +10:00
|
|
|
action 'DirectStream' or 'Transcode'
|
2016-01-04 05:17:59 +11:00
|
|
|
|
2016-01-03 23:36:00 +11:00
|
|
|
quality: {
|
2016-02-05 06:23:04 +11:00
|
|
|
'videoResolution': e.g. '1024x768',
|
|
|
|
'videoQuality': e.g. '60',
|
|
|
|
'maxVideoBitrate': e.g. '2000' (in kbits)
|
2016-01-03 23:36:00 +11:00
|
|
|
}
|
2016-01-06 02:05:20 +11:00
|
|
|
(one or several of these options)
|
2016-01-02 00:40:40 +11:00
|
|
|
Output:
|
2016-01-06 02:05:20 +11:00
|
|
|
final URL to pull in PMS transcoder
|
2016-01-04 05:17:59 +11:00
|
|
|
|
|
|
|
TODO: mediaIndex
|
2016-01-02 00:40:40 +11:00
|
|
|
"""
|
2016-09-08 23:55:49 +10:00
|
|
|
if self.mediastream is None:
|
|
|
|
self.getMediastreamNumber()
|
2016-07-13 04:03:10 +10:00
|
|
|
if quality is None:
|
|
|
|
quality = {}
|
2016-09-03 00:53:16 +10:00
|
|
|
xargs = self.client.getXArgsDeviceInfo()
|
2016-02-05 06:23:04 +11:00
|
|
|
# For DirectPlay, path/key of PART is needed
|
2016-06-26 20:32:19 +10:00
|
|
|
# trailers are 'clip' with PMS xmls
|
2016-04-12 02:57:20 +10:00
|
|
|
if action == "DirectStream":
|
2016-09-08 23:55:49 +10:00
|
|
|
path = self.item[self.mediastream][self.part].attrib['key']
|
2016-02-03 23:01:13 +11:00
|
|
|
url = self.server + path
|
2016-02-07 22:38:50 +11:00
|
|
|
# e.g. Trailers already feature an '?'!
|
2016-02-03 23:01:13 +11:00
|
|
|
if '?' in url:
|
|
|
|
url += '&' + urlencode(xargs)
|
|
|
|
else:
|
|
|
|
url += '?' + urlencode(xargs)
|
2016-01-06 02:05:20 +11:00
|
|
|
return url
|
|
|
|
|
2016-04-14 00:14:55 +10:00
|
|
|
# For Transcoding
|
2016-02-05 06:23:04 +11:00
|
|
|
# Path/key to VIDEO item of xml PMS response is needed, not part
|
|
|
|
path = self.item.attrib['key']
|
2016-01-02 00:40:40 +11:00
|
|
|
transcodePath = self.server + \
|
2016-02-07 22:38:50 +11:00
|
|
|
'/video/:/transcode/universal/start.m3u8?'
|
2016-01-02 00:40:40 +11:00
|
|
|
args = {
|
2016-03-24 22:34:39 +11:00
|
|
|
'protocol': 'hls', # seen in the wild: 'dash', 'http', 'hls'
|
2016-09-03 00:53:16 +10:00
|
|
|
'session': self.client.getDeviceId(),
|
2016-03-24 22:34:39 +11:00
|
|
|
'fastSeek': 1,
|
2016-01-02 00:40:40 +11:00
|
|
|
'path': path,
|
2016-09-08 23:55:49 +10:00
|
|
|
'mediaIndex': self.mediastream,
|
2016-01-06 02:05:20 +11:00
|
|
|
'partIndex': self.part,
|
2016-03-24 22:34:39 +11:00
|
|
|
# 'copyts': 1,
|
2016-02-07 22:38:50 +11:00
|
|
|
# 'offset': 0, # Resume point
|
2016-01-02 00:40:40 +11:00
|
|
|
}
|
2016-03-24 20:24:11 +11:00
|
|
|
# Seem like PHT to let the PMS use the transcoding profile
|
|
|
|
xargs['X-Plex-Device'] = 'Plex Home Theater'
|
2016-02-07 22:38:50 +11:00
|
|
|
# Currently not used!
|
2016-01-06 02:05:20 +11:00
|
|
|
if action == "DirectStream":
|
|
|
|
argsUpdate = {
|
|
|
|
'directPlay': '0',
|
|
|
|
'directStream': '1',
|
|
|
|
}
|
|
|
|
args.update(argsUpdate)
|
|
|
|
elif action == 'Transcode':
|
|
|
|
argsUpdate = {
|
|
|
|
'directPlay': '0',
|
|
|
|
'directStream': '0'
|
|
|
|
}
|
2016-09-03 00:53:16 +10:00
|
|
|
log.debug("Setting transcode quality to: %s" % quality)
|
2016-01-02 00:40:40 +11:00
|
|
|
args.update(quality)
|
2016-01-06 02:05:20 +11:00
|
|
|
args.update(argsUpdate)
|
2016-03-24 20:24:11 +11:00
|
|
|
url = transcodePath + urlencode(xargs) + '&' + urlencode(args)
|
2016-01-06 02:05:20 +11:00
|
|
|
return url
|
2016-01-02 00:40:40 +11:00
|
|
|
|
|
|
|
def externalSubs(self, playurl):
|
|
|
|
externalsubs = []
|
|
|
|
mapping = {}
|
|
|
|
item = self.item
|
|
|
|
try:
|
2016-02-01 20:33:33 +11:00
|
|
|
mediastreams = item[0][self.part]
|
2016-01-02 00:40:40 +11:00
|
|
|
except (TypeError, KeyError, IndexError):
|
|
|
|
return
|
|
|
|
kodiindex = 0
|
|
|
|
for stream in mediastreams:
|
|
|
|
index = stream.attrib['id']
|
2016-04-13 01:18:32 +10:00
|
|
|
# Since Emby returns all possible tracks together, have to pull
|
|
|
|
# only external subtitles.
|
2016-02-07 22:38:50 +11:00
|
|
|
key = stream.attrib.get('key')
|
2016-01-02 00:40:40 +11:00
|
|
|
# IsTextSubtitleStream if true, is available to download from emby.
|
2016-02-07 22:38:50 +11:00
|
|
|
if stream.attrib.get('streamType') == "3" and key:
|
2016-01-02 00:40:40 +11:00
|
|
|
# Direct stream
|
2016-02-07 22:38:50 +11:00
|
|
|
url = ("%s%s" % (self.server, key))
|
|
|
|
url = self.addPlexCredentialsToUrl(url)
|
2016-01-02 00:40:40 +11:00
|
|
|
# map external subtitles for mapping
|
|
|
|
mapping[kodiindex] = index
|
|
|
|
externalsubs.append(url)
|
|
|
|
kodiindex += 1
|
|
|
|
mapping = json.dumps(mapping)
|
2016-09-03 00:53:16 +10:00
|
|
|
window('emby_%s.indexMapping' % playurl, value=mapping)
|
|
|
|
log.info('Found external subs: %s' % externalsubs)
|
2016-01-02 00:40:40 +11:00
|
|
|
return externalsubs
|
2016-03-15 03:47:05 +11:00
|
|
|
|
2016-06-19 22:58:42 +10:00
|
|
|
def CreateListItemFromPlexItem(self,
|
|
|
|
listItem=None,
|
|
|
|
appendShowTitle=False,
|
|
|
|
appendSxxExx=False):
|
|
|
|
if self.getType() == 'photo':
|
2016-09-03 00:53:16 +10:00
|
|
|
listItem = self._createPhotoListItem(listItem)
|
2016-06-19 22:58:42 +10:00
|
|
|
else:
|
2016-09-03 00:53:16 +10:00
|
|
|
listItem = self._createVideoListItem(listItem,
|
|
|
|
appendShowTitle,
|
|
|
|
appendSxxExx)
|
2016-06-19 22:58:42 +10:00
|
|
|
return listItem
|
|
|
|
|
|
|
|
def GetKodiPremierDate(self):
|
2016-06-13 01:22:22 +10:00
|
|
|
"""
|
2016-06-19 22:58:42 +10:00
|
|
|
Takes Plex' originallyAvailableAt of the form "yyyy-mm-dd" and returns
|
|
|
|
Kodi's "dd.mm.yyyy"
|
2016-06-13 01:22:22 +10:00
|
|
|
"""
|
2016-06-19 22:58:42 +10:00
|
|
|
date = self.getPremiereDate()
|
|
|
|
if date is None:
|
|
|
|
return
|
2016-06-13 01:22:22 +10:00
|
|
|
try:
|
2016-06-19 22:58:42 +10:00
|
|
|
date = re.sub(r'(\d+)-(\d+)-(\d+)', r'\3.\2.\1', date)
|
2016-06-13 01:22:22 +10:00
|
|
|
except:
|
2016-06-19 22:58:42 +10:00
|
|
|
date = None
|
|
|
|
return date
|
2016-06-13 01:22:22 +10:00
|
|
|
|
2016-09-03 00:53:16 +10:00
|
|
|
def _createPhotoListItem(self, listItem=None):
|
2016-06-19 22:58:42 +10:00
|
|
|
"""
|
|
|
|
Use for photo items only
|
2016-03-15 03:47:05 +11:00
|
|
|
"""
|
2016-06-19 22:58:42 +10:00
|
|
|
title, _ = self.getTitle()
|
|
|
|
if listItem is None:
|
|
|
|
listItem = xbmcgui.ListItem(title)
|
|
|
|
else:
|
|
|
|
listItem.setLabel(title)
|
|
|
|
listItem.setProperty('IsPlayable', 'true')
|
2016-09-03 00:53:16 +10:00
|
|
|
if settings('useDirectPaths') == '0':
|
2016-06-20 00:12:56 +10:00
|
|
|
# Addon paths
|
|
|
|
path = self.addPlexCredentialsToUrl(
|
2016-09-03 00:53:16 +10:00
|
|
|
'%s%s' % (window('pms_server'),
|
2016-06-20 00:12:56 +10:00
|
|
|
self.item[0][0].attrib['key']))
|
|
|
|
else:
|
|
|
|
# Native direct paths
|
2016-07-13 04:27:59 +10:00
|
|
|
path = self.validatePlayurl(
|
|
|
|
self.getFilePath(forceFirstMediaStream=True),
|
|
|
|
'photo')
|
2016-09-03 00:53:16 +10:00
|
|
|
path = tryEncode(path)
|
2016-06-19 22:58:42 +10:00
|
|
|
metadata = {
|
|
|
|
'date': self.GetKodiPremierDate(),
|
|
|
|
'picturepath': path,
|
|
|
|
'size': long(self.item[0][0].attrib.get('size', 0)),
|
|
|
|
'exif:width': self.item[0].attrib.get('width', ''),
|
|
|
|
'exif:height': self.item[0].attrib.get('height', ''),
|
|
|
|
'title': title
|
|
|
|
}
|
|
|
|
listItem.setInfo('pictures', infoLabels=metadata)
|
|
|
|
try:
|
|
|
|
if int(metadata['exif:width']) > int(metadata['exif:height']):
|
|
|
|
# add image as fanart for use with skinhelper auto thumb/
|
|
|
|
# backgrund creation
|
|
|
|
listItem.setArt({'fanart': path})
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
# Stuff that we CANNOT set with listItem.setInfo
|
|
|
|
listItem.setProperty('path', path)
|
|
|
|
listItem.setProperty('plot', self.getPlot())
|
|
|
|
listItem.setProperty('plexid', self.getRatingKey())
|
|
|
|
# We do NOT set these props
|
|
|
|
# listItem.setProperty('isPlayable', 'true')
|
|
|
|
# listItem.setProperty('isFolder', 'true')
|
|
|
|
# Further stuff
|
|
|
|
listItem.setIconImage('DefaultPicture.png')
|
|
|
|
return listItem
|
|
|
|
|
2016-09-03 00:53:16 +10:00
|
|
|
def _createVideoListItem(self,
|
|
|
|
listItem=None,
|
|
|
|
appendShowTitle=False,
|
|
|
|
appendSxxExx=False):
|
2016-06-19 22:58:42 +10:00
|
|
|
"""
|
|
|
|
Use for video items only
|
2016-03-15 03:47:05 +11:00
|
|
|
Call on a child level of PMS xml response (e.g. in a for loop)
|
|
|
|
|
2016-04-27 18:37:24 +10:00
|
|
|
listItem : existing xbmcgui.ListItem to work with
|
|
|
|
otherwise, a new one is created
|
|
|
|
appendShowTitle : True to append TV show title to episode title
|
|
|
|
appendSxxExx : True to append SxxExx to episode title
|
2016-03-15 03:47:05 +11:00
|
|
|
|
|
|
|
Returns XBMC listitem for this PMS library item
|
|
|
|
"""
|
|
|
|
title, sorttitle = self.getTitle()
|
2016-06-13 01:22:22 +10:00
|
|
|
typus = self.getType()
|
2016-03-15 03:47:05 +11:00
|
|
|
|
|
|
|
if listItem is None:
|
2016-04-22 18:26:40 +10:00
|
|
|
listItem = xbmcgui.ListItem(title)
|
|
|
|
listItem.setProperty('IsPlayable', 'true')
|
2016-03-15 03:47:05 +11:00
|
|
|
|
2016-06-19 22:58:42 +10:00
|
|
|
# Video items, e.g. movies and episodes or clips
|
|
|
|
people = self.getPeople()
|
|
|
|
userdata = self.getUserData()
|
|
|
|
metadata = {
|
|
|
|
'genre': self.joinList(self.getGenres()),
|
|
|
|
'year': self.getYear(),
|
|
|
|
'rating': self.getAudienceRating(),
|
|
|
|
'playcount': userdata['PlayCount'],
|
|
|
|
'cast': people['Cast'],
|
|
|
|
'director': self.joinList(people.get('Director')),
|
|
|
|
'plot': self.getPlot(),
|
|
|
|
'sorttitle': sorttitle,
|
|
|
|
'duration': userdata['Runtime'],
|
|
|
|
'studio': self.joinList(self.getStudios()),
|
|
|
|
'tagline': self.getTagline(),
|
|
|
|
'writer': self.joinList(people.get('Writer')),
|
|
|
|
'premiered': self.getPremiereDate(),
|
|
|
|
'dateadded': self.getDateCreated(),
|
|
|
|
'lastplayed': userdata['LastPlayedDate'],
|
|
|
|
'mpaa': self.getMpaa(),
|
|
|
|
'aired': self.getPremiereDate()
|
|
|
|
}
|
|
|
|
listItem.setProperty('resumetime', str(userdata['Resume']))
|
|
|
|
listItem.setProperty('totaltime', str(userdata['Runtime']))
|
2016-03-15 03:47:05 +11:00
|
|
|
|
2016-06-13 01:22:22 +10:00
|
|
|
if typus == "episode":
|
2016-03-15 03:47:05 +11:00
|
|
|
# Only for tv shows
|
|
|
|
key, show, season, episode = self.getEpisodeDetails()
|
2016-04-22 18:26:40 +10:00
|
|
|
season = -1 if season is None else int(season)
|
|
|
|
episode = -1 if episode is None else int(episode)
|
2016-04-19 23:20:03 +10:00
|
|
|
metadata['episode'] = episode
|
|
|
|
metadata['season'] = season
|
2016-03-15 03:47:05 +11:00
|
|
|
metadata['tvshowtitle'] = show
|
2016-04-22 18:26:40 +10:00
|
|
|
if season and episode:
|
|
|
|
listItem.setProperty('episodeno',
|
|
|
|
"s%.2de%.2d" % (season, episode))
|
2016-04-27 18:37:24 +10:00
|
|
|
if appendSxxExx is True:
|
|
|
|
title = "S%.2dE%.2d - %s" % (season, episode, title)
|
2016-04-22 18:26:40 +10:00
|
|
|
listItem.setIconImage('DefaultTVShows.png')
|
2016-04-27 18:37:24 +10:00
|
|
|
if appendShowTitle is True:
|
2016-06-19 22:58:42 +10:00
|
|
|
title = "%s - %s " % (show, title)
|
2016-06-13 01:22:22 +10:00
|
|
|
elif typus == "movie":
|
2016-04-22 18:26:40 +10:00
|
|
|
listItem.setIconImage('DefaultMovies.png')
|
|
|
|
else:
|
2016-06-13 01:22:22 +10:00
|
|
|
# E.g. clips, trailers, ...
|
2016-04-22 18:26:40 +10:00
|
|
|
listItem.setIconImage('DefaultVideo.png')
|
2016-04-19 23:20:03 +10:00
|
|
|
|
2016-04-22 18:26:40 +10:00
|
|
|
plexId = self.getRatingKey()
|
2016-05-31 16:06:42 +10:00
|
|
|
listItem.setProperty('plexid', plexId)
|
2016-04-22 18:26:40 +10:00
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
|
|
|
try:
|
|
|
|
listItem.setProperty('dbid',
|
|
|
|
str(emby_db.getItem_byId(plexId)[0]))
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
# Expensive operation
|
2016-04-27 18:37:24 +10:00
|
|
|
metadata['title'] = title
|
|
|
|
listItem.setLabel(title)
|
2016-06-19 22:58:42 +10:00
|
|
|
listItem.setInfo('video', infoLabels=metadata)
|
2016-03-15 03:47:05 +11:00
|
|
|
return listItem
|
|
|
|
|
|
|
|
def AddStreamInfo(self, listItem):
|
|
|
|
"""
|
|
|
|
Add media stream information to xbmcgui.ListItem
|
|
|
|
"""
|
|
|
|
mediastreams = self.getMediaStreams()
|
|
|
|
videostreamFound = False
|
|
|
|
if mediastreams:
|
|
|
|
for key, value in mediastreams.iteritems():
|
|
|
|
if key == "video" and value:
|
|
|
|
videostreamFound = True
|
|
|
|
if value:
|
|
|
|
listItem.addStreamInfo(key, value)
|
|
|
|
if not videostreamFound:
|
|
|
|
# just set empty streamdetails to prevent errors in the logs
|
|
|
|
listItem.addStreamInfo(
|
|
|
|
"video", {'duration': self.getRuntime()[1]})
|
2016-03-17 04:33:18 +11:00
|
|
|
|
2016-04-13 22:15:31 +10:00
|
|
|
def validatePlayurl(self, path, typus, forceCheck=False, folder=False):
|
2016-03-17 04:33:18 +11:00
|
|
|
"""
|
2016-04-13 01:18:32 +10:00
|
|
|
Returns a valid path for Kodi, e.g. with '\' substituted to '\\' in
|
|
|
|
Unicode. Returns None if this is not possible
|
|
|
|
|
|
|
|
path : Unicode
|
|
|
|
typus : Plex type from PMS xml
|
|
|
|
forceCheck : Will always try to check validity of path
|
|
|
|
Will also skip confirmation dialog if path not found
|
2016-04-13 22:15:31 +10:00
|
|
|
folder : Set to True if path is a folder
|
2016-03-17 04:33:18 +11:00
|
|
|
"""
|
2016-04-13 01:18:32 +10:00
|
|
|
if path is None:
|
|
|
|
return None
|
|
|
|
types = {
|
|
|
|
'movie': 'movie',
|
|
|
|
'show': 'tv',
|
|
|
|
'season': 'tv',
|
|
|
|
'episode': 'tv',
|
|
|
|
'artist': 'music',
|
|
|
|
'album': 'music',
|
|
|
|
'song': 'music',
|
|
|
|
'track': 'music',
|
2016-06-05 02:48:22 +10:00
|
|
|
'clip': 'clip',
|
|
|
|
'photo': 'photo'
|
2016-04-13 01:18:32 +10:00
|
|
|
}
|
|
|
|
typus = types[typus]
|
2016-09-17 01:42:39 +10:00
|
|
|
if settings('remapSMB') == 'true':
|
|
|
|
path = path.replace(settings('remapSMB%sOrg' % typus),
|
|
|
|
settings('remapSMB%sNew' % typus),
|
2016-05-18 04:15:46 +10:00
|
|
|
1)
|
2016-03-17 04:33:18 +11:00
|
|
|
# There might be backslashes left over:
|
2016-04-13 01:18:32 +10:00
|
|
|
path = path.replace('\\', '/')
|
2016-09-17 01:42:39 +10:00
|
|
|
elif settings('replaceSMB') == 'true':
|
2016-04-13 01:18:32 +10:00
|
|
|
if path.startswith('\\\\'):
|
|
|
|
path = 'smb:' + path.replace('\\', '/')
|
2016-09-17 01:42:39 +10:00
|
|
|
if settings('plex_pathverified') == 'true' and forceCheck is False:
|
2016-04-13 01:18:32 +10:00
|
|
|
return path
|
|
|
|
|
2016-04-13 22:15:31 +10:00
|
|
|
# exist() needs a / or \ at the end to work for directories
|
|
|
|
if folder is False:
|
|
|
|
# files
|
2016-09-03 00:53:16 +10:00
|
|
|
check = xbmcvfs.exists(tryEncode(path)) == 1
|
2016-04-13 22:15:31 +10:00
|
|
|
else:
|
|
|
|
# directories
|
|
|
|
if "\\" in path:
|
|
|
|
# Add the missing backslash
|
2016-09-03 00:53:16 +10:00
|
|
|
check = xbmcvfs.exists(tryEncode(path + "\\")) == 1
|
2016-04-13 22:15:31 +10:00
|
|
|
else:
|
2016-09-03 00:53:16 +10:00
|
|
|
check = xbmcvfs.exists(tryEncode(path + "/")) == 1
|
2016-04-13 22:15:31 +10:00
|
|
|
|
2016-04-13 01:18:32 +10:00
|
|
|
if check is False:
|
|
|
|
if forceCheck is False:
|
|
|
|
# Validate the path is correct with user intervention
|
|
|
|
if self.askToValidate(path):
|
2016-09-03 00:53:16 +10:00
|
|
|
window('plex_shouldStop', value="true")
|
2016-04-13 01:18:32 +10:00
|
|
|
path = None
|
2016-09-17 01:42:39 +10:00
|
|
|
settings('plex_pathverified', value='true')
|
2016-04-13 01:18:32 +10:00
|
|
|
else:
|
|
|
|
path = None
|
|
|
|
elif forceCheck is False:
|
2016-09-17 01:42:39 +10:00
|
|
|
if settings('plex_pathverified') != 'true':
|
2016-09-03 00:53:16 +10:00
|
|
|
settings('plex_pathverified', value='true')
|
2016-04-13 01:18:32 +10:00
|
|
|
return path
|
2016-03-17 04:33:18 +11:00
|
|
|
|
|
|
|
def askToValidate(self, url):
|
|
|
|
"""
|
|
|
|
Displays a YESNO dialog box:
|
|
|
|
Kodi can't locate file: <url>. Please verify the path.
|
|
|
|
You may need to verify your network credentials in the
|
|
|
|
add-on settings or use different Plex paths. Stop syncing?
|
|
|
|
|
|
|
|
Returns True if sync should stop, else False
|
|
|
|
"""
|
2016-09-03 00:53:16 +10:00
|
|
|
log.warn('Cannot access file: %s' % url)
|
2016-03-17 04:33:18 +11:00
|
|
|
resp = xbmcgui.Dialog().yesno(
|
2016-09-03 00:53:16 +10:00
|
|
|
heading=addonName,
|
|
|
|
line1=lang(39031) + url,
|
|
|
|
line2=lang(39032))
|
2016-03-17 04:33:18 +11:00
|
|
|
return resp
|