From aaa31e82b0fdd42e5a6eb45485932ee27ab4aaca Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Thu, 1 Oct 2020 14:27:05 -1000 Subject: [PATCH] revert to Python2.x calls --- libs/actions.py | 14 +++++++------- libs/api_utils.py | 6 +++--- libs/cache.py | 5 +++-- libs/data_utils.py | 6 +++--- libs/debugger.py | 2 +- libs/settings.py | 6 +++--- libs/tmdb.py | 16 ++++++++-------- libs/utils.py | 4 +++- 8 files changed, 31 insertions(+), 28 deletions(-) diff --git a/libs/actions.py b/libs/actions.py index 3040903..b426bbf 100644 --- a/libs/actions.py +++ b/libs/actions.py @@ -22,7 +22,7 @@ from __future__ import absolute_import, unicode_literals -import sys, urllib.parse +import sys, urlparse import xbmcgui, xbmcplugin from . import tmdb, data_utils from .utils import logger, safe_get @@ -37,7 +37,7 @@ def find_show(title, year=None): # type: (Union[Text, bytes], Optional[Text]) -> None """Find a show by title""" - if not isinstance(title, str): + if not isinstance(title, unicode): title = title.decode('utf-8') logger.debug('Searching for TV show {} ({})'.format(title, year)) search_results = tmdb.search_show(title, year) @@ -123,13 +123,13 @@ def get_episode_list(show_id): # pylint: disable=missing-docstring for episode in show_info['episodes']: list_item = xbmcgui.ListItem(episode['name'], offscreen=True) list_item = data_utils.add_episode_info(list_item, episode, full_info=False) - encoded_ids = urllib.parse.urlencode( + encoded_ids = urlparse.urlencode( {'show_id': str(show_info['id']), 'episode_id': str(theindex)} ) theindex = theindex + 1 # Below "url" is some unique ID string (may be an actual URL to an episode page) # that allows to retrieve information about a specific episode. - url = urllib.parse.quote(encoded_ids) + url = urlparse.quote(encoded_ids) xbmcplugin.addDirectoryItem( HANDLE, url=url, @@ -140,8 +140,8 @@ def get_episode_list(show_id): # pylint: disable=missing-docstring def get_episode_details(encoded_ids): # pylint: disable=missing-docstring # type: (Text) -> None - encoded_ids = urllib.parse.unquote(encoded_ids) - decoded_ids = dict(urllib.parse.parse_qsl(encoded_ids)) + encoded_ids = urlparse.unquote(encoded_ids) + decoded_ids = dict(urlparse.parse_qsl(encoded_ids)) logger.debug('Getting episode details for {}'.format(decoded_ids)) episode_info = tmdb.load_episode_info( decoded_ids['show_id'], decoded_ids['episode_id'] @@ -181,7 +181,7 @@ def router(paramstring): :param paramstring: url-encoded query string :raises RuntimeError: on unknown call action """ - params = dict(urllib.parse.parse_qsl(paramstring)) + params = dict(urlparse.parse_qsl(paramstring)) logger.debug('Called addon with params: {}'.format(sys.argv)) if params['action'] == 'find': find_show(params['title'], params.get('year')) diff --git a/libs/api_utils.py b/libs/api_utils.py index b3ec045..bc91a9f 100644 --- a/libs/api_utils.py +++ b/libs/api_utils.py @@ -20,9 +20,9 @@ from __future__ import absolute_import, unicode_literals import json -from urllib.request import Request, urlopen -from urllib.error import URLError -from urllib.parse import urlencode +from urllib2 import Request, urlopen +from urllib2 import URLError +from urllib import urlencode from pprint import pformat from . import settings from .utils import logger diff --git a/libs/cache.py b/libs/cache.py index 03a1e31..2efd6b6 100644 --- a/libs/cache.py +++ b/libs/cache.py @@ -22,7 +22,8 @@ from __future__ import absolute_import, unicode_literals -import os, pickle +import os +import cPickle as pickle from datetime import datetime, timedelta import xbmc, xbmcvfs @@ -40,6 +41,7 @@ def _get_cache_directory(): # pylint: disable=missing-docstring # type: () -> Text profile_dir = xbmc.translatePath(ADDON.getAddonInfo('profile')) + profile_dir = profile_dir.decode('utf-8') cache_dir = os.path.join(profile_dir, 'cache') if not xbmcvfs.exists(cache_dir): xbmcvfs.mkdir(cache_dir) @@ -75,7 +77,6 @@ def load_show_info_from_cache(show_id): try: with open(os.path.join(CACHE_DIR, file_name), 'rb') as fo: load_kwargs = {} - load_kwargs['encoding'] = 'bytes' cache = pickle.load(fo, **load_kwargs) if datetime.now() - cache['timestamp'] > CACHING_DURATION: return None diff --git a/libs/data_utils.py b/libs/data_utils.py index b727b25..acdcb98 100644 --- a/libs/data_utils.py +++ b/libs/data_utils.py @@ -113,7 +113,7 @@ def _set_unique_ids(ext_ids, list_item): # type: (InfoType, ListItem) -> ListItem """Extract unique ID in various online databases""" unique_ids = {} - for key, value in ext_ids.items(): + for key, value in ext_ids.iteritems(): if key in VALIDEXTIDS and value: key = key[:-3] unique_ids[key] = str(value) @@ -145,7 +145,7 @@ def _add_season_info(show_info, list_item): if image: url = settings.IMAGEROOTURL + image list_item.addAvailableArtwork(url, 'poster', season=season['season_number']) - for image_type, image_list in season.get('images', {}).items(): + for image_type, image_list in season.get('images', {}).iteritems(): if image_type == 'posters': destination = 'poster' else: @@ -162,7 +162,7 @@ def _add_season_info(show_info, list_item): def set_show_artwork(show_info, list_item): # type: (InfoType, ListItem) -> ListItem """Set available images for a show""" - for image_type, image_list in show_info.get('images', {}).items(): + for image_type, image_list in show_info.get('images', {}).iteritems(): if image_type == 'backdrops': fanart_list = [] for image in image_list: diff --git a/libs/debugger.py b/libs/debugger.py index fd63eff..417e909 100644 --- a/libs/debugger.py +++ b/libs/debugger.py @@ -49,7 +49,7 @@ def _format_vars(variables): :return: formatted string with sorted ``var = val`` pairs :rtype: str """ - var_list = [(var, val) for var, val in variables.items() + var_list = [(var, val) for var, val in variables.iteritems() if not (var.startswith('__') or var.endswith('__'))] var_list.sort(key=lambda i: i[0]) lines = [] diff --git a/libs/settings.py b/libs/settings.py index 96cba24..79560f2 100644 --- a/libs/settings.py +++ b/libs/settings.py @@ -16,7 +16,7 @@ # along with this program. If not, see . # pylint: disable=missing-docstring -import json, sys, urllib.parse +import json, sys, urlparse from .utils import logger from pprint import pformat @@ -40,7 +40,7 @@ 'seasonthumb': 'seasonlandscape' } try: - source_params = dict(urllib.parse.parse_qsl(sys.argv[2])) + source_params = dict(urlparse.parse_qsl(sys.argv[2])) except IndexError: source_params = {} source_settings = json.loads(source_params.get('pathSettings', {})) @@ -65,5 +65,5 @@ RATING_TYPES.append('tmdb') FANARTTV_CLIENTKEY = source_settings.get('fanarttv_clientkey', '') FANARTTV_ART = {} -for fanarttv_type, tmdb_type in FANARTTV_MAPPING.items(): +for fanarttv_type, tmdb_type in FANARTTV_MAPPING.iteritems(): FANARTTV_ART[tmdb_type] = source_settings.get('enable_fanarttv_%s' % tmdb_type, False) diff --git a/libs/tmdb.py b/libs/tmdb.py index c3ee3ff..3dcf1d7 100644 --- a/libs/tmdb.py +++ b/libs/tmdb.py @@ -121,7 +121,7 @@ def load_episode_list(show_info, season_map, ep_grouping): else: logger.debug('Getting episodes from standard season list') show_info['seasons'] = [] - for key, value in season_map.items(): + for key, value in season_map.iteritems(): show_info['seasons'].append(value) for season in show_info.get('seasons', []): for episode in season.get('episodes', []): @@ -258,7 +258,7 @@ def load_fanarttv_art(show_info): artwork = api_utils.load_info(fanarttv_url, params=FANARTTV_PARAMS) if artwork is None: return show_info - for fanarttv_type, tmdb_type in settings.FANARTTV_MAPPING.items(): + for fanarttv_type, tmdb_type in settings.FANARTTV_MAPPING.iteritems(): if settings.FANARTTV_ART[tmdb_type]: if not show_info['images'].get(tmdb_type) and not tmdb_type.startswith('season'): show_info['images'][tmdb_type] = [] @@ -297,7 +297,7 @@ def trim_artwork(show_info): image_counts = {} image_total = 0 backdrops_total = 0 - for image_type, image_list in show_info.get('images', {}).items(): + for image_type, image_list in show_info.get('images', {}).iteritems(): total = len(image_list) if image_type == 'backdrops': backdrops_total = backdrops_total + total @@ -305,7 +305,7 @@ def trim_artwork(show_info): image_counts[image_type] = {'total':total} image_total = image_total + total for season in show_info.get('seasons', []): - for image_type, image_list in season.get('images', {}).items(): + for image_type, image_list in season.get('images', {}).iteritems(): total = len(image_list) thetype = '%s_%s' % (str(season['season_number']), image_type) image_counts[thetype] = {'total':total} @@ -321,7 +321,7 @@ def trim_artwork(show_info): reduction = (image_total - settings.MAXIMAGES)/image_total logger.error('there are %s non-fanart images' % str(image_total)) logger.error('that is more than the max of %s, image results will be trimmed by %s' % (str(settings.MAXIMAGES), str(reduction))) - for key, value in image_counts.items(): + for key, value in image_counts.iteritems(): total = value['total'] reduce = int(floor(total * reduction)) target = total - reduce @@ -331,14 +331,14 @@ def trim_artwork(show_info): reduce = -1 * reduce image_counts[key]['reduce'] = reduce logger.debug('%s: %s' % (key, pformat(image_counts[key]))) - for image_type, image_list in show_info.get('images', {}).items(): + for image_type, image_list in show_info.get('images', {}).iteritems(): if image_type == 'backdrops': continue # already handled backdrops above reduce = image_counts[image_type]['reduce'] if reduce != 0: del show_info['images'][image_type][reduce:] for s in range(len(show_info.get('seasons', []))): - for image_type, image_list in show_info['seasons'][s].get('images', {}).items(): + for image_type, image_list in show_info['seasons'][s].get('images', {}).iteritems(): thetype = '%s_%s' % (str(show_info['seasons'][s]['season_number']), image_type) reduce = image_counts[thetype]['reduce'] if reduce != 0: @@ -347,7 +347,7 @@ def trim_artwork(show_info): def _sort_image_types(imagelist): - for image_type, images in imagelist.items(): + for image_type, images in imagelist.iteritems(): imagelist[image_type] = _image_sort(images) return imagelist diff --git a/libs/utils.py b/libs/utils.py index 48961b4..85722d2 100644 --- a/libs/utils.py +++ b/libs/utils.py @@ -38,8 +38,10 @@ class logger: @staticmethod def log(message, level=xbmc.LOGDEBUG): # type: (Text, int) -> None - if isinstance(message, bytes): + if isinstance(message, str): message = message.decode('utf-8') + if isinstance(message, unicode): + message = message.encode('utf-8') message = logger.log_message_prefix + message xbmc.log(message, level)