From 1f3598a7c4b8e08f78e1824e2ea61ca910e01897 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Wed, 6 Apr 2016 18:23:51 +0200 Subject: [PATCH] Cleanup and Fixes - Disable SSL warnings, finally - Fix Plex Companion errors due to downloadutils switch --- resources/lib/PlexAPI.py | 68 +--------------------- resources/lib/PlexFunctions.py | 9 +-- resources/lib/artwork.py | 6 +- resources/lib/downloadutils.py | 23 +++++--- resources/lib/entrypoint.py | 4 +- resources/lib/image_cache_thread.py | 6 +- resources/lib/musicutils.py | 4 +- resources/lib/plexbmchelper/functions.py | 6 +- resources/lib/plexbmchelper/httppersist.py | 1 + resources/lib/plexbmchelper/listener.py | 11 ++-- resources/lib/plexbmchelper/subscribers.py | 15 ++--- resources/lib/utils.py | 2 +- 12 files changed, 48 insertions(+), 107 deletions(-) diff --git a/resources/lib/PlexAPI.py b/resources/lib/PlexAPI.py index 0e52015f..c831a5c1 100644 --- a/resources/lib/PlexAPI.py +++ b/resources/lib/PlexAPI.py @@ -33,27 +33,21 @@ import time import urllib2 import socket from threading import Thread -import requests import xml.etree.ElementTree as etree - import re import json from urllib import urlencode, quote_plus, unquote -import clientinfo -import utils -import downloadutils import xbmcaddon import xbmcgui import xbmc import xbmcvfs +import clientinfo +import utils +import downloadutils from PlexFunctions import PlexToKodiTimefactor, PMSHttpsEnabled -# Disable requests logging -from requests.packages.urllib3.exceptions import InsecureRequestWarning -requests.packages.urllib3.disable_warnings(InsecureRequestWarning) - @utils.logging class PlexAPI(): @@ -258,62 +252,6 @@ class PlexAPI(): self.logMsg('Successfully retrieved code and id from plex.tv', 1) return code, identifier - def TalkToPlexServer(self, url, talkType="GET", verify=True, token=None): - """ - Start request with PMS with url. - - 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. - """ - header = self.getXArgsDeviceInfo() - if token: - header['X-Plex-Token'] = token - timeout = (3, 10) - try: - if talkType == "GET": - answer = requests.get(url, - headers={}, - params=header, - verify=verify, - timeout=timeout) - # Only seems to be used for initial plex.tv sign in - 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. " - "Error message: %s" - % (url, e), -1) - return False - except requests.exceptions.ReadTimeout: - 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: - 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 - def CheckConnection(self, url, token=None, verifySSL=None): """ Checks connection to a Plex server, available at url. Can also be used diff --git a/resources/lib/PlexFunctions.py b/resources/lib/PlexFunctions.py index db977784..36d9a6a0 100644 --- a/resources/lib/PlexFunctions.py +++ b/resources/lib/PlexFunctions.py @@ -4,19 +4,12 @@ from ast import literal_eval from urlparse import urlparse, parse_qs import re from copy import deepcopy -import requests - -from xbmcaddon import Addon import downloadutils from utils import logMsg, settings -# Disable requests logging -from requests.packages.urllib3.exceptions import InsecureRequestWarning -requests.packages.urllib3.disable_warnings(InsecureRequestWarning) - -addonName = Addon().getAddonInfo('name') +addonName = 'PlexKodiConnect' title = "%s %s" % (addonName, __name__) diff --git a/resources/lib/artwork.py b/resources/lib/artwork.py index 3b2c21f3..3631536c 100644 --- a/resources/lib/artwork.py +++ b/resources/lib/artwork.py @@ -15,9 +15,9 @@ import xbmcvfs import utils import image_cache_thread -# Disable requests logging -from requests.packages.urllib3.exceptions import InsecureRequestWarning -requests.packages.urllib3.disable_warnings(InsecureRequestWarning) +# Disable annoying requests warnings +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() ############################################################################### diff --git a/resources/lib/downloadutils.py b/resources/lib/downloadutils.py index c1c6ce30..1f094ba1 100644 --- a/resources/lib/downloadutils.py +++ b/resources/lib/downloadutils.py @@ -10,10 +10,9 @@ import clientinfo ############################################################################### -# Disable requests logging -from requests.packages.urllib3.exceptions import InsecureRequestWarning -requests.packages.urllib3.disable_warnings(InsecureRequestWarning) -# logging.getLogger('requests').setLevel(logging.WARNING) +# Disable annoying requests warnings +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() ############################################################################### @@ -263,10 +262,18 @@ class DownloadUtils(): r = r.json() return r except: - self.logMsg("Unable to convert the response for: %s" - % url, -1) - self.logMsg("Received headers were: %s" % r.headers, -1) - return False + r.encoding = 'utf-8' + if '200 OK' in r.text: + # Received fucked up OK from PMS on playstate update + pass + else: + self.logMsg("Unable to convert the response for: %s" + % url, -1) + self.logMsg("Received headers were: %s" + % r.headers, -1) + self.logMsg('Received text:', -1) + self.logMsg(r.text, -1) + return True else: self.logMsg('Unknown answer from PMS %s with status code %s. ' 'Message:' % (url, r.status_code), -1) diff --git a/resources/lib/entrypoint.py b/resources/lib/entrypoint.py index 9253d688..fccab5d1 100644 --- a/resources/lib/entrypoint.py +++ b/resources/lib/entrypoint.py @@ -28,14 +28,12 @@ import PlexAPI ############################################################################### # For logging only -addonName = clientinfo.ClientInfo().getAddonName() +addonName = 'PlexKodiConnect' title = "%s %s" % (addonName, __name__) def plexCompanion(fullurl, params): params = PlexFunctions.LiteralEval(params[26:]) - utils.logMsg("entrypoint - plexCompanion", - "params is: %s" % params, -1) if (params['machineIdentifier'] != utils.window('plex_machineIdentifier')): diff --git a/resources/lib/image_cache_thread.py b/resources/lib/image_cache_thread.py index d40164e7..0f589eaa 100644 --- a/resources/lib/image_cache_thread.py +++ b/resources/lib/image_cache_thread.py @@ -3,9 +3,9 @@ import utils import xbmc import requests -# Disable requests logging -from requests.packages.urllib3.exceptions import InsecureRequestWarning -requests.packages.urllib3.disable_warnings(InsecureRequestWarning) +# Disable annoying requests warnings +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() @utils.logging diff --git a/resources/lib/musicutils.py b/resources/lib/musicutils.py index 97db089d..1f977781 100644 --- a/resources/lib/musicutils.py +++ b/resources/lib/musicutils.py @@ -5,13 +5,11 @@ import os import xbmc -import xbmcaddon import xbmcvfs -from mutagen.flac import FLAC, Picture +from mutagen.flac import FLAC from mutagen.id3 import ID3 from mutagen import id3 -import base64 import read_embyserver as embyserver import utils diff --git a/resources/lib/plexbmchelper/functions.py b/resources/lib/plexbmchelper/functions.py index 64508825..df5ff228 100644 --- a/resources/lib/plexbmchelper/functions.py +++ b/resources/lib/plexbmchelper/functions.py @@ -1,7 +1,9 @@ import base64 import json import string + import xbmc + from utils import logging @@ -48,7 +50,7 @@ def plex_type(xbmc_type): def getXMLHeader(): - return ''+"\r\n" + return '\r\n' def getOKMsg(): @@ -131,7 +133,7 @@ class jsonClass(): "X-Plex-Product": "PlexKodiConnect", "X-Plex-Device-Name": self.settings['client_name'], "X-Plex-Platform": "Kodi", - "X-Plex-Model": getPlatform(), + "X-Plex-Model": self.settings['platform'], "X-Plex-Device": "PC", } if self.settings['myplex_user']: diff --git a/resources/lib/plexbmchelper/httppersist.py b/resources/lib/plexbmchelper/httppersist.py index 75477bff..8f9a9fe4 100644 --- a/resources/lib/plexbmchelper/httppersist.py +++ b/resources/lib/plexbmchelper/httppersist.py @@ -3,6 +3,7 @@ import traceback import string import errno from socket import error as socket_error + from utils import logging diff --git a/resources/lib/plexbmchelper/listener.py b/resources/lib/plexbmchelper/listener.py index e2685bab..f92069fe 100644 --- a/resources/lib/plexbmchelper/listener.py +++ b/resources/lib/plexbmchelper/listener.py @@ -17,7 +17,6 @@ class MyHandler(BaseHTTPRequestHandler): def __init__(self, *args, **kwargs): BaseHTTPRequestHandler.__init__(self, *args, **kwargs) self.serverlist = [] - self.settings = self.server.settings def getServerByHost(self, host): if len(self.serverlist) == 1: @@ -39,7 +38,7 @@ class MyHandler(BaseHTTPRequestHandler): def do_OPTIONS(s): s.send_response(200) s.send_header('Content-Length', '0') - s.send_header('X-Plex-Client-Identifier', s.settings['uuid']) + s.send_header('X-Plex-Client-Identifier', s.server.settings['uuid']) s.send_header('Content-Type', 'text/plain') s.send_header('Connection', 'close') s.send_header('Access-Control-Max-Age', '1209600') @@ -71,6 +70,8 @@ class MyHandler(BaseHTTPRequestHandler): s.serverlist = s.server.client.getServerList() s.subMgr = s.server.subscriptionManager s.js = s.server.jsonClass + s.settings = s.server.settings + try: request_path = s.path[1:] request_path = re.sub(r"\?.*", "", request_path) @@ -97,7 +98,7 @@ class MyHandler(BaseHTTPRequestHandler): resp += ' protocolCapabilities="navigation,playback,timeline"' resp += ' machineIdentifier="%s"' % s.settings['uuid'] resp += ' product="PlexKodiConnect"' - resp += ' platform="%s"' % getPlatform() + resp += ' platform="%s"' % s.settings['platform'] resp += ' platformVersion="%s"' % s.settings['plexbmc_version'] resp += ' deviceClass="pc"' resp += "/>" @@ -215,8 +216,10 @@ class MyHandler(BaseHTTPRequestHandler): elif request_path == "player/navigation/back": s.response(getOKMsg(), s.js.getPlexHeaders()) s.js.jsonrpc("Input.Back") + except: - traceback.print_exc() + s.logMsg('Error encountered. Traceback:', -1) + s.logMsg(traceback.print_exc(), -1) class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): diff --git a/resources/lib/plexbmchelper/subscribers.py b/resources/lib/plexbmchelper/subscribers.py index 9c4caf10..eba693bf 100644 --- a/resources/lib/plexbmchelper/subscribers.py +++ b/resources/lib/plexbmchelper/subscribers.py @@ -1,11 +1,12 @@ import re import threading -from functions import * from xbmc import Player + import downloadutils from utils import window, logging import PlexFunctions as pf +from functions import * @logging @@ -23,7 +24,7 @@ class SubscriptionManager: self.protocol = "http" self.port = "" self.playerprops = {} - self.download = downloadutils.DownloadUtils() + self.doUtils = downloadutils.DownloadUtils().downloadUrl self.xbmcplayer = Player() self.js = jsonClass @@ -167,7 +168,7 @@ class SubscriptionManager: url = serv.get('protocol', 'http') + '://' \ + serv.get('server', 'localhost') + ':' \ + serv.get('port', '32400') + "/:/timeline" - self.download.downloadUrl(url, type="GET", parameters=params) + self.doUtils(url, type="GET", parameters=params) # requests.getwithparams(serv.get('server', 'localhost'), serv.get('port', 32400), "/:/timeline", params, getPlexHeaders(), serv.get('protocol', 'http')) self.logMsg("params: %s" % params, 2) self.logMsg("players: %s" % players, 2) @@ -244,7 +245,7 @@ class Subscriber: self.commandID = int(commandID) or 0 self.navlocationsent = False self.age = 0 - self.download = downloadutils.DownloadUtils() + self.doUtils = downloadutils.DownloadUtils().downloadUrl self.subMgr = subMgr self.RequestMgr = RequestMgr @@ -278,8 +279,8 @@ class Subscriber: Threaded POST request, because they stall due to PMS response missing the Content-Length header :-( """ - response = self.download.downloadUrl(url, - postBody=msg, - type="POST") + response = self.doUtils(url, + postBody=msg, + type="POST") if response in [False, None, 401]: self.subMgr.removeSubscriber(self.uuid) diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 5dfd748f..2865f2e2 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -23,7 +23,7 @@ import xbmcvfs ############################################################################### -addonName = xbmcaddon.Addon().getAddonInfo('name') +addonName = 'PlexKodiConnect' def DateToKodi(stamp):