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
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 ""

View File

@ -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

View File

@ -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

View File

@ -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...

View File

@ -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

View File

@ -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')

View File

@ -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)