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...)
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
# Specific to PlexDB:
|
|
|
|
import clientinfo
|
|
|
|
import utils
|
|
|
|
import downloadutils
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcgui
|
2015-12-28 02:27:49 +11:00
|
|
|
import xbmc
|
2015-12-27 22:09:07 +11:00
|
|
|
|
|
|
|
import struct
|
|
|
|
import time
|
|
|
|
import urllib2
|
|
|
|
import httplib
|
|
|
|
import socket
|
|
|
|
import StringIO
|
|
|
|
import gzip
|
|
|
|
from threading import Thread
|
|
|
|
import Queue
|
|
|
|
import traceback
|
2016-01-14 20:20:19 +11:00
|
|
|
import requests
|
2015-12-27 22:09:07 +11:00
|
|
|
|
2015-12-29 04:47:16 +11:00
|
|
|
import re
|
2016-01-02 00:40:40 +11:00
|
|
|
import json
|
2016-01-23 01:37:20 +11:00
|
|
|
from urllib import urlencode, quote_plus
|
2015-12-29 04:47:16 +11:00
|
|
|
|
2016-01-30 06:07:21 +11:00
|
|
|
from PlexFunctions import PlexToKodiTimefactor
|
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
try:
|
|
|
|
import xml.etree.cElementTree as etree
|
|
|
|
except ImportError:
|
|
|
|
import xml.etree.ElementTree as etree
|
|
|
|
|
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
@utils.logging
|
2015-12-27 22:09:07 +11:00
|
|
|
class PlexAPI():
|
|
|
|
# CONSTANTS
|
|
|
|
# Timeout for POST/GET commands, I guess in seconds
|
|
|
|
timeout = 60
|
|
|
|
# VARIABLES
|
|
|
|
|
|
|
|
def __init__(self):
|
2015-12-28 02:27:49 +11:00
|
|
|
self.__language__ = xbmcaddon.Addon().getLocalizedString
|
2015-12-27 22:09:07 +11:00
|
|
|
self.g_PMS = {}
|
|
|
|
client = clientinfo.ClientInfo()
|
|
|
|
self.addonId = client.getAddonId()
|
|
|
|
self.clientId = client.getDeviceId()
|
|
|
|
self.deviceName = client.getDeviceName()
|
|
|
|
self.plexversion = client.getVersion()
|
|
|
|
self.platform = client.getPlatform()
|
2016-01-02 00:40:40 +11:00
|
|
|
self.userId = utils.window('emby_currUser')
|
|
|
|
self.token = utils.window('emby_accessToken%s' % self.userId)
|
|
|
|
self.server = utils.window('emby_server%s' % self.userId)
|
2015-12-27 22:09:07 +11:00
|
|
|
|
2015-12-29 04:47:16 +11:00
|
|
|
self.doUtils = downloadutils.DownloadUtils()
|
|
|
|
|
2016-01-13 03:23:55 +11:00
|
|
|
def GetPlexLoginFromSettings(self):
|
|
|
|
"""
|
2016-03-04 23:34:30 +11:00
|
|
|
Returns a dict:
|
|
|
|
'plexLogin': utils.settings('plexLogin'),
|
|
|
|
'plexToken': utils.settings('plexToken'),
|
|
|
|
'plexhome': utils.settings('plexhome'),
|
|
|
|
'plexid': utils.settings('plexid'),
|
|
|
|
'myplexlogin': utils.settings('myplexlogin')
|
|
|
|
|
2016-03-07 23:01:45 +11:00
|
|
|
plexLogin is unicode or empty unicode string u''
|
|
|
|
|
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-03-07 23:01:45 +11:00
|
|
|
'plexLogin': utils.settings('plexLogin').decode('utf-8'),
|
2016-03-04 23:34:30 +11:00
|
|
|
'plexToken': utils.settings('plexToken'),
|
|
|
|
'plexhome': utils.settings('plexhome'),
|
|
|
|
'plexid': utils.settings('plexid'),
|
|
|
|
'myplexlogin': utils.settings('myplexlogin')
|
|
|
|
}
|
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.
|
|
|
|
"""
|
2016-03-04 00:00:48 +11:00
|
|
|
string = self.__language__
|
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
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-03-08 23:00:03 +11:00
|
|
|
plexLogin = dialog.input(self.addonName + string(39300),
|
|
|
|
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-03-08 23:00:03 +11:00
|
|
|
string(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-03-07 23:01:45 +11:00
|
|
|
{'X-Plex-Client-Identifier': self.clientId})
|
2016-01-15 00:47:34 +11:00
|
|
|
self.logMsg("plex.tv username and token: %s, %s"
|
|
|
|
% (plexLogin, authtoken), 1)
|
2015-12-27 22:09:07 +11:00
|
|
|
if plexLogin == '':
|
2016-03-04 00:00:48 +11:00
|
|
|
# Could not sign in user
|
2016-03-07 23:01:45 +11:00
|
|
|
dialog.ok(self.addonName,
|
2016-03-08 23:00:03 +11:00
|
|
|
string(39302) + plexLogin)
|
2015-12-27 22:09:07 +11:00
|
|
|
# Write to Kodi settings file
|
2016-01-15 00:47:34 +11:00
|
|
|
utils.settings('plexLogin', value=retrievedPlexLogin)
|
|
|
|
utils.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-01-25 20:36:24 +11:00
|
|
|
Writes plexhome, username and token to Kodi settings file. 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':
|
|
|
|
'avatar': URL to user avator
|
|
|
|
'token':
|
|
|
|
}
|
|
|
|
Returns False if authentication did not work.
|
|
|
|
"""
|
2016-03-04 00:00:48 +11:00
|
|
|
string = self.__language__
|
|
|
|
|
2016-01-14 20:20:19 +11:00
|
|
|
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-03-08 23:00:03 +11:00
|
|
|
dialog.ok(self.addonName, string(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-01-14 20:20:19 +11:00
|
|
|
answer = dialog.yesno(self.addonName,
|
2016-03-08 23:00:03 +11:00
|
|
|
string(39304) + "\n\n",
|
|
|
|
code)
|
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)
|
|
|
|
if xml:
|
|
|
|
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
|
|
|
|
if not xml:
|
2016-03-04 00:00:48 +11:00
|
|
|
# Could not sign in to plex.tv Try again later
|
2016-03-08 23:00:03 +11:00
|
|
|
dialog.ok(self.addonName, string(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', '')
|
|
|
|
avatar = xml.get('thumb')
|
|
|
|
token = xml.findtext('authentication-token')
|
|
|
|
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,
|
|
|
|
'plexid': userid
|
2016-01-14 20:20:19 +11:00
|
|
|
}
|
2016-01-30 06:07:21 +11:00
|
|
|
utils.settings('plexLogin', username)
|
|
|
|
utils.settings('plexToken', token)
|
|
|
|
utils.settings('plexhome', home)
|
2016-03-04 23:34:30 +11:00
|
|
|
utils.settings('plexid', userid)
|
|
|
|
# Let Kodi log into plex.tv on startup from now on
|
|
|
|
utils.settings('myplexlogin', 'true')
|
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
|
|
|
|
url = 'https://plex.tv/pins/%s.xml' % identifier
|
|
|
|
xml = self.TalkToPlexServer(url, talkType="GET2")
|
|
|
|
try:
|
|
|
|
temp_token = xml.find('auth_token').text
|
|
|
|
except:
|
|
|
|
self.logMsg("Error: Could not find token in plex.tv answer.", -1)
|
|
|
|
return False
|
|
|
|
self.logMsg("temp token from plex.tv is: %s" % temp_token, 2)
|
|
|
|
if not temp_token:
|
|
|
|
return False
|
|
|
|
# Use temp token to get the final plex credentials
|
|
|
|
url = 'https://plex.tv/users/account?X-Plex-Token=%s' % temp_token
|
|
|
|
xml = self.TalkToPlexServer(url, talkType="GET")
|
|
|
|
return xml
|
|
|
|
|
|
|
|
def GetPlexPin(self):
|
|
|
|
"""
|
|
|
|
For plex.tv sign-in: returns 4-digit code and identifier as 2 str
|
|
|
|
"""
|
|
|
|
url = 'https://plex.tv/pins.xml'
|
|
|
|
code = None
|
|
|
|
identifier = None
|
|
|
|
# Download
|
|
|
|
xml = self.TalkToPlexServer(url, talkType="POST")
|
|
|
|
if not xml:
|
|
|
|
return code, identifier
|
|
|
|
try:
|
|
|
|
code = xml.find('code').text
|
|
|
|
identifier = xml.find('id').text
|
2016-03-07 23:01:45 +11:00
|
|
|
self.logMsg('Successfully retrieved code and id from plex.tv', 1)
|
|
|
|
return code, identifier
|
2016-01-14 20:20:19 +11:00
|
|
|
except:
|
|
|
|
self.logMsg("Error, no PIN from plex.tv provided", -1)
|
2016-03-07 23:01:45 +11:00
|
|
|
return None, None
|
2016-01-14 20:20:19 +11:00
|
|
|
|
2016-01-15 00:47:34 +11:00
|
|
|
def TalkToPlexServer(self, url, talkType="GET", verify=True, token=None):
|
2016-01-14 20:20:19 +11:00
|
|
|
"""
|
|
|
|
Start request with PMS with url.
|
|
|
|
|
2016-01-15 00:47:34 +11:00
|
|
|
Returns the parsed XML answer as an etree object.
|
|
|
|
False if the server could not be reached/timeout occured.
|
|
|
|
False if HTTP error code of >=400 was returned.
|
2016-01-14 20:20:19 +11:00
|
|
|
"""
|
|
|
|
header = self.getXArgsDeviceInfo()
|
2016-01-15 00:47:34 +11:00
|
|
|
if token:
|
|
|
|
header['X-Plex-Token'] = token
|
2016-01-14 20:20:19 +11:00
|
|
|
timeout = (3, 10)
|
|
|
|
try:
|
|
|
|
if talkType == "GET":
|
|
|
|
answer = requests.get(url,
|
|
|
|
headers={},
|
|
|
|
params=header,
|
|
|
|
verify=verify,
|
|
|
|
timeout=timeout)
|
2016-01-15 00:47:34 +11:00
|
|
|
# Only seems to be used for initial plex.tv sign in
|
2016-01-14 20:20:19 +11:00
|
|
|
if talkType == "GET2":
|
|
|
|
answer = requests.get(url,
|
|
|
|
headers=header,
|
|
|
|
params={},
|
|
|
|
verify=verify,
|
|
|
|
timeout=timeout)
|
|
|
|
elif talkType == "POST":
|
|
|
|
answer = requests.post(url,
|
|
|
|
data='',
|
|
|
|
headers=header,
|
|
|
|
params={},
|
|
|
|
verify=verify,
|
|
|
|
timeout=timeout)
|
|
|
|
except requests.exceptions.ConnectionError as e:
|
|
|
|
self.logMsg("Server is offline or cannot be reached. Url: %s."
|
|
|
|
"Header: %s. Error message: %s"
|
|
|
|
% (url, header, e), -1)
|
|
|
|
return False
|
|
|
|
except requests.exceptions.ReadTimeout:
|
|
|
|
self.logMsg("Server timeout reached for Url %s with header %s"
|
|
|
|
% (url, header), -1)
|
|
|
|
return False
|
|
|
|
# We received an answer from the server, but not as expected.
|
|
|
|
if answer.status_code >= 400:
|
|
|
|
self.logMsg("Error, answer from server %s was not as expected. "
|
|
|
|
"HTTP status code: %s" % (url, answer.status_code), -1)
|
|
|
|
return False
|
|
|
|
xml = answer.text.encode('utf-8')
|
|
|
|
self.logMsg("xml received from server %s: %s" % (url, xml), 2)
|
|
|
|
try:
|
|
|
|
xml = etree.fromstring(xml)
|
|
|
|
except:
|
|
|
|
self.logMsg("Error parsing XML answer from %s" % url, -1)
|
|
|
|
return False
|
|
|
|
return xml
|
|
|
|
|
2016-03-04 23:34:30 +11:00
|
|
|
def CheckConnection(self, url, token=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.
|
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
|
|
|
|
e.g. 200 if connection was successfull
|
|
|
|
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-01-15 00:47:34 +11:00
|
|
|
header = self.getXArgsDeviceInfo()
|
|
|
|
if token is not None:
|
|
|
|
header['X-Plex-Token'] = token
|
|
|
|
sslverify = utils.settings('sslverify')
|
|
|
|
if sslverify == "true":
|
|
|
|
sslverify = True
|
|
|
|
else:
|
|
|
|
sslverify = False
|
2016-03-08 00:10:47 +11:00
|
|
|
self.logMsg("Checking connection to server %s with sslverify=%s"
|
|
|
|
% (url, sslverify), 1)
|
2016-01-15 00:47:34 +11:00
|
|
|
timeout = (3, 10)
|
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-01-15 00:47:34 +11:00
|
|
|
try:
|
|
|
|
answer = requests.get(url,
|
|
|
|
headers={},
|
|
|
|
params=header,
|
|
|
|
verify=sslverify,
|
|
|
|
timeout=timeout)
|
|
|
|
except requests.exceptions.ConnectionError as e:
|
2016-01-30 06:07:21 +11:00
|
|
|
self.logMsg("Server is offline or cannot be reached. Url: %s "
|
|
|
|
"Header: %s Error message: %s"
|
2016-01-15 00:47:34 +11:00
|
|
|
% (url, header, e), -1)
|
|
|
|
return False
|
|
|
|
except requests.exceptions.ReadTimeout:
|
|
|
|
self.logMsg("Server timeout reached for Url %s with header %s"
|
|
|
|
% (url, header), -1)
|
|
|
|
return False
|
|
|
|
result = answer.status_code
|
|
|
|
self.logMsg("Result was: %s" % result, 1)
|
|
|
|
return result
|
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
|
|
|
|
|
|
|
|
def declarePMS(self, ATV_udid, uuid, name, scheme, ip, port):
|
|
|
|
"""
|
|
|
|
Plex Media Server handling
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
ATV_udid
|
|
|
|
uuid - PMS ID
|
|
|
|
name, scheme, ip, port, type, owned, token
|
|
|
|
"""
|
|
|
|
# store PMS information in g_PMS database
|
|
|
|
if ATV_udid not in self.g_PMS:
|
|
|
|
self.g_PMS[ATV_udid] = {}
|
|
|
|
|
|
|
|
address = ip + ':' + port
|
|
|
|
baseURL = scheme+'://'+ip+':'+port
|
|
|
|
self.g_PMS[ATV_udid][uuid] = { 'name': name,
|
|
|
|
'scheme':scheme, 'ip': ip , 'port': port,
|
|
|
|
'address': address,
|
|
|
|
'baseURL': baseURL,
|
|
|
|
'local': '1',
|
|
|
|
'owned': '1',
|
|
|
|
'accesstoken': '',
|
|
|
|
'enableGzip': False
|
|
|
|
}
|
|
|
|
|
|
|
|
def updatePMSProperty(self, ATV_udid, uuid, tag, value):
|
|
|
|
# set property element of PMS by UUID
|
|
|
|
if not ATV_udid in self.g_PMS:
|
|
|
|
return '' # no server known for this aTV
|
|
|
|
if not uuid in self.g_PMS[ATV_udid]:
|
|
|
|
return '' # requested PMS not available
|
|
|
|
|
|
|
|
self.g_PMS[ATV_udid][uuid][tag] = value
|
|
|
|
|
|
|
|
def getPMSProperty(self, ATV_udid, uuid, tag):
|
|
|
|
# get name of PMS by UUID
|
|
|
|
if not ATV_udid in self.g_PMS:
|
|
|
|
return '' # no server known for this aTV
|
|
|
|
if not uuid in self.g_PMS[ATV_udid]:
|
|
|
|
return '' # requested PMS not available
|
|
|
|
|
|
|
|
return self.g_PMS[ATV_udid][uuid].get(tag, '')
|
|
|
|
|
|
|
|
def getPMSFromAddress(self, ATV_udid, address):
|
|
|
|
# find PMS by IP, return UUID
|
|
|
|
if not ATV_udid in self.g_PMS:
|
|
|
|
return '' # no server known for this aTV
|
|
|
|
|
|
|
|
for uuid in self.g_PMS[ATV_udid]:
|
|
|
|
if address in self.g_PMS[ATV_udid][uuid].get('address', None):
|
|
|
|
return uuid
|
|
|
|
return '' # IP not found
|
|
|
|
|
|
|
|
def getPMSAddress(self, ATV_udid, uuid, data):
|
|
|
|
# get address of PMS by UUID
|
|
|
|
if not ATV_udid in data:
|
|
|
|
return '' # no server known for this aTV
|
|
|
|
if not uuid in data[ATV_udid]:
|
|
|
|
return '' # requested PMS not available
|
|
|
|
return data[ATV_udid][uuid]['ip'] + ':' + data[ATV_udid][uuid]['port']
|
|
|
|
|
|
|
|
def getPMSCount(self, ATV_udid):
|
|
|
|
# get count of discovered PMS by UUID
|
|
|
|
if not ATV_udid in self.g_PMS:
|
|
|
|
return 0 # no server known for this aTV
|
|
|
|
|
|
|
|
return len(self.g_PMS[ATV_udid])
|
|
|
|
|
|
|
|
def PlexGDM(self):
|
|
|
|
"""
|
|
|
|
PlexGDM
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
none
|
|
|
|
result:
|
|
|
|
PMS_list - dict() of PMSs found
|
|
|
|
"""
|
|
|
|
IP_PlexGDM = '239.0.0.250' # multicast to PMS
|
|
|
|
Port_PlexGDM = 32414
|
|
|
|
Msg_PlexGDM = 'M-SEARCH * HTTP/1.0'
|
|
|
|
# dprint(__name__, 0, "***")
|
|
|
|
# dprint(__name__, 0, "PlexGDM - looking up Plex Media Server")
|
|
|
|
# dprint(__name__, 0, "***")
|
|
|
|
|
|
|
|
# setup socket for discovery -> multicast message
|
|
|
|
GDM = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
GDM.settimeout(1.0)
|
|
|
|
|
|
|
|
# Set the time-to-live for messages to 1 for local network
|
|
|
|
ttl = struct.pack('b', 1)
|
|
|
|
GDM.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
|
|
|
|
|
|
|
|
returnData = []
|
|
|
|
try:
|
|
|
|
# Send data to the multicast group
|
|
|
|
# dprint(__name__, 1, "Sending discovery message: {0}", Msg_PlexGDM)
|
|
|
|
GDM.sendto(Msg_PlexGDM, (IP_PlexGDM, Port_PlexGDM))
|
|
|
|
|
|
|
|
# Look for responses from all recipients
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data, server = GDM.recvfrom(1024)
|
|
|
|
# dprint(__name__, 1, "Received data from {0}", server)
|
|
|
|
# dprint(__name__, 1, "Data received:\n {0}", data)
|
|
|
|
returnData.append( { 'from' : server,
|
|
|
|
'data' : data } )
|
|
|
|
except socket.timeout:
|
|
|
|
break
|
|
|
|
finally:
|
|
|
|
GDM.close()
|
|
|
|
|
|
|
|
discovery_complete = True
|
|
|
|
|
|
|
|
PMS_list = {}
|
|
|
|
if returnData:
|
|
|
|
for response in returnData:
|
|
|
|
update = { 'ip' : response.get('from')[0] }
|
|
|
|
|
|
|
|
# Check if we had a positive HTTP response
|
|
|
|
if "200 OK" in response.get('data'):
|
|
|
|
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:
|
|
|
|
update['serverName'] = each.split(':')[1].strip().decode('utf-8', 'replace') # store in utf-8
|
|
|
|
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()
|
|
|
|
|
|
|
|
PMS_list[update['uuid']] = update
|
|
|
|
|
|
|
|
# if PMS_list=={}:
|
|
|
|
# dprint(__name__, 0, "GDM: No servers discovered")
|
|
|
|
# else:
|
|
|
|
# dprint(__name__, 0, "GDM: Servers discovered: {0}", len(PMS_list))
|
|
|
|
# for uuid in PMS_list:
|
|
|
|
# dprint(__name__, 1, "{0} {1}:{2}", PMS_list[uuid]['serverName'], PMS_list[uuid]['ip'], PMS_list[uuid]['port'])
|
|
|
|
|
|
|
|
return PMS_list
|
|
|
|
|
|
|
|
def discoverPMS(self, ATV_udid, CSettings, IP_self, tokenDict={}):
|
|
|
|
"""
|
|
|
|
discoverPMS
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
ATV_udid
|
|
|
|
CSettings - for manual PMS configuration. this one looks strange.
|
|
|
|
IP_self
|
|
|
|
optional:
|
|
|
|
tokenDict - dictionary of tokens for MyPlex, PlexHome
|
|
|
|
result:
|
|
|
|
self.g_PMS dictionary for ATV_udid
|
|
|
|
"""
|
|
|
|
self.g_PMS[ATV_udid] = {}
|
|
|
|
|
|
|
|
# install plex.tv "virtual" PMS - for myPlex, PlexHome
|
|
|
|
self.declarePMS(ATV_udid, 'plex.tv', 'plex.tv', 'https', 'plex.tv', '443')
|
|
|
|
self.updatePMSProperty(ATV_udid, 'plex.tv', 'local', '-')
|
|
|
|
self.updatePMSProperty(ATV_udid, 'plex.tv', 'owned', '-')
|
|
|
|
self.updatePMSProperty(ATV_udid, 'plex.tv', 'accesstoken', tokenDict.get('MyPlexToken', ''))
|
|
|
|
|
|
|
|
if 'PlexHomeToken' in tokenDict:
|
|
|
|
authtoken = tokenDict.get('PlexHomeToken')
|
|
|
|
else:
|
|
|
|
authtoken = tokenDict.get('MyPlexToken', '')
|
|
|
|
|
|
|
|
if authtoken == '':
|
|
|
|
# not logged into myPlex
|
|
|
|
# local PMS
|
|
|
|
# PlexGDM
|
|
|
|
PMS_list = self.PlexGDM()
|
2016-01-15 00:47:34 +11:00
|
|
|
for uuid_id in PMS_list:
|
|
|
|
PMS = PMS_list[uuid_id]
|
2015-12-27 22:09:07 +11:00
|
|
|
self.declarePMS(ATV_udid, PMS['uuid'], PMS['serverName'], 'http', PMS['ip'], PMS['port']) # dflt: token='', local, owned
|
|
|
|
else:
|
|
|
|
# MyPlex servers
|
|
|
|
self.getPMSListFromMyPlex(ATV_udid, authtoken)
|
|
|
|
# all servers - update enableGzip
|
2016-01-15 00:47:34 +11:00
|
|
|
for uuid_id in self.g_PMS.get(ATV_udid, {}):
|
2015-12-27 22:09:07 +11:00
|
|
|
# enable Gzip if not on same host, local&remote PMS depending
|
|
|
|
# on setting
|
2016-01-15 00:47:34 +11:00
|
|
|
enableGzip = (not self.getPMSProperty(ATV_udid, uuid_id, 'ip') == IP_self) \
|
2015-12-27 22:09:07 +11:00
|
|
|
and (
|
2016-01-15 00:47:34 +11:00
|
|
|
(self.getPMSProperty(ATV_udid, uuid_id, 'local') == '1'
|
2015-12-27 22:09:07 +11:00
|
|
|
and False)
|
|
|
|
or
|
2016-01-15 00:47:34 +11:00
|
|
|
(self.getPMSProperty(ATV_udid, uuid_id, 'local') == '0'
|
2015-12-27 22:09:07 +11:00
|
|
|
and True) == 'True'
|
|
|
|
)
|
2016-01-15 00:47:34 +11:00
|
|
|
self.updatePMSProperty(ATV_udid, uuid_id, 'enableGzip', enableGzip)
|
|
|
|
# Delete plex.tv again
|
|
|
|
del self.g_PMS[ATV_udid]['plex.tv']
|
2015-12-27 22:09:07 +11:00
|
|
|
|
|
|
|
def getPMSListFromMyPlex(self, ATV_udid, authtoken):
|
|
|
|
"""
|
|
|
|
getPMSListFromMyPlex
|
|
|
|
|
|
|
|
get Plex media Server List from plex.tv/pms/resources
|
|
|
|
"""
|
|
|
|
# dprint(__name__, 0, "***")
|
|
|
|
# dprint(__name__, 0, "poke plex.tv - request Plex Media Server list")
|
|
|
|
# dprint(__name__, 0, "***")
|
|
|
|
XML = self.getXMLFromPMS('https://plex.tv', '/api/resources?includeHttps=1', {}, authtoken)
|
|
|
|
if XML==False:
|
|
|
|
pass # no data from MyPlex
|
|
|
|
else:
|
|
|
|
queue = Queue.Queue()
|
|
|
|
threads = []
|
|
|
|
|
|
|
|
for Dir in XML.getiterator('Device'):
|
|
|
|
if Dir.get('product','') == "Plex Media Server" and Dir.get('provides','') == "server":
|
|
|
|
uuid = Dir.get('clientIdentifier')
|
|
|
|
name = Dir.get('name')
|
|
|
|
token = Dir.get('accessToken', authtoken)
|
|
|
|
owned = Dir.get('owned', '0')
|
|
|
|
local = Dir.get('publicAddressMatches')
|
|
|
|
|
|
|
|
if Dir.find('Connection') == None:
|
|
|
|
continue # no valid connection - skip
|
|
|
|
|
|
|
|
uri = "" # flag to set first connection, possibly overwrite later with more suitable
|
|
|
|
for Con in Dir.getiterator('Connection'):
|
|
|
|
if uri=="" or Con.get('local','') == local:
|
|
|
|
protocol = Con.get('protocol')
|
|
|
|
ip = Con.get('address')
|
|
|
|
port = Con.get('port')
|
|
|
|
uri = Con.get('uri')
|
|
|
|
# todo: handle unforeseen - like we get multiple suitable connections. how to choose one?
|
|
|
|
|
2016-01-15 00:47:34 +11:00
|
|
|
# check MyPlex data age - skip if >1 days
|
2015-12-27 22:09:07 +11:00
|
|
|
infoAge = time.time() - int(Dir.get('lastSeenAt'))
|
|
|
|
oneDayInSec = 60*60*24
|
2016-01-15 00:47:34 +11:00
|
|
|
if infoAge > 1*oneDayInSec:
|
|
|
|
self.logMsg("Server %s not updated for 1 day - "
|
|
|
|
"skipping." % name, 0)
|
2015-12-27 22:09:07 +11:00
|
|
|
continue
|
2016-01-15 00:47:34 +11:00
|
|
|
|
2015-12-27 22:09:07 +11:00
|
|
|
# poke PMS, own thread for each poke
|
|
|
|
PMSInfo = { 'uuid': uuid, 'name': name, 'token': token, 'owned': owned, 'local': local, \
|
|
|
|
'protocol': protocol, 'ip': ip, 'port': port, 'uri': uri }
|
|
|
|
PMS = { 'baseURL': uri, 'path': '/', 'options': None, 'token': token, \
|
|
|
|
'data': PMSInfo }
|
|
|
|
t = Thread(target=self.getXMLFromPMSToQueue, args=(PMS, queue))
|
|
|
|
t.start()
|
|
|
|
threads.append(t)
|
|
|
|
|
|
|
|
# wait for requests being answered
|
|
|
|
for t in threads:
|
|
|
|
t.join()
|
|
|
|
|
|
|
|
# declare new PMSs
|
|
|
|
while not queue.empty():
|
|
|
|
(PMSInfo, PMS) = queue.get()
|
|
|
|
|
|
|
|
if PMS==False:
|
|
|
|
continue
|
|
|
|
|
|
|
|
uuid = PMSInfo['uuid']
|
|
|
|
name = PMSInfo['name']
|
|
|
|
token = PMSInfo['token']
|
|
|
|
owned = PMSInfo['owned']
|
|
|
|
local = PMSInfo['local']
|
|
|
|
protocol = PMSInfo['protocol']
|
|
|
|
ip = PMSInfo['ip']
|
|
|
|
port = PMSInfo['port']
|
|
|
|
uri = PMSInfo['uri']
|
|
|
|
|
|
|
|
self.declarePMS(ATV_udid, uuid, name, protocol, ip, port) # dflt: token='', local, owned - updated later
|
|
|
|
self.updatePMSProperty(ATV_udid, uuid, 'accesstoken', token)
|
|
|
|
self.updatePMSProperty(ATV_udid, uuid, 'owned', owned)
|
|
|
|
self.updatePMSProperty(ATV_udid, uuid, 'local', local)
|
|
|
|
self.updatePMSProperty(ATV_udid, uuid, 'baseURL', uri) # set in declarePMS, overwrite for https encryption
|
|
|
|
|
|
|
|
def getXMLFromPMS(self, baseURL, path, options={}, authtoken='', enableGzip=False):
|
|
|
|
"""
|
|
|
|
Plex Media Server communication
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
host
|
|
|
|
path
|
|
|
|
options - dict() of PlexConnect-options as received from aTV
|
|
|
|
None for no
|
|
|
|
std. X-Plex-Args
|
|
|
|
authtoken - authentication answer from MyPlex Sign In
|
|
|
|
result:
|
|
|
|
returned XML or 'False' in case of error
|
|
|
|
"""
|
|
|
|
xargs = {}
|
|
|
|
if options is not None:
|
|
|
|
xargs = self.getXArgsDeviceInfo(options)
|
|
|
|
if not authtoken == '':
|
|
|
|
xargs['X-Plex-Token'] = authtoken
|
|
|
|
|
|
|
|
self.logMsg("URL for XML download: %s%s" % (baseURL, path), 1)
|
|
|
|
|
|
|
|
request = urllib2.Request(baseURL+path, None, xargs)
|
|
|
|
request.add_header('User-agent', 'PlexDB')
|
|
|
|
if enableGzip:
|
|
|
|
request.add_header('Accept-encoding', 'gzip')
|
|
|
|
|
|
|
|
try:
|
|
|
|
response = urllib2.urlopen(request, timeout=20)
|
|
|
|
except (urllib2.URLError, httplib.HTTPException) as e:
|
|
|
|
self.logMsg("No Response from Plex Media Server", 0)
|
|
|
|
if hasattr(e, 'reason'):
|
|
|
|
self.logMsg("We failed to reach a server. Reason: %s" % e.reason, 0)
|
|
|
|
elif hasattr(e, 'code'):
|
|
|
|
self.logMsg("The server couldn't fulfill the request. Error code: %s" % e.code, 0)
|
|
|
|
self.logMsg("Traceback:\n%s" % traceback.format_exc(), 0)
|
|
|
|
return False
|
|
|
|
except IOError:
|
|
|
|
self.logMsg("Error loading response XML from Plex Media Server:\n%s" % traceback.format_exc(), 0)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if response.info().get('Content-Encoding') == 'gzip':
|
|
|
|
buf = StringIO.StringIO(response.read())
|
|
|
|
file = gzip.GzipFile(fileobj=buf)
|
|
|
|
XML = etree.parse(file)
|
|
|
|
else:
|
|
|
|
# parse into etree
|
|
|
|
XML = etree.parse(response)
|
|
|
|
# Log received XML if debugging enabled.
|
|
|
|
self.logMsg("====== received PMS-XML ======", 1)
|
|
|
|
self.logMsg(XML.getroot(), 1)
|
|
|
|
self.logMsg("====== PMS-XML finished ======", 1)
|
|
|
|
return XML
|
|
|
|
|
|
|
|
def getXMLFromPMSToQueue(self, PMS, queue):
|
|
|
|
XML = self.getXMLFromPMS(PMS['baseURL'],PMS['path'],PMS['options'],PMS['token'])
|
|
|
|
queue.put( (PMS['data'], XML) )
|
|
|
|
|
2016-02-01 20:33:33 +11:00
|
|
|
def getXArgsDeviceInfo(self, options={}):
|
2015-12-27 22:09:07 +11:00
|
|
|
"""
|
|
|
|
Returns a dictionary that can be used as headers for GET and POST
|
|
|
|
requests. An authentication option is NOT yet added.
|
|
|
|
|
|
|
|
Inputs:
|
|
|
|
options: dictionary of options that will override the
|
|
|
|
standard header options otherwise set.
|
|
|
|
Output:
|
|
|
|
header dictionary
|
|
|
|
"""
|
|
|
|
# Get addon infos
|
2016-01-02 00:40:40 +11:00
|
|
|
xargs = {
|
2016-02-07 22:38:50 +11:00
|
|
|
'Accept': '*/*',
|
2016-02-11 20:30:29 +11:00
|
|
|
'Connection': 'keep-alive',
|
2016-01-30 06:07:21 +11:00
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
# "Access-Control-Allow-Origin": "*",
|
2016-01-14 20:20:19 +11:00
|
|
|
'X-Plex-Language': 'en',
|
|
|
|
'X-Plex-Device': self.addonName,
|
2016-01-02 00:40:40 +11:00
|
|
|
'X-Plex-Client-Platform': self.platform,
|
2016-01-14 20:20:19 +11:00
|
|
|
'X-Plex-Device-Name': self.deviceName,
|
|
|
|
'X-Plex-Platform': self.addonName,
|
2016-01-25 02:12:28 +11:00
|
|
|
'X-Plex-Platform-Version': 'unknown',
|
2016-01-14 20:20:19 +11:00
|
|
|
'X-Plex-Model': 'unknown',
|
2016-01-02 00:40:40 +11:00
|
|
|
'X-Plex-Product': self.addonName,
|
|
|
|
'X-Plex-Version': self.plexversion,
|
|
|
|
'X-Plex-Client-Identifier': self.clientId,
|
2016-01-06 02:05:20 +11:00
|
|
|
'X-Plex-Provides': 'player',
|
2016-02-07 22:38:50 +11:00
|
|
|
'X-Plex-Client-Capabilities': 'protocols=shoutcast,http-video;videoDecoders=h264{profile:high&resolution:1080&level:51};audioDecoders=mp3,aac,dts{bitrate:800000&channels:8},ac3{bitrate:800000&channels:8}',
|
|
|
|
'X-Plex-Client-Profile-Extra': 'add-transcode-target-audio-codec(type=videoProfile&context=streaming&protocol=*&audioCodec=dca,ac3)',
|
2016-01-02 00:40:40 +11:00
|
|
|
}
|
|
|
|
|
2016-01-14 20:20:19 +11:00
|
|
|
if self.token:
|
2016-01-02 00:40:40 +11:00
|
|
|
xargs['X-Plex-Token'] = self.token
|
2015-12-31 20:53:22 +11:00
|
|
|
if options:
|
|
|
|
xargs.update(options)
|
2015-12-27 22:09:07 +11:00
|
|
|
return xargs
|
|
|
|
|
|
|
|
def getXMLFromMultiplePMS(self, ATV_udid, path, type, options={}):
|
|
|
|
"""
|
|
|
|
provide combined XML representation of local servers' XMLs, eg. /library/section
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
ATV_udid
|
|
|
|
path
|
|
|
|
type - owned <> shared (previously: local, myplex)
|
|
|
|
options
|
|
|
|
result:
|
|
|
|
XML
|
|
|
|
"""
|
|
|
|
queue = Queue.Queue()
|
|
|
|
threads = []
|
|
|
|
|
|
|
|
root = etree.Element("MediaConverter")
|
|
|
|
root.set('friendlyName', type+' Servers')
|
|
|
|
|
|
|
|
for uuid in g_PMS.get(ATV_udid, {}):
|
|
|
|
if (type=='all' and getPMSProperty(ATV_udid, uuid, 'name')!='plex.tv') or \
|
|
|
|
(type=='owned' and getPMSProperty(ATV_udid, uuid, 'owned')=='1') or \
|
|
|
|
(type=='shared' and getPMSProperty(ATV_udid, uuid, 'owned')=='0') or \
|
|
|
|
(type=='local' and getPMSProperty(ATV_udid, uuid, 'local')=='1') or \
|
|
|
|
(type=='remote' and getPMSProperty(ATV_udid, uuid, 'local')=='0'):
|
|
|
|
Server = etree.SubElement(root, 'Server') # create "Server" node
|
|
|
|
Server.set('name', getPMSProperty(ATV_udid, uuid, 'name'))
|
|
|
|
Server.set('address', getPMSProperty(ATV_udid, uuid, 'ip'))
|
|
|
|
Server.set('port', getPMSProperty(ATV_udid, uuid, 'port'))
|
|
|
|
Server.set('baseURL', getPMSProperty(ATV_udid, uuid, 'baseURL'))
|
|
|
|
Server.set('local', getPMSProperty(ATV_udid, uuid, 'local'))
|
|
|
|
Server.set('owned', getPMSProperty(ATV_udid, uuid, 'owned'))
|
|
|
|
|
|
|
|
baseURL = getPMSProperty(ATV_udid, uuid, 'baseURL')
|
|
|
|
token = getPMSProperty(ATV_udid, uuid, 'accesstoken')
|
|
|
|
PMS_mark = 'PMS(' + getPMSProperty(ATV_udid, uuid, 'address') + ')'
|
|
|
|
|
|
|
|
Server.set('searchKey', PMS_mark + getURL('', '', '/Search/Entry.xml'))
|
|
|
|
|
|
|
|
# request XMLs, one thread for each
|
|
|
|
PMS = { 'baseURL':baseURL, 'path':path, 'options':options, 'token':token, \
|
|
|
|
'data': {'uuid': uuid, 'Server': Server} }
|
|
|
|
t = Thread(target=getXMLFromPMSToQueue, args=(PMS, queue))
|
|
|
|
t.start()
|
|
|
|
threads.append(t)
|
|
|
|
|
|
|
|
# wait for requests being answered
|
|
|
|
for t in threads:
|
|
|
|
t.join()
|
|
|
|
|
|
|
|
# add new data to root XML, individual Server
|
|
|
|
while not queue.empty():
|
|
|
|
(data, XML) = queue.get()
|
|
|
|
uuid = data['uuid']
|
|
|
|
Server = data['Server']
|
|
|
|
|
|
|
|
baseURL = getPMSProperty(ATV_udid, uuid, 'baseURL')
|
|
|
|
token = getPMSProperty(ATV_udid, uuid, 'accesstoken')
|
|
|
|
PMS_mark = 'PMS(' + getPMSProperty(ATV_udid, uuid, 'address') + ')'
|
|
|
|
|
|
|
|
if XML==False:
|
|
|
|
Server.set('size', '0')
|
|
|
|
else:
|
|
|
|
Server.set('size', XML.getroot().get('size', '0'))
|
|
|
|
|
|
|
|
for Dir in XML.getiterator('Directory'): # copy "Directory" content, add PMS to links
|
|
|
|
key = Dir.get('key') # absolute path
|
|
|
|
Dir.set('key', PMS_mark + getURL('', path, key))
|
|
|
|
Dir.set('refreshKey', getURL(baseURL, path, key) + '/refresh')
|
|
|
|
if 'thumb' in Dir.attrib:
|
|
|
|
Dir.set('thumb', PMS_mark + getURL('', path, Dir.get('thumb')))
|
|
|
|
if 'art' in Dir.attrib:
|
|
|
|
Dir.set('art', PMS_mark + getURL('', path, Dir.get('art')))
|
|
|
|
Server.append(Dir)
|
|
|
|
|
|
|
|
for Playlist in XML.getiterator('Playlist'): # copy "Playlist" content, add PMS to links
|
|
|
|
key = Playlist.get('key') # absolute path
|
|
|
|
Playlist.set('key', PMS_mark + getURL('', path, key))
|
|
|
|
if 'composite' in Playlist.attrib:
|
|
|
|
Playlist.set('composite', PMS_mark + getURL('', path, Playlist.get('composite')))
|
|
|
|
Server.append(Playlist)
|
|
|
|
|
|
|
|
for Video in XML.getiterator('Video'): # copy "Video" content, add PMS to links
|
|
|
|
key = Video.get('key') # absolute path
|
|
|
|
Video.set('key', PMS_mark + getURL('', path, key))
|
|
|
|
if 'thumb' in Video.attrib:
|
|
|
|
Video.set('thumb', PMS_mark + getURL('', path, Video.get('thumb')))
|
|
|
|
if 'parentKey' in Video.attrib:
|
|
|
|
Video.set('parentKey', PMS_mark + getURL('', path, Video.get('parentKey')))
|
|
|
|
if 'parentThumb' in Video.attrib:
|
|
|
|
Video.set('parentThumb', PMS_mark + getURL('', path, Video.get('parentThumb')))
|
|
|
|
if 'grandparentKey' in Video.attrib:
|
|
|
|
Video.set('grandparentKey', PMS_mark + getURL('', path, Video.get('grandparentKey')))
|
|
|
|
if 'grandparentThumb' in Video.attrib:
|
|
|
|
Video.set('grandparentThumb', PMS_mark + getURL('', path, Video.get('grandparentThumb')))
|
|
|
|
Server.append(Video)
|
|
|
|
|
|
|
|
root.set('size', str(len(root.findall('Server'))))
|
|
|
|
|
|
|
|
XML = etree.ElementTree(root)
|
|
|
|
|
|
|
|
dprint(__name__, 1, "====== Local Server/Sections XML ======")
|
|
|
|
dprint(__name__, 1, XML.getroot())
|
|
|
|
dprint(__name__, 1, "====== Local Server/Sections XML finished ======")
|
|
|
|
|
|
|
|
return XML # XML representation - created "just in time". Do we need to cache it?
|
|
|
|
|
|
|
|
def getURL(self, baseURL, path, key):
|
|
|
|
if key.startswith('http://') or key.startswith('https://'): # external server
|
|
|
|
URL = key
|
|
|
|
elif key.startswith('/'): # internal full path.
|
|
|
|
URL = baseURL + key
|
|
|
|
elif key == '': # internal path
|
|
|
|
URL = baseURL + path
|
|
|
|
else: # internal path, add-on
|
|
|
|
URL = baseURL + path + '/' + key
|
|
|
|
|
|
|
|
return URL
|
|
|
|
|
|
|
|
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
|
|
|
|
xargs = self.getXArgsDeviceInfo(options)
|
|
|
|
self.logMsg("Header is: %s" % xargs, 1)
|
|
|
|
request = urllib2.Request(MyPlexURL, None, xargs)
|
|
|
|
request.get_method = lambda: 'POST'
|
|
|
|
# turn into 'POST'
|
|
|
|
# done automatically with data!=None. But we don't have data.
|
|
|
|
|
|
|
|
# no certificate, will fail with "401 - Authentification required"
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
f = urllib2.urlopen(request)
|
|
|
|
except urllib2.HTTPError, e:
|
|
|
|
print e.headers
|
|
|
|
print "has WWW_Authenticate:", e.headers.has_key('WWW-Authenticate')
|
|
|
|
print
|
|
|
|
"""
|
|
|
|
|
|
|
|
# provide credentials
|
|
|
|
### optional... when 'realm' is unknown
|
|
|
|
##passmanager = urllib2.HTTPPasswordMgrWithDefaultRealm()
|
|
|
|
##passmanager.add_password(None, address, username, password) # None: default "realm"
|
|
|
|
passmanager = urllib2.HTTPPasswordMgr()
|
|
|
|
passmanager.add_password(MyPlexHost, MyPlexURL, username, password)
|
|
|
|
authhandler = urllib2.HTTPBasicAuthHandler(passmanager)
|
|
|
|
urlopener = urllib2.build_opener(authhandler)
|
|
|
|
|
|
|
|
# sign in, get MyPlex response
|
|
|
|
try:
|
|
|
|
response = urlopener.open(request).read()
|
|
|
|
except urllib2.HTTPError as e:
|
|
|
|
if e.code == 401:
|
|
|
|
self.logMsg("Authentication failed", 0)
|
|
|
|
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):
|
|
|
|
# MyPlex web address
|
|
|
|
MyPlexHost = 'plex.tv'
|
|
|
|
MyPlexSignOutPath = '/users/sign_out.xml'
|
|
|
|
MyPlexURL = 'http://' + MyPlexHost + MyPlexSignOutPath
|
|
|
|
|
|
|
|
# create POST request
|
|
|
|
xargs = { 'X-Plex-Token': authtoken }
|
|
|
|
request = urllib2.Request(MyPlexURL, None, xargs)
|
|
|
|
request.get_method = lambda: 'POST' # turn into 'POST' - done automatically with data!=None. But we don't have data.
|
|
|
|
|
|
|
|
response = urllib2.urlopen(request).read()
|
|
|
|
|
|
|
|
dprint(__name__, 1, "====== MyPlex sign out XML ======")
|
|
|
|
dprint(__name__, 1, response)
|
|
|
|
dprint(__name__, 1, "====== MyPlex sign out XML finished ======")
|
|
|
|
dprint(__name__, 0, 'MyPlex Sign Out done')
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
plexToken = utils.settings('plexToken')
|
|
|
|
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:
|
|
|
|
self.logMsg("Could not get userlist from plex.tv.", 1)
|
|
|
|
self.logMsg("No URL for user avatar.", 1)
|
|
|
|
return False
|
|
|
|
for user in users:
|
2016-03-07 23:01:45 +11:00
|
|
|
self.logMsg('type user: %s, type username: %s'
|
|
|
|
% (type(user['title']), type(username)))
|
2015-12-28 20:35:27 +11:00
|
|
|
if username in user['title']:
|
|
|
|
url = user['thumb']
|
|
|
|
self.logMsg("Avatar url for user %s is: %s" % (username, url), 1)
|
|
|
|
return url
|
|
|
|
|
2015-12-28 02:27:49 +11:00
|
|
|
def ChoosePlexHomeUser(self):
|
|
|
|
"""
|
|
|
|
Let's user choose from a list of Plex home users. Will switch to that
|
|
|
|
user accordingly.
|
|
|
|
|
|
|
|
Output:
|
|
|
|
username
|
2015-12-28 03:12:46 +11:00
|
|
|
userid
|
2015-12-28 02:27:49 +11:00
|
|
|
authtoken
|
2015-12-28 20:35:27 +11:00
|
|
|
|
|
|
|
Will return empty strings if failed.
|
2015-12-28 02:27:49 +11:00
|
|
|
"""
|
2016-03-04 00:00:48 +11:00
|
|
|
string = self.__language__
|
2016-03-07 23:01:45 +11:00
|
|
|
dialog = xbmcgui.Dialog()
|
2016-03-04 00:00:48 +11:00
|
|
|
|
2016-01-24 01:53:24 +11:00
|
|
|
plexLogin = utils.settings('plexLogin')
|
|
|
|
plexToken = utils.settings('plexToken')
|
2016-01-15 00:47:34 +11:00
|
|
|
machineIdentifier = utils.settings('plex_machineIdentifier')
|
2015-12-28 02:27:49 +11:00
|
|
|
self.logMsg("Getting user list.", 1)
|
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)
|
|
|
|
# Download users failed. Set username to Plex login
|
|
|
|
if not users:
|
|
|
|
utils.settings('username', value=plexLogin)
|
2016-01-15 00:47:34 +11:00
|
|
|
self.logMsg("User download failed. Set username = plexlogin", 0)
|
2015-12-28 03:12:46 +11:00
|
|
|
return ('', '', '')
|
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-07 23:01:45 +11:00
|
|
|
userlistCoded.append(username.encode('utf-8'))
|
2015-12-28 19:00:43 +11:00
|
|
|
usernumber = len(userlist)
|
2015-12-28 02:27:49 +11:00
|
|
|
usertoken = ''
|
2015-12-28 19:00:43 +11:00
|
|
|
# Plex home not in use: only 1 user returned
|
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-03-08 23:00:03 +11:00
|
|
|
self.addonName + string(39306),
|
2016-03-07 23:01:45 +11:00
|
|
|
userlistCoded)
|
2015-12-28 19:17:17 +11:00
|
|
|
if user_select == -1:
|
|
|
|
self.logMsg("No user selected.", 1)
|
2016-01-15 00:47:34 +11:00
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)'
|
|
|
|
% self.addonId)
|
2015-12-28 19:17:17 +11:00
|
|
|
return ('', '', '')
|
|
|
|
# No Plex home in use - only 1 user
|
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]
|
|
|
|
self.logMsg("Selected user: %s" % selected_user, 1)
|
|
|
|
utils.settings('username', value=selected_user)
|
|
|
|
user = users[user_select]
|
2015-12-28 02:27:49 +11:00
|
|
|
# Ask for PIN, if protected:
|
|
|
|
if user['protected'] == '1':
|
2016-03-04 00:00:48 +11:00
|
|
|
# Please enter pin for user
|
2016-03-04 23:34:30 +11:00
|
|
|
self.logMsg('Asking for users PIN', 1)
|
2015-12-28 02:27:49 +11:00
|
|
|
pin = dialog.input(
|
2016-03-08 23:00:03 +11:00
|
|
|
string(39307) + selected_user,
|
2015-12-28 02:27:49 +11:00
|
|
|
type=xbmcgui.INPUT_NUMERIC,
|
2016-03-04 23:34:30 +11:00
|
|
|
option=xbmcgui.ALPHANUM_HIDE_INPUT)
|
|
|
|
# User chose to cancel
|
|
|
|
if pin is None:
|
|
|
|
break
|
2015-12-28 03:12:46 +11:00
|
|
|
else:
|
|
|
|
pin = None
|
2015-12-28 02:27:49 +11:00
|
|
|
# Switch to this Plex Home user, if applicable
|
2016-03-07 23:10:38 +11:00
|
|
|
# Plex bug: don't call url for protected user with empty PIN
|
|
|
|
if user['protected'] == '1' and not pin:
|
|
|
|
break
|
2016-01-15 00:47:34 +11:00
|
|
|
username, usertoken = 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,
|
|
|
|
machineIdentifier
|
2015-12-28 02:27:49 +11:00
|
|
|
)
|
2015-12-28 19:00:43 +11:00
|
|
|
# Couldn't get user auth
|
2015-12-28 02:27:49 +11:00
|
|
|
if not username:
|
2016-03-04 00:00:48 +11:00
|
|
|
# Could not login user, please try again
|
2016-03-08 23:00:03 +11:00
|
|
|
if not dialog.yesno(self.addonName,
|
|
|
|
string(39308) + selected_user,
|
|
|
|
string(39309)):
|
2016-03-04 23:34:30 +11:00
|
|
|
# User chose to cancel
|
|
|
|
break
|
2015-12-28 19:00:43 +11:00
|
|
|
# Successfully retrieved: break out of while loop
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
trials += trials
|
|
|
|
if not username:
|
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
|
2016-03-04 23:34:30 +11:00
|
|
|
return ('', '', '')
|
2015-12-28 03:12:46 +11:00
|
|
|
return (username, user['id'], usertoken)
|
2015-12-28 02:27:49 +11:00
|
|
|
|
2016-01-15 00:47:34 +11:00
|
|
|
def PlexSwitchHomeUser(self, userId, pin, token, machineId):
|
2015-12-28 02:27:49 +11:00
|
|
|
"""
|
|
|
|
Retrieves Plex home token for a Plex home user.
|
|
|
|
|
|
|
|
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
|
|
|
|
machineId Plex PMS machineIdentifier
|
2015-12-28 02:27:49 +11:00
|
|
|
|
|
|
|
Output:
|
2016-01-15 00:47:34 +11:00
|
|
|
(username, token)
|
2015-12-28 02:27:49 +11:00
|
|
|
|
2016-01-15 00:47:34 +11:00
|
|
|
Returns 2 empty strings if unsuccessful
|
2015-12-28 02:27:49 +11:00
|
|
|
"""
|
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-03-07 23:10:38 +11:00
|
|
|
self.logMsg('Switching to user %s' % userId, 0)
|
2016-01-15 00:47:34 +11:00
|
|
|
answer = self.TalkToPlexServer(url, talkType="POST", token=token)
|
|
|
|
if not answer:
|
|
|
|
self.logMsg('Error: plex.tv switch HomeUser change failed', -1)
|
|
|
|
return ('', '')
|
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-01-15 00:47:34 +11:00
|
|
|
# Get final token
|
|
|
|
url = 'https://plex.tv/pms/servers.xml'
|
|
|
|
answer = self.TalkToPlexServer(url, talkType="GET", token=token)
|
|
|
|
if not answer:
|
|
|
|
self.logMsg('Error: plex.tv switch HomeUser change failed', -1)
|
|
|
|
return ('', '')
|
|
|
|
|
|
|
|
found = 0
|
|
|
|
for child in answer:
|
|
|
|
if child.attrib['machineIdentifier'] == machineId:
|
|
|
|
token = child.attrib['accessToken']
|
|
|
|
self.logMsg('Found a plex home user token', 1)
|
|
|
|
found += 1
|
|
|
|
if found == 0:
|
|
|
|
self.logMsg('Error: plex.tv switch HomeUser change failed', -1)
|
|
|
|
return ('', '')
|
|
|
|
self.logMsg('Plex.tv switch HomeUser change successfull', 0)
|
2016-03-04 01:28:44 +11:00
|
|
|
self.logMsg("username: %s, token: xxxx. "
|
|
|
|
"Saving to window and file settings" % username, 0)
|
|
|
|
utils.window('emby_currUser', value=userId)
|
2016-03-07 23:01:45 +11:00
|
|
|
utils.settings('userId', value=userId)
|
2016-03-04 01:28:44 +11:00
|
|
|
utils.settings('username', value=username)
|
|
|
|
utils.window('emby_accessToken%s' % userId, value=token)
|
2016-01-15 00:47:34 +11:00
|
|
|
return (username, token)
|
2015-12-27 22:09:07 +11:00
|
|
|
|
|
|
|
def MyPlexListHomeUsers(self, authtoken):
|
|
|
|
"""
|
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:
|
|
|
|
authtoken for plex.tv
|
|
|
|
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
|
|
|
"""
|
|
|
|
XML = self.getXMLFromPMS('https://plex.tv', '/api/home/users/', {}, authtoken)
|
|
|
|
if not XML:
|
|
|
|
# Download failed; quitting with False
|
|
|
|
return False
|
|
|
|
# analyse response
|
|
|
|
root = XML.getroot()
|
|
|
|
users = []
|
|
|
|
for user in root:
|
|
|
|
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:
|
|
|
|
if AuthToken=='':
|
|
|
|
path = key
|
|
|
|
else:
|
|
|
|
xargs = dict()
|
|
|
|
xargs['X-Plex-Token'] = AuthToken
|
|
|
|
if key.find('?')==-1:
|
|
|
|
path = key + '?' + urlencode(xargs)
|
|
|
|
else:
|
|
|
|
path = key + '&' + urlencode(xargs)
|
|
|
|
|
|
|
|
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
|
|
|
|
path = path.encode('utf8')
|
|
|
|
|
|
|
|
# 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...
|
|
|
|
transcodePath = '/photo/:/transcode/' +str(width)+'x'+str(height)+ '/' + quote_plus(path)
|
|
|
|
|
|
|
|
args = dict()
|
|
|
|
args['width'] = width
|
|
|
|
args['height'] = height
|
|
|
|
args['url'] = path
|
|
|
|
|
|
|
|
if not AuthToken=='':
|
|
|
|
args['X-Plex-Token'] = AuthToken
|
|
|
|
|
|
|
|
return transcodePath + '?' + urlencode(args)
|
|
|
|
|
|
|
|
def getDirectImagePath(self, path, AuthToken):
|
|
|
|
"""
|
|
|
|
Direct Image support
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
path
|
|
|
|
AuthToken
|
|
|
|
result:
|
|
|
|
final path to image file
|
|
|
|
"""
|
|
|
|
if not AuthToken=='':
|
|
|
|
xargs = dict()
|
|
|
|
xargs['X-Plex-Token'] = AuthToken
|
|
|
|
if path.find('?')==-1:
|
|
|
|
path = path + '?' + urlencode(xargs)
|
|
|
|
else:
|
|
|
|
path = path + '&' + urlencode(xargs)
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
|
|
|
transcodePath = '/music/:/transcode/universal/start.mp3?'
|
|
|
|
|
|
|
|
args = dict()
|
|
|
|
args['path'] = path
|
|
|
|
args['session'] = UDID
|
|
|
|
args['protocol'] = 'http'
|
|
|
|
args['maxAudioBitrate'] = maxAudioBitrate
|
|
|
|
|
|
|
|
xargs = getXArgsDeviceInfo(options)
|
|
|
|
if not AuthToken=='':
|
|
|
|
xargs['X-Plex-Token'] = AuthToken
|
|
|
|
|
|
|
|
return transcodePath + urlencode(args) + '&' + urlencode(xargs)
|
|
|
|
|
|
|
|
def getDirectAudioPath(self, path, AuthToken):
|
|
|
|
"""
|
|
|
|
Direct Audio support
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
path
|
|
|
|
AuthToken
|
|
|
|
result:
|
|
|
|
final path to audio file
|
|
|
|
"""
|
|
|
|
if not AuthToken=='':
|
|
|
|
xargs = dict()
|
|
|
|
xargs['X-Plex-Token'] = AuthToken
|
|
|
|
if path.find('?')==-1:
|
|
|
|
path = path + '?' + urlencode(xargs)
|
|
|
|
else:
|
|
|
|
path = path + '&' + urlencode(xargs)
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
def returnServerList(self, ATV_udid, data):
|
|
|
|
"""
|
|
|
|
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:
|
|
|
|
ATV_udid Unique client ID
|
|
|
|
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
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
serverlist = []
|
|
|
|
for key, value in data[ATV_udid].items():
|
|
|
|
serverlist.append({
|
|
|
|
'name': value['name'],
|
|
|
|
'address': value['address'],
|
|
|
|
'ip': value['ip'],
|
|
|
|
'port': value['port'],
|
|
|
|
'scheme': value['scheme'],
|
|
|
|
'local': value['local'],
|
|
|
|
'owned': value['owned'],
|
|
|
|
'machineIdentifier': key,
|
|
|
|
'accesstoken': value['accesstoken'],
|
|
|
|
'baseURL': value['baseURL']
|
|
|
|
})
|
|
|
|
return serverlist
|
|
|
|
|
2016-01-02 06:36:02 +11:00
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
@utils.logging
|
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
|
2015-12-29 04:47:16 +11:00
|
|
|
self.clientinfo = clientinfo.ClientInfo()
|
2016-01-02 00:40:40 +11:00
|
|
|
self.clientId = self.clientinfo.getDeviceId()
|
2015-12-31 02:37:30 +11:00
|
|
|
self.userId = utils.window('emby_currUser')
|
|
|
|
self.server = utils.window('emby_server%s' % self.userId)
|
|
|
|
self.token = utils.window('emby_accessToken%s' % self.userId)
|
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-02-01 20:33:33 +11:00
|
|
|
def DateToKodi(self, stamp):
|
2015-12-29 04:47:16 +11:00
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
converts a Unix time stamp (seconds passed sinceJanuary 1 1970) to a
|
|
|
|
propper, human-readable time stamp used by Kodi
|
2016-02-03 23:01:13 +11:00
|
|
|
|
|
|
|
Output: Y-m-d h:m:s = 2009-04-05 23:16:04
|
2015-12-29 04:47:16 +11:00
|
|
|
"""
|
|
|
|
# DATEFORMAT = xbmc.getRegion('dateshort')
|
|
|
|
# TIMEFORMAT = xbmc.getRegion('meridiem')
|
|
|
|
# date_time = time.localtime(stamp)
|
|
|
|
# if DATEFORMAT[1] == 'd':
|
|
|
|
# localdate = time.strftime('%d-%m-%Y', date_time)
|
|
|
|
# elif DATEFORMAT[1] == 'm':
|
|
|
|
# localdate = time.strftime('%m-%d-%Y', date_time)
|
|
|
|
# else:
|
|
|
|
# localdate = time.strftime('%Y-%m-%d', date_time)
|
|
|
|
# if TIMEFORMAT != '/':
|
|
|
|
# localtime = time.strftime('%I:%M%p', date_time)
|
|
|
|
# else:
|
|
|
|
# localtime = time.strftime('%H:%M', date_time)
|
|
|
|
# return localtime + ' ' + localdate
|
2016-02-01 20:33:33 +11:00
|
|
|
try:
|
2016-02-13 02:53:49 +11:00
|
|
|
# DATEFORMAT = xbmc.getRegion('dateshort')
|
|
|
|
# TIMEFORMAT = xbmc.getRegion('meridiem')
|
2016-02-01 20:33:33 +11:00
|
|
|
date_time = time.localtime(float(stamp))
|
2016-02-03 23:01:13 +11:00
|
|
|
localdate = time.strftime('%Y-%m-%d %H:%M:%S', date_time)
|
2016-02-01 20:33:33 +11:00
|
|
|
except:
|
|
|
|
localdate = None
|
2015-12-29 04:47:16 +11:00
|
|
|
return localdate
|
|
|
|
|
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-02-01 20:33:33 +11:00
|
|
|
return self.item.attrib.get('type', None)
|
2016-01-02 19:28:31 +11:00
|
|
|
|
2015-12-29 04:47:16 +11:00
|
|
|
def getChecksum(self):
|
|
|
|
"""
|
2016-01-24 01:53:24 +11:00
|
|
|
Returns a string, not int
|
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-02-13 02:53:49 +11:00
|
|
|
return self.item.attrib.get('ratingKey', None)
|
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-02-01 20:33:33 +11:00
|
|
|
return self.item.attrib.get('key', None)
|
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-02-01 20:33:33 +11:00
|
|
|
return self.item.attrib.get('index', None)
|
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
|
|
|
"""
|
|
|
|
Returns the date when this library item was created
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
return self.DateToKodi(self.item.attrib.get('addedAt', None))
|
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
|
2015-12-29 04:47:16 +11:00
|
|
|
# Default
|
|
|
|
favorite = False
|
|
|
|
playcount = None
|
|
|
|
played = False
|
|
|
|
lastPlayedDate = None
|
|
|
|
resume = 0
|
|
|
|
rating = 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
playcount = int(item['viewCount'])
|
2016-02-13 02:53:49 +11:00
|
|
|
except:
|
2015-12-29 04:47:16 +11:00
|
|
|
playcount = None
|
|
|
|
|
|
|
|
if playcount:
|
|
|
|
played = True
|
|
|
|
|
|
|
|
try:
|
2016-02-01 20:33:33 +11:00
|
|
|
lastPlayedDate = self.DateToKodi(int(item['lastViewedAt']))
|
2016-02-13 02:53:49 +11:00
|
|
|
except:
|
2015-12-29 04:47:16 +11:00
|
|
|
lastPlayedDate = None
|
|
|
|
|
2016-02-13 02:53:49 +11:00
|
|
|
try:
|
|
|
|
userrating = int(item['userRating'])
|
|
|
|
except:
|
|
|
|
userrating = None
|
|
|
|
|
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-02-03 23:01:13 +11:00
|
|
|
providername: e.g. 'imdb'
|
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':
|
|
|
|
regex = re.compile(r'''/(tt\d+)''')
|
|
|
|
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
|
|
|
"""
|
|
|
|
Returns the audience rating or None
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
return self.item.attrib.get('audienceRating', None)
|
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'])
|
2015-12-31 20:53:22 +11:00
|
|
|
except KeyError:
|
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'])
|
2015-12-29 04:47:16 +11:00
|
|
|
except KeyError:
|
2016-01-02 06:36:02 +11:00
|
|
|
resume = 0.0
|
2016-01-15 02:17:24 +11:00
|
|
|
|
2016-02-01 20:33:33 +11:00
|
|
|
runtime = int(runtime * PlexToKodiTimefactor())
|
|
|
|
resume = int(resume * PlexToKodiTimefactor())
|
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-02-01 20:33:33 +11:00
|
|
|
return self.item.attrib.get('originallyAvailableAt', None)
|
2016-01-02 19:28:31 +11:00
|
|
|
|
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-01-10 02:14:02 +11:00
|
|
|
key = item['grandparentRatingKey']
|
|
|
|
title = item['grandparentTitle']
|
|
|
|
season = item['parentIndex']
|
|
|
|
episode = item['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-02-03 23:01:13 +11:00
|
|
|
xargs = PlexAPI().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 '?'
|
|
|
|
"""
|
|
|
|
if '?' not in url:
|
|
|
|
url = "%s?X-Plex-Token=%s" % (url, self.token)
|
|
|
|
else:
|
|
|
|
url = "%s&X-Plex-Token=%s" % (url, self.token)
|
|
|
|
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'
|
|
|
|
'bitrate': xxx, e.g. 10642 (an int!)
|
|
|
|
'container': xxx e.g. 'mkv'
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
videoCodec = {
|
|
|
|
'videocodec': videocodec,
|
|
|
|
'resolution': resolution,
|
|
|
|
'height': height,
|
|
|
|
'width': width,
|
|
|
|
'aspectratio': aspectratio,
|
|
|
|
'bitrate': bitrate,
|
|
|
|
'container': container
|
|
|
|
}
|
|
|
|
return videoCodec
|
|
|
|
|
2016-01-12 21:47:48 +11:00
|
|
|
def getExtras(self):
|
|
|
|
"""
|
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:
|
|
|
|
'1': Trailer
|
|
|
|
'5': Behind the scenes
|
|
|
|
|
|
|
|
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-02-01 20:33:33 +11:00
|
|
|
for extra in self.item.find('Extras'):
|
2016-01-12 21:47:48 +11:00
|
|
|
# Trailer:
|
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)
|
|
|
|
extraType = extra.attrib.get('extraType', None)
|
|
|
|
originallyAvailableAt = extra.attrib.get(
|
|
|
|
'originallyAvailableAt', None)
|
|
|
|
elements.append(
|
|
|
|
{'key': key,
|
|
|
|
'title': title,
|
|
|
|
'thumb': thumb,
|
|
|
|
'duration': int(duration * PlexToKodiTimefactor()),
|
|
|
|
'extraType': extraType,
|
|
|
|
'originallyAvailableAt': originallyAvailableAt,
|
|
|
|
'year': year})
|
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
|
|
|
"""
|
|
|
|
Returns the media streams
|
|
|
|
|
|
|
|
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()
|
2015-12-29 04:47:16 +11:00
|
|
|
profile = mediaStream['codecID'].lower()
|
2016-01-15 20:04:47 +11:00
|
|
|
if "dca" in audiotrack['codec'] and "dts-hd ma" in profile:
|
|
|
|
audiotrack['codec'] = "dtshd_ma"
|
2015-12-29 04:47:16 +11:00
|
|
|
audiotrack['channels'] = mediaStream.get('channels')
|
|
|
|
try:
|
2016-01-15 20:04:47 +11:00
|
|
|
audiotrack['language'] = mediaStream.get('language')
|
2015-12-29 04:47:16 +11:00
|
|
|
except KeyError:
|
2016-01-15 20:04:47 +11:00
|
|
|
audiotrack['language'] = 'unknown'
|
2015-12-29 04:47:16 +11:00
|
|
|
audiotracks.append(audiotrack)
|
|
|
|
|
2016-02-01 20:33:33 +11:00
|
|
|
elif mediaType == 3: # Subtitle streams
|
2015-12-29 04:47:16 +11:00
|
|
|
try:
|
|
|
|
subtitlelanguages.append(mediaStream['language'])
|
|
|
|
except:
|
|
|
|
subtitlelanguages.append("Unknown")
|
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
|
|
|
|
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.
|
2015-12-31 01:57:55 +11:00
|
|
|
|
2016-01-03 23:36:00 +11:00
|
|
|
Output:
|
|
|
|
{
|
|
|
|
'Primary': Plex key: "thumb". Only 1 pix
|
|
|
|
'Art':,
|
|
|
|
'Banner':,
|
|
|
|
'Logo':,
|
|
|
|
'Thumb':,
|
|
|
|
'Disc':,
|
|
|
|
'Backdrop': [] Plex key: "art". Only 1 pix
|
|
|
|
}
|
|
|
|
"""
|
2016-02-01 20:33:33 +11:00
|
|
|
item = self.item.attrib
|
2016-01-30 06:07:21 +11:00
|
|
|
|
2015-12-31 01:57:55 +11:00
|
|
|
maxHeight = 10000
|
|
|
|
maxWidth = 10000
|
|
|
|
customquery = ""
|
|
|
|
|
|
|
|
if utils.settings('compressArt') == "true":
|
|
|
|
customquery = "&Quality=90"
|
|
|
|
|
|
|
|
if utils.settings('enableCoverArt') == "false":
|
|
|
|
customquery += "&EnableImageEnhancers=false"
|
|
|
|
|
|
|
|
allartworks = {
|
|
|
|
'Primary': "",
|
|
|
|
'Art': "",
|
|
|
|
'Banner': "",
|
|
|
|
'Logo': "",
|
|
|
|
'Thumb': "",
|
|
|
|
'Disc': "",
|
|
|
|
'Backdrop': []
|
|
|
|
}
|
|
|
|
# Process backdrops
|
|
|
|
# Get background artwork URL
|
|
|
|
try:
|
|
|
|
background = item['art']
|
2016-02-01 20:33:33 +11:00
|
|
|
background = "%s%s" % (self.server, background)
|
2015-12-31 02:37:30 +11:00
|
|
|
background = self.addPlexCredentialsToUrl(background)
|
2015-12-31 01:57:55 +11:00
|
|
|
except KeyError:
|
|
|
|
background = ""
|
|
|
|
allartworks['Backdrop'].append(background)
|
|
|
|
# Get primary "thumb" pictures:
|
|
|
|
try:
|
|
|
|
primary = item['thumb']
|
2016-02-01 20:33:33 +11:00
|
|
|
primary = "%s%s" % (self.server, primary)
|
2015-12-31 02:37:30 +11:00
|
|
|
primary = self.addPlexCredentialsToUrl(primary)
|
2015-12-31 01:57:55 +11:00
|
|
|
except KeyError:
|
|
|
|
primary = ""
|
|
|
|
allartworks['Primary'] = primary
|
|
|
|
|
|
|
|
# Process parent items if the main item is missing artwork
|
|
|
|
if parentInfo:
|
|
|
|
|
|
|
|
# Process parent backdrops
|
|
|
|
if not allartworks['Backdrop']:
|
|
|
|
|
|
|
|
parentId = item.get('ParentBackdropItemId')
|
|
|
|
if parentId:
|
|
|
|
# If there is a parentId, go through the parent backdrop list
|
|
|
|
parentbackdrops = item['ParentBackdropImageTags']
|
|
|
|
|
|
|
|
backdropIndex = 0
|
|
|
|
for parentbackdroptag in parentbackdrops:
|
|
|
|
artwork = (
|
|
|
|
"%s/emby/Items/%s/Images/Backdrop/%s?"
|
|
|
|
"MaxWidth=%s&MaxHeight=%s&Format=original&Tag=%s%s"
|
2016-02-01 20:33:33 +11:00
|
|
|
% (self.server, parentId, backdropIndex,
|
2015-12-31 01:57:55 +11:00
|
|
|
maxWidth, maxHeight, parentbackdroptag, customquery))
|
|
|
|
allartworks['Backdrop'].append(artwork)
|
|
|
|
backdropIndex += 1
|
|
|
|
|
|
|
|
# Process the rest of the artwork
|
|
|
|
parentartwork = ['Logo', 'Art', 'Thumb']
|
|
|
|
for parentart in parentartwork:
|
|
|
|
|
|
|
|
if not allartworks[parentart]:
|
|
|
|
|
|
|
|
parentId = item.get('Parent%sItemId' % parentart)
|
|
|
|
if parentId:
|
|
|
|
|
|
|
|
parentTag = item['Parent%sImageTag' % parentart]
|
|
|
|
artwork = (
|
|
|
|
"%s/emby/Items/%s/Images/%s/0?"
|
|
|
|
"MaxWidth=%s&MaxHeight=%s&Format=original&Tag=%s%s"
|
2016-02-01 20:33:33 +11:00
|
|
|
% (self.server, parentId, parentart,
|
2015-12-31 01:57:55 +11:00
|
|
|
maxWidth, maxHeight, parentTag, customquery))
|
|
|
|
allartworks[parentart] = artwork
|
|
|
|
|
|
|
|
# Parent album works a bit differently
|
|
|
|
if not allartworks['Primary']:
|
|
|
|
|
|
|
|
parentId = item.get('AlbumId')
|
|
|
|
if parentId and item.get('AlbumPrimaryImageTag'):
|
|
|
|
|
|
|
|
parentTag = item['AlbumPrimaryImageTag']
|
|
|
|
artwork = (
|
|
|
|
"%s/emby/Items/%s/Images/Primary/0?"
|
|
|
|
"MaxWidth=%s&MaxHeight=%s&Format=original&Tag=%s%s"
|
2016-02-01 20:33:33 +11:00
|
|
|
% (self.server, parentId, maxWidth, maxHeight, parentTag, customquery))
|
2015-12-31 01:57:55 +11:00
|
|
|
allartworks['Primary'] = artwork
|
2015-12-31 02:37:30 +11:00
|
|
|
return allartworks
|
2016-01-02 00:40:40 +11:00
|
|
|
|
2016-02-07 22:38:50 +11:00
|
|
|
def getTranscodeVideoPath(self, action, quality={}):
|
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-01-06 02:05:20 +11:00
|
|
|
action 'DirectPlay', '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-02-03 23:01:13 +11:00
|
|
|
|
2016-02-07 22:38:50 +11:00
|
|
|
xargs = PlexAPI().getXArgsDeviceInfo()
|
2016-02-05 06:23:04 +11:00
|
|
|
# For DirectPlay, path/key of PART is needed
|
2016-01-06 02:05:20 +11:00
|
|
|
if action == "DirectPlay":
|
2016-02-05 06:23:04 +11:00
|
|
|
path = self.item[0][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
|
|
|
|
|
|
|
|
# For Direct Streaming or 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-02-05 06:23:04 +11:00
|
|
|
'copyts': 1,
|
2016-01-02 00:40:40 +11:00
|
|
|
'path': path,
|
2016-01-04 05:17:59 +11:00
|
|
|
'mediaIndex': 0, # Probably refering to XML reply sheme
|
2016-01-06 02:05:20 +11:00
|
|
|
'partIndex': self.part,
|
2016-02-07 22:38:50 +11:00
|
|
|
'protocol': 'hls', # seen in the wild: 'dash', 'http', 'hls'
|
2016-02-05 06:23:04 +11:00
|
|
|
'session': self.clientId,
|
2016-02-07 22:38:50 +11:00
|
|
|
# 'offset': 0, # Resume point
|
2016-01-06 02:05:20 +11:00
|
|
|
'fastSeek': 1
|
2016-01-02 00:40:40 +11:00
|
|
|
}
|
2016-01-06 02:05:20 +11:00
|
|
|
|
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-01-02 00:40:40 +11:00
|
|
|
self.logMsg("Setting transcode quality to: %s" % quality, 1)
|
|
|
|
args.update(quality)
|
2016-01-06 02:05:20 +11:00
|
|
|
args.update(argsUpdate)
|
2016-01-02 00:40:40 +11:00
|
|
|
|
2016-01-06 02:05:20 +11:00
|
|
|
url = transcodePath + \
|
|
|
|
urlencode(xargs) + '&' + \
|
|
|
|
urlencode(args)
|
|
|
|
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']
|
|
|
|
# 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)
|
|
|
|
utils.window('emby_%s.indexMapping' % playurl, value=mapping)
|
|
|
|
|
|
|
|
return externalsubs
|