From c44dbcd4d3a36d4aa04f0550915c65f4fb137cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Arnauts?= Date: Sat, 21 Mar 2020 20:34:07 +0100 Subject: [PATCH] Improve authentication handling --- .../resource.language.en_gb/strings.po | 6 +++++- .../resource.language.nl_nl/strings.po | 8 +++++-- resources/lib/kodiutils.py | 8 +++++++ resources/lib/modules/player.py | 21 ++++++++++++++----- resources/lib/modules/search.py | 2 +- resources/lib/viervijfzes/content.py | 15 +++++++------ test/test_api.py | 19 +++++++---------- 7 files changed, 53 insertions(+), 26 deletions(-) diff --git a/resources/language/resource.language.en_gb/strings.po b/resources/language/resource.language.en_gb/strings.po index 4eca69c..aa07791 100644 --- a/resources/language/resource.language.en_gb/strings.po +++ b/resources/language/resource.language.en_gb/strings.po @@ -92,6 +92,10 @@ msgstr "" ### MESSAGES +msgctxt "#30701" +msgid "To watch a video, you need to enter your credentials. Do you want to enter them now?" +msgstr "" + msgctxt "#30709" msgid "Geo-blocked video" msgstr "" @@ -113,7 +117,7 @@ msgid "The requested video was not found in the guide." msgstr "" msgctxt "#30717" -msgid "This program is not available in the Vier/Vijf/Zes catalogue." +msgid "This program is not available in the catalogue." msgstr "" diff --git a/resources/language/resource.language.nl_nl/strings.po b/resources/language/resource.language.nl_nl/strings.po index 585ae26..07ae5a4 100644 --- a/resources/language/resource.language.nl_nl/strings.po +++ b/resources/language/resource.language.nl_nl/strings.po @@ -93,6 +93,10 @@ msgstr "Morgen" ### MESSAGES +msgctxt "#30701" +msgid "To watch a video, you need to enter your credentials. Do you want to enter them now?" +msgstr "Om een video te bekijken moet je je inloggegevens ingeven. Wil je dit nu doen?" + msgctxt "#30709" msgid "Geo-blocked video" msgstr "Video is geografisch geblokkeerd" @@ -114,8 +118,8 @@ msgid "The requested video was not found in the guide." msgstr "De gevraagde video werd niet gevonden in de tv-gids." msgctxt "#30717" -msgid "This program is not available in the Vier/Vijf/Zes catalogue." -msgstr "Dit programma is niet beschikbaar in de Vier/Vijf/Zes catalogus." +msgid "This program is not available in the catalogue." +msgstr "Dit programma is niet beschikbaar in de catalogus." ### SETTINGS diff --git a/resources/lib/kodiutils.py b/resources/lib/kodiutils.py index c56fcef..60e7ca2 100644 --- a/resources/lib/kodiutils.py +++ b/resources/lib/kodiutils.py @@ -221,6 +221,14 @@ def ok_dialog(heading='', message=''): return Dialog().ok(heading=heading, line1=message) +def yesno_dialog(heading='', message='', nolabel=None, yeslabel=None): + """Show Kodi's OK dialog""" + from xbmcgui import Dialog + if not heading: + heading = addon_name() + return Dialog().yesno(heading=heading, line1=message, nolabel=nolabel, yeslabel=yeslabel) + + def notification(heading='', message='', icon='info', time=4000): """Show a Kodi notification""" from xbmcgui import Dialog diff --git a/resources/lib/modules/player.py b/resources/lib/modules/player.py index c7dd59e..0eb86a3 100644 --- a/resources/lib/modules/player.py +++ b/resources/lib/modules/player.py @@ -17,8 +17,6 @@ class Player: def __init__(self): """ Initialise object """ - self._auth = AuthApi(kodiutils.get_setting('username'), kodiutils.get_setting('password'), kodiutils.get_tokens_path()) - self._api = ContentApi(self._auth.get_token()) def play_from_page(self, channel, path): """ Play the requested item. @@ -26,19 +24,32 @@ class Player: :type path: string """ # Get episode information - episode = self._api.get_episode(channel, path) + episode = ContentApi().get_episode(channel, path) # Play this now we have the uuid self.play(channel, episode.uuid) - def play(self, channel, item): + @staticmethod + def play(channel, item): """ Play the requested item. :type channel: string :type item: string """ try: + # Check if we have credentials + if not kodiutils.get_setting('username') or not kodiutils.get_setting('password'): + 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? + if confirm: + kodiutils.open_settings() + kodiutils.end_of_directory() + return + + # Fetch an auth token now + auth = AuthApi(kodiutils.get_setting('username'), kodiutils.get_setting('password'), kodiutils.get_tokens_path()) + token = auth.get_token() + # Get stream information - resolved_stream = self._api.get_stream(channel, item) + resolved_stream = ContentApi(token).get_stream(channel, item) except GeoblockedException: kodiutils.ok_dialog(heading=kodiutils.localize(30709), message=kodiutils.localize(30710)) # This video is geo-blocked... diff --git a/resources/lib/modules/search.py b/resources/lib/modules/search.py index b212d94..74f0c5a 100644 --- a/resources/lib/modules/search.py +++ b/resources/lib/modules/search.py @@ -26,7 +26,7 @@ class Search: """ if not query: # Ask for query - query = kodiutils.get_search_string(heading=kodiutils.localize(30009)) # Search Vier/Vijf/Zes + query = kodiutils.get_search_string(heading=kodiutils.localize(30009)) # Search if not query: kodiutils.end_of_directory() return diff --git a/resources/lib/viervijfzes/content.py b/resources/lib/viervijfzes/content.py index 398caaa..dd95e13 100644 --- a/resources/lib/viervijfzes/content.py +++ b/resources/lib/viervijfzes/content.py @@ -8,8 +8,8 @@ import logging import re from datetime import datetime -from six.moves.html_parser import HTMLParser import requests +from six.moves.html_parser import HTMLParser from resources.lib.viervijfzes import CHANNELS @@ -130,12 +130,10 @@ class ContentApi: """ Vier/Vijf/Zes Content API""" API_ENDPOINT = 'https://api.viervijfzes.be' - def __init__(self, token): + def __init__(self, token=None): """ Initialise object """ - self._token = token - self._session = requests.session() - self._session.headers['authorization'] = token + self._token = token def get_notifications(self): """ Get a list of notifications for your account. @@ -321,7 +319,12 @@ class ContentApi: :type url: str :rtype str """ - response = self._session.get(url, params=params) + if self._token: + response = self._session.get(url, params=params, headers={ + 'authorization': self._token, + }) + else: + response = self._session.get(url, params=params) if response.status_code != 200: raise Exception('Could not fetch data') diff --git a/test/test_api.py b/test/test_api.py index 2a83390..b87a864 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -8,9 +8,9 @@ from __future__ import absolute_import, division, print_function, unicode_litera import logging import unittest -from resources.lib import kodiutils -from resources.lib.viervijfzes.auth import AuthApi +import resources.lib.kodiutils as kodiutils from resources.lib.viervijfzes.content import ContentApi, Program, Episode +from resources.lib.viervijfzes.auth import AuthApi _LOGGER = logging.getLogger('test-api') @@ -18,22 +18,16 @@ _LOGGER = logging.getLogger('test-api') class TestApi(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestApi, self).__init__(*args, **kwargs) - self._auth = AuthApi(kodiutils.get_setting('username'), kodiutils.get_setting('password'), kodiutils.get_tokens_path()) - - def test_notifications(self): - api = ContentApi(self._auth.get_token()) - notifications = api.get_notifications() - self.assertIsInstance(notifications, list) def test_programs(self): - api = ContentApi(self._auth.get_token()) + api = ContentApi() for channel in ['vier', 'vijf', 'zes']: channels = api.get_programs(channel) self.assertIsInstance(channels, list) def test_episodes(self): - api = ContentApi(self._auth.get_token()) + api = ContentApi() for channel, program in [('vier', 'auwch'), ('vijf', 'zo-man-zo-vrouw')]: program = api.get_program(channel, program) @@ -45,7 +39,10 @@ class TestApi(unittest.TestCase): _LOGGER.info('Got program: %s', program) def test_get_stream(self): - api = ContentApi(self._auth.get_token()) + auth = AuthApi(kodiutils.get_setting('username'), kodiutils.get_setting('password'), kodiutils.get_tokens_path()) + token = auth.get_token() + + api = ContentApi(token) program = api.get_program('vier', 'auwch') episode = program.episodes[0] video = api.get_stream(episode.channel, episode.uuid)