Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various small fixes #216

Merged
merged 1 commit into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions resources/lib/kodiwrappers/kodiwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, unicode_literals
from contextlib import contextmanager

import xbmc
import xbmcplugin

Expand Down Expand Up @@ -54,15 +56,15 @@ def __init__(self, handle, url, addon):
self._max_log_level = log_levels.get(self.get_setting('max_log_level'), 3)
self._usemenucaching = self.get_setting('usemenucaching') == 'true'

def show_listing(self, list_items, sort='unsorted', ascending=True, content_type=None, cache=None):
def show_listing(self, list_items, sort='unsorted', ascending=True, content=None, cache=None):
import xbmcgui
listing = []

if cache is None:
cache = self._usemenucaching

if content_type:
xbmcplugin.setContent(self._handle, content=content_type)
if content:
xbmcplugin.setContent(self._handle, content=content)

# FIXME: Since there is no way to influence descending order, we force it here
if not ascending:
Expand Down Expand Up @@ -253,11 +255,14 @@ def check_if_path_exists(self, path):
import xbmcvfs
return xbmcvfs.exists(path)

def open_path(self, path):
import json
return json.loads(open(path, 'r').read())
@contextmanager
def open_file(self, path, flags='r'):
import xbmcvfs
f = xbmcvfs.File(path, flags)
yield f
f.close()

def delete_path(self, path):
def delete_file(self, path):
import xbmcvfs
return xbmcvfs.delete(path)

Expand Down
6 changes: 3 additions & 3 deletions resources/lib/vrtplayer/streamservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from urllib.error import HTTPError
from urllib.request import build_opener, install_opener, urlopen, ProxyHandler
except ImportError:
from urllib2 import build_opener, install_opener, urlopen, ProxyHandler, quote, HTTPError
from urllib import urlencode # pylint: disable=ungrouped-imports
from urllib2 import build_opener, install_opener, urlopen, ProxyHandler, quote, HTTPError


class StreamService:
Expand Down Expand Up @@ -210,9 +210,9 @@ def get_stream(self, video, retry=False, api_data=None):
if not retry and roaming_xvrttoken is not None:
# Delete cached playertokens
if api_data.is_live_stream:
self._kodiwrapper.delete_path(self._kodiwrapper.get_userdata_path() + 'live_vrtPlayerToken')
self._kodiwrapper.delete_file(self._kodiwrapper.get_userdata_path() + 'live_vrtPlayerToken')
else:
self._kodiwrapper.delete_path(self._kodiwrapper.get_userdata_path() + 'ondemand_vrtPlayerToken')
self._kodiwrapper.delete_file(self._kodiwrapper.get_userdata_path() + 'ondemand_vrtPlayerToken')
# Update api_data with roaming_xvrttoken and try again
api_data.xvrttoken = roaming_xvrttoken
return self.get_stream(video, retry=True, api_data=api_data)
Expand Down
33 changes: 18 additions & 15 deletions resources/lib/vrtplayer/tokenresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from resources.lib.helperobjects import helperobjects

try:
from urllib.parse import urlencode
from urllib.parse import urlencode, unquote
from urllib.request import build_opener, install_opener, ProxyHandler, HTTPErrorProcessor, urlopen, Request
except ImportError:
from urllib2 import build_opener, install_opener, ProxyHandler, HTTPErrorProcessor, urlopen, Request
from urllib import urlencode # pylint: disable=ungrouped-imports
from urllib2 import build_opener, install_opener, ProxyHandler, HTTPErrorProcessor, unquote, urlopen, Request


class NoRedirection(HTTPErrorProcessor):
Expand Down Expand Up @@ -75,10 +75,11 @@ def get_cookie_from_cookiejar(cookiename, cookiejar):

def _get_new_playertoken(self, path, token_url, headers):
import json
self._kodiwrapper.log_notice('URL post: ' + token_url, 'Verbose')
self._kodiwrapper.log_notice('URL post: ' + unquote(token_url), 'Verbose')
req = Request(token_url, data='', headers=headers)
playertoken = json.loads(urlopen(req).read())
json.dump(playertoken, open(path, 'w'))
with self._kodiwrapper.open_file(path, 'w') as f:
json.dump(playertoken, f)
return playertoken.get('vrtPlayerToken')

def _get_cached_token(self, path, token_name):
Expand All @@ -89,15 +90,16 @@ def _get_cached_token(self, path, token_name):
import dateutil.parser
import dateutil.tz
import json
token = json.loads(open(path, 'r').read())
with self._kodiwrapper.open_file(path) as f:
token = json.loads(f.read())
now = datetime.now(dateutil.tz.tzlocal())
exp = dateutil.parser.parse(token.get('expirationDate'))
if exp > now:
self._kodiwrapper.log_notice('Got cached token', 'Verbose')
cached_token = token.get(token_name)
else:
self._kodiwrapper.log_notice('Cached token deleted', 'Info')
self._kodiwrapper.delete_path(path)
self._kodiwrapper.delete_file(path)
return cached_token

def _get_new_xvrttoken(self, path, get_roaming_token):
Expand All @@ -113,7 +115,7 @@ def _get_new_xvrttoken(self, path, get_roaming_token):
APIKey=self._API_KEY,
targetEnv='jssdk',
)
self._kodiwrapper.log_notice('URL post: ' + self._LOGIN_URL, 'Verbose')
self._kodiwrapper.log_notice('URL post: ' + unquote(self._LOGIN_URL), 'Verbose')
req = Request(self._LOGIN_URL, data=urlencode(data))
logon_json = json.loads(urlopen(req).read())
token = None
Expand All @@ -127,15 +129,16 @@ def _get_new_xvrttoken(self, path, get_roaming_token):
email=cred.username,
)
headers = {'Content-Type': 'application/json', 'Cookie': login_cookie}
self._kodiwrapper.log_notice('URL post: ' + self._TOKEN_GATEWAY_URL, 'Verbose')
self._kodiwrapper.log_notice('URL post: ' + unquote(self._TOKEN_GATEWAY_URL), 'Verbose')
req = Request(self._TOKEN_GATEWAY_URL, data=json.dumps(payload), headers=headers)
cookie_data = urlopen(req).info().getheader('Set-Cookie').split('X-VRT-Token=')[1].split('; ')
xvrttoken = TokenResolver._create_token_dictionary_from_urllib(cookie_data)
if get_roaming_token:
xvrttoken = self._get_roaming_xvrttoken(xvrttoken)
if xvrttoken is not None:
token = xvrttoken.get('X-VRT-Token')
json.dump(xvrttoken, open(path, 'w'))
with self._kodiwrapper.open_file(path, 'w') as f:
json.dump(xvrttoken, f)
else:
self._handle_error(logon_json, cred)
return token
Expand All @@ -160,15 +163,15 @@ def _get_roaming_xvrttoken(self, xvrttoken):
cookie_value = 'X-VRT-Token=' + xvrttoken.get('X-VRT-Token')
headers = {'Cookie': cookie_value}
opener = build_opener(NoRedirection, ProxyHandler(self._proxies))
self._kodiwrapper.log_notice('URL post: ' + url, 'Verbose')
self._kodiwrapper.log_notice('URL post: ' + unquote(url), 'Verbose')
req = Request(url, headers=headers)
req_info = opener.open(req).info()
cookie_value += '; state=' + req_info.getheader('Set-Cookie').split('state=')[1].split('; ')[0]
url = req_info.getheader('Location')
url = opener.open(url).info().getheader('Location')
headers = {'Cookie': cookie_value}
if url is not None:
self._kodiwrapper.log_notice('URL post: ' + url, 'Verbose')
self._kodiwrapper.log_notice('URL post: ' + unquote(url), 'Verbose')
req = Request(url, headers=headers)
cookie_data = opener.open(req).info().getheader('Set-Cookie').split('X-VRT-Token=')[1].split('; ')
roaming_xvrttoken = TokenResolver._create_token_dictionary_from_urllib(cookie_data)
Expand Down Expand Up @@ -203,7 +206,7 @@ def reset_cookies(self):
live = user_data_path + self._LIVE_COOKIE
xvrt = user_data_path + self._XVRT_TOKEN_COOKIE
roaming = user_data_path + self._ROAMING_XVRTTOKEN_COOKIE
self._kodiwrapper.delete_path(ondemand)
self._kodiwrapper.delete_path(live)
self._kodiwrapper.delete_path(xvrt)
self._kodiwrapper.delete_path(roaming)
self._kodiwrapper.delete_file(ondemand)
self._kodiwrapper.delete_file(live)
self._kodiwrapper.delete_file(xvrt)
self._kodiwrapper.delete_file(roaming)
4 changes: 2 additions & 2 deletions resources/lib/vrtplayer/tvguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ def show_tvguide(self, params):

if not date:
date_items = self.show_date_menu()
self._kodiwrapper.show_listing(date_items, content_type='files')
self._kodiwrapper.show_listing(date_items, content='files')

elif not channel:
channel_items = self.show_channel_menu(date)
self._kodiwrapper.show_listing(channel_items)

else:
episode_items = self.show_episodes(date, channel)
self._kodiwrapper.show_listing(episode_items, content_type='episodes', cache=False)
self._kodiwrapper.show_listing(episode_items, content='episodes', cache=False)

def show_date_menu(self):
now = datetime.now(dateutil.tz.tzlocal())
Expand Down
10 changes: 5 additions & 5 deletions resources/lib/vrtplayer/vrtapihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_tvshow_items(self, category=None, channel=None):
params['facets[programBrands]'] = channel

api_url = self._VRTNU_SUGGEST_URL + '?' + urlencode(params)
self._kodiwrapper.log_notice('URL get: ' + api_url, 'Verbose')
self._kodiwrapper.log_notice('URL get: ' + unquote(api_url), 'Verbose')
api_json = json.loads(urlopen(api_url).read())
return self._map_to_tvshow_items(api_json)

Expand Down Expand Up @@ -102,7 +102,7 @@ def get_episode_items(self, path=None, page=None, all_seasons=False):
'facets[programBrands]': '[een,canvas,sporza,vrtnws,vrtnxt,radio1,radio2,klara,stubru,mnm]',
}
api_url = self._VRTNU_SEARCH_URL + '?' + urlencode(params)
self._kodiwrapper.log_notice('URL get: ' + api_url, 'Verbose')
self._kodiwrapper.log_notice('URL get: ' + unquote(api_url), 'Verbose')
api_json = json.loads(urlopen(api_url).read())
episode_items, sort, ascending = self._map_to_episode_items(api_json.get('results', []), titletype='recent')

Expand All @@ -116,7 +116,7 @@ def get_episode_items(self, path=None, page=None, all_seasons=False):
api_url = self._VRTNU_SEARCH_URL + '?' + urlencode(params)
else:
api_url = path
self._kodiwrapper.log_notice('URL get: ' + api_url, 'Verbose')
self._kodiwrapper.log_notice('URL get: ' + unquote(api_url), 'Verbose')
api_json = json.loads(urlopen(api_url).read())

episodes = api_json.get('results', [{}])
Expand Down Expand Up @@ -280,7 +280,7 @@ def search(self, search_string, page=1):
'q': search_string,
}
api_url = 'https://search.vrt.be/search?' + urlencode(params)
self._kodiwrapper.log_notice('URL get: ' + api_url, 'Verbose')
self._kodiwrapper.log_notice('URL get: ' + unquote(api_url), 'Verbose')
api_json = json.loads(urlopen(api_url).read())

episodes = api_json.get('results', [{}])
Expand All @@ -296,7 +296,7 @@ def __delete_cached_thumbnail(self, url):
crc = self.__get_crc32(url)
ext = url.split('.')[-1]
path = 'special://thumbnails/%s/%s.%s' % (crc[0], crc, ext)
self._kodiwrapper.delete_path(path)
self._kodiwrapper.delete_file(path)

@staticmethod
def __get_crc32(string):
Expand Down
14 changes: 7 additions & 7 deletions resources/lib/vrtplayer/vrtplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ def show_main_menu_items(self):

def show_tvshow_menu_items(self, category=None):
tvshow_items = self._apihelper.get_tvshow_items(category=category)
self._kodiwrapper.show_listing(tvshow_items, sort='label', content_type='tvshows')
self._kodiwrapper.show_listing(tvshow_items, sort='label', content='tvshows')

def show_category_menu_items(self):
category_items = self.__get_category_menu_items()
self._kodiwrapper.show_listing(category_items, sort='label', content_type='files')
self._kodiwrapper.show_listing(category_items, sort='label', content='files')

def show_channels_menu_items(self, channel=None):
if channel:
tvshow_items = self._apihelper.get_tvshow_items(channel=channel)
self._kodiwrapper.show_listing(tvshow_items, sort='label', content_type='tvshows')
self._kodiwrapper.show_listing(tvshow_items, sort='label', content='tvshows')
else:
from resources.lib.vrtplayer import CHANNELS
self.show_channels(action=actions.LISTING_CHANNELS, channels=[c.get('name') for c in CHANNELS])
Expand Down Expand Up @@ -134,11 +134,11 @@ def show_channels(self, action=actions.PLAY, channels=None):

def show_episodes(self, path):
episode_items, sort, ascending = self._apihelper.get_episode_items(path=path)
self._kodiwrapper.show_listing(episode_items, sort=sort, ascending=ascending, content_type='episodes')
self._kodiwrapper.show_listing(episode_items, sort=sort, ascending=ascending, content='episodes')

def show_all_episodes(self, path):
episode_items, sort, ascending = self._apihelper.get_episode_items(path=path, all_seasons=True)
self._kodiwrapper.show_listing(episode_items, sort=sort, ascending=ascending, content_type='episodes')
self._kodiwrapper.show_listing(episode_items, sort=sort, ascending=ascending, content='episodes')

def show_recent(self, page):
try:
Expand All @@ -157,7 +157,7 @@ def show_recent(self, page):
video_dict=dict(),
))

self._kodiwrapper.show_listing(episode_items, sort=sort, ascending=ascending, content_type='episodes', cache=False)
self._kodiwrapper.show_listing(episode_items, sort=sort, ascending=ascending, content='episodes', cache=False)

def play(self, params):
from resources.lib.vrtplayer import streamservice, tokenresolver
Expand Down Expand Up @@ -194,7 +194,7 @@ def search(self, search_string=None, page=1):
video_dict=dict(),
))

self._kodiwrapper.show_listing(search_items, sort=sort, ascending=ascending, content_type='episodes', cache=False)
self._kodiwrapper.show_listing(search_items, sort=sort, ascending=ascending, content='episodes', cache=False)

def __get_category_menu_items(self):
try:
Expand Down