2020-03-19 16:45:31 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
""" Player module """
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from resources.lib import kodiutils
|
2020-04-20 08:59:10 +02:00
|
|
|
from resources.lib.modules.menu import Menu
|
2020-11-04 12:48:33 +01:00
|
|
|
from resources.lib.viervijfzes import CHANNELS, ResolvedStream
|
2020-03-19 16:45:31 +01:00
|
|
|
from resources.lib.viervijfzes.auth import AuthApi
|
2021-02-09 20:54:40 +01:00
|
|
|
from resources.lib.viervijfzes.aws.cognito_idp import AuthenticationException, InvalidLoginException
|
2021-03-22 13:40:26 +01:00
|
|
|
from resources.lib.viervijfzes.content import CACHE_PREVENT, ContentApi, GeoblockedException, UnavailableException
|
2020-03-19 16:45:31 +01:00
|
|
|
|
2021-02-17 07:42:24 +01:00
|
|
|
try: # Python 3
|
|
|
|
from urllib.parse import quote, urlencode
|
|
|
|
except ImportError: # Python 2
|
|
|
|
from urllib import quote, urlencode
|
|
|
|
|
2020-10-26 10:25:57 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Player:
|
|
|
|
""" Code responsible for playing media """
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
""" Initialise object """
|
2020-04-20 08:59:10 +02:00
|
|
|
auth = AuthApi(kodiutils.get_setting('username'), kodiutils.get_setting('password'), kodiutils.get_tokens_path())
|
|
|
|
self._api = ContentApi(auth, cache_path=kodiutils.get_cache_path())
|
|
|
|
|
|
|
|
# Workaround for Raspberry Pi 3 and older
|
|
|
|
kodiutils.set_global_setting('videoplayer.useomxplayer', True)
|
2020-03-19 16:45:31 +01:00
|
|
|
|
2020-05-25 20:41:38 +02:00
|
|
|
@staticmethod
|
|
|
|
def live(channel):
|
|
|
|
""" Play the live channel.
|
|
|
|
:type channel: string
|
|
|
|
"""
|
|
|
|
channel_name = CHANNELS.get(channel, dict(name=channel))
|
|
|
|
kodiutils.ok_dialog(message=kodiutils.localize(30718, channel=channel_name.get('name'))) # There is no live stream available for {channel}.
|
|
|
|
kodiutils.end_of_directory()
|
|
|
|
|
2021-02-01 08:53:13 +01:00
|
|
|
def play_from_page(self, path):
|
2020-03-19 16:45:31 +01:00
|
|
|
""" Play the requested item.
|
|
|
|
:type path: string
|
|
|
|
"""
|
|
|
|
# Get episode information
|
2021-03-22 13:40:26 +01:00
|
|
|
episode = self._api.get_episode(path, cache=CACHE_PREVENT)
|
2020-04-20 08:59:10 +02:00
|
|
|
resolved_stream = None
|
2020-03-19 16:45:31 +01:00
|
|
|
|
2020-05-25 20:41:38 +02:00
|
|
|
if episode is None:
|
|
|
|
kodiutils.ok_dialog(message=kodiutils.localize(30712))
|
|
|
|
return
|
|
|
|
|
2020-04-20 08:59:10 +02:00
|
|
|
if episode.stream:
|
|
|
|
# We already have a resolved stream. Nice!
|
|
|
|
# We don't need credentials for these streams.
|
2020-11-04 12:48:33 +01:00
|
|
|
resolved_stream = ResolvedStream(
|
|
|
|
uuid=episode.uuid,
|
|
|
|
url=episode.stream,
|
|
|
|
)
|
2020-05-11 14:12:13 +02:00
|
|
|
_LOGGER.debug('Already got a resolved stream: %s', resolved_stream)
|
2020-03-19 16:45:31 +01:00
|
|
|
|
2020-04-20 08:59:10 +02:00
|
|
|
if episode.uuid:
|
|
|
|
# Lookup the stream
|
|
|
|
resolved_stream = self._resolve_stream(episode.uuid)
|
2020-05-11 14:12:13 +02:00
|
|
|
_LOGGER.debug('Resolved stream: %s', resolved_stream)
|
2020-04-20 08:59:10 +02:00
|
|
|
|
|
|
|
if resolved_stream:
|
|
|
|
titleitem = Menu.generate_titleitem(episode)
|
2020-11-04 12:48:33 +01:00
|
|
|
if resolved_stream.license_url:
|
|
|
|
# Generate license key
|
|
|
|
license_key = self.create_license_key(resolved_stream.license_url,
|
|
|
|
key_headers=dict(
|
|
|
|
customdata=resolved_stream.auth,
|
|
|
|
))
|
|
|
|
else:
|
|
|
|
license_key = None
|
|
|
|
|
|
|
|
kodiutils.play(resolved_stream.url,
|
|
|
|
resolved_stream.stream_type,
|
|
|
|
license_key,
|
|
|
|
info_dict=titleitem.info_dict,
|
|
|
|
art_dict=titleitem.art_dict,
|
|
|
|
prop_dict=titleitem.prop_dict)
|
2020-04-20 08:59:10 +02:00
|
|
|
|
|
|
|
def play(self, uuid):
|
2020-03-19 16:45:31 +01:00
|
|
|
""" Play the requested item.
|
2020-04-20 08:59:10 +02:00
|
|
|
:type uuid: string
|
2020-03-19 16:45:31 +01:00
|
|
|
"""
|
2020-04-20 08:59:10 +02:00
|
|
|
# Lookup the stream
|
|
|
|
resolved_stream = self._resolve_stream(uuid)
|
2020-11-04 12:48:33 +01:00
|
|
|
if resolved_stream.license_url:
|
|
|
|
# Generate license key
|
|
|
|
license_key = self.create_license_key(resolved_stream.license_url, key_headers=dict(
|
|
|
|
customdata=resolved_stream.auth,
|
|
|
|
))
|
|
|
|
else:
|
|
|
|
license_key = None
|
|
|
|
|
|
|
|
kodiutils.play(resolved_stream.url, resolved_stream.stream_type, license_key)
|
2020-03-25 07:50:45 +01:00
|
|
|
|
2020-04-20 08:59:10 +02:00
|
|
|
@staticmethod
|
|
|
|
def _resolve_stream(uuid):
|
|
|
|
""" Resolve the stream for the requested item
|
|
|
|
:type uuid: string
|
|
|
|
"""
|
2020-03-19 16:45:31 +01:00
|
|
|
try:
|
2020-03-21 20:34:07 +01:00
|
|
|
# Check if we have credentials
|
|
|
|
if not kodiutils.get_setting('username') or not kodiutils.get_setting('password'):
|
2020-03-22 10:30:23 +01:00
|
|
|
confirm = kodiutils.yesno_dialog(
|
|
|
|
message=kodiutils.localize(30701)) # To watch a video, you need to enter your credentials. Do you want to enter them now?
|
2020-03-21 20:34:07 +01:00
|
|
|
if confirm:
|
|
|
|
kodiutils.open_settings()
|
|
|
|
kodiutils.end_of_directory()
|
2020-04-20 08:59:10 +02:00
|
|
|
return None
|
2020-03-21 20:34:07 +01:00
|
|
|
|
|
|
|
# Fetch an auth token now
|
2020-03-22 10:30:23 +01:00
|
|
|
try:
|
2020-04-01 11:01:22 +02:00
|
|
|
auth = AuthApi(kodiutils.get_setting('username'), kodiutils.get_setting('password'), kodiutils.get_tokens_path())
|
2020-03-22 15:37:15 +01:00
|
|
|
|
|
|
|
# Get stream information
|
2020-04-20 08:59:10 +02:00
|
|
|
resolved_stream = ContentApi(auth).get_stream_by_uuid(uuid)
|
|
|
|
return resolved_stream
|
2020-03-22 15:37:15 +01:00
|
|
|
|
2020-03-22 10:30:23 +01:00
|
|
|
except (InvalidLoginException, AuthenticationException) as ex:
|
2020-11-28 17:13:21 +01:00
|
|
|
_LOGGER.exception(ex)
|
2020-03-22 15:37:15 +01:00
|
|
|
kodiutils.ok_dialog(message=kodiutils.localize(30702, error=str(ex)))
|
2020-03-22 10:30:23 +01:00
|
|
|
kodiutils.end_of_directory()
|
2020-04-20 08:59:10 +02:00
|
|
|
return None
|
2020-03-21 20:34:07 +01:00
|
|
|
|
2020-03-19 16:45:31 +01:00
|
|
|
except GeoblockedException:
|
|
|
|
kodiutils.ok_dialog(heading=kodiutils.localize(30709), message=kodiutils.localize(30710)) # This video is geo-blocked...
|
2020-04-20 08:59:10 +02:00
|
|
|
return None
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
except UnavailableException:
|
|
|
|
kodiutils.ok_dialog(heading=kodiutils.localize(30711), message=kodiutils.localize(30712)) # The video is unavailable...
|
2020-04-20 08:59:10 +02:00
|
|
|
return None
|
2020-11-04 12:48:33 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_license_key(key_url, key_type='R', key_headers=None, key_value=None):
|
|
|
|
""" Create a license key string that we need for inputstream.adaptive.
|
|
|
|
|
|
|
|
:param str key_url:
|
|
|
|
:param str key_type:
|
|
|
|
:param dict[str, str] key_headers:
|
|
|
|
:param str key_value:
|
|
|
|
:rtype: str
|
|
|
|
"""
|
|
|
|
header = ''
|
|
|
|
if key_headers:
|
|
|
|
header = urlencode(key_headers)
|
|
|
|
|
|
|
|
if key_type in ('A', 'R', 'B'):
|
|
|
|
key_value = key_type + '{SSM}'
|
|
|
|
elif key_type == 'D':
|
|
|
|
if 'D{SSM}' not in key_value:
|
|
|
|
raise ValueError('Missing D{SSM} placeholder')
|
|
|
|
key_value = quote(key_value)
|
|
|
|
|
|
|
|
return '%s|%s|%s|' % (key_url, header, key_value)
|