Skip to content

Commit

Permalink
Move tokens to userdata/tokens/ (#432)
Browse files Browse the repository at this point in the history
This PR includes:
- Move tokens to tokens/ subdirectory
- Clean up old tokens temporarily
  • Loading branch information
dagwieers authored Aug 13, 2019
1 parent f439216 commit bc799c8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ msbuild.wrn
test/userdata/cache/
test/userdata/credentials.json
test/userdata/search_history.json
test/userdata/tokens/
test/userdata/*.tkn
5 changes: 5 additions & 0 deletions resources/lib/kodiwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def __init__(self, addon):
self._max_log_level = log_levels.get(self.get_setting('max_log_level', 'Debug'), 3)
self._usemenucaching = self.get_setting('usemenucaching', 'true') == 'true'
self._cache_path = self.get_userdata_path() + 'cache/'
self._tokens_path = self.get_userdata_path() + 'tokens/'
self._system_locale_works = None

def url_for(self, name, *args, **kwargs):
Expand Down Expand Up @@ -449,6 +450,10 @@ def get_userdata_path(self):
''' Return the profile's userdata path '''
return xbmc.translatePath(self._addon.getAddonInfo('profile'))

def get_tokens_path(self):
''' Return the userdata tokens path '''
return self._tokens_path

def get_addon_path(self):
''' Return the addon path '''
return xbmc.translatePath(self._addon.getAddonInfo('path'))
Expand Down
12 changes: 11 additions & 1 deletion resources/lib/tokenresolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get_xvrttoken(self, token_variant=None):
def _get_token_path(self, token_name, token_variant):
''' Create token path following predefined file naming rules '''
prefix = token_variant + '_' if token_variant else ''
token_path = self._kodi.get_userdata_path() + prefix + token_name.replace('-', '') + '.tkn'
token_path = self._kodi.get_tokens_path() + prefix + token_name.replace('-', '') + '.tkn'
return token_path

def _get_cached_token(self, token_name, token_variant=None):
Expand Down Expand Up @@ -112,6 +112,10 @@ def _set_cached_token(self, token, token_variant=None):
''' Save token to cache'''
token_name = list(token.keys())[0]
path = self._get_token_path(token_name, token_variant)

if not self._kodi.check_if_path_exists(self._kodi.get_tokens_path()):
self._kodi.mkdir(self._kodi.get_tokens_path())

with self._kodi.open_file(path, 'w') as f:
json.dump(token, f)

Expand Down Expand Up @@ -307,8 +311,14 @@ def _create_token_dictionary(cookie_data, cookie_name='X-VRT-Token'):

def delete_tokens(self):
''' Delete all cached tokens '''
# Remove old tokens
# FIXME: Deprecate and simplify this part in a future version
dirs, files = self._kodi.listdir(self._kodi.get_userdata_path()) # pylint: disable=unused-variable
token_files = [item for item in files if item.endswith('.tkn')]
# Empty userdata/tokens/ directory
if self._kodi.check_if_path_exists(self._kodi.get_tokens_path()):
dirs, files = self._kodi.listdir(self._kodi.get_tokens_path()) # pylint: disable=unused-variable
token_files += ['tokens/' + item for item in files]
if token_files:
for item in token_files:
self._kodi.delete_file(self._kodi.get_userdata_path() + item)
Expand Down
5 changes: 4 additions & 1 deletion resources/lib/vrtplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ def _version_check(self):
# 2.0.0 version: changed plugin:// url interface: show warning that favourites and what-was-watched will break
if settings_version == '' and self._kodi.credentials_filled_in():
self._kodi.show_ok_dialog(self._kodi.localize(30978), self._kodi.localize(30979))
# 2.2.0 version: changed artwork: delete old cached artwork
if addon_version == '2.2.0':
# 2.2.0 version: changed artwork: delete old cached artwork
self._kodi.delete_cached_thumbnail(self._kodi.get_addon_info('fanart').replace('.png', '.jpg'))
self._kodi.delete_cached_thumbnail(self._kodi.get_addon_info('icon'))
# 2.2.0 version: moved tokens: delete old tokens
from tokenresolver import TokenResolver
TokenResolver(self._kodi).delete_tokens()

def _first_run(self):
'''Check if this add-on version is run for the first time'''
Expand Down

0 comments on commit bc799c8

Please sign in to comment.