2018-07-26 11:30:15 +02:00
|
|
|
# A Kodi Addon to play video hosted on the peertube service (http://joinpeertube.org/)
|
|
|
|
#
|
|
|
|
# TODO: - Delete downloaded files by default
|
|
|
|
# - Allow people to choose if they want to keep their download after watching?
|
2018-07-26 13:04:47 +02:00
|
|
|
# - Do sanity checks on received data
|
2018-07-30 12:00:27 +02:00
|
|
|
# - Handle languages better (with .po files)
|
2018-07-31 16:50:22 +02:00
|
|
|
# - Get the best quality torrent given settings and/or available bandwidth
|
2021-03-28 23:27:28 +02:00
|
|
|
# See how they do that in the peerTube client's code
|
|
|
|
import sys
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Python 3.x
|
|
|
|
from urllib.parse import parse_qsl
|
|
|
|
except ImportError:
|
|
|
|
# Python 2.x
|
|
|
|
from urlparse import parse_qsl
|
2018-07-26 11:30:15 +02:00
|
|
|
|
2018-07-26 17:53:27 +02:00
|
|
|
import AddonSignals
|
2021-03-28 23:27:28 +02:00
|
|
|
import requests
|
|
|
|
from requests.compat import urljoin, urlencode
|
|
|
|
import xbmc
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcplugin
|
|
|
|
import xbmcvfs
|
|
|
|
|
2018-07-26 11:30:15 +02:00
|
|
|
|
2018-07-26 17:53:27 +02:00
|
|
|
class PeertubeAddon():
|
|
|
|
"""
|
|
|
|
Main class of the addon
|
|
|
|
"""
|
2018-07-26 11:30:15 +02:00
|
|
|
|
2018-07-29 22:14:06 +02:00
|
|
|
def __init__(self, plugin, plugin_id):
|
2018-07-26 17:53:27 +02:00
|
|
|
"""
|
2018-07-27 14:43:21 +02:00
|
|
|
Initialisation of the PeertubeAddon class
|
2018-07-31 15:08:46 +02:00
|
|
|
:param plugin, plugin_id: str, int
|
2018-07-28 13:35:28 +02:00
|
|
|
:return: None
|
2018-07-26 17:53:27 +02:00
|
|
|
"""
|
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
# These 2 steps must be done first since the logging function requires
|
|
|
|
# the add-on name
|
|
|
|
# Get an Addon instance
|
|
|
|
addon = xbmcaddon.Addon()
|
|
|
|
# Get the add-on name
|
|
|
|
self.addon_name = addon.getAddonInfo('name')
|
|
|
|
|
|
|
|
self.debug('Initialising')
|
|
|
|
|
2018-07-29 22:14:06 +02:00
|
|
|
# Save addon URL and ID
|
|
|
|
self.plugin_url = plugin
|
|
|
|
self.plugin_id = plugin_id
|
|
|
|
|
2018-07-28 13:44:57 +02:00
|
|
|
# Select preferred instance by default
|
2021-03-28 23:27:28 +02:00
|
|
|
self.selected_inst = addon.getSetting('preferred_instance')
|
2018-07-28 13:44:57 +02:00
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
# Get the number of videos to show per page
|
2018-07-31 12:23:16 +02:00
|
|
|
self.items_per_page = int(addon.getSetting('items_per_page'))
|
2018-07-28 13:35:28 +02:00
|
|
|
|
2018-07-31 15:26:27 +02:00
|
|
|
# Get the video sort method
|
|
|
|
self.sort_method = addon.getSetting('video_sort_method')
|
|
|
|
|
2018-08-02 16:53:09 +02:00
|
|
|
# Get the preferred resolution for video
|
|
|
|
self.preferred_resolution = addon.getSetting('preferred_resolution')
|
|
|
|
|
2018-07-26 17:53:27 +02:00
|
|
|
# Nothing to play at initialisation
|
|
|
|
self.play = 0
|
2018-07-26 19:59:47 +02:00
|
|
|
self.torrent_name = ''
|
2018-11-24 17:15:33 +01:00
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
# Get the video filter from the settings that will be used when
|
|
|
|
# browsing the videos. The value from the settings is converted into
|
|
|
|
# one of the expected values by the REST APIs ("local" or "all-local")
|
|
|
|
if 'all-local' in addon.getSetting('video_filter'):
|
|
|
|
self.video_filter = 'all-local'
|
|
|
|
else:
|
|
|
|
self.video_filter = 'local'
|
|
|
|
|
2018-07-26 17:53:27 +02:00
|
|
|
return None
|
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
def debug(self, message):
|
|
|
|
"""Log a message in Kodi's log with the level xbmc.LOGDEBUG
|
|
|
|
|
|
|
|
:param message: Message to log
|
|
|
|
:type message: str
|
|
|
|
"""
|
|
|
|
xbmc.log('{0}: {1}'.format(self.addon_name, message), xbmc.LOGDEBUG)
|
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
def query_peertube(self, req):
|
2018-07-26 17:53:27 +02:00
|
|
|
"""
|
2018-07-31 16:50:22 +02:00
|
|
|
Issue a PeerTube API request and return the results
|
|
|
|
:param req: str
|
|
|
|
:result data: dict
|
2018-07-26 17:53:27 +02:00
|
|
|
"""
|
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
# Send the PeerTube REST API request
|
2021-03-28 23:27:28 +02:00
|
|
|
self.debug('Issuing request {0}'.format(req))
|
|
|
|
response = requests.get(url=req)
|
|
|
|
data = response.json()
|
|
|
|
|
|
|
|
# Use Request.raise_for_status() to raise an exception if the HTTP
|
|
|
|
# request returned an unsuccessful status code.
|
2018-07-31 16:50:22 +02:00
|
|
|
try:
|
2021-03-28 23:27:28 +02:00
|
|
|
response.raise_for_status()
|
|
|
|
except requests.HTTPError as e:
|
|
|
|
xbmcgui.Dialog().notification('Communication error',
|
|
|
|
'Error during request on {0}'
|
|
|
|
.format(self.selected_inst),
|
|
|
|
xbmcgui.NOTIFICATION_ERROR)
|
|
|
|
# Print the JSON as it may contain an 'error' key with the details
|
|
|
|
# of the error
|
|
|
|
self.debug('Error => "{}"'.format(data['error']))
|
|
|
|
raise e
|
2018-07-26 17:53:27 +02:00
|
|
|
|
2018-07-30 21:17:08 +02:00
|
|
|
# Return when no results are found
|
|
|
|
if data['total'] == 0:
|
2021-03-28 23:27:28 +02:00
|
|
|
self.debug('No result found')
|
2018-07-31 16:50:22 +02:00
|
|
|
return None
|
2018-07-29 22:14:06 +02:00
|
|
|
else:
|
2021-03-28 23:27:28 +02:00
|
|
|
self.debug('Found {0} results'.format(data['total']))
|
2018-07-31 16:50:22 +02:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
2018-07-31 21:46:16 +02:00
|
|
|
def create_list(self, lst, data_type, start):
|
2018-07-31 16:50:22 +02:00
|
|
|
"""
|
2018-07-31 21:46:16 +02:00
|
|
|
Create an array of xmbcgui.ListItem's from the lst parameter
|
|
|
|
:param lst, data_type, start: dict, str, str
|
2018-07-31 16:50:22 +02:00
|
|
|
:result listing: array
|
|
|
|
"""
|
2018-07-26 17:53:27 +02:00
|
|
|
# Create a list for our items.
|
2018-07-31 16:50:22 +02:00
|
|
|
listing = []
|
2018-07-31 21:46:16 +02:00
|
|
|
for data in lst['data']:
|
2018-07-26 17:53:27 +02:00
|
|
|
|
|
|
|
# Create a list item with a text label
|
2018-07-31 21:46:16 +02:00
|
|
|
list_item = xbmcgui.ListItem(label=data['name'])
|
2021-03-28 23:27:28 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
if data_type == 'videos':
|
|
|
|
# Add thumbnail
|
2018-07-31 21:46:16 +02:00
|
|
|
list_item.setArt({'thumb': self.selected_inst + '/' + data['thumbnailPath']})
|
2018-07-31 16:50:22 +02:00
|
|
|
|
|
|
|
# Set a fanart image for the list item.
|
2021-03-28 23:27:28 +02:00
|
|
|
# list_item.setProperty('fanart_image', data['thumb'])
|
2018-07-31 16:50:22 +02:00
|
|
|
|
|
|
|
# Compute media info from item's metadata
|
2021-03-28 23:27:28 +02:00
|
|
|
info = {'title': data['name'],
|
2018-07-31 21:46:16 +02:00
|
|
|
'playcount': data['views'],
|
|
|
|
'plotoutline': data['description'],
|
|
|
|
'duration': data['duration']
|
2018-07-31 16:50:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# For videos, add a rating based on likes and dislikes
|
2018-07-31 21:46:16 +02:00
|
|
|
if data['likes'] > 0 or data['dislikes'] > 0:
|
|
|
|
info['rating'] = data['likes']/(data['likes'] + data['dislikes'])
|
2018-07-31 16:50:22 +02:00
|
|
|
|
|
|
|
# Set additional info for the list item.
|
2021-03-28 23:27:28 +02:00
|
|
|
list_item.setInfo('video', info)
|
2018-07-31 16:50:22 +02:00
|
|
|
|
|
|
|
# Videos are playable
|
|
|
|
list_item.setProperty('IsPlayable', 'true')
|
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
# Find the URL of the best possible video matching user's preferences
|
2018-07-31 16:50:22 +02:00
|
|
|
# TODO: Error handling
|
2018-08-02 17:29:36 +02:00
|
|
|
current_res = 0
|
|
|
|
higher_res = -1
|
|
|
|
torrent_url = ''
|
2021-03-28 23:27:28 +02:00
|
|
|
response = requests.get(self.selected_inst + '/api/v1/videos/'
|
|
|
|
+ data['uuid'])
|
|
|
|
metadata = response.json()
|
|
|
|
self.debug('Looking for the best possible video quality matching user preferences')
|
2018-07-31 16:50:22 +02:00
|
|
|
for f in metadata['files']:
|
2021-03-28 23:27:28 +02:00
|
|
|
# Get file resolution
|
|
|
|
res = f['resolution']['id']
|
|
|
|
if res == self.preferred_resolution:
|
|
|
|
# Stop directly, when we find the exact same resolution as the user's preferred one
|
|
|
|
self.debug('Found video with preferred resolution')
|
|
|
|
torrent_url = f['torrentUrl']
|
|
|
|
break
|
|
|
|
elif res < self.preferred_resolution and res > current_res:
|
|
|
|
# Else, try to find the best one just below the user's preferred one
|
|
|
|
self.debug('Found video with good lower resolution'
|
|
|
|
'({0})'.format(f['resolution']['label']))
|
|
|
|
torrent_url = f['torrentUrl']
|
|
|
|
current_res = res
|
|
|
|
elif res > self.preferred_resolution and (res < higher_res or higher_res == -1):
|
|
|
|
# In the worst case, we'll take the one just above the user's preferred one
|
|
|
|
self.debug('Saving video with higher resolution ({0})'
|
|
|
|
'as a possible alternative'
|
|
|
|
.format(f['resolution']['label']))
|
|
|
|
backup_url = f['torrentUrl']
|
|
|
|
higher_res = res
|
2018-08-02 17:29:36 +02:00
|
|
|
|
|
|
|
# Use smallest file with an higher resolution, when we didn't find a resolution equal or
|
2021-03-28 23:27:28 +02:00
|
|
|
# lower than the user's preferred one
|
2018-08-02 17:29:36 +02:00
|
|
|
if not torrent_url:
|
2021-03-28 23:27:28 +02:00
|
|
|
self.debug('Using video with higher resolution as alternative')
|
|
|
|
torrent_url = backup_url
|
2018-08-02 17:29:36 +02:00
|
|
|
|
|
|
|
# Compose the correct URL for Kodi
|
2021-03-28 23:27:28 +02:00
|
|
|
url = self.build_kodi_url({
|
|
|
|
'action': 'play_video',
|
|
|
|
'url': torrent_url
|
|
|
|
})
|
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
elif data_type == 'instances':
|
2018-07-31 16:52:45 +02:00
|
|
|
# TODO: Add a context menu to select instance as preferred instance
|
2018-07-31 16:50:22 +02:00
|
|
|
# Instances are not playable
|
|
|
|
list_item.setProperty('IsPlayable', 'false')
|
|
|
|
|
|
|
|
# Set URL to select this instance
|
2021-03-28 23:27:28 +02:00
|
|
|
url = self.build_kodi_url({
|
|
|
|
'action': 'select_instance',
|
|
|
|
'url': data['host']
|
|
|
|
})
|
2018-07-26 11:30:15 +02:00
|
|
|
|
2018-07-26 21:44:13 +02:00
|
|
|
# Add our item to the listing as a 3-element tuple.
|
|
|
|
listing.append((url, list_item, False))
|
2018-07-26 11:30:15 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
# Add a 'Next page' button when there are more data to show
|
2018-07-31 10:43:17 +02:00
|
|
|
start = int(start) + self.items_per_page
|
2018-07-31 21:46:16 +02:00
|
|
|
if lst['total'] > start:
|
2021-03-28 23:27:28 +02:00
|
|
|
list_item = xbmcgui.ListItem(label='Next page ({0})'
|
|
|
|
.format(start/self.items_per_page))
|
|
|
|
url = self.build_kodi_url({
|
|
|
|
'action': 'browse_{0}'.format(data_type),
|
|
|
|
'start': start})
|
2018-07-29 22:14:06 +02:00
|
|
|
listing.append((url, list_item, True))
|
2018-07-28 13:54:19 +02:00
|
|
|
|
2018-07-30 12:00:27 +02:00
|
|
|
return listing
|
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
def build_video_rest_api_request(self, search, start):
|
|
|
|
"""Build the URL of an HTTP request using the PeerTube videos REST API.
|
|
|
|
|
|
|
|
The same function is used for browsing and searching videos.
|
|
|
|
|
|
|
|
:param search: keywords to search
|
|
|
|
:type search: string
|
|
|
|
:param start: offset
|
|
|
|
:type start: int
|
|
|
|
:return: the URL of the request
|
|
|
|
:rtype: str
|
|
|
|
|
|
|
|
Didn't yet find a correct way to do a search with a filter set to
|
|
|
|
local. Then if a search value is given it won't filter on local
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Common parameters of the request
|
|
|
|
params = {
|
|
|
|
'count': self.items_per_page,
|
|
|
|
'start': start,
|
|
|
|
'sort': self.sort_method
|
|
|
|
}
|
|
|
|
|
|
|
|
# Depending on the type of request (search or list videos), add
|
|
|
|
# specific parameters and define the API to use
|
2018-11-24 17:15:33 +01:00
|
|
|
if search is None:
|
2021-03-28 23:27:28 +02:00
|
|
|
# Video API does not provide "search" but provides "filter" so add
|
|
|
|
# it to the parameters
|
|
|
|
params.update({'filter': self.video_filter})
|
|
|
|
api_url = '/api/v1/videos'
|
2018-11-24 17:15:33 +01:00
|
|
|
else:
|
2021-03-28 23:27:28 +02:00
|
|
|
# Search API does not provide "filter" but provides "search" so add
|
|
|
|
# it to the parameters
|
|
|
|
params.update({'search': search})
|
|
|
|
api_url = '/api/v1/search/videos'
|
|
|
|
|
|
|
|
# Build the full URL of the request (instance + API + parameters)
|
|
|
|
req = '{0}?{1}'.format(urljoin(self.selected_inst, api_url),
|
|
|
|
urlencode(params))
|
|
|
|
|
|
|
|
return req
|
|
|
|
|
|
|
|
def build_browse_instances_rest_api_request(self, start):
|
|
|
|
"""Build the URL of an HTTP request using the PeerTube REST API to
|
|
|
|
browse the PeerTube instances.
|
|
|
|
|
|
|
|
:param start: offset
|
|
|
|
:type start: int
|
|
|
|
:return: the URL of the request
|
|
|
|
:rtype: str
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Create the parameters of the request
|
|
|
|
params = {
|
|
|
|
'count': self.items_per_page,
|
|
|
|
'start': start
|
|
|
|
}
|
|
|
|
|
|
|
|
# Join the base URL with the REST API and the parameters
|
|
|
|
req = '{0}?{1}'.format('https://instances.joinpeertube.org/api/v1/instances',
|
|
|
|
urlencode(params))
|
|
|
|
|
2018-11-24 17:15:33 +01:00
|
|
|
return req
|
|
|
|
|
2018-07-30 12:00:27 +02:00
|
|
|
def search_videos(self, start):
|
|
|
|
"""
|
2018-07-31 16:50:22 +02:00
|
|
|
Function to search for videos on a PeerTube instance and navigate in the results
|
2018-07-30 12:00:27 +02:00
|
|
|
:param start: string
|
|
|
|
:result: None
|
|
|
|
"""
|
|
|
|
|
2018-07-30 21:23:44 +02:00
|
|
|
# Show a 'Search videos' dialog
|
|
|
|
search = xbmcgui.Dialog().input(heading='Search videos on ' + self.selected_inst, type=xbmcgui.INPUT_ALPHANUM)
|
2018-07-31 15:08:46 +02:00
|
|
|
|
2018-07-30 12:00:27 +02:00
|
|
|
# Go back to main menu when user cancels
|
|
|
|
if not search:
|
2018-07-31 22:36:48 +02:00
|
|
|
return None
|
2021-03-28 23:27:28 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
# Create the PeerTube REST API request for searching videos
|
2021-03-28 23:27:28 +02:00
|
|
|
req = self.build_video_rest_api_request(search, start)
|
2018-07-30 12:00:27 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
# Send the query
|
|
|
|
results = self.query_peertube(req)
|
2018-07-30 12:00:27 +02:00
|
|
|
|
2018-07-31 22:36:48 +02:00
|
|
|
# Exit directly when no result is found
|
|
|
|
if not results:
|
|
|
|
xbmcgui.Dialog().notification('No videos found', 'No videos found matching query', xbmcgui.NOTIFICATION_WARNING)
|
|
|
|
return None
|
|
|
|
|
2018-07-30 12:33:37 +02:00
|
|
|
# Create array of xmbcgui.ListItem's
|
2018-07-31 16:50:22 +02:00
|
|
|
listing = self.create_list(results, 'videos', start)
|
2018-07-30 12:00:27 +02:00
|
|
|
|
|
|
|
# Add our listing to Kodi.
|
|
|
|
xbmcplugin.addDirectoryItems(self.plugin_id, listing, len(listing))
|
|
|
|
xbmcplugin.endOfDirectory(self.plugin_id)
|
2018-07-31 16:50:22 +02:00
|
|
|
|
|
|
|
return None
|
2021-03-28 23:27:28 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
def browse_videos(self, start):
|
2018-07-30 12:00:27 +02:00
|
|
|
"""
|
2018-07-31 16:50:22 +02:00
|
|
|
Function to navigate through all the video published by a PeerTube instance
|
2018-07-30 12:00:27 +02:00
|
|
|
:param start: string
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
# Create the PeerTube REST API request for listing videos
|
2021-03-28 23:27:28 +02:00
|
|
|
req = self.build_video_rest_api_request(None, start)
|
2018-07-31 16:50:22 +02:00
|
|
|
|
|
|
|
# Send the query
|
|
|
|
results = self.query_peertube(req)
|
2018-07-30 12:00:27 +02:00
|
|
|
|
2018-07-30 12:33:37 +02:00
|
|
|
# Create array of xmbcgui.ListItem's
|
2018-07-31 16:50:22 +02:00
|
|
|
listing = self.create_list(results, 'videos', start)
|
2018-07-30 12:00:27 +02:00
|
|
|
|
2018-07-26 17:53:27 +02:00
|
|
|
# Add our listing to Kodi.
|
2018-07-29 22:14:06 +02:00
|
|
|
xbmcplugin.addDirectoryItems(self.plugin_id, listing, len(listing))
|
|
|
|
xbmcplugin.endOfDirectory(self.plugin_id)
|
2018-07-26 11:30:15 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def browse_instances(self, start):
|
2021-03-28 23:27:28 +02:00
|
|
|
"""
|
2018-07-31 16:50:22 +02:00
|
|
|
Function to navigate through all PeerTube instances
|
|
|
|
:param start: str
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Create the PeerTube REST API request for browsing PeerTube instances
|
2021-03-28 23:27:28 +02:00
|
|
|
req = self.build_browse_instances_rest_api_request(start)
|
2018-07-31 16:50:22 +02:00
|
|
|
|
|
|
|
# Send the query
|
|
|
|
results = self.query_peertube(req)
|
|
|
|
|
|
|
|
# Create array of xmbcgui.ListItem's
|
|
|
|
listing = self.create_list(results, 'instances', start)
|
|
|
|
|
|
|
|
# Add our listing to Kodi.
|
|
|
|
xbmcplugin.addDirectoryItems(self.plugin_id, listing, len(listing))
|
|
|
|
xbmcplugin.endOfDirectory(self.plugin_id)
|
2021-03-28 23:27:28 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
return None
|
|
|
|
|
2018-07-27 18:40:24 +02:00
|
|
|
def play_video_continue(self, data):
|
2018-07-26 17:53:27 +02:00
|
|
|
"""
|
2018-07-26 19:59:47 +02:00
|
|
|
Callback function to let the play_video function resume when the PeertubeDownloader
|
|
|
|
has downloaded all the torrent's metadata
|
|
|
|
:param data: dict
|
|
|
|
:return: None
|
2018-07-26 17:53:27 +02:00
|
|
|
"""
|
2018-07-26 11:30:15 +02:00
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
self.debug('Received metadata_downloaded signal, will start playing media')
|
|
|
|
self.play = 1
|
2018-07-27 10:53:28 +02:00
|
|
|
self.torrent_f = data['file']
|
2018-07-26 19:59:47 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
return None
|
2018-07-26 17:53:27 +02:00
|
|
|
|
2018-07-27 10:53:28 +02:00
|
|
|
def play_video(self, torrent_url):
|
2018-07-26 17:53:27 +02:00
|
|
|
"""
|
|
|
|
Start the torrent's download and play it while being downloaded
|
2018-07-27 10:53:28 +02:00
|
|
|
:param torrent_url: str
|
2018-07-26 17:53:27 +02:00
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
self.debug('Starting torrent download ({0})'.format(torrent_url))
|
2018-07-31 15:16:24 +02:00
|
|
|
|
2018-07-26 17:53:27 +02:00
|
|
|
# Start a downloader thread
|
2018-07-27 10:53:28 +02:00
|
|
|
AddonSignals.sendSignal('start_download', {'url': torrent_url})
|
2018-07-26 17:53:27 +02:00
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
# Wait until the PeerTubeDownloader has downloaded all the torrent's metadata
|
2018-07-26 21:59:50 +02:00
|
|
|
AddonSignals.registerSlot('plugin.video.peertube', 'metadata_downloaded', self.play_video_continue)
|
2018-07-31 15:16:24 +02:00
|
|
|
timeout = 0
|
|
|
|
while self.play == 0 and timeout < 10:
|
2018-07-26 17:53:27 +02:00
|
|
|
xbmc.sleep(1000)
|
2018-07-31 15:16:24 +02:00
|
|
|
timeout += 1
|
|
|
|
|
|
|
|
# Abort in case of timeout
|
|
|
|
if timeout == 10:
|
|
|
|
xbmcgui.Dialog().notification('Download timeout', 'Timeout fetching ' + torrent_url, xbmcgui.NOTIFICATION_ERROR)
|
2018-07-31 16:50:22 +02:00
|
|
|
return None
|
2018-07-31 15:16:24 +02:00
|
|
|
else:
|
|
|
|
# Wait a little before starting playing the torrent
|
|
|
|
xbmc.sleep(3000)
|
2018-07-26 17:53:27 +02:00
|
|
|
|
|
|
|
# Pass the item to the Kodi player for actual playback.
|
2021-03-28 23:27:28 +02:00
|
|
|
self.debug('Starting video playback ({0})'.format(torrent_url))
|
2018-07-27 10:53:28 +02:00
|
|
|
play_item = xbmcgui.ListItem(path=self.torrent_f)
|
2018-07-29 22:14:06 +02:00
|
|
|
xbmcplugin.setResolvedUrl(self.plugin_id, True, listitem=play_item)
|
2018-07-26 17:53:27 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
return None
|
2021-03-28 23:27:28 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
def select_instance(self, instance):
|
|
|
|
"""
|
|
|
|
Change currently selected instance to 'instance' parameter
|
|
|
|
:param instance: str
|
|
|
|
:return: None
|
|
|
|
"""
|
2021-03-28 23:27:28 +02:00
|
|
|
|
2018-07-31 22:06:58 +02:00
|
|
|
self.selected_inst = 'https://' + instance
|
|
|
|
xbmcgui.Dialog().notification('Current instance changed', 'Changed current instance to {0}'.format(self.selected_inst), xbmcgui.NOTIFICATION_INFO)
|
2021-03-28 23:27:28 +02:00
|
|
|
self.debug('Changing currently selected instance to {0}'
|
|
|
|
.format(self.selected_inst))
|
2018-07-31 16:50:22 +02:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
def build_kodi_url(self, parameters):
|
|
|
|
"""Build a Kodi URL based on the parameters.
|
|
|
|
|
|
|
|
:param parameters: dict containing all the parameters that will be
|
|
|
|
encoded in the URL
|
|
|
|
"""
|
2018-11-24 17:15:33 +01:00
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
return '{0}?{1}'.format(self.plugin_url, urlencode(parameters))
|
2018-11-24 17:15:33 +01:00
|
|
|
|
2018-07-28 13:35:28 +02:00
|
|
|
def main_menu(self):
|
|
|
|
"""
|
2018-07-31 15:08:46 +02:00
|
|
|
Addon's main menu
|
|
|
|
:param: None
|
|
|
|
:return: None
|
2018-07-28 13:35:28 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Create a list for our items.
|
|
|
|
listing = []
|
|
|
|
|
|
|
|
# 1st menu entry
|
|
|
|
list_item = xbmcgui.ListItem(label='Browse selected instance')
|
2021-03-28 23:27:28 +02:00
|
|
|
url = self.build_kodi_url({'action': 'browse_videos', 'start': 0})
|
2018-07-29 22:14:06 +02:00
|
|
|
listing.append((url, list_item, True))
|
2018-07-28 13:35:28 +02:00
|
|
|
|
|
|
|
# 2nd menu entry
|
|
|
|
list_item = xbmcgui.ListItem(label='Search on selected instance')
|
2021-03-28 23:27:28 +02:00
|
|
|
url = self.build_kodi_url({'action': 'search_videos', 'start': 0})
|
2018-07-31 22:00:33 +02:00
|
|
|
listing.append((url, list_item, True))
|
2018-07-28 13:35:28 +02:00
|
|
|
|
|
|
|
# 3rd menu entry
|
|
|
|
list_item = xbmcgui.ListItem(label='Select other instance')
|
2021-03-28 23:27:28 +02:00
|
|
|
url = self.build_kodi_url({'action': 'browse_instances', 'start': 0})
|
2018-07-31 22:00:33 +02:00
|
|
|
listing.append((url, list_item, True))
|
2018-07-28 13:35:28 +02:00
|
|
|
|
|
|
|
# Add our listing to Kodi.
|
2018-07-29 22:14:06 +02:00
|
|
|
xbmcplugin.addDirectoryItems(self.plugin_id, listing, len(listing))
|
2018-07-28 13:35:28 +02:00
|
|
|
|
|
|
|
# Finish creating a virtual folder.
|
2018-07-29 22:14:06 +02:00
|
|
|
xbmcplugin.endOfDirectory(self.plugin_id)
|
2021-03-28 23:27:28 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
return None
|
2018-07-28 13:35:28 +02:00
|
|
|
|
2018-07-26 17:53:27 +02:00
|
|
|
def router(self, paramstring):
|
|
|
|
"""
|
|
|
|
Router function that calls other functions
|
|
|
|
depending on the provided paramstring
|
|
|
|
:param paramstring: dict
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Parse a URL-encoded paramstring to the dictionary of
|
|
|
|
# {<parameter>: <value>} elements
|
|
|
|
params = dict(parse_qsl(paramstring[1:]))
|
|
|
|
|
|
|
|
# Check the parameters passed to the plugin
|
|
|
|
if params:
|
2018-11-24 17:15:33 +01:00
|
|
|
action = params['action']
|
|
|
|
if action == 'browse_videos':
|
2018-07-31 16:50:22 +02:00
|
|
|
# Browse videos on selected instance
|
|
|
|
self.browse_videos(params['start'])
|
2018-11-24 17:15:33 +01:00
|
|
|
elif action == 'search_videos':
|
2018-07-30 21:17:08 +02:00
|
|
|
# Search for videos on selected instance
|
2018-07-30 12:33:37 +02:00
|
|
|
self.search_videos(params['start'])
|
2018-11-24 17:15:33 +01:00
|
|
|
elif action == 'browse_instances':
|
2018-07-31 16:50:22 +02:00
|
|
|
# Browse peerTube instances
|
|
|
|
self.browse_instances(params['start'])
|
2018-11-24 17:15:33 +01:00
|
|
|
elif action == 'play_video':
|
2018-07-28 13:35:28 +02:00
|
|
|
# Play video from provided URL.
|
|
|
|
self.play_video(params['url'])
|
2018-11-24 17:15:33 +01:00
|
|
|
elif action == 'select_instance':
|
2018-07-31 16:50:22 +02:00
|
|
|
self.select_instance(params['url'])
|
2018-07-26 17:53:27 +02:00
|
|
|
else:
|
2018-07-28 13:35:28 +02:00
|
|
|
# Display the addon's main menu when the plugin is called from Kodi UI without any parameters
|
|
|
|
self.main_menu()
|
2018-07-26 11:30:15 +02:00
|
|
|
|
2018-07-31 16:50:22 +02:00
|
|
|
return None
|
|
|
|
|
2021-03-28 23:27:28 +02:00
|
|
|
|
2018-07-26 11:30:15 +02:00
|
|
|
if __name__ == '__main__':
|
2018-07-26 17:53:27 +02:00
|
|
|
|
|
|
|
# Initialise addon
|
2018-07-29 22:14:06 +02:00
|
|
|
addon = PeertubeAddon(sys.argv[0], int(sys.argv[1]))
|
2018-07-26 11:30:15 +02:00
|
|
|
# Call the router function and pass the plugin call parameters to it.
|
2018-07-26 17:53:27 +02:00
|
|
|
addon.router(sys.argv[2])
|