diff --git a/resources/lib/b808common/__init__.py b/resources/lib/b808common/__init__.py index 6dd44b0..bb23fea 100644 --- a/resources/lib/b808common/__init__.py +++ b/resources/lib/b808common/__init__.py @@ -1,2 +1,2 @@ #dummy file to make this a package -from b808common import * +from .b808common import * diff --git a/resources/lib/b808common/b808common.py b/resources/lib/b808common/b808common.py index 78af863..f6776bd 100644 --- a/resources/lib/b808common/b808common.py +++ b/resources/lib/b808common/b808common.py @@ -11,7 +11,7 @@ import xbmcplugin import xbmcvfs import xbmcgui -import urllib +import urllib.request, urllib.parse, urllib.error import sys import os import platform @@ -32,15 +32,17 @@ def log(message, inst=None, level=xbmc.LOGDEBUG): if isinstance (message,str): - message = message.decode("utf-8") - message = u'### %s - %s ### %s' % (ADDONNAME,VERSION, message) + #message = message.decode("utf-8") + message = '### %s - %s ### %s' % (ADDONNAME,VERSION, message) else: - message = u'### %s - %s ### %s' % (ADDONNAME,VERSION, message) + message = '### %s - %s ### %s' % (ADDONNAME,VERSION, message) if inst is None: - xbmc.log(message.encode("utf-8"), level ) + #xbmc.log(message.encode("utf-8"), level ) + xbmc.log(message, level ) else: - xbmc.log(message.encode("utf-8"), level ) + #xbmc.log(message.encode("utf-8"), level ) + xbmc.log(message, level ) xbmc.log("### " + ADDONNAME + "-" + VERSION + " ### Exception:" + format_exc(inst), level ) #log something even if debug logging is off - for important stuff only! @@ -106,7 +108,7 @@ def frontPadTo9Chars(shortStr): # Reverse the key value pairs in a dict def swap_dictionary(original_dict): - return dict([(v, k) for (k, v) in original_dict.iteritems()]) + return dict([(v, k) for (k, v) in list(original_dict.items())]) ################################################################################ @@ -143,17 +145,17 @@ def unquoteUni(text): #return urllib.unquote(text) _hexdig = '0123456789ABCDEFabcdef' _hextochr = dict((a+b, chr(int(a+b,16))) for a in _hexdig for b in _hexdig) - if isinstance(text, unicode): + if isinstance(text, str): text = text.encode('utf-8') res = text.split('%') - for i in xrange(1, len(res)): + for i in range(1, len(res)): item = res[i] try: res[i] = _hextochr[item[:2]] + item[2:] except KeyError: res[i] = '%' + item except UnicodeDecodeError: - res[i] = unichr(int(item[:2], 16)) + item[2:] + res[i] = chr(int(item[:2], 16)) + item[2:] return "".join(res) ############################################################################## @@ -183,7 +185,7 @@ def getParams(): # Build a plugin URL with urlencoded parameters def buildPluginURL(params): - return PLUGINSTUB + urllib.urlencode(params) + return PLUGINSTUB + urllib.parse.urlencode(params) ################################################################################ # Strip given chararacters from all members of a given list @@ -322,6 +324,3 @@ def getThumbnailModeID(): XBMC_VERSION = "P*" if VERSION_NUMBER >= 22.9: XBMC_VERSION = ">P" - - - diff --git a/resources/lib/bom.py b/resources/lib/bom.py index 094a1ef..1d5a94e 100644 --- a/resources/lib/bom.py +++ b/resources/lib/bom.py @@ -21,7 +21,7 @@ import os, sys, shutil import time import ftplib -import urllib, urllib2 +import urllib.request, urllib.parse, urllib.error, urllib3 try: from xbmc import log as log @@ -41,7 +41,7 @@ def log(str): def downloadBackground(radarCode, fileName, backgroundsPath): # Needed due to bug in python 2.7 urllib - https://stackoverflow.com/questions/44733710/downloading-second-file-from-ftp-fails - urllib.urlcleanup() + urllib.request.urlcleanup() outFileName = fileName @@ -76,21 +76,42 @@ def downloadBackground(radarCode, fileName, backgroundsPath): log("Downloading missing background image....[%s] as [%s]" % (fileName, outFileName)) #ok get ready to retrieve some images - image = urllib.URLopener() + #image = urllib.request.URLopener() imageFileIndexed = backgroundsPath + "idx." + fileName imageFileRGB = backgroundsPath + outFileName + USERAGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.1) Gecko/2008070208 Firefox/3.6" + headers = urllib3.util.request.make_headers(accept_encoding='gzip, deflate', keep_alive=True, user_agent=USERAGENT) + #special case for national radar background (already an RGB image) if "background.png" in fileName and '00004' in fileName: - image.retrieve(FTPSTUB + 'IDE00035.background.png', imageFileRGB ) + http = urllib3.PoolManager() + r = http.request('GET', HTTPSTUB + 'IDE00035.background.png', preload_content=False, headers=headers) + with open(imageFileRGB, 'wb') as out: + while True: + data = r.read(65536) + if not data: + break + out.write(data) + r.release_conn() + #image.retrieve(HTTPSTUB + 'IDE00035.background.png', imageFileRGB ) log("Got IDE00035.background.png as " + outFileName) #all other images...need to be converted from indexed colour to RGB else: try: #log(FTPSTUB + fileName) - image.retrieve(FTPSTUB + fileName, imageFileIndexed ) + #image.retrieve(HTTPSTUB + fileName, imageFileIndexed ) + http = urllib3.PoolManager() + r = http.request('GET', HTTPSTUB + fileName, preload_content=False, headers=headers) + with open(imageFileRGB, 'wb') as out: + while True: + data = r.read(65536) + if not data: + break + out.write(data) + r.release_conn() except Exception as inst: - log("ftp failed with error: " + str(inst)) + log("http failed with error: " + str(inst)) try: log("Downloaded background texture...now converting from indexed file [" + imageFileIndexed + "] to RGB: " + fileName) @@ -230,7 +251,7 @@ def buildImages(radarCode, updateRadarBackgrounds, backgroundsPath, overlayLoopP log("Retrieving new radar image: " + imageToRetrieve) log("Output to file: " + outputFile) try: - radarImage = urllib2.urlopen(imageToRetrieve) + radarImage = urllib.request.urlopen(imageToRetrieve) fh = open( overlayLoopPath + "/" + outputFile , "wb") fh.write(radarImage.read()) fh.close() @@ -277,7 +298,3 @@ def buildImages(radarCode, updateRadarBackgrounds, backgroundsPath, overlayLoopP buildImages(radarCode, True, backgroundsPath, overlayLoopPath) log(os.listdir(backgroundsPath)) log(os.listdir(overlayLoopPath)) - - - - \ No newline at end of file diff --git a/resources/lib/weatherzone.py b/resources/lib/weatherzone.py index 283db95..5700838 100644 --- a/resources/lib/weatherzone.py +++ b/resources/lib/weatherzone.py @@ -19,7 +19,7 @@ import requests import re from bs4 import BeautifulSoup -from urlparse import urlparse +from urllib.parse import urlparse import datetime try: @@ -649,7 +649,7 @@ def getWeatherData(urlPath, extendedFeatures=True, XBMC_VERSION=17.0): if header is not None and header.text == "Wind Speed": windSpeedData = row.find_all("td") - for i in xrange(0, len(windSpeedData), 2): + for i in range(0,len(windSpeedData),2): windSpeeds9am.append(windSpeedData[i].text) windSpeeds3pm.append(windSpeedData[i + 1].text) except Exception as inst: @@ -663,18 +663,18 @@ def getWeatherData(urlPath, extendedFeatures=True, XBMC_VERSION=17.0): try: header = row.find("th") if header is not None and header.text == "Wind Direction": + windDirectionData = row.find_all("td") + for i in range(0,len(windDirectionData),2): + windDirections9am.append(windDirectionData[i].text.replace("\n","")) + windDirections3pm.append(windDirectionData[i+1].text.replace("\n","")) - windDirectionData = row.find_all("td") - for i in xrange(0, len(windDirectionData), 2): - windDirections9am.append(windDirectionData[i].text.replace("\n", "")) - windDirections3pm.append(windDirectionData[i + 1].text.replace("\n", "")) except Exception as inst: log(str(inst)) windDirections9am.append("?") windDirections3pm.append("?") # Now join the stored wind data and set it... - for i in xrange(0, len(windSpeeds9am)): + for i in range(0,len(windSpeeds9am)): # setKey(i, "WindSpeed", "9am - " + windSpeeds9am[i] + ", 3pm - " + windSpeeds3pm[i]) setKey(i, "WindSpeed", windSpeeds3pm[i]) # setKey(i, "WindDirection", "9am - " + windDirections9am[i] + ", 3pm - " + windDirections3pm[i])