From cdcc95c22f452556b4e3148230e170d2580d24af Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Mon, 15 Jul 2019 18:07:04 +0200 Subject: [PATCH] Attempt to provide sensible errors on network issues Currently the add-on works fine if all is well, however various things can go wrong which makes the add-on bail out with no proper error. And since the cause of network errors could be anywhere, we do not want the end-user to have to dig deep into Kodi logs to find probable suspects. So we do want to return the exact error message (i.e. Connection refused, No route to host, etc.) so the user can relate it to other possible problems. (WIFI issues, Internet down or unreliable, etc.) --- .../resource.language.en_gb/strings.po | 8 +++ .../resource.language.nl_nl/strings.po | 8 +++ resources/lib/tokenresolver.py | 59 +++++++++++++------ 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/resources/language/resource.language.en_gb/strings.po b/resources/language/resource.language.en_gb/strings.po index ec3a8ebec..cde7e5476 100644 --- a/resources/language/resource.language.en_gb/strings.po +++ b/resources/language/resource.language.en_gb/strings.po @@ -653,6 +653,14 @@ msgctxt "#30962" msgid "There is a problem with this VRT NU {protocol} stream and Kodi {version} does not support the alternative stream. Please upgrade to a newer Kodi version or try to play this program from the VRT NU website. Please report this problem at https://www.vrt.be/vrtnu/help/" msgstr "" +msgctxt "#30963" +msgid "Unable to open URL" +msgstr "" + +msgctxt "#30964" +msgid "There is a problem connecting to the Internet. This could be related to Kodi, your network, your ISP or VRT NU.\n\nError: {error}" +msgstr "" + msgctxt "#30965" msgid "Using a SOCKS proxy requires the PySocks library (script.module.pysocks) installed." msgstr "" diff --git a/resources/language/resource.language.nl_nl/strings.po b/resources/language/resource.language.nl_nl/strings.po index 69faa8a15..73bf25084 100644 --- a/resources/language/resource.language.nl_nl/strings.po +++ b/resources/language/resource.language.nl_nl/strings.po @@ -545,6 +545,14 @@ msgctxt "#30962" msgid "There is a problem with this VRT NU {protocol} stream and Kodi {version} does not support the alternative stream. Please upgrade to a newer Kodi version or try to play this program from the VRT NU website. Please report this problem at https://www.vrt.be/vrtnu/help/" msgstr "Er is een probleem met deze VRT NU {protocol}-stream en Kodi {version} ondersteunt de alternatieve stream niet. Schakel over op een nieuwere versie van Kodi of probeer dit programma af te spelen vanaf de VRT NU-website. Meld dit probleem op https://www.vrt.be/vrtnu/help/" +msgctxt "#30963" +msgid "Unable to open URL" +msgstr "Probleem bij het openen van een URL" + +msgctxt "#30964" +msgid "There is a problem connecting to the Internet. This could be related to Kodi, your network, your ISP or VRT NU.\n\nError: {error}" +msgstr "Er is een fout opgetreden met het Internet gebruik. Dit kan liggen aan Kodi, aan jouw netwerk, aan jouw ISP of aan VRT NU.\n\nFout: {error}" + msgctxt "#30965" msgid "Using a SOCKS proxy requires the PySocks library (script.module.pysocks) installed." msgstr "Het gebruik van SOCKS proxies vereist dat de PySocks library (script.module.pysocks) geïnstalleerd is." diff --git a/resources/lib/tokenresolver.py b/resources/lib/tokenresolver.py index 9bb6192c1..4c522067b 100644 --- a/resources/lib/tokenresolver.py +++ b/resources/lib/tokenresolver.py @@ -10,11 +10,12 @@ try: # Python 3 import http.cookiejar as cookielib - from urllib.parse import urlencode, unquote - from urllib.request import build_opener, install_opener, ProxyHandler, HTTPCookieProcessor, HTTPErrorProcessor, urlopen, Request + from urllib.error import URLError + from urllib.parse import unquote, urlencode + from urllib.request import HTTPCookieProcessor, HTTPErrorProcessor, ProxyHandler, Request, build_opener, install_opener, urlopen except ImportError: # Python 2 from urllib import urlencode - from urllib2 import build_opener, install_opener, ProxyHandler, HTTPCookieProcessor, HTTPErrorProcessor, unquote, urlopen, Request + from urllib2 import HTTPCookieProcessor, HTTPErrorProcessor, ProxyHandler, Request, URLError, build_opener, install_opener, unquote, urlopen import cookielib @@ -231,9 +232,17 @@ def _get_new_user_xvrttoken(self): cookiejar = cookielib.CookieJar() opener = build_opener(HTTPCookieProcessor(cookiejar), ProxyHandler(self._proxies)) self._kodi.log_notice('URL get: ' + unquote(self._USER_TOKEN_GATEWAY_URL), 'Verbose') - opener.open(self._USER_TOKEN_GATEWAY_URL) + try: + opener.open(self._USER_TOKEN_GATEWAY_URL) + except URLError as e: + self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=unquote(self._USER_TOKEN_GATEWAY_URL))) + raise self._kodi.log_notice('URL post: ' + unquote(self._VRT_LOGIN_URL), 'Verbose') - opener.open(self._VRT_LOGIN_URL, data=data) + try: + opener.open(self._VRT_LOGIN_URL, data=data) + except URLError as e: + self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=unquote(self._VRT_LOGIN_URL))) + raise xvrttoken = TokenResolver._create_token_dictionary(cookiejar) refreshtoken = TokenResolver._create_token_dictionary(cookiejar, cookie_name='vrtlogin-rt') if xvrttoken is not None: @@ -253,7 +262,11 @@ def _get_fresh_token(self, refresh_token, token_name, token_variant=None): opener = build_opener(HTTPCookieProcessor(cookiejar), ProxyHandler(self._proxies)) self._kodi.log_notice('URL get: ' + refresh_url, 'Verbose') req = Request(refresh_url, headers=headers) - opener.open(req) + try: + opener.open(req) + except URLError as e: + self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=req.full_url)) + raise token = TokenResolver._create_token_dictionary(cookiejar, token_name) self._set_cached_token(token, token_variant) return list(token.values())[0] @@ -273,13 +286,16 @@ def _handle_login_error(self, logon_json, cred): def _get_roaming_xvrttoken(self, xvrttoken): ''' Get new 'roaming' X-VRT-Token from VRT NU website ''' - roaming_xvrttoken = None cookie_value = 'X-VRT-Token=' + xvrttoken.get('X-VRT-Token') headers = {'Cookie': cookie_value} opener = build_opener(NoRedirection, ProxyHandler(self._proxies)) self._kodi.log_notice('URL get: ' + unquote(self._ROAMING_TOKEN_GATEWAY_URL), 'Verbose') req = Request(self._ROAMING_TOKEN_GATEWAY_URL, headers=headers) - req_info = opener.open(req).info() + try: + req_info = opener.open(req).info() + except URLError as e: + self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=req.full_url)) + raise try: # Python 3 cookie_value += '; state=' + req_info.get('Set-Cookie').split('state=')[1].split('; ')[0] url = req_info.get('Location') @@ -287,20 +303,29 @@ def _get_roaming_xvrttoken(self, xvrttoken): cookie_value += '; state=' + req_info.getheader('Set-Cookie').split('state=')[1].split('; ')[0] url = req_info.getheader('Location') self._kodi.log_notice('URL get: ' + unquote(url), 'Verbose') - try: # Python 3 - url = opener.open(url).info().get('Location') - except AttributeError: # Python 2 - url = opener.open(url).info().getheader('Location') + try: + try: # Python 3 + url = opener.open(url).info().get('Location') + except AttributeError: # Python 2 + url = opener.open(url).info().getheader('Location') + except URLError as e: + self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=url)) + raise + if url is None: + return None + headers = {'Cookie': cookie_value} - if url is not None: - self._kodi.log_notice('URL get: ' + unquote(url), 'Verbose') - req = Request(url, headers=headers) + self._kodi.log_notice('URL get: ' + unquote(url), 'Verbose') + req = Request(url, headers=headers) + try: try: # Python 3 setcookie_header = opener.open(req).info().get('Set-Cookie') except AttributeError: # Python 2 setcookie_header = opener.open(req).info().getheader('Set-Cookie') - roaming_xvrttoken = TokenResolver._create_token_dictionary(setcookie_header) - return roaming_xvrttoken + except URLError as e: + self._kodi.show_ok_dialog(heading=self._kodi.localize(30963), message=self._kodi.localize(30964, error=e, url=req.full_url)) + raise + return TokenResolver._create_token_dictionary(setcookie_header) @staticmethod def _create_token_dictionary(cookie_data, cookie_name='X-VRT-Token'):