Cleanup PlexFunctions.py

This commit is contained in:
tomkat83 2016-09-02 17:26:17 +02:00
parent fed0c0b9e4
commit 136f0b3157

View file

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging
from urllib import urlencode from urllib import urlencode
from ast import literal_eval from ast import literal_eval
from urlparse import urlparse, parse_qsl from urlparse import urlparse, parse_qsl
@ -6,11 +7,15 @@ import re
from copy import deepcopy from copy import deepcopy
import downloadutils import downloadutils
from utils import logMsg, settings from utils import settings
###############################################################################
log = logging.getLogger("PLEX."+__name__)
addonName = 'PlexKodiConnect' addonName = 'PlexKodiConnect'
title = "%s %s" % (addonName, __name__)
###############################################################################
def PlexToKodiTimefactor(): def PlexToKodiTimefactor():
@ -193,7 +198,7 @@ def GetPlexMetadata(key):
xml.attrib xml.attrib
# Nope we did not receive a valid XML # Nope we did not receive a valid XML
except AttributeError: except AttributeError:
logMsg(title, "Error retrieving metadata for %s" % url, -1) log.error("Error retrieving metadata for %s" % url)
xml = None xml = None
return xml return xml
@ -244,7 +249,7 @@ def DownloadChunks(url, containerSize):
xml.attrib xml.attrib
except AttributeError: except AttributeError:
# Nope, not an XML, abort # Nope, not an XML, abort
logMsg(title, "Error getting url %s" % url[:-1], -1) log.error("Error getting url %s" % url[:-1])
return None return None
else: else:
return xml return xml
@ -263,8 +268,8 @@ def DownloadChunks(url, containerSize):
try: try:
xmlpart.attrib xmlpart.attrib
except AttributeError: except AttributeError:
logMsg(title, 'Error while downloading chunks: %s' log.error('Error while downloading chunks: %s'
% (url + urlencode(args)), -1) % (url + urlencode(args)))
pos += containerSize pos += containerSize
errorCounter += 1 errorCounter += 1
continue continue
@ -285,7 +290,7 @@ def DownloadChunks(url, containerSize):
break break
pos += containerSize pos += containerSize
if errorCounter == 10: if errorCounter == 10:
logMsg(title, 'Fatal error while downloading chunks for %s' % url, -1) log.error('Fatal error while downloading chunks for %s' % url)
return None return None
return xml return xml
@ -354,7 +359,7 @@ def GetPlexCollections(mediatype):
try: try:
xml.attrib xml.attrib
except AttributeError: except AttributeError:
logMsg(title, 'Could not download PMS sections for %s' % url, -1) log.error('Could not download PMS sections for %s' % url)
return {} return {}
for item in xml: for item in xml:
contentType = item['type'] contentType = item['type']
@ -381,8 +386,8 @@ def GetPlexPlaylist(itemid, librarySectionUUID, mediatype='movie'):
url = "{server}/playQueues" url = "{server}/playQueues"
args = { args = {
'type': mediatype, 'type': mediatype,
'uri': 'library://' + librarySectionUUID + 'uri': 'library://%s/item/%2Flibrary%2Fmetadata%2F%s'
'/item/%2Flibrary%2Fmetadata%2F' + itemid, % (librarySectionUUID, itemid),
'includeChapters': '1', 'includeChapters': '1',
'extrasPrefixCount': trailerNumber, 'extrasPrefixCount': trailerNumber,
'shuffle': '0', 'shuffle': '0',
@ -393,7 +398,7 @@ def GetPlexPlaylist(itemid, librarySectionUUID, mediatype='movie'):
try: try:
xml[0].tag xml[0].tag
except (IndexError, TypeError, AttributeError): except (IndexError, TypeError, AttributeError):
logMsg(title, "Error retrieving metadata for %s" % url, -1) log.error("Error retrieving metadata for %s" % url)
return None return None
return xml return xml
@ -433,7 +438,7 @@ def PMSHttpsEnabled(url):
try: try:
res.attrib res.attrib
except AttributeError: except AttributeError:
logMsg(title, "Could not contact PMS %s" % url, -1) log.error("Could not contact PMS %s" % url)
return None return None
else: else:
# Received a valid XML. Server wants to talk HTTP # Received a valid XML. Server wants to talk HTTP
@ -456,11 +461,10 @@ def GetMachineIdentifier(url):
try: try:
machineIdentifier = xml.attrib['machineIdentifier'] machineIdentifier = xml.attrib['machineIdentifier']
except (AttributeError, KeyError): except (AttributeError, KeyError):
logMsg(title, 'Could not get the PMS machineIdentifier for %s' log.error('Could not get the PMS machineIdentifier for %s' % url)
% url, -1)
return None return None
logMsg(title, 'Found machineIdentifier %s for the PMS %s' log.debug('Found machineIdentifier %s for the PMS %s'
% (machineIdentifier, url), 1) % (machineIdentifier, url))
return machineIdentifier return machineIdentifier
@ -521,4 +525,4 @@ def scrobble(ratingKey, state):
else: else:
return return
downloadutils.DownloadUtils().downloadUrl(url) downloadutils.DownloadUtils().downloadUrl(url)
logMsg(title, "Toggled watched state for Plex item %s" % ratingKey, 1) log.info("Toggled watched state for Plex item %s" % ratingKey)