-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
160 lines (131 loc) · 5.08 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# -*- coding: utf-8 -*-
"""
PeerTube service to download torrents in the background
Copyright (C) 2018 Cyrille Bollu
Copyright (C) 2021 Thomas Bétous
SPDX-License-Identifier: GPL-3.0-only
See LICENSE.txt for more information.
"""
import AddonSignals # Module exists only in Kodi - pylint: disable=import-error
from threading import Thread
import xbmc
import xbmcvfs
from resources.lib.kodi_utils import kodi
class PeertubeDownloader(Thread):
"""
A class to download PeerTube torrents in the background
"""
def __init__(self, url, temp_dir):
"""
Initialise a PeertubeDownloader instance for downloading the torrent
specified by url
:param url, temp_dir: str
:return: None
"""
Thread.__init__(self)
self.torrent = url
self.temp_dir = temp_dir
def debug(self, message):
"""Log a debug message
:param str message: Message to log (will be prefixed with the name of
the class)
"""
kodi.debug(message=message, prefix="PeertubeDownloader")
def run(self):
"""
Download the torrent specified by self.torrent
:param: None
:return: None
"""
self.debug("Opening BitTorent session")
# Open BitTorrent session
ses = libtorrent.session()
ses.listen_on(6881, 6891)
# Add torrent
self.debug("Adding torrent {}".format(self.torrent))
h = ses.add_torrent({"url": self.torrent, "save_path": self.temp_dir})
# Set sequential mode to allow watching while downloading
h.set_sequential_download(True)
# Download torrent
self.debug("Downloading torrent {}".format(self.torrent))
signal_sent = 0
while not h.is_seed():
xbmc.sleep(1000)
s = h.status()
# Inform addon that all the metadata has been downloaded and that
# it may start playing the torrent
if s.state >=3 and signal_sent == 0:
self.debug("Received all torrent metadata, notifying"
" PeertubeAddon")
i = h.torrent_file()
f = self.temp_dir + i.name()
AddonSignals.sendSignal("metadata_downloaded", {"file": f})
signal_sent = 1
class PeertubeService():
"""
Class used to run a service when Kodi starts
"""
def __init__(self):
"""
PeertubeService initialisation function
"""
# Create our temporary directory
self.temp = "{}{}".format(xbmc.translatePath("special://temp"),
"plugin.video.peertube/")
if not xbmcvfs.exists(self.temp):
xbmcvfs.mkdir(self.temp)
def debug(self, message):
"""Log a debug message
:param str message: Message to log (will be prefixed with the name of
the class)
"""
kodi.debug(message=message, prefix="PeertubeService")
def download_torrent(self, data):
"""
Start a downloader thread to download torrent specified by data["url"]
:param data: dict
:return: None
"""
self.debug("Received a start_download signal")
downloader = PeertubeDownloader(data["url"], self.temp)
downloader.start()
def run(self):
"""
Main loop of the PeertubeService class
It registers the start_download signal to start a PeertubeDownloader
thread when needed, and exit when Kodi is shutting down.
"""
self.debug("Starting")
# Launch the download_torrent callback function when the
# "start_download" signal is received
AddonSignals.registerSlot(kodi.addon_id,
"start_download",
self.download_torrent)
# Monitor Kodi's shutdown signal
self.debug("Service started, waiting for signals")
if kodi.get_setting("service_start_notif") == "true":
kodi.notif_info(title=kodi.get_string(30400),
message=kodi.get_string(30401))
monitor = xbmc.Monitor()
while not monitor.abortRequested():
if monitor.waitForAbort(1):
# Abort was requested while waiting. We must exit
# TODO: Clean temporary directory
self.debug("Exiting")
break
if __name__ == "__main__":
# Create a PeertubeService instance
service = PeertubeService()
# Import libtorrent here to manage when the library is not installed
try:
from python_libtorrent import libtorrent
LIBTORRENT_IMPORTED = True
except ImportError as exception:
LIBTORRENT_IMPORTED = False
service.debug("The libtorrent library could not be imported because of"
" the following error:\n{}".format(exception))
# Save whether libtorrent could be imported as a window property so that
# this information can be retrieved by the add-on
kodi.set_property("libtorrent_imported", str(LIBTORRENT_IMPORTED))
# Start the service
service.run()