From 5eb7a46f34f1a69ced355cd16ec106c8ce6f1b2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20B=C3=A9tous?= Date: Mon, 27 Sep 2021 17:32:14 +0200 Subject: [PATCH] Use the new JSON string returned by read() * Adapt the code for the new information returned by read() * Display a warning if there are more than 1 file in the torrent because the add-on will play only the first file * Manage the error use case when calling write() * Remove the "start downloading" notification since an equivalent message is now displayed by vfs.libtorrent --- .../resource.language.de_de/strings.po | 16 ++++++---- .../resource.language.en_gb/strings.po | 16 ++++++---- .../resource.language.fr_fr/strings.po | 16 ++++++---- resources/lib/addon.py | 29 ++++++++++++------- resources/lib/kodi_utils.py | 14 +++++++++ 5 files changed, 63 insertions(+), 28 deletions(-) diff --git a/resources/language/resource.language.de_de/strings.po b/resources/language/resource.language.de_de/strings.po index bac04e8..a15b9cf 100644 --- a/resources/language/resource.language.de_de/strings.po +++ b/resources/language/resource.language.de_de/strings.po @@ -137,13 +137,9 @@ msgctxt "#30413" msgid "PeerTube cannot play videos without libtorrent\nPlease follow the instructions at {}" msgstr "PeerTube kann keine Videos ohne libtorrent abspielen\nBitte folgen Sie den Anweisungen unter {}" -msgctxt "#30414" -msgid "Download started" -msgstr "Download gestartet" +# 30414 is not used anymore -msgctxt "#30415" -msgid "The video will be played soon." -msgstr "Das Video wird in kürze abgespielt." +# 30415 is not used anymore msgctxt "#30416" msgid "Download timeout" @@ -164,3 +160,11 @@ msgstr "{} ist nun die ausgewählte Instanz." msgctxt "#30420" msgid "You can still browse and search videos but you will not be able to play them (except live videos).\nPlease follow the instructions at {}" msgstr "Sie können weiterhin Videos durchsuchen und suchen, aber Sie können sie nicht abspielen (außer Live-Videos).\nBefolgen Sie bitte die Anweisungen unter {}" + +msgctxt "#30421" +msgid "Download error" +msgstr "" + +msgctxt "#30422" +msgid "Error when trying to download the video. Check the log for more information." +msgstr "" diff --git a/resources/language/resource.language.en_gb/strings.po b/resources/language/resource.language.en_gb/strings.po index 48d73c4..4c61059 100644 --- a/resources/language/resource.language.en_gb/strings.po +++ b/resources/language/resource.language.en_gb/strings.po @@ -137,13 +137,9 @@ msgctxt "#30413" msgid "PeerTube cannot play videos without libtorrent\nPlease follow the instructions at {}" msgstr "" -msgctxt "#30414" -msgid "Download started" -msgstr "" +# 30414 is not used anymore -msgctxt "#30415" -msgid "The video will be played soon." -msgstr "" +# 30415 is not used anymore msgctxt "#30416" msgid "Download timeout" @@ -164,3 +160,11 @@ msgstr "" msgctxt "#30420" msgid "You can still browse and search videos but you will not be able to play them (except live videos).\nPlease follow the instructions at {}" msgstr "" + +msgctxt "#30421" +msgid "Download error" +msgstr "" + +msgctxt "#30422" +msgid "Error when trying to download the video. Check the log for more information." +msgstr "" diff --git a/resources/language/resource.language.fr_fr/strings.po b/resources/language/resource.language.fr_fr/strings.po index b630091..2e4ff0d 100644 --- a/resources/language/resource.language.fr_fr/strings.po +++ b/resources/language/resource.language.fr_fr/strings.po @@ -137,13 +137,9 @@ msgctxt "#30413" msgid "PeerTube cannot play videos without libtorrent\nPlease follow the instructions at {}" msgstr "PeerTube ne peut pas lire de vidéos sans libtorrent.\nMerci de suivre les instructions depuis {}" -msgctxt "#30414" -msgid "Download started" -msgstr "Démarrage du téléchargement" +# 30414 is not used anymore -msgctxt "#30415" -msgid "The video will be played soon." -msgstr "La video va être bientôt lue." +# 30415 is not used anymore msgctxt "#30416" msgid "Download timeout" @@ -164,3 +160,11 @@ msgstr "{} est maintenant l'instance sélectionnée." msgctxt "#30420" msgid "You can still browse and search videos but you will not be able to play them (except live videos).\nPlease follow the instructions at {}" msgstr "Vous pouvez parcourir ou chercher des vidéos mais vous ne pourrez pas les lire (sauf les live).\nMerci de suivre les instructions depuis {}" + +msgctxt "#30421" +msgid "Download error" +msgstr "Erreur de téléchargement" + +msgctxt "#30422" +msgid "Error when trying to download the video. Check the log for more information." +msgstr "Une erreur est survenue pendant le téléchargement de la vidéo. Voir le journal pour plus d'informations." diff --git a/resources/lib/addon.py b/resources/lib/addon.py index 521ae92..b8709d4 100644 --- a/resources/lib/addon.py +++ b/resources/lib/addon.py @@ -8,6 +8,7 @@ SPDX-License-Identifier: GPL-3.0-only See LICENSE.txt for more information. """ +import json import os.path from urllib import quote_plus @@ -342,24 +343,32 @@ class PeerTubeAddon(): """ kodi.debug("Starting torrent download ({})".format(torrent_url)) - kodi.notif_info(title=kodi.get_string(30414), - message=kodi.get_string(30415)) # Download the torrent using vfs.libtorrent: the torrent URL must be # URL encoded to be correctly read by vfs.libtorrent vfs_url = "torrent://{}".format(quote_plus(torrent_url)) - kodi.debug("URL sent to vfs.libtorrent = {}".format(vfs_url)) torrent = xbmcvfs.File(vfs_url) - # Get the path of the downloaded file - self.torrent_file = torrent.read() + # Download the file + if(torrent.write("download")): - # Close the file handler because no other information is required - torrent.close() + # Get information about the torrent + torrent_info = json.loads(torrent.read()) - # Play the file - kodi.debug("Starting video playback of {}".format(self.torrent_file)) - kodi.play(self.torrent_file) + # Build the path of the downloaded file + self.torrent_file = os.path.join(torrent_info["save_path"], + torrent_info["files"][0]["path"]) + + if torrent_info["nb_files"] > 1: + kodi.warning("There are more than 1 file in {} but only the" + " first one will be played.".format(torrent_url)) + + # Play the file + kodi.debug("Starting video playback of {}".format(self.torrent_file)) + kodi.play(self.torrent_file) + else: + kodi.notif_error(title=kodi.get_string(30421), + message=kodi.get_string(30422)) def _select_instance(self, instance): """ diff --git a/resources/lib/kodi_utils.py b/resources/lib/kodi_utils.py index f6d0684..12b62b9 100644 --- a/resources/lib/kodi_utils.py +++ b/resources/lib/kodi_utils.py @@ -281,4 +281,18 @@ class KodiUtils: self.addon_handle = int(argv[1]) self.addon_parameters = argv[2] + def warning(self, message, prefix=None): + """Log a message in Kodi's log with the level xbmc.LOGWARNING + + 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.LOGWARNING) + kodi = KodiUtils()