Replace python-libtorrent with vfs.libtorrent

Now torrents are downloaded with vfs.libtorrent[1].

Some parts of the code were removed as they will probably be useless
in the future. For instance, the service is disabled (temporarily?)
because the download in background/foreground is now managed by
vfs.libtorrent.

Use the Read() API of vfs.libtorrent to retrieve the path where the
torrent was downloaded.
Also:
* Display a warning if there are more than 1 file in the torrent
because the add-on will play only the first file
* Remove the "start downloading" notification since an equivalent
  message is now displayed by vfs.libtorrent

[1]:https://framagit.org/thombet/vfs.libtorrent
This commit is contained in:
Thomas Bétous 2021-09-27 20:20:47 +02:00
parent 11b92d5896
commit 108fea515d
6 changed files with 83 additions and 74 deletions

View File

@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="plugin.video.peertube" name="PeerTube" version="1.2.0" provider-name="Cyrille B. + Thomas B.">
<addon id="plugin.video.peertube" name="PeerTube" version="1.3.0-dev" provider-name="Cyrille B. + Thomas B.">
<requires>
<import addon="xbmc.python" version="3.0.0"/>
<import addon="script.module.addon.signals" version="0.0.6"/>
<!--<import addon="script.module.addon.signals" version="0.0.6"/>-->
<import addon="script.module.requests" version="2.25.1"/>
<!-- <import addon="script.module.libtorrent" version="1.2.0"/> -->
<import addon="vfs.libtorrent"/>
</requires>
<extension point="xbmc.python.pluginsource" library="main.py">
<provides>video</provides>
</extension>
<extension point="xbmc.service" library="service.py"/>
<!--<extension point="xbmc.service" library="service.py"/>-->
<extension point="xbmc.addon.metadata">
<summary lang="de_DE">Add-on für PeerTube</summary>
<description lang="de_DE">PeerTube ist eine kostenlose, dezentralisierte und föderierte Alternative zu anderen Videoplattformen (wie YouTube).</description>

View File

@ -167,13 +167,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"
@ -194,3 +190,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

@ -167,13 +167,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"
@ -194,3 +190,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

@ -173,13 +173,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"
@ -200,3 +196,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,11 +8,14 @@
SPDX-License-Identifier: GPL-3.0-only
See LICENSE.txt for more information.
"""
import AddonSignals # Module exists only in Kodi - pylint: disable=import-error
import json
import os.path
from urllib.parse import quote_plus
from resources.lib.kodi_utils import kodi
from resources.lib.peertube import PeerTube, list_instances
import xbmcvfs
class PeerTubeAddon():
"""
@ -338,57 +341,34 @@ class PeerTubeAddon():
:param str torrent_url: URL of the torrent file to download and play
"""
# If libtorrent could not be imported, display a message and do not try
# download nor play the video as it will fail.
if not self.libtorrent_imported:
kodi.open_dialog(
title=kodi.get_string(30412),
message=kodi.get_string(30413).format(self.HELP_URL))
return
kodi.debug("Starting torrent download ({})".format(torrent_url))
kodi.notif_info(title=kodi.get_string(30414),
message=kodi.get_string(30415))
# Start a downloader thread
AddonSignals.sendSignal("start_download", {"url": torrent_url})
# 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))
torrent = xbmcvfs.File(vfs_url)
# Wait until the PeerTubeDownloader has downloaded all the torrent's
# metadata
AddonSignals.registerSlot(kodi.addon_id,
"metadata_downloaded",
self._play_video_continue)
timeout = 0
while not self.play and timeout < 10:
kodi.sleep(1000)
timeout += 1
# Download the file
if(torrent.write("download")):
# Abort in case of timeout
if timeout == 10:
kodi.notif_error(
title=kodi.get_string(30416),
message=kodi.get_string(30417).format(torrent_url))
return
# Get information about the torrent
torrent_info = json.loads(torrent.read())
# 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:
# Wait a little before starting playing the torrent
kodi.sleep(3000)
# Pass the item to the Kodi player for actual playback.
kodi.debug("Starting video playback ({})".format(self.torrent_file))
kodi.play(self.torrent_file)
def _play_video_continue(self, data):
"""
Callback function to let the _play_video method resume when the
PeertubeDownloader has downloaded all the torrent's metadata
:param data: dict of information sent from PeertubeDownloader
"""
kodi.debug(
"Received metadata_downloaded signal, will start playing media")
self.play = True
self.torrent_file = data["file"].encode("utf-8")
kodi.notif_error(title=kodi.get_string(30421),
message=kodi.get_string(30422))
def _select_instance(self, instance):
"""
@ -455,9 +435,3 @@ class PeerTubeAddon():
# Display the addon's main menu when the plugin is called from
# Kodi UI without any parameters
self._home_page()
# Display a warning if libtorrent could not be imported
if not self.libtorrent_imported:
kodi.open_dialog(
title=kodi.get_string(30412),
message=kodi.get_string(30420).format(self.HELP_URL))

View File

@ -258,6 +258,15 @@ class KodiUtils:
"""
xbmc.sleep(time_us)
def translate_path(self, path):
"""Translate a path using Kodi specialprotocol to an actual path
:param str path: Path using Kodi special protocol
:return: Translated path
:rtype: str
"""
return xbmc.translatePath(path)
def update_call_info(self, argv):
"""Update the attributes related to the current call of the add-on
@ -267,4 +276,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()