Download torrents with vfs.libtorrent

Replace python-libtorrent with vfs.libtorrent.
Some parts of the code were removed as they will probably be useless
in the future.
The service is disabled (temporarily?) because currently the playback starts
only when the whole file is downloaded (no background download anymore).

Note that the path of the downloaded file is built manually because
vfs.libtorrent does not return this information yet.
The extension of the file is forced to "mp4" so it may not work for
all the videos.
This commit is contained in:
Thomas Bétous 2021-09-13 17:14:49 +02:00
parent e82b916bbf
commit a1b52d1396
3 changed files with 34 additions and 74 deletions

View File

@ -1,17 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="plugin.video.peertube" name="PeerTube" version="1.2.0-dev" 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="2.25.0"/>
<import addon="script.module.addon.signals" version="0.0.3"/>
<import addon="script.module.requests" version="2.22.0"/>
<!-- <import addon="script.module.libtorrent" version="1.2.0"/> -->
<!-- <import addon="vfs.libarchive"/> -->
<!-- <import addon="vfs.libtorrent"/> -->
<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

@ -8,6 +8,9 @@
SPDX-License-Identifier: GPL-3.0-only
See LICENSE.txt for more information.
"""
import os.path
from urllib import quote_plus as url_quote
import AddonSignals # Module exists only in Kodi - pylint: disable=import-error
from resources.lib.kodi_utils import kodi
@ -340,58 +343,31 @@ 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(url_quote(torrent_url))
kodi.debug("vfs_url = {}".format(vfs_url))
f = xbmcvfs.File(vfs_url)
# data = f.read(1)
# kodi.debug("type(data) = {}".format(type(data)))
# kodi.debug("data = {}".format(data))
f.close()
# Currently vfs.libtorrent does not return the path of the downloaded
# file so we build it manually (only mp4 videos are supported currently)
filename = os.path.basename(torrent_url).replace(".torrent", ".mp4")
self.torrent_file = kodi.translate_path(
"special://temp/vfs.libtorrent/{}".format(filename))
# 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
# 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
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))
# When the download is over, play the file
kodi.debug("Starting video playback of {}".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")
def _select_instance(self, instance):
"""
Change currently selected instance to "instance" parameter
@ -423,8 +399,6 @@ class PeerTubeAddon():
:param dict params: Parameters the add-on was called with
"""
from urllib import quote_plus as url_quote
# Check the parameters passed to the plugin
if params:
action = params["action"]
@ -433,25 +407,10 @@ class PeerTubeAddon():
self._browse_videos(int(params["start"]))
elif action == "search_videos":
# Search for videos on the selected instance
# self._search_videos(int(params["start"]))
filename = "torrent://{}".format(url_quote(
"https://framatube.org/static/torrents/c448032c-9d98-4190-a533-02afe7a214b1-270.torrent"))
kodi.debug("filename = {}".format(filename))
f = xbmcvfs.File(filename)
data = f.read()
kodi.debug("data = {}".format(data))
f.close()
self._search_videos(int(params["start"]))
elif action == "browse_instances":
# Browse PeerTube instances
# self._browse_instances(int(params["start"]))
if xbmcvfs.exists("/Users/Thomas/Desktop/c448032c-9d98-4190-a533-02afe7a214b1-270.torrent"):
kodi.debug("Exists returned True")
else:
kodi.debug("Exists returned False")
self._browse_instances(int(params["start"]))
elif action == "play_video":
# This action comes with the id of the video to play as
# parameter. The instance may also be in the parameters. Use
@ -474,9 +433,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

@ -263,6 +263,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