1
0
mirror of https://framagit.org/StCyr/plugin.video.peertube synced 2025-06-05 22:09:16 +02:00

Redesign the main file of the add-on

New features:
* Add the description of each video and each instance. The total number
  of local videos and users of an instance are also added to the
  description of the instance in Kodi.
* Add the total number of pages in the "Next page" item (+ fix the
  number of the current page)
* Display a notification when the download of the torrent starts (will
  help the user to know that something is going on, especially on slow
  machines)
* Support instance URL that are prefixed with "https://" in the settings

Internal changes:
* Create a smaller entry point file to match Kodi's best practices
  (main.py)
* Create a new main module (addon.py) containing only the code related
  to the add-on execution. The other lines of code were moved to the
  classes PeerTube or KodiUtils.
* KodiUtils is now a class and an instance of this class is made
  available to all the modules of the add-on to reuse easily its methods
  and attributes.
* Create helper functions in KodiUtils for creating items in Kodi UI
  easily

See merge request StCyr/plugin.video.peertube!17 for more information
This commit is contained in:
Thomas
2021-04-22 20:48:20 +00:00
parent 6f94e05398
commit 7a1a4e8485
8 changed files with 806 additions and 560 deletions

View File

@@ -7,90 +7,246 @@
SPDX-License-Identifier: GPL-3.0-only
See LICENSE.txt for more information.
"""
try:
# Python 3.x
from urllib.parse import parse_qsl
except ImportError:
# Python 2.x
from urlparse import parse_qsl
from requests.compat import urlencode
import xbmc # Kodistubs for Leia is not compatible with python3 / pylint: disable=syntax-error
import xbmcaddon
import xbmcgui # Kodistubs for Leia is not compatible with python3 / pylint: disable=syntax-error
import xbmcplugin
def debug(message):
"""Log a message in Kodi's log with the level xbmc.LOGDEBUG
class KodiUtils:
"""Utility class to call Kodi APIs"""
:param str message: Message to log prefixed with the name of the add-on
(the name is hard-coded to avoid calling xbmcaddon each time since the name
should not change)
"""
xbmc.log("[PeerTube] {}".format(message), xbmc.LOGDEBUG)
def __init__(self):
"""Initialize the object with information about the add-on"""
self.addon_name = xbmcaddon.Addon().getAddonInfo("name")
self.addon_id = xbmcaddon.Addon().getAddonInfo("id")
def get_property(name):
"""Retrieve the value of a window property related to the add-on
# Prepare other attributes that will be initialized with sys.argv
self.addon_url = ""
self.addon_handle = 0
self.addon_parameters = ""
:param str name: Name of the property which value will be retrieved (the
actual name of the property is prefixed with "peertube_")
:return: Value of the window property
:rtype: str
"""
return xbmcgui.Window(10000).getProperty("peertube_{}".format(name))
def build_kodi_url(self, parameters):
"""Build a Kodi URL based on the parameters.
def get_setting(setting_name):
"""Retrieve the value of a setting
This URL will be used to call the add-on with the expected parameters.
:param str setting_name: Name of the setting
:return: Value of the setting named setting_name
:rtype: str
"""
return xbmcaddon.Addon().getSetting(setting_name)
:param dict parameters: The parameters that will be encoded in the URL
"""
def notif_error(title, message):
"""Display a notification with the error icon
return "{}?{}".format(self.addon_url, urlencode(parameters))
:param str title: Title of the notification
:param str message: Message of the notification
"""
xbmcgui.Dialog().notification(heading=title,
message=message,
icon=xbmcgui.NOTIFICATION_ERROR)
def create_items_in_ui(self, items_info):
"""Create items in Kodi UI
def notif_info(title, message):
"""Display a notification with the info icon
:param list items_info: A list of dict containing all the required
information to create the items (i.e. the return value of the method
generate_item_info)
"""
# Tell Kodi to use the "video" viewtypes
xbmcplugin.setContent(handle=self.addon_handle, content="videos")
:param str title: Title of the notification
:param str message: Message of the notification
"""
xbmcgui.Dialog().notification(heading=title,
message=message,
icon=xbmcgui.NOTIFICATION_INFO)
list_of_items = []
def notif_warning(title, message):
"""Display a notification with the warning icon
for info in items_info:
# Create the ListItem object
list_item = xbmcgui.ListItem(label=info["name"])
:param str title: Title of the notification
:param str message: Message of the notification
"""
xbmcgui.Dialog().notification(heading=title,
message=message,
icon=xbmcgui.NOTIFICATION_WARNING)
# Add the general info of the item
list_item.setInfo("video", info["info"])
def open_dialog(title, message):
"""Open a dialog box with an "OK" button
# Add the art info of the item
list_item.setArt(info["art"])
:param str title: Title of the box
:param str message: Message in the box
"""
xbmcgui.Dialog().ok(heading=title, line1=message)
if not info["is_folder"]:
list_item.setProperty("IsPlayable", "true")
def set_property(name, value):
"""Modify the value of a window property related to the add-on
# Add to the list the tuple expected by addDirectoryItems
list_of_items.append((info["url"], list_item, info["is_folder"]))
:param str name: Name of the property which value will be modified (the
actual name of the property is prefixed with "peertube_")
:param str value: New value of the property
"""
xbmcgui.Window(10000).setProperty("peertube_{}".format(name), value)
# Create the items
xbmcplugin.addDirectoryItems(
handle=self.addon_handle,
items=list_of_items,
totalItems=len(list_of_items)
)
def set_setting(setting_name, setting_value):
"""Modify the value of a setting
# Terminate the items creation
xbmcplugin.endOfDirectory(self.addon_handle)
:param str setting_name: Name of the setting
:param str setting_value: New value of the setting
"""
xbmcaddon.Addon().setSetting(setting_name, setting_value)
def debug(self, message, prefix=None):
"""Log a message in Kodi's log with the level xbmc.LOGDEBUG
The message will be prefixed with the prefix passed as argument or with
the name of the add-on.
:param str message: Message to log
:param str prefix: String to prefix the message with
"""
if not prefix:
prefix = self.addon_name
xbmc.log("[{}] {}".format(prefix, message), xbmc.LOGDEBUG)
def generate_item_info(self, name, url, is_folder=True, thumbnail="",
aired="", duration=0, plot="",):
"""Return all the information required to create an item in Kodi UI
This function makes the creation of an item easier: it allows to pass
to the function only the known information about an item, and it will
return a dict with all the keys expected by create_items_in_ui
correctly initialized (including the ones that were not passed).
:param str name: Name of the item
:param str url: URL to reach when the item is used
:param bool is_folder: Whether the item is a folder or is playable
:param <other>: The other parameters are the ones expected by
ListItem.setInfo() and ListItem.setArt()
:return: Information required to create the item in Kodi UI
:rtype: dict
"""
return {
"name": name,
"url": url,
"is_folder": is_folder,
"art": {
"thumb": thumbnail,
},
"info": {
"aired": aired,
"duration": duration,
"plot": plot,
"title": name
}
}
def get_run_parameters(self):
"""Return the parameter the add-on was called with
The parameters are read in the method "update_call_info"
:return: The extracted parameters
:rtype: dict
"""
# The first character ("?") is skipped
return dict(parse_qsl(self.addon_parameters[1:]))
def get_property(self, 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
actual name of the property is prefixed with "peertube_")
:return: Value of the window property
:rtype: str
"""
return xbmcgui.Window(10000).getProperty("peertube_{}".format(name))
def get_setting(self, 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(self, title, message):
"""Display a notification with the error icon
:param str title: Title of the notification
:param str message: Message of the notification
"""
xbmcgui.Dialog().notification(heading=title,
message=message,
icon=xbmcgui.NOTIFICATION_ERROR)
def notif_info(self, title, message):
"""Display a notification with the info icon
:param str title: Title of the notification
:param str message: Message of the notification
"""
xbmcgui.Dialog().notification(heading=title,
message=message,
icon=xbmcgui.NOTIFICATION_INFO)
def notif_warning(self, title, message):
"""Display a notification with the warning icon
:param str title: Title of the notification
:param str message: Message of the notification
"""
xbmcgui.Dialog().notification(heading=title,
message=message,
icon=xbmcgui.NOTIFICATION_WARNING)
def open_dialog(self, title, message):
"""Open a dialog box with an "OK" button
:param str title: Title of the box
:param str message: Message in the box
"""
xbmcgui.Dialog().ok(heading=title, line1=message)
def open_input_box(self, title):
"""Open a box for the user to input alphanumeric data
:param str title: Title of the box
:return: Entered data or an empty string
:rtype: str
"""
return xbmcgui.Dialog().input(heading=title,
type=xbmcgui.INPUT_ALPHANUM)
def play(self, url):
"""Play the media behind the URL
:param str url: URL of the media to play
"""
xbmcplugin.setResolvedUrl(handle=self.addon_handle,
succeeded=True,
listitem=xbmcgui.ListItem(path=url))
def set_property(self, name, value):
"""Modify the value of a window property related to the add-on
:param str name: Name of the property which value will be modified (the
actual name of the property is prefixed with "peertube_")
:param str value: New value of the property
"""
xbmcgui.Window(10000).setProperty("peertube_{}".format(name), value)
def set_setting(self, 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)
def sleep(self, time_us):
"""Sleep for some micro seconds
:param int time_us: Sleep time in micro seconds
"""
xbmc.sleep(time_us)
def update_call_info(self, argv):
"""Update the attributes related to the current call of the add-on
:param list argv: System arguments
"""
self.addon_url = argv[0]
self.addon_handle = int(argv[1])
self.addon_parameters = argv[2]
kodi = KodiUtils()