Improve authentication handling

This commit is contained in:
Michaël Arnauts 2020-03-21 20:34:07 +01:00
parent f5786437f5
commit c44dbcd4d3
7 changed files with 53 additions and 26 deletions

View File

@ -92,6 +92,10 @@ msgstr ""
### MESSAGES ### MESSAGES
msgctxt "#30701"
msgid "To watch a video, you need to enter your credentials. Do you want to enter them now?"
msgstr ""
msgctxt "#30709" msgctxt "#30709"
msgid "Geo-blocked video" msgid "Geo-blocked video"
msgstr "" msgstr ""
@ -113,7 +117,7 @@ msgid "The requested video was not found in the guide."
msgstr "" msgstr ""
msgctxt "#30717" msgctxt "#30717"
msgid "This program is not available in the Vier/Vijf/Zes catalogue." msgid "This program is not available in the catalogue."
msgstr "" msgstr ""

View File

@ -93,6 +93,10 @@ msgstr "Morgen"
### MESSAGES ### 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" msgctxt "#30709"
msgid "Geo-blocked video" msgid "Geo-blocked video"
msgstr "Video is geografisch geblokkeerd" 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." msgstr "De gevraagde video werd niet gevonden in de tv-gids."
msgctxt "#30717" msgctxt "#30717"
msgid "This program is not available in the Vier/Vijf/Zes catalogue." msgid "This program is not available in the catalogue."
msgstr "Dit programma is niet beschikbaar in de Vier/Vijf/Zes catalogus." msgstr "Dit programma is niet beschikbaar in de catalogus."
### SETTINGS ### SETTINGS

View File

@ -221,6 +221,14 @@ def ok_dialog(heading='', message=''):
return Dialog().ok(heading=heading, line1=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): def notification(heading='', message='', icon='info', time=4000):
"""Show a Kodi notification""" """Show a Kodi notification"""
from xbmcgui import Dialog from xbmcgui import Dialog

View File

@ -17,8 +17,6 @@ class Player:
def __init__(self): def __init__(self):
""" Initialise object """ """ 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): def play_from_page(self, channel, path):
""" Play the requested item. """ Play the requested item.
@ -26,19 +24,32 @@ class Player:
:type path: string :type path: string
""" """
# Get episode information # Get episode information
episode = self._api.get_episode(channel, path) episode = ContentApi().get_episode(channel, path)
# Play this now we have the uuid # Play this now we have the uuid
self.play(channel, episode.uuid) self.play(channel, episode.uuid)
def play(self, channel, item): @staticmethod
def play(channel, item):
""" Play the requested item. """ Play the requested item.
:type channel: string :type channel: string
:type item: string :type item: string
""" """
try: 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 # Get stream information
resolved_stream = self._api.get_stream(channel, item) resolved_stream = ContentApi(token).get_stream(channel, item)
except GeoblockedException: except GeoblockedException:
kodiutils.ok_dialog(heading=kodiutils.localize(30709), message=kodiutils.localize(30710)) # This video is geo-blocked... kodiutils.ok_dialog(heading=kodiutils.localize(30709), message=kodiutils.localize(30710)) # This video is geo-blocked...

View File

@ -26,7 +26,7 @@ class Search:
""" """
if not query: if not query:
# Ask for 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: if not query:
kodiutils.end_of_directory() kodiutils.end_of_directory()
return return

View File

@ -8,8 +8,8 @@ import logging
import re import re
from datetime import datetime from datetime import datetime
from six.moves.html_parser import HTMLParser
import requests import requests
from six.moves.html_parser import HTMLParser
from resources.lib.viervijfzes import CHANNELS from resources.lib.viervijfzes import CHANNELS
@ -130,12 +130,10 @@ class ContentApi:
""" Vier/Vijf/Zes Content API""" """ Vier/Vijf/Zes Content API"""
API_ENDPOINT = 'https://api.viervijfzes.be' API_ENDPOINT = 'https://api.viervijfzes.be'
def __init__(self, token): def __init__(self, token=None):
""" Initialise object """ """ Initialise object """
self._token = token
self._session = requests.session() self._session = requests.session()
self._session.headers['authorization'] = token self._token = token
def get_notifications(self): def get_notifications(self):
""" Get a list of notifications for your account. """ Get a list of notifications for your account.
@ -321,7 +319,12 @@ class ContentApi:
:type url: str :type url: str
:rtype 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: if response.status_code != 200:
raise Exception('Could not fetch data') raise Exception('Could not fetch data')

View File

@ -8,9 +8,9 @@ from __future__ import absolute_import, division, print_function, unicode_litera
import logging import logging
import unittest import unittest
from resources.lib import kodiutils import resources.lib.kodiutils as kodiutils
from resources.lib.viervijfzes.auth import AuthApi
from resources.lib.viervijfzes.content import ContentApi, Program, Episode from resources.lib.viervijfzes.content import ContentApi, Program, Episode
from resources.lib.viervijfzes.auth import AuthApi
_LOGGER = logging.getLogger('test-api') _LOGGER = logging.getLogger('test-api')
@ -18,22 +18,16 @@ _LOGGER = logging.getLogger('test-api')
class TestApi(unittest.TestCase): class TestApi(unittest.TestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(TestApi, self).__init__(*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): def test_programs(self):
api = ContentApi(self._auth.get_token()) api = ContentApi()
for channel in ['vier', 'vijf', 'zes']: for channel in ['vier', 'vijf', 'zes']:
channels = api.get_programs(channel) channels = api.get_programs(channel)
self.assertIsInstance(channels, list) self.assertIsInstance(channels, list)
def test_episodes(self): def test_episodes(self):
api = ContentApi(self._auth.get_token()) api = ContentApi()
for channel, program in [('vier', 'auwch'), ('vijf', 'zo-man-zo-vrouw')]: for channel, program in [('vier', 'auwch'), ('vijf', 'zo-man-zo-vrouw')]:
program = api.get_program(channel, program) program = api.get_program(channel, program)
@ -45,7 +39,10 @@ class TestApi(unittest.TestCase):
_LOGGER.info('Got program: %s', program) _LOGGER.info('Got program: %s', program)
def test_get_stream(self): 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') program = api.get_program('vier', 'auwch')
episode = program.episodes[0] episode = program.episodes[0]
video = api.get_stream(episode.channel, episode.uuid) video = api.get_stream(episode.channel, episode.uuid)