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:
parent
914bcb9234
commit
3cbf940e82
25
peertube.py
25
peertube.py
|
@ -95,16 +95,10 @@ class PeertubeAddon():
|
||||||
metadata = json.load(resp)
|
metadata = json.load(resp)
|
||||||
for f in metadata['files']:
|
for f in metadata['files']:
|
||||||
if f['size'] < min_size or min_size == -1:
|
if f['size'] < min_size or min_size == -1:
|
||||||
magnet = f['magnetUri']
|
torrent_url = f['torrentUrl']
|
||||||
|
|
||||||
# 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()
|
|
||||||
|
|
||||||
# Add our item to the listing as a 3-element tuple.
|
# 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))
|
listing.append((url, list_item, False))
|
||||||
|
|
||||||
# Add our listing to Kodi.
|
# Add our listing to Kodi.
|
||||||
|
@ -122,20 +116,20 @@ class PeertubeAddon():
|
||||||
|
|
||||||
xbmc.log('PeertubeAddon: Received metadata_downloaded signal, will start playing media', xbmc.LOGINFO)
|
xbmc.log('PeertubeAddon: Received metadata_downloaded signal, will start playing media', xbmc.LOGINFO)
|
||||||
self.play = 1
|
self.play = 1
|
||||||
self.torrent_name = data['name']
|
self.torrent_f = data['file']
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def play_video(self, magnet_f):
|
def play_video(self, torrent_url):
|
||||||
"""
|
"""
|
||||||
Start the torrent's download and play it while being downloaded
|
Start the torrent's download and play it while being downloaded
|
||||||
:param magnet_f: str
|
:param torrent_url: str
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
xbmc.log('PeertubeAddon: playing video ' + magnet_f, xbmc.LOGINFO)
|
xbmc.log('PeertubeAddon: playing video ' + torrent_url, xbmc.LOGINFO)
|
||||||
# Start a downloader thread
|
# 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
|
# 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)
|
AddonSignals.registerSlot('plugin.video.peertube', 'metadata_downloaded', self.play_video_continue)
|
||||||
|
@ -144,8 +138,7 @@ class PeertubeAddon():
|
||||||
xbmc.sleep(3000)
|
xbmc.sleep(3000)
|
||||||
|
|
||||||
# Pass the item to the Kodi player for actual playback.
|
# Pass the item to the Kodi player for actual playback.
|
||||||
path = fpath + self.torrent_name
|
play_item = xbmcgui.ListItem(path=self.torrent_f)
|
||||||
play_item = xbmcgui.ListItem(path=path)
|
|
||||||
xbmcplugin.setResolvedUrl(__handle__, True, listitem=play_item)
|
xbmcplugin.setResolvedUrl(__handle__, True, listitem=play_item)
|
||||||
|
|
||||||
def router(self, paramstring):
|
def router(self, paramstring):
|
||||||
|
@ -163,7 +156,7 @@ class PeertubeAddon():
|
||||||
# Check the parameters passed to the plugin
|
# Check the parameters passed to the plugin
|
||||||
if params:
|
if params:
|
||||||
# Play a video from a provided URL.
|
# Play a video from a provided URL.
|
||||||
self.play_video(params['magnet'])
|
self.play_video(params['url'])
|
||||||
else:
|
else:
|
||||||
# Display the list of videos when the plugin is called from Kodi UI without any parameters
|
# Display the list of videos when the plugin is called from Kodi UI without any parameters
|
||||||
self.list_videos()
|
self.list_videos()
|
||||||
|
|
41
service.py
41
service.py
|
@ -9,39 +9,37 @@ class PeertubeDownloader(Thread):
|
||||||
A class to download peertube torrents in the background
|
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
|
:return: None
|
||||||
"""
|
"""
|
||||||
Thread.__init__(self)
|
Thread.__init__(self)
|
||||||
self.magnet_f = magnet_f
|
self.torrent = url
|
||||||
|
self.temp_dir = temp_dir
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""
|
"""
|
||||||
Download the torrent specified by self.magnet_f
|
Download the torrent specified by self.torrent
|
||||||
:param: None
|
:param: None
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
xbmc.log('PeertubeDownloader: Starting a torrent download', xbmc.LOGINFO)
|
xbmc.log('PeertubeDownloader: Opening bitTorent session', xbmc.LOGINFO)
|
||||||
# Open bitTorrent session
|
# Open bitTorrent session
|
||||||
ses = libtorrent.session()
|
ses = libtorrent.session()
|
||||||
ses.listen_on(6881, 6891)
|
ses.listen_on(6881, 6891)
|
||||||
|
|
||||||
# Read magnet's data
|
|
||||||
f = xbmcvfs.File(self.magnet_f, 'r')
|
|
||||||
magnet = f.read()
|
|
||||||
|
|
||||||
# Add torrent
|
# Add torrent
|
||||||
xbmc.log('PeertubeDownloader: Adding torrent ' + magnet, xbmc.LOGINFO)
|
xbmc.log('PeertubeDownloader: Adding torrent ' + self.torrent, xbmc.LOGINFO)
|
||||||
fpath = xbmc.translatePath('special://temp')
|
h = ses.add_torrent({'url': torrent, 'save_path': self.temp_dir})
|
||||||
h = ses.add_torrent({'url': magnet, 'save_path': fpath})
|
|
||||||
|
|
||||||
# Set sequential mode to allow watching while downloading
|
# Set sequential mode to allow watching while downloading
|
||||||
h.set_sequential_download(True)
|
h.set_sequential_download(True)
|
||||||
|
|
||||||
# Download torrent
|
# Download torrent
|
||||||
|
xbmc.log('PeertubeDownloader: Downloading torrent ' + self.torrent, xbmc.LOGINFO)
|
||||||
signal_sent = 0
|
signal_sent = 0
|
||||||
while not h.is_seed():
|
while not h.is_seed():
|
||||||
xbmc.sleep(1000)
|
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
|
# 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:
|
if s.state >=3 and signal_sent == 0:
|
||||||
xbmc.log('PeertubeDownloader: Received all torrent metadata, notifying PeertubeAddon', xbmc.LOGINFO)
|
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
|
signal_sent = 1
|
||||||
|
|
||||||
# Everything is done
|
# Everything is done
|
||||||
|
@ -66,19 +66,21 @@ class PeertubeService():
|
||||||
|
|
||||||
xbmc.log('PeertubeService: Initialising', xbmc.LOGINFO)
|
xbmc.log('PeertubeService: Initialising', xbmc.LOGINFO)
|
||||||
# Create our temporary directory
|
# Create our temporary directory
|
||||||
fpath = xbmc.translatePath('special://temp') + '/plugin.video.peertube'
|
self.temp = xbmc.translatePath('special://temp') + '/plugin.video.peertube/'
|
||||||
if not xbmcvfs.exists(fpath):
|
if not xbmcvfs.exists(self.temp):
|
||||||
xbmcvfs.mkdir(fpath)
|
xbmcvfs.mkdir(self.temp)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
def download_torrent(self, data):
|
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
|
:param data: dict
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
xbmc.log('PeertubeService: Received a start_download signal', xbmc.LOGINFO)
|
xbmc.log('PeertubeService: Received a start_download signal', xbmc.LOGINFO)
|
||||||
downloader = PeertubeDownloader(data['magnet_f'])
|
downloader = PeertubeDownloader(data['url'], self.temp)
|
||||||
downloader.start()
|
downloader.start()
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -98,12 +100,11 @@ class PeertubeService():
|
||||||
while not monitor.abortRequested():
|
while not monitor.abortRequested():
|
||||||
if monitor.waitForAbort(1):
|
if monitor.waitForAbort(1):
|
||||||
# Abort was requested while waiting. We must exit
|
# Abort was requested while waiting. We must exit
|
||||||
# TODO: I must delete all temp files before exiting
|
# TODO: Clean temporary directory
|
||||||
break
|
break
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Start a peertubeService instance
|
# Start a peertubeService instance
|
||||||
xbmc.log('PeertubeService: Starting', xbmc.LOGINFO)
|
xbmc.log('PeertubeService: Starting', xbmc.LOGINFO)
|
||||||
|
|
Loading…
Reference in New Issue