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
This commit is contained in:
Thomas Bétous 2021-09-27 17:32:14 +02:00
parent 4a1a02afef
commit 5eb7a46f34
5 changed files with 63 additions and 28 deletions

View File

@ -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 ""

View File

@ -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 ""

View File

@ -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."

View File

@ -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):
"""

View File

@ -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()