From c723ced0c6ed84768fe1d478a8dcf48df979883e Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 11 Apr 2021 08:44:18 +0000 Subject: [PATCH] Fix the selection of instances When a new instance was selected from the list of instances, it had no effect because the new instance URL was saved in an attribute that was reset at the next call of the add-on. Now when the user selects a new instance, the associated setting is updated so that this value can be reused the next time the add-on is called or started. Also took this opportunity to refactor the access to the add-on's settings: there are now wrapper methods in kodi_utils.py which encapsulates the call to Kodi APIs to make the code simpler. See merge request StCyr/plugin.video.peertube!10 for more information --- README.md | 4 ++-- peertube.py | 41 +++++++++++++++++++++---------------- resources/lib/kodi_utils.py | 22 ++++++++++++++++++-- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 4a0a494..f7ec5b8 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ to improve it. * Browse all videos on a PeerTube instance * Search for videos on a PeerTube instance -* Select PeerTube instance to use (**doesn't work yet**) +* Select the PeerTube instance to use * Select the preferred video resolution: the plugin will try to play the select video resolution. If it's not available, it will play the lower resolution that is the closest @@ -53,7 +53,7 @@ the add-on with: * The add-on doesn't delete the downloaded files at the moment. So, it may fill up your disk. -# Installation and requirements +# Installation and prerequisites Please read the [wiki](https://framagit.org/StCyr/plugin.video.peertube/-/wikis/home) diff --git a/peertube.py b/peertube.py index 26d16e2..7490e0c 100644 --- a/peertube.py +++ b/peertube.py @@ -26,8 +26,9 @@ import xbmcaddon import xbmcgui import xbmcplugin -from resources.lib.kodi_utils import debug, get_property, notif_error, \ - notif_info, notif_warning, open_dialog +from resources.lib.kodi_utils import ( + debug, get_property, get_setting, notif_error, notif_info, notif_warning, + open_dialog, set_setting) class PeertubeAddon(): @@ -44,29 +45,26 @@ class PeertubeAddon(): :param plugin, plugin_id: str, int """ - # These 2 steps must be done first since the logging function requires - # the add-on name - # Get an Addon instance - addon = xbmcaddon.Addon() - # Get the add-on name - self.addon_name = addon.getAddonInfo('name') + # This step must be done first because the logging function requires + # the name of the add-on + self.addon_name = xbmcaddon.Addon().getAddonInfo('name') # Save addon URL and ID self.plugin_url = plugin self.plugin_id = plugin_id # Select preferred instance by default - self.selected_inst = 'https://{}'\ - .format(addon.getSetting('preferred_instance')) + self.selected_inst ='https://{}'\ + .format(get_setting('preferred_instance')) # Get the number of videos to show per page - self.items_per_page = int(addon.getSetting('items_per_page')) + self.items_per_page = int(get_setting('items_per_page')) # Get the video sort method - self.sort_method = addon.getSetting('video_sort_method') + self.sort_method = get_setting('video_sort_method') # Get the preferred resolution for video - self.preferred_resolution = addon.getSetting('preferred_resolution') + self.preferred_resolution = get_setting('preferred_resolution') # Nothing to play at initialisation self.play = 0 @@ -75,7 +73,7 @@ class PeertubeAddon(): # Get the video filter from the settings that will be used when # browsing the videos. The value from the settings is converted into # one of the expected values by the REST APIs ("local" or "all-local") - if 'all-local' in addon.getSetting('video_filter'): + if 'all-local' in get_setting('video_filter'): self.video_filter = 'all-local' else: self.video_filter = 'local' @@ -475,12 +473,19 @@ class PeertubeAddon(): :param instance: str """ + # Update the object attribute even though it is not used currently but + # it may be useful in case reuselanguageinvoker is enabled. self.selected_inst = 'https://{}'.format(instance) + + # Update the preferred instance in the settings so that this choice is + # reused on the next run and the next call of the add-on + set_setting('preferred_instance', instance) + + # Notify the user and log the event + message = '{0} is now the selected instance'.format(self.selected_inst) notif_info(title='Current instance changed', - message='Changed current instance to {0}' - .format(self.selected_inst)) - self.debug('Changing currently selected instance to {0}' - .format(self.selected_inst)) + message=message) + self.debug(message) def build_kodi_url(self, parameters): """Build a Kodi URL based on the parameters. diff --git a/resources/lib/kodi_utils.py b/resources/lib/kodi_utils.py index 7414f31..00d9b20 100644 --- a/resources/lib/kodi_utils.py +++ b/resources/lib/kodi_utils.py @@ -8,6 +8,7 @@ See LICENSE.txt for more information. """ import xbmc +import xbmcaddon import xbmcgui @@ -21,13 +22,22 @@ def debug(message): def get_property(name): """Retrieve the value of a window property related to the add-on - :param str name: name of the property which value will be retrieved (the + :param str name: Name of the property which value will be retrieved (the actual name of the property is prefixed with "peertube_") - :return: the value of the window property + :return: Value of the window property :rtype: str """ return xbmcgui.Window(10000).getProperty('peertube_{}'.format(name)) +def get_setting(setting_name): + """Retrieve the value of a setting + + :param str setting_name: Name of the setting + :return: Value of the setting named setting_name + :rtype: str + """ + return xbmcaddon.Addon().getSetting(setting_name) + def notif_error(title, message): """Display a notification with the error icon @@ -74,3 +84,11 @@ def set_property(name, value): :param str value: New value of the property """ xbmcgui.Window(10000).setProperty('peertube_{}'.format(name), value) + +def set_setting(setting_name, setting_value): + """Modify the value of a setting + + :param str setting_name: Name of the setting + :param str setting_value: New value of the setting + """ + xbmcaddon.Addon().setSetting(setting_name, setting_value)