Skip to content

Commit

Permalink
Attempt to provide sensible errors on network issues
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
dagwieers committed Aug 13, 2019
1 parent 83defb2 commit 6c236fa
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
8 changes: 8 additions & 0 deletions resources/language/resource.language.en_gb/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,14 @@ msgctxt "#30966"
msgid "Using a SOCKS proxy requires the PySocks library (script.module.pysocks) installed."
msgstr ""

msgctxt "#30967"
msgid "Unable to open URL"
msgstr ""

msgctxt "#30968"
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}\nURL: {url}"
msgstr ""

msgctxt "#30971"
msgid "Install Widevine using 'InputStream Helper' add-on"
msgstr ""
Expand Down
8 changes: 8 additions & 0 deletions resources/language/resource.language.nl_nl/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,14 @@ msgctxt "#30966"
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."

msgctxt "#30967"
msgid "Unable to open URL"
msgstr "Probleem bij het openen van een URL"

msgctxt "#30968"
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}\nURL: {url}"
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}\nURL: {url}"

msgctxt "#30971"
msgid "Install Widevine using 'InputStream Helper' add-on"
msgstr "Installeer Widevine met de 'InputStream Helper' add-on"
Expand Down
59 changes: 42 additions & 17 deletions resources/lib/tokenresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,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


Expand Down Expand Up @@ -228,9 +229,17 @@ def _get_new_user_xvrttoken(self, token_variant):
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:
Expand All @@ -250,41 +259,57 @@ 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]

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')
except AttributeError: # Python 2
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'):
Expand Down

0 comments on commit 6c236fa

Please sign in to comment.