Merge branch 'develop'

This commit is contained in:
tomkat83 2016-03-08 18:35:52 +01:00
commit 8363c04ae3
21 changed files with 347 additions and 216 deletions

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="plugin.video.plexkodiconnect"
name="PlexKodiConnect"
version="1.0.3"
version="1.0.4"
provider-name="croneter">
<requires>
<import addon="xbmc.python" version="2.1.0"/>

View file

@ -1,3 +1,14 @@
version 1.0.4
- Sleep for a while in loops - drastically reduces CPU load
- Connect to remote PMS!
- New Setting to reset all PMS and the plex.tv connection
- Correct encoding
- Much shorter download timeouts
- Improve sync resiliance and GDM discovery
- Reduce number of unsuccesful retries to 3 before telling user
- Clean-up library sync loop
- Language strings for library sync
version 1.0.3
- Hotfix database minimum version = 1.0.2

View file

@ -66,7 +66,7 @@ class Main:
'companion': entrypoint.plexCompanion,
'switchuser': entrypoint.switchPlexUser,
'deviceid': entrypoint.resetDeviceId,
'doPlexTvLogin': entrypoint.doPlexTvLogin
'reConnect': entrypoint.reConnect
}
if "/extrafanart" in sys.argv[0]:

View file

@ -362,7 +362,7 @@
<string id="39021">[COLOR yellow]Sync Emby Theme Media to Kodi[/COLOR]</string>
<string id="39022"> (local)</string>
<string id="39023">Failed to authenticate. Did you login to plex.tv?</string>
<string id="39024">[COLOR yellow]Log into plex.tv[/COLOR]</string>
<string id="39024">[COLOR yellow]Reset PMS and plex.tv connections to re-login[/COLOR]</string>
<string id="39025">Automatically log into plex.tv on startup</string>
<string id="39026">Enable constant background sync (restart Kodi!)</string>
@ -375,6 +375,9 @@
<string id="39204">Perform manual library sync</string>
<string id="39205">Unable to run the sync, the add-on is not connected to a Plex server.</string>
<string id="39206">Plex might lock your account if you fail to log in too many times. Proceed anyway?</string>
<string id="39207">Reseting PMS connections, please wait</string>
<string id="39208">Failed to reset PMS and plex.tv connects. Try to restart Kodi.</string>
<string id="39209">[COLOR yellow]Log-in to plex.tv[/COLOR]</string>
<!-- Plex Artwork.py -->
@ -393,4 +396,13 @@
<string id="39308">Could not log in user </string>
<string id="39309">Please try again.</string>
<!-- Plex Librarysync.py -->
<string id="39400">Library sync thread has crashed. You should restart Kodi now. Please report this on the forum</string>
<string id="39401">Detected Kodi database needs to be recreated for this version. This might take a while. Proceed?</string>
<string id="39402"> may not work correctly until the database is reset.</string>
<string id="39403">Cancelling the database syncing process. Current Kodi version is unsupported. Please verify your logs for more info.</string>
<string id="39404">Startup syncing process failed repeatedly. Try restarting Kodi. Stopping Sync for now.</string>
<string id="39405">Plex playlists/nodes refreshed</string>
<string id="39406">Plex playlists/nodes refresh failed</string>
</strings>

View file

@ -293,7 +293,7 @@
<string id="39021">[COLOR yellow]Plex Themes zu Kodi synchronisieren[/COLOR]</string>
<string id="39022"> (lokal)</string>
<string id="39023">Plex Media Server Authentifizierung fehlgeschlagen. Haben Sie sich bei plex.tv eingeloggt?</string>
<string id="39024">[COLOR yellow]Bei plex.tv einloggen[/COLOR]</string>
<string id="39024">[COLOR yellow]PMS und plex.tv Verbindungen zurücksetzen für erneuten Login[/COLOR]</string>
<string id="39025">Automatisch beim Starten bei plex.tv einloggen</string>
<string id="39026">Laufende Synchronisierung im Hintergrund aktivieren (Neustart!)</string>
@ -305,6 +305,11 @@
<string id="39204">Manuellen Scan der Plex Bibliotheken starten</string>
<string id="39205">Plex Bibliothek kann nicht gescannt werden, da keine Verbindung mit einem Plex Server besteht.</string>
<string id="39206">Plex könnte möglicherweise Ihren Account sperren, wenn Sie zu oft versuchen, sich erfolglos anzumelden. Trotzdem fortfahren?</string>
<string id="39207">PMS Verbindungen werden zurückgesetzt</string>
<string id="39208">PMS und plex.tv Verbindungen konnten nicht zurückgesetzt werden. Bitte versuchen Sie, Kodi neu zu starten, um das Problem zu beheben.</string>
<string id="39209">[COLOR yellow]Bei plex.tv einloggen[/COLOR]</string>
<!-- Plex Artwork.py -->
<string id="39250">Alle Plex Bilder in Kodi zwischenzuspeichern kann sehr lange dauern. Möchten Sie wirklich fortfahren?</string>
@ -322,4 +327,13 @@
<string id="39308">Anmeldung fehlgeschlagen für Benutzer </string>
<string id="39309">Bitte erneut versuchen.</string>
<!-- Plex Librarysync.py -->
<string id="39400">Die Synchronisierung der Plex Bibliotheken ist abgestürzt. Bitte Kodi neu starten. Danke, wenn Sie sich die Zeit nehmen und im Plex Forum vom Absturz berichten.</string>
<string id="39401">Die Kodi Datenbank muss neu kreiert werden für diese Version. Das kann eine Weile dauern. Fortfahren?</string>
<string id="39402"> funktioniert möglicherweise nicht richtig, bis die Kodi Datenbank zurückgesetzt worden ist.</string>
<string id="39403">Synchronisierung der Plex Bibliotheken wird abgebrochen. Die momentane Kodi Version wird nicht unterstützt. Für weitere Informationen bitte das Kodi Log konsultieren.</string>
<string id="39404">Der Startup Synchronisations-Prozess der Plex Bibliotheken ist mehrmals fehlgeschlagen. Bitte Kodi neu starten. Synch wird jetzt gestoppt.</string>
<string id="39405">Plex Playlisten/Nodes aktualisiert</string>
<string id="39406">Plex Playlisten/Nodes Aktualisierung fehlgeschlagen</string>
</strings>

View file

@ -55,7 +55,7 @@ import re
import json
from urllib import urlencode, quote_plus
from PlexFunctions import PlexToKodiTimefactor
from PlexFunctions import PlexToKodiTimefactor, PMSHttpsEnabled
try:
import xml.etree.cElementTree as etree
@ -131,17 +131,14 @@ class PlexAPI():
dialog = xbmcgui.Dialog()
while retrievedPlexLogin == '' and plexLogin != '':
# Enter plex.tv username. Or nothing to cancel.
plexLogin = dialog.input(
self.addonName.encode('utf-8') + string(39300).encode('utf-8'),
type=xbmcgui.INPUT_ALPHANUM,
)
plexLogin = dialog.input(self.addonName + string(39300),
type=xbmcgui.INPUT_ALPHANUM)
if plexLogin != "":
# Enter password for plex.tv user
plexPassword = dialog.input(
string(39301).encode('utf-8') + plexLogin.encode('utf-8'),
string(39301) + plexLogin,
type=xbmcgui.INPUT_ALPHANUM,
option=xbmcgui.ALPHANUM_HIDE_INPUT
)
option=xbmcgui.ALPHANUM_HIDE_INPUT)
retrievedPlexLogin, authtoken = self.MyPlexSignIn(
plexLogin,
plexPassword,
@ -151,8 +148,7 @@ class PlexAPI():
if plexLogin == '':
# Could not sign in user
dialog.ok(self.addonName,
string(39302).encode('utf-8')
+ plexLogin.encode('utf-8'))
string(39302) + plexLogin)
# Write to Kodi settings file
utils.settings('plexLogin', value=retrievedPlexLogin)
utils.settings('plexToken', value=authtoken)
@ -177,12 +173,12 @@ class PlexAPI():
dialog = xbmcgui.Dialog()
if not code:
# Problems trying to contact plex.tv. Try again later
dialog.ok(self.addonName, string(39303).encode('utf-8'))
dialog.ok(self.addonName, string(39303))
return False
# Go to https://plex.tv/pin and enter the code:
answer = dialog.yesno(self.addonName,
(string(39304) + "\n\n").encode('utf-8'),
code.encode('utf-8'))
string(39304) + "\n\n",
code)
if not answer:
return False
count = 0
@ -196,7 +192,7 @@ class PlexAPI():
count += 1
if not xml:
# Could not sign in to plex.tv Try again later
dialog.ok(self.addonName, string(39305).encode('utf-8'))
dialog.ok(self.addonName, string(39305))
return False
# Parse xml
userid = xml.attrib.get('id')
@ -300,12 +296,12 @@ class PlexAPI():
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)
"Error message: %s"
% (url, e), -1)
return False
except requests.exceptions.ReadTimeout:
self.logMsg("Server timeout reached for Url %s with header %s"
% (url, header), -1)
self.logMsg("Server timeout reached for Url %s"
% url, -1)
return False
# We received an answer from the server, but not as expected.
if answer.status_code >= 400:
@ -634,6 +630,12 @@ class PlexAPI():
self.getPMSListFromMyPlex(ATV_udid, authtoken)
# all servers - update enableGzip
for uuid_id in self.g_PMS.get(ATV_udid, {}):
# Ping to check whether we need HTTPs or HTTP
url = (self.getPMSProperty(ATV_udid, uuid_id, 'ip') + ':'
+ self.getPMSProperty(ATV_udid, uuid_id, 'port'))
if PMSHttpsEnabled(url):
self.logMsg('PMS %s talks HTTPS' % uuid_id, 1)
self.updatePMSProperty(ATV_udid, uuid_id, 'scheme', 'https')
# enable Gzip if not on same host, local&remote PMS depending
# on setting
enableGzip = (not self.getPMSProperty(ATV_udid, uuid_id, 'ip') == IP_self) \
@ -1089,7 +1091,7 @@ class PlexAPI():
if usernumber > 1:
# Select user
user_select = dialog.select(
(self.addonName + string(39306)).encode('utf-8'),
self.addonName + string(39306),
userlistCoded)
if user_select == -1:
self.logMsg("No user selected.", 1)
@ -1108,7 +1110,7 @@ class PlexAPI():
# Please enter pin for user
self.logMsg('Asking for users PIN', 1)
pin = dialog.input(
(string(39307) + selected_user).encode('utf-8'),
string(39307) + selected_user,
type=xbmcgui.INPUT_NUMERIC,
option=xbmcgui.ALPHANUM_HIDE_INPUT)
# User chose to cancel
@ -1129,11 +1131,9 @@ class PlexAPI():
# Couldn't get user auth
if not username:
# Could not login user, please try again
if not dialog.yesno(
self.addonName,
(string(39308) + selected_user).encode('utf-8'),
string(39309).encode('utf-8')
):
if not dialog.yesno(self.addonName,
string(39308) + selected_user,
string(39309)):
# User chose to cancel
break
# Successfully retrieved: break out of while loop

View file

@ -87,9 +87,11 @@ class PlexCompanion(threading.Thread):
subscribers.subMgr.notify()
settings['serverList'] = self.client.getServerList()
xbmc.sleep(50)
except:
self.logMsg("Error in loop, continuing anyway", 1)
self.logMsg(traceback.print_exc(), 1)
xbmc.sleep(50)
self.client.stop_all()
try:

View file

@ -347,3 +347,24 @@ def getPlexRepeat(kodiRepeat):
'all': '2' # does this work?!?
}
return plexRepeat.get(kodiRepeat)
def PMSHttpsEnabled(url):
"""
Returns True if the PMS wants to talk https, False otherwise
With with e.g. url=192.168.0.1:32400 (NO http/https)
This is done by GET /identity (returns an error if https is enabled and we
are trying to use http)
"""
xml = downloadutils.DownloadUtils().downloadUrl('http://%s/identity' % url)
try:
# received a valid XML - http connection is possible
xml.attrib
logMsg('PMSHttpsEnabled', 'PMS on %s talks HTTP' % url, 1)
return False
except:
# couldn't get an xml - switch to https traffic
logMsg('PMSHttpsEnabled', 'PMS on %s talks HTTPS' % url, 1)
return True

View file

@ -37,7 +37,7 @@ class DownloadUtils():
# Requests session
s = None
timeout = 30
timeout = 3
def __init__(self):
@ -359,7 +359,7 @@ class DownloadUtils():
except requests.exceptions.ConnectionError as e:
# Make the addon aware of status
if utils.window('emby_online') != "false":
self.logMsg("Server unreachable at: %s" % url, 0)
self.logMsg("Server unreachable at: %s" % url, -1)
self.logMsg(e, 2)
utils.window('emby_online', value="false")

View file

@ -72,18 +72,48 @@ def plexCompanion(fullurl, params):
title, "Not knowing what to do for now - no playQueue sent", -1)
def doPlexTvLogin():
def reConnect():
"""
Triggers login to plex.tv
Triggers login to plex.tv and re-authorization
"""
string = xbmcaddon.Addon().getLocalizedString
utils.logMsg("entrypoint reConnect",
"Connection resets requested", 0)
# Pause library sync thread - user needs to be auth in order to sync
utils.window('suspend_LibraryThread', value='true')
# Suspend the user client during procedure
utils.window('suspend_Userclient', value='true')
dialog = xbmcgui.Dialog()
dialog.notification(
heading=addonName,
message=string(39207),
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
sound=False)
# Wait max for 20 seconds for all lib scans to finish
counter = 0
while utils.window('emby_dbScan') == 'true':
xbmc.sleep(1000)
counter += 1
if counter > 20:
dialog.ok(
heading=addonName,
message=string(39208),
)
# Resuming threads, just in case
utils.window('suspend_LibraryThread', clear=True)
utils.window('suspend_Userclient', clear=True)
# Abort reConnection
return
import initialsetup
initialsetup.InitialSetup().setup(forcePlexTV=True)
utils.logMsg("PLEX", "Reset login attempts.", 1)
utils.window('emby_serverStatus', value="Auth")
# Log out currently signed in user:
utils.window('emby_serverStatus', value="401")
# Restart user client
utils.window('suspend_Userclient', clear=True)
# Request lib sync to get user view data (e.g. watched/unwatched)
utils.window('plex_runLibScan', value='full')
def PassPlaylist(xml, resume=None):
@ -155,7 +185,7 @@ def resetAuth():
string = xbmcaddon.Addon().getLocalizedString
resp = xbmcgui.Dialog().yesno(
heading="Warning",
line1=string(39206).encode('utf-8'))
line1=string(39206))
if resp == 1:
utils.logMsg("PLEX", "Reset login attempts.", 1)
utils.window('emby_serverStatus', value="Auth")
@ -225,14 +255,14 @@ def resetDeviceId():
"Failed to generate a new device Id: %s" % e, 1)
dialog.ok(
heading=addonName,
line1=language(33032).encode('utf-8'))
line1=language(33032))
else:
utils.logMsg(addonName,
"Successfully removed old deviceId: %s New deviceId: %s"
% (deviceId_old, deviceId), 1)
dialog.ok(
heading=addonName,
line1=language(33033).encode('utf-8'))
line1=language(33033))
xbmc.executebuiltin('RestartApp')
##### ADD ADDITIONAL USERS #####
@ -1159,6 +1189,6 @@ def RunLibScan(mode):
# Server is not online, do not run the sync
string = xbmcaddon.Addon().getLocalizedString
xbmcgui.Dialog().ok(heading=addonName,
line1=string(39205).encode('utf-8'))
line1=string(39205))
else:
utils.window('plex_runLibScan', value='full')

View file

@ -40,7 +40,7 @@ class image_cache_thread(threading.Thread):
"http://%s:%s/image/image://%s"
% (self.xbmc_host, self.xbmc_port, self.urlToProcess)),
auth=(self.xbmc_username, self.xbmc_password),
timeout=(35.1, 35.1))
timeout=(0.1, 0.1))
# We don't need the result
except: pass

View file

@ -44,7 +44,7 @@ class InitialSetup():
plexLogin = plexdict['plexLogin']
plexToken = plexdict['plexToken']
plexid = plexdict['plexid']
self.logMsg('Plex info retrieved from settings: %s' % plexdict, 1)
self.logMsg('Plex info retrieved from settings', 1)
dialog = xbmcgui.Dialog()
@ -57,10 +57,8 @@ class InitialSetup():
# Delete token in the settings
utils.settings('plexToken', value='')
# Could not login, please try again
dialog.ok(
self.addonName,
string(39009).encode('utf-8')
)
dialog.ok(self.addonName,
string(39009))
result = self.plx.PlexTvSignInWithPin()
if result:
plexLogin = result['username']
@ -68,10 +66,8 @@ class InitialSetup():
plexid = result['plexid']
elif chk is False or chk >= 400:
# Problems connecting to plex.tv. Network or internet issue?
dialog.ok(
self.addonName,
string(39010).encode('utf-8')
)
dialog.ok(self.addonName,
string(39010))
# If a Plex server IP has already been set, return.
if server and forcePlexTV is False:
self.logMsg("Server is already set.", 0)
@ -100,8 +96,8 @@ class InitialSetup():
self.logMsg("Result of setting g_PMS variable: %s"
% self.plx.g_PMS, 1)
isconnected = False
serverlist = self.plx.returnServerList(
clientId, self.plx.g_PMS)
serverlist = self.plx.returnServerList(clientId,
self.plx.g_PMS)
self.logMsg('PMS serverlist: %s' % serverlist)
# Let user pick server from a list
# Get a nicer list
@ -110,22 +106,27 @@ class InitialSetup():
if len(serverlist) == 0:
dialog.ok(
self.addonName,
string(39011).encode('utf-8')
string(39011)
)
break
for server in serverlist:
if server['local'] == '1':
# server is in the same network as client. Add "local"
dialoglist.append(
server['name'].encode('utf-8')
+ string(39022).encode('utf-8'))
server['name']
+ string(39022))
else:
dialoglist.append(server['name'].encode('utf-8'))
resp = dialog.select(string(39012).encode('utf-8'), dialoglist)
dialoglist.append(server['name'])
resp = dialog.select(string(39012), dialoglist)
server = serverlist[resp]
activeServer = server['machineIdentifier']
url = server['scheme'] + '://' + server['ip'] + ':' + \
server['port']
# Re-direct via plex if remote - will lead to the correct SSL
# certificate
if server['local'] == '1':
url = server['scheme'] + '://' + server['ip'] + ':' \
+ server['port']
else:
url = server['baseURL']
# Deactive SSL verification if the server is local!
if server['local'] == '1':
utils.settings('sslverify', 'false')
@ -146,9 +147,8 @@ class InitialSetup():
# Not yet authorized for Plex server
# Please sign in to plex.tv
dialog.ok(self.addonName,
string(39013).encode('utf-8')
+ server['name'].encode('utf-8'),
string(39014).encode('utf-8'))
string(39013) + server['name'],
string(39014))
result = self.plx.PlexTvSignInWithPin()
if result:
plexLogin = result['username']
@ -161,7 +161,7 @@ class InitialSetup():
elif chk >= 400 or chk is False:
# Problems connecting to server. Pick another server?
resp = dialog.yesno(self.addonName,
string(39015).encode('utf-8'))
string(39015))
# Exit while loop if user chooses No
if not resp:
break
@ -171,18 +171,21 @@ class InitialSetup():
break
if not isconnected:
# Enter Kodi settings instead
if dialog.yesno(
heading=self.addonName,
line1=string(39016).encode('utf-8')):
self.logMsg("User opted to disable Plex music library.", 1)
utils.settings('enableMusic', value="false")
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
return
# Write to Kodi settings file
utils.settings('plex_machineIdentifier', activeServer)
if server['local'] == '1':
scheme = server['scheme']
utils.settings('ipaddress', server['ip'])
utils.settings('port', server['port'])
if server['scheme'] == 'https':
else:
baseURL = server['baseURL'].split(':')
scheme = baseURL[0]
utils.settings('ipaddress', baseURL[1].replace('//', ''))
utils.settings('port', baseURL[2])
if scheme == 'https':
utils.settings('https', 'true')
else:
utils.settings('https', 'false')
@ -207,14 +210,14 @@ class InitialSetup():
if forcePlexTV:
return
if dialog.yesno(
heading=self.addonName,
line1=string(39016).encode('utf-8')):
# Disable Plex music?
if dialog.yesno(heading=self.addonName,
line1=string(39016)):
self.logMsg("User opted to disable Plex music library.", 1)
utils.settings('enableMusic', value="false")
if dialog.yesno(
heading=self.addonName,
line1=string(39017).encode('utf-8')):
# Open Settings page now?
if dialog.yesno(heading=self.addonName,
line1=string(39017)):
xbmc.executebuiltin(
'Addon.OpenSettings(plugin.video.plexkodiconnect)')

View file

@ -8,6 +8,7 @@ import Queue
import xbmc
import xbmcgui
import xbmcvfs
import xbmcaddon
import utils
import clientinfo
@ -24,6 +25,7 @@ import PlexFunctions
###############################################################################
@utils.logging
@utils.ThreadMethodsAdditionalStop('emby_shouldStop')
@utils.ThreadMethods
class ThreadedGetMetadata(Thread):
@ -38,10 +40,11 @@ class ThreadedGetMetadata(Thread):
the downloaded metadata XMLs as etree objects
lock Lock(), used for counting where we are
"""
def __init__(self, queue, out_queue, lock):
def __init__(self, queue, out_queue, lock, processlock):
self.queue = queue
self.out_queue = out_queue
self.lock = lock
self.processlock = processlock
Thread.__init__(self)
def run(self):
@ -49,8 +52,10 @@ class ThreadedGetMetadata(Thread):
queue = self.queue
out_queue = self.out_queue
lock = self.lock
processlock = self.processlock
threadStopped = self.threadStopped
global getMetadataCount
global processMetadataCount
while threadStopped() is False:
# grabs Plex item from queue
try:
@ -63,6 +68,14 @@ class ThreadedGetMetadata(Thread):
plexXML = PlexFunctions.GetPlexMetadata(updateItem['itemId'])
if plexXML is None:
# Did not receive a valid XML - skip that item for now
self.logMsg("Could not get metadata for %s. "
"Skipping that item for now"
% updateItem['itemId'], -1)
# Increase BOTH counters - since metadata won't be processed
with lock:
getMetadataCount += 1
with processlock:
processMetadataCount += 1
queue.task_done()
continue
@ -113,25 +126,20 @@ class ThreadedProcessMetadata(Thread):
except Queue.Empty:
xbmc.sleep(100)
continue
# Do the work; lock to be sure we've only got 1 Thread
# Do the work
plexitem = updateItem['XML']
method = updateItem['method']
viewName = updateItem['viewName']
viewId = updateItem['viewId']
title = updateItem['title']
itemSubFkt = getattr(item, method)
with lock:
# Get the one child entry in the xml and process
for child in plexitem:
if method == 'add_updateAlbum':
item.add_updateAlbum(child,
viewtag=viewName,
viewid=viewId)
else:
itemSubFkt(child,
viewtag=viewName,
viewid=viewId)
# Keep track of where we are at
with lock:
processMetadataCount += 1
processingViewName = title
# signals to queue job is done
@ -164,10 +172,10 @@ class ThreadedShowSyncInfo(Thread):
threadStopped = self.threadStopped
downloadLock = self.locks[0]
processLock = self.locks[1]
dialog.create(("%s: Sync %s: %s items"
dialog.create("%s: Sync %s: %s items"
% (self.addonName,
self.itemType,
str(total))).encode('utf-8'),
str(total)),
"Starting")
global getMetadataCount
global processMetadataCount
@ -185,15 +193,11 @@ class ThreadedShowSyncInfo(Thread):
percentage = int(float(totalProgress) / float(total)*100.0)
except ZeroDivisionError:
percentage = 0
try:
dialog.update(
percentage,
message=("Downloaded: %s. Processed: %s: %s"
% (getMetadataProgress, processMetadataProgress,
viewName))).encode('utf-8')
except:
# Wierd formating of the string viewName?!?
pass
dialog.update(percentage,
message="Downloaded: %s. Processed: %s: %s"
% (getMetadataProgress,
processMetadataProgress,
viewName))
# Sleep for x milliseconds
xbmc.sleep(500)
dialog.close()
@ -213,6 +217,8 @@ class LibrarySync(Thread):
self.__dict__ = self._shared_state
self.__language__ = xbmcaddon.Addon().getLocalizedString
self.clientInfo = clientinfo.ClientInfo()
self.user = userclient.UserClient()
self.emby = embyserver.Read_EmbyServer()
@ -226,33 +232,23 @@ class LibrarySync(Thread):
self.enableMusic = True if utils.settings('enableMusic') == "true" \
else False
self.enableBackgroundSync = True if utils.settings(
'enableBackgroundSync') == "true" \
else False
'enableBackgroundSync') == "true" else False
Thread.__init__(self)
def showKodiNote(self, message, forced=False):
"""
Shows a Kodi popup, if user selected to do so
Shows a Kodi popup, if user selected to do so. Pass message in unicode
or string
"""
if not (self.showDbSync or forced):
return
xbmcgui.Dialog().notification(
heading=self.addonName,
message=message.encode('utf-8'),
message=message,
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
sound=False)
def startSync(self):
utils.window('emby_dbScan', value="true")
completed = self.fastSync()
if not completed:
# Fast sync failed or server plugin is not found
self.logMsg("Something went wrong, starting full sync", -1)
completed = self.fullSync(manualrun=True)
utils.window('emby_dbScan', clear=True)
return completed
def fastSync(self):
"""
Fast incremential lib sync
@ -291,8 +287,8 @@ class LibrarySync(Thread):
if self.threadStopped():
return True
# Get items per view
items = PlexFunctions.GetAllPlexLeaves(
view['id'], updatedAt=lastSync)
items = PlexFunctions.GetAllPlexLeaves(view['id'],
updatedAt=lastSync)
# Just skip item if something went wrong
if not items:
continue
@ -456,7 +452,7 @@ class LibrarySync(Thread):
"Found viewid: %s" % folderid,
"viewname: %s" % current_viewname,
"viewtype: %s" % current_viewtype,
"tagid: %s" % current_tagid)), 2)
"tagid: %s" % current_tagid)), 1)
# Remove views that are still valid to delete rest later
try:
@ -739,7 +735,8 @@ class LibrarySync(Thread):
for i in range(min(self.syncThreadNumber, itemNumber)):
thread = ThreadedGetMetadata(getMetadataQueue,
processMetadataQueue,
getMetadataLock)
getMetadataLock,
processMetadataLock)
thread.setDaemon(True)
thread.start()
threads.append(thread)
@ -1121,37 +1118,46 @@ class LibrarySync(Thread):
self.run_internal()
except Exception as e:
utils.window('emby_dbScan', clear=True)
self.logMsg('LibrarySync thread crashed', -1)
# Library sync thread has crashed
xbmcgui.Dialog().ok(
heading=self.addonName,
line1=("Library sync thread has crashed. "
"You should restart Kodi now. "
"Please report this on the forum."))
line1=self.__language__(39400))
raise
def run_internal(self):
# Re-assign handles to have faster calls
window = utils.window
settings = utils.settings
log = self.logMsg
threadStopped = self.threadStopped
threadSuspended = self.threadSuspended
installSyncDone = self.installSyncDone
enableBackgroundSync = self.enableBackgroundSync
fullSync = self.fullSync
fastSync = self.fastSync
string = self.__language__
dialog = xbmcgui.Dialog()
startupComplete = False
self.views = []
count = 0
errorcount = 0
self.logMsg("---===### Starting LibrarySync ###===---", 0)
while not self.threadStopped():
log("---===### Starting LibrarySync ###===---", 0)
while not threadStopped():
# In the event the server goes offline, or an item is playing
while self.threadSuspended():
while threadSuspended():
# Set in service.py
if self.threadStopped():
if threadStopped():
# Abort was requested while waiting. We should exit
log("###===--- LibrarySync Stopped ---===###", 0)
return
xbmc.sleep(1000)
if (window('emby_dbCheck') != "true" and
self.installSyncDone):
if (window('emby_dbCheck') != "true" and installSyncDone):
# Verify the validity of the database
currentVersion = settings('dbCreatedWithVersion')
minVersion = window('emby_minDBVersion')
@ -1160,17 +1166,14 @@ class LibrarySync(Thread):
if not uptoDate:
log("Db version out of date: %s minimum version required: "
"%s" % (currentVersion, minVersion), 0)
resp = xbmcgui.Dialog().yesno(
heading="Db Version",
line1=("Detected the database needs to be recreated "
"for this version of " + self.addonName +
"Proceed?"))
# DB out of date. Proceed to recreate?
resp = dialog.yesno(heading=self.addonName,
line1=string(39401))
if not resp:
log("Db version out of date! USER IGNORED!", 0)
xbmcgui.Dialog().ok(
heading=self.addonName,
line1=(self.addonName + " may not work correctly "
"until the database is reset."))
# PKC may not work correctly until reset
dialog.ok(heading=self.addonName,
line1=(self.addonName + string(39402)))
else:
utils.reset()
break
@ -1184,20 +1187,19 @@ class LibrarySync(Thread):
if not xbmcvfs.exists(videoDb):
# Database does not exists
log("The current Kodi version is incompatible "
"to know which Kodi versions are supported.", 0)
xbmcgui.Dialog().ok(
heading=self.addonName,
line1=("Cancelling the database syncing process. "
"Current Kodi version: %s is unsupported. "
"Please verify your logs for more info."
% xbmc.getInfoLabel('System.BuildVersion')))
"to know which Kodi versions are supported.", -1)
log('Current Kodi version: %s' % xbmc.getInfoLabel(
'System.BuildVersion').decode('utf-8'))
# "Current Kodi version is unsupported, cancel lib sync"
dialog.ok(heading=self.addonName,
line1=string(39403))
break
# Run start up sync
window('emby_dbScan', value="true")
log("Db version: %s" % settings('dbCreatedWithVersion'), 0)
log("Initial start-up full sync starting", 0)
librarySync = self.fullSync(manualrun=True)
librarySync = fullSync(manualrun=True)
window('emby_dbScan', clear=True)
if librarySync:
log("Initial start-up full sync successful", 0)
@ -1205,17 +1207,16 @@ class LibrarySync(Thread):
settings('SyncInstallRunDone', value="true")
settings("dbCreatedWithVersion",
self.clientInfo.getVersion())
self.installSyncDone = True
installSyncDone = True
else:
log("Initial start-up full sync unsuccessful", -1)
errorcount += 1
if errorcount > 2:
log("Startup full sync failed. Stopping sync", -1)
xbmcgui.Dialog().ok(
heading=self.addonName,
line1=("Startup syncing process failed repeatedly."
" Try restarting Kodi. Stopping Sync for "
"now."))
# "Startup syncing process failed repeatedly"
# "Please restart"
dialog.ok(heading=self.addonName,
line1=string(39404))
break
# Currently no db scan, so we can start a new scan
@ -1225,7 +1226,7 @@ class LibrarySync(Thread):
log('Full library scan requested, starting', 0)
window('emby_dbScan', value="true")
window('plex_runLibScan', clear=True)
self.fullSync(manualrun=True)
fullSync(manualrun=True)
window('emby_dbScan', clear=True)
count = 0
# Reset views was requested from somewhere else
@ -1239,36 +1240,45 @@ class LibrarySync(Thread):
# Remove video nodes
utils.deleteNodes()
# Kick off refresh
dialog = xbmcgui.Dialog()
if self.maintainViews():
# Ran successfully
log("Refresh playlists/nodes completed", 0)
# "Plex playlists/nodes refreshed"
dialog.notification(
heading=self.addonName,
message="Plex playlists/nodes refreshed",
message=string(39405),
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
time=3000,
sound=True)
else:
self.logMsg("Refresh playlists/nodes failed", -1)
# Failed
log("Refresh playlists/nodes failed", -1)
# "Plex playlists/nodes refresh failed"
dialog.notification(
heading=self.addonName,
message="Plex playlists/nodes refresh failed",
message=string(39406),
icon=xbmcgui.NOTIFICATION_ERROR,
time=3000,
sound=True)
window('emby_dbScan', clear=True)
elif self.enableBackgroundSync:
elif enableBackgroundSync:
# Run full lib scan approx every 30min
if count >= 1800:
count = 0
window('emby_dbScan', value="true")
log('Running automatic full lib scan', 0)
self.fullSync(manualrun=True)
log('Running background full lib scan', 0)
fullSync(manualrun=True)
window('emby_dbScan', clear=True)
# Run fast sync otherwise (ever 2 seconds or so)
# Run fast sync otherwise (ever second or so)
else:
self.startSync()
window('emby_dbScan', value="true")
if not fastSync():
# Fast sync failed or server plugin is not found
log("Something went wrong, starting full sync", -1)
fullSync(manualrun=True)
window('emby_dbScan', clear=True)
xbmc.sleep(2000)
xbmc.sleep(1000)
count += 1
log("###===--- LibrarySync Stopped ---===###", 0)

View file

@ -524,8 +524,8 @@ class Player(xbmc.Player):
offerDelete = False
if percentComplete >= markPlayedAt and offerDelete:
resp = xbmcgui.Dialog().yesno(
lang(30091).encode('utf-8'),
lang(33015).encode('utf-8'),
lang(30091),
lang(33015),
autoclose=120000)
if not resp:
log("User skipped deletion.", 1)

View file

@ -34,6 +34,8 @@ class PlayUtils():
"""
Returns the playurl for the part with number partNumber
(movie might consist of several files)
playurl is utf-8 encoded!
"""
log = self.logMsg
window = utils.window
@ -64,6 +66,7 @@ class PlayUtils():
}
playurl = self.API.getTranscodeVideoPath('Transcode',
quality=quality)
playurl = playurl.encode('utf-8')
# Set playmethod property
window('emby_%s.playmethod' % playurl, value="Transcode")
@ -371,7 +374,7 @@ class PlayUtils():
subNum += 1
if audioNum > 1:
resp = dialog.select(lang(33013).encode('utf-8'), audioStreams)
resp = dialog.select(lang(33013), audioStreams)
if resp > -1:
# User selected audio
playurlprefs['audioStreamID'] = audioStreamsList[resp]
@ -384,7 +387,7 @@ class PlayUtils():
playurlprefs['audioBoost'] = utils.settings('audioBoost')
if subNum > 1:
resp = dialog.select(lang(33014).encode('utf-8'), subtitleStreams)
resp = dialog.select(lang(33014), subtitleStreams)
if resp == 0:
# User selected no subtitles
playurlprefs["skipSubtitles"] = 1

View file

@ -31,7 +31,10 @@ import re
import threading
import time
import urllib2
import downloadutils
from PlexFunctions import PMSHttpsEnabled
class plexgdm:
@ -56,6 +59,7 @@ class plexgdm:
self.discovery_complete = False
self.client_registered = False
self.debug = debug
self.download = downloadutils.DownloadUtils().downloadUrl
def __printDebug(self, message, level=1):
if self.debug:
@ -139,13 +143,19 @@ class plexgdm:
try:
media_server=self.server_list[0]['server']
media_port=self.server_list[0]['port']
scheme = self.server_list[0]['protocol']
self.__printDebug("Checking server [%s] on port [%s]" % (media_server, media_port) ,2)
client_result = downloadutils.DownloadUtils().downloadUrl(
'http://%s:%s/clients' % (media_server, media_port))
client_result = self.download(
'%s://%s:%s/clients' % (scheme, media_server, media_port))
# f = urllib2.urlopen('http://%s:%s/clients' % (media_server, media_port))
# client_result = f.read()
if self.client_id in str(client_result):
registered = False
for client in client_result:
if (client.attrib.get('machineIdentifier') ==
self.client_id):
registered = True
if registered:
self.__printDebug("Client registration successful",1)
self.__printDebug("Client data is: %s" % client_result, 3)
return True
@ -208,7 +218,6 @@ class plexgdm:
if "200 OK" in response.get('data'):
for each in response.get('data').split('\r\n'):
update['discovery'] = "auto"
update['owned']='1'
update['master']= 1
@ -230,6 +239,12 @@ class plexgdm:
elif "Server-Class:" in each:
update['class'] = each.split(':')[1].strip()
# Quickly test if we need https
if PMSHttpsEnabled(
'%s:%s' % (update['server'], update['port'])):
update['protocol'] = 'https'
else:
update['protocol'] = 'http'
discovered_servers.append(update)
self.server_list = discovered_servers
@ -239,7 +254,7 @@ class plexgdm:
else:
self.__printDebug("Number of servers Discovered: %s" % len(self.server_list),1)
for items in self.server_list:
self.__printDebug("Server Discovered: %s" % items['serverName'] ,2)
self.__printDebug("Server Discovered: %s" % items, 2)
def setInterval(self, interval):

View file

@ -46,23 +46,16 @@ class UserClient(threading.Thread):
threading.Thread.__init__(self)
def getAdditionalUsers(self):
additionalUsers = utils.settings('additionalUsers')
if additionalUsers:
self.AdditionalUser = additionalUsers.split(',')
def getUsername(self):
"""
Returns username as unicode
"""
username = utils.settings('username').decode('utf-8')
username = utils.settings('username')
if not username:
self.logMsg("No username saved, trying to get Plex username", 0)
username = utils.settings('plexLogin').decode('utf-8')
username = utils.settings('plexLogin')
if not username:
self.logMsg("Also no Plex username found", 0)
return ""
@ -245,9 +238,8 @@ class UserClient(threading.Thread):
log("Access is granted.", 1)
self.HasAccess = True
window('emby_serverStatus', clear=True)
xbmcgui.Dialog().notification(
self.addonName,
utils.language(33007).encode('utf-8'))
xbmcgui.Dialog().notification(self.addonName,
utils.language(33007))
def loadCurrUser(self, authenticated=False):
self.logMsg('Loading current user', 0)
@ -318,7 +310,8 @@ class UserClient(threading.Thread):
dialog = xbmcgui.Dialog()
# Get /profile/addon_data
addondir = xbmc.translatePath(self.addon.getAddonInfo('profile')).decode('utf-8')
addondir = xbmc.translatePath(
self.addon.getAddonInfo('profile')).decode('utf-8')
hasSettings = xbmcvfs.exists("%ssettings.xml" % addondir)
# If there's no settings.xml
@ -373,7 +366,7 @@ class UserClient(threading.Thread):
if username:
dialog.notification(
heading=self.addonName,
message=("Welcome " + username).encode('utf-8'),
message="Welcome " + username,
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png")
else:
dialog.notification(
@ -388,11 +381,11 @@ class UserClient(threading.Thread):
settings('userId', value="")
# Give attempts at entering password / selecting user
if self.retry >= 5:
log("Too many retries.", 1)
if self.retry >= 2:
log("Too many retries to login.", -1)
window('emby_serverStatus', value="Stop")
dialog.ok(lang(33001).encode('utf-8'),
lang(39023).encode('utf-8'))
dialog.ok(lang(33001),
lang(39023))
xbmc.executebuiltin(
'Addon.OpenSettings(plugin.video.plexkodiconnect)')
@ -423,7 +416,7 @@ class UserClient(threading.Thread):
while self.threadSuspended():
if self.threadStopped():
break
xbmc.sleep(3000)
xbmc.sleep(1000)
status = window('emby_serverStatus')
if status:
@ -455,5 +448,8 @@ class UserClient(threading.Thread):
log("Server found: %s" % server, 2)
self.auth = True
# Minimize CPU load
xbmc.sleep(500)
self.doUtils.stopSession()
log("##===---- UserClient Stopped ----===##", 0)

View file

@ -192,32 +192,47 @@ def logMsg(title, msg, level=1):
logLevel = int(window('emby_logLevel'))
except ValueError:
logLevel = 0
kodiLevel = {
-1: xbmc.LOGERROR,
0: xbmc.LOGNOTICE,
1: xbmc.LOGNOTICE,
2: xbmc.LOGNOTICE
}
if logLevel >= level:
if logLevel == 2: # inspect is expensive
func = inspect.currentframe().f_back.f_back.f_code
try:
xbmc.log("%s -> %s : %s" % (
title, func.co_name, msg))
title, func.co_name, msg), level=kodiLevel[level])
except UnicodeEncodeError:
try:
xbmc.log("%s -> %s : %s" % (
title, func.co_name, msg.encode('utf-8')))
title, func.co_name, msg.encode('utf-8')),
level=kodiLevel[level])
except:
xbmc.log("%s -> %s : %s" % (title, func.co_name, 'COULDNT LOG'))
xbmc.log("%s -> %s : %s" % (
title, func.co_name, 'COULDNT LOG'),
level=kodiLevel[level])
else:
try:
xbmc.log("%s -> %s" % (title, msg))
xbmc.log("%s -> %s" % (title, msg), level=kodiLevel[level])
except UnicodeEncodeError:
try:
xbmc.log("%s -> %s" % (title, msg.encode('utf-8')))
xbmc.log("%s -> %s" % (title, msg.encode('utf-8')),
level=kodiLevel[level])
except:
xbmc.log("%s -> %s " % (title, 'COULDNT LOG'))
xbmc.log("%s -> %s " % (title, 'COULDNT LOG'),
level=kodiLevel[level])
def window(property, value=None, clear=False, windowid=10000):
# Get or set window property
"""
Get or set window property - thread safe!
Returns unicode.
Property needs to be string; value may be string or unicode
"""
WINDOW = xbmcgui.Window(windowid)
#setproperty accepts both string and unicode but utf-8 strings are adviced by kodi devs because some unicode can give issues
@ -228,14 +243,13 @@ def window(property, value=None, clear=False, windowid=10000):
if clear:
WINDOW.clearProperty(property)
elif value is not None:
# Takes unicode or string by default!
WINDOW.setProperty(property, value)
else: #getproperty returns string so convert to unicode
return WINDOW.getProperty(property)
WINDOW.setProperty(property, value.encode('utf-8'))
else:
return WINDOW.getProperty(property).decode('utf-8')
def settings(setting, value=None):
"""
Get or add addon setting.
Get or add addon setting. Returns unicode
Settings needs to be string
Value can either be unicode or string
@ -244,10 +258,10 @@ def settings(setting, value=None):
if value is not None:
# Takes string or unicode by default!
addon.setSetting(setting, value)
addon.setSetting(setting, value.encode('utf-8'))
else:
# Returns unicode by default!
return addon.getSetting(setting)
# Should return unicode by default, but just in case
return addon.getSetting(setting).decode('utf-8')
def language(stringid):
# Central string retrieval
@ -443,7 +457,7 @@ def reset():
logMsg("PLEX", "Deleting: settings.xml", 1)
dialog.ok(
heading="Emby for Kodi",
heading=addonName,
line1="Database reset has completed, Kodi will now restart to apply the changes.")
xbmc.executebuiltin('RestartApp')

View file

@ -4,7 +4,6 @@
import shutil
import xml.etree.ElementTree as etree
from os import path as ospath
import xbmc
import xbmcvfs

View file

@ -21,12 +21,13 @@
<setting label="30517" type="action" action="RunPlugin(plugin://plugin.video.plexkodiconnect?mode=passwords)" option="close" /><!-- Network credentials -->
<setting id="accessToken" type="text" visible="false" default="" />
<setting id="pathsub" type="bool" visible="false" default="false" />
<setting label="39024" type="action" action="RunPlugin(plugin://plugin.video.plexkodiconnect?mode=reConnect)" option="close" />
</category>
<category label="plex.tv"><!-- plex.tv -->
<!-- Primary address -->
<setting id="myplexlogin" label="39025" type="bool" default="true" /> <!-- Log into plex.tv on startup -->
<setting id="doPlexTvLogin" label="39024" type="action" action="RunPlugin(plugin://plugin.video.plexkodiconnect?mode=doPlexTvLogin)" option="close" />
<setting label="39209" type="action" action="RunPlugin(plugin://plugin.video.plexkodiconnect?mode=reConnect)" option="close" />
<setting id="plexLogin" label="plex.tv username" type="text" default="" visible="false" />
<setting id="plexhome" label="Plex home in use" type="bool" default="" visible="false" />

View file

@ -171,8 +171,7 @@ class Service():
self.welcome_msg = False
xbmcgui.Dialog().notification(
heading=self.addonName,
message=("%s %s" % (lang(33000), user.currUser)
).encode('utf-8'),
message="%s %s" % (lang(33000), user.currUser),
icon="special://home/addons/plugin.video.plexkodiconnect/icon.png",
time=2000,
sound=False)
@ -225,8 +224,9 @@ class Service():
window('emby_online', value="false")
xbmcgui.Dialog().notification(
heading=lang(33001).encode('utf-8'),
message=("%s %s" % (self.addonName, lang(33002))).encode('utf-8'),
heading=lang(33001),
message="%s %s"
% (self.addonName, lang(33002)),
icon="special://home/addons/plugin.video."
"plexkodiconnect/icon.png",
sound=False)
@ -244,7 +244,7 @@ class Service():
# Alert the user that server is online.
xbmcgui.Dialog().notification(
heading=self.addonName,
message=lang(33003).encode('utf-8'),
message=lang(33003),
icon="special://home/addons/plugin.video."
"plexkodiconnect/icon.png",
time=2000,