Updated code to use torrent files rather than magnets (For some reason magnet files

provided by peerTube don't work) + refactored code related to the management of the
addon's temporary directory
This commit is contained in:
Cyrille Bollu 2018-07-27 10:53:28 +02:00
parent 914bcb9234
commit 3cbf940e82
2 changed files with 31 additions and 37 deletions

View File

@ -95,16 +95,10 @@ class PeertubeAddon():
metadata = json.load(resp)
for f in metadata['files']:
if f['size'] < min_size or min_size == -1:
magnet = f['magnetUri']
# Save magnet link temporarily.
tmp_f = xbmc.translatePath('special://temp') + '/plugin.video.peertube/' + video['uuid']
f = xbmcvfs.File(tmp_f, 'w')
f.write(bytearray(magnet, 'utf8'))
f.close()
torrent_url = f['torrentUrl']
# Add our item to the listing as a 3-element tuple.
url = '{0}?action=play&magnet={1}'.format(__url__, tmp_f)
url = '{0}?action=play&url={1}'.format(__url__, torrent_url)
listing.append((url, list_item, False))
# Add our listing to Kodi.
@ -122,20 +116,20 @@ class PeertubeAddon():
xbmc.log('PeertubeAddon: Received metadata_downloaded signal, will start playing media', xbmc.LOGINFO)
self.play = 1
self.torrent_name = data['name']
self.torrent_f = data['file']
return
def play_video(self, magnet_f):
def play_video(self, torrent_url):
"""
Start the torrent's download and play it while being downloaded
:param magnet_f: str
:param torrent_url: str
:return: None
"""
xbmc.log('PeertubeAddon: playing video ' + magnet_f, xbmc.LOGINFO)
xbmc.log('PeertubeAddon: playing video ' + torrent_url, xbmc.LOGINFO)
# Start a downloader thread
AddonSignals.sendSignal('start_download', {'magnet_f': magnet_f})
AddonSignals.sendSignal('start_download', {'url': torrent_url})
# Wait until the PeerTubeDownloader has downloaded all the torrent's metadata + a little bit more
AddonSignals.registerSlot('plugin.video.peertube', 'metadata_downloaded', self.play_video_continue)
@ -144,8 +138,7 @@ class PeertubeAddon():
xbmc.sleep(3000)
# Pass the item to the Kodi player for actual playback.
path = fpath + self.torrent_name
play_item = xbmcgui.ListItem(path=path)
play_item = xbmcgui.ListItem(path=self.torrent_f)
xbmcplugin.setResolvedUrl(__handle__, True, listitem=play_item)
def router(self, paramstring):
@ -163,7 +156,7 @@ class PeertubeAddon():
# Check the parameters passed to the plugin
if params:
# Play a video from a provided URL.
self.play_video(params['magnet'])
self.play_video(params['url'])
else:
# Display the list of videos when the plugin is called from Kodi UI without any parameters
self.list_videos()

View File

@ -9,39 +9,37 @@ class PeertubeDownloader(Thread):
A class to download peertube torrents in the background
"""
def __init__(self, magnet_f):
def __init__(self, url, temp_dir):
"""
:param magnet_f: str
Initialise a PeertubeDownloader instance for downloading the torrent specified by url
:param url, temp_dir: str
:return: None
"""
Thread.__init__(self)
self.magnet_f = magnet_f
self.torrent = url
self.temp_dir = temp_dir
def run(self):
"""
Download the torrent specified by self.magnet_f
Download the torrent specified by self.torrent
:param: None
:return: None
"""
xbmc.log('PeertubeDownloader: Starting a torrent download', xbmc.LOGINFO)
xbmc.log('PeertubeDownloader: Opening bitTorent session', xbmc.LOGINFO)
# Open bitTorrent session
ses = libtorrent.session()
ses.listen_on(6881, 6891)
# Read magnet's data
f = xbmcvfs.File(self.magnet_f, 'r')
magnet = f.read()
# Add torrent
xbmc.log('PeertubeDownloader: Adding torrent ' + magnet, xbmc.LOGINFO)
fpath = xbmc.translatePath('special://temp')
h = ses.add_torrent({'url': magnet, 'save_path': fpath})
xbmc.log('PeertubeDownloader: Adding torrent ' + self.torrent, xbmc.LOGINFO)
h = ses.add_torrent({'url': torrent, 'save_path': self.temp_dir})
# Set sequential mode to allow watching while downloading
h.set_sequential_download(True)
# Download torrent
xbmc.log('PeertubeDownloader: Downloading torrent ' + self.torrent, xbmc.LOGINFO)
signal_sent = 0
while not h.is_seed():
xbmc.sleep(1000)
@ -49,7 +47,9 @@ class PeertubeDownloader(Thread):
# 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:
xbmc.log('PeertubeDownloader: Received all torrent metadata, notifying PeertubeAddon', xbmc.LOGINFO)
AddonSignals.sendSignal('metadata_downloaded', {'name': h.name()} )
i = h.torrent_file()
f = self.temp_dir + i.name()
AddonSignals.sendSignal('metadata_downloaded', {'file': f})
signal_sent = 1
# Everything is done
@ -65,20 +65,22 @@ class PeertubeService():
"""
xbmc.log('PeertubeService: Initialising', xbmc.LOGINFO)
# Create our temporary directory
fpath = xbmc.translatePath('special://temp') + '/plugin.video.peertube'
if not xbmcvfs.exists(fpath):
xbmcvfs.mkdir(fpath)
# Create our temporary directory
self.temp = xbmc.translatePath('special://temp') + '/plugin.video.peertube/'
if not xbmcvfs.exists(self.temp):
xbmcvfs.mkdir(self.temp)
return
def download_torrent(self, data):
"""
Start a downloader thread to download torrent specified by data['magnet_f']
Start a downloader thread to download torrent specified by data['url']
:param data: dict
:return: None
"""
xbmc.log('PeertubeService: Received a start_download signal', xbmc.LOGINFO)
downloader = PeertubeDownloader(data['magnet_f'])
downloader = PeertubeDownloader(data['url'], self.temp)
downloader.start()
return
@ -98,12 +100,11 @@ class PeertubeService():
while not monitor.abortRequested():
if monitor.waitForAbort(1):
# Abort was requested while waiting. We must exit
# TODO: I must delete all temp files before exiting
# TODO: Clean temporary directory
break
return
if __name__ == '__main__':
# Start a peertubeService instance
xbmc.log('PeertubeService: Starting', xbmc.LOGINFO)