From 234ea5abf931e97e58c4807f35281dba1e0586c9 Mon Sep 17 00:00:00 2001 From: enen92 <92enen@gmail.com> Date: Wed, 7 Sep 2016 17:44:31 +0100 Subject: [PATCH] [script.screensaver.football.panel] 1.4.0 Set default timedelta for cache operations as 2w Use with statement for file IO Changelog wrong version --- script.screensaver.football.panel/addon.xml | 2 +- .../changelog.txt | 3 + .../interface.py | 72 +++++++++++--- .../resources/language/English/strings.po | 19 ++++ .../resources/language/Portuguese/strings.po | 20 ++++ .../resources/lib/cache.py | 97 +++++++++++++++++++ .../resources/lib/common_addon.py | 14 +++ .../resources/settings.xml | 4 + 8 files changed, 216 insertions(+), 15 deletions(-) create mode 100644 script.screensaver.football.panel/resources/lib/cache.py diff --git a/script.screensaver.football.panel/addon.xml b/script.screensaver.football.panel/addon.xml index 3b8e2019a8..b8383d895f 100644 --- a/script.screensaver.football.panel/addon.xml +++ b/script.screensaver.football.panel/addon.xml @@ -1,5 +1,5 @@ - + diff --git a/script.screensaver.football.panel/changelog.txt b/script.screensaver.football.panel/changelog.txt index e7bfdac8f8..880f315e1d 100644 --- a/script.screensaver.football.panel/changelog.txt +++ b/script.screensaver.football.panel/changelog.txt @@ -1,3 +1,6 @@ +Version 1.4.0 (8/9/2016) +-Implement cache mechanisms for league and team logos to reduce the load on the database + Version 1.3.1 (22/8/2016) -Changed default timezone to match XMLSoccer timezone (UTC) diff --git a/script.screensaver.football.panel/interface.py b/script.screensaver.football.panel/interface.py index 19f0de8a2c..521d694630 100644 --- a/script.screensaver.football.panel/interface.py +++ b/script.screensaver.football.panel/interface.py @@ -30,6 +30,7 @@ import datetime from resources.lib import ignoreleagues from resources.lib import ssutils +from resources.lib.cache import AddonCache from resources.lib.common_addon import * api = thesportsdb.Api("7723457519235") @@ -59,6 +60,7 @@ def __init__( self, *args, **kwargs ): self.table_index = -1 self.teamObjs = {} + self.cache_object = AddonCache() def onInit(self): xbmc.log(msg="[Football Panel] Script started", level=xbmc.LOGDEBUG) @@ -141,20 +143,59 @@ def getLeagueTable(self): if self.table_index > (len(self.tables)-1): self.table_index = 0 - league = api.Lookups().League(self.tables[self.table_index])[0] - table = api.Lookups().Table(self.tables[self.table_index],objects=True) + leagueid = self.tables[self.table_index] + table = api.Lookups().Table(leagueid) + + #Look for cached data first + t2 = datetime.datetime.now() + hoursList = [24, 48, 96, 168, 336] + interval = int(addon.getSetting("new_request_interval")) + + #league data + update_league_data = True + if self.cache_object.isCachedLeague(leagueid): + update_league_data = abs(t2 - self.cache_object.getCachedLeagueTimeStamp(leagueid)) > datetime.timedelta(hours=hoursList[interval]) + + if update_league_data: + xbmc.log(msg="[Football Panel] Timedelta was reached for league %s new request to be made..." % (str(leagueid)), level=xbmc.LOGDEBUG) + league = api.Lookups().League(self.tables[self.table_index])[0] + self.cache_object.cacheLeague(leagueid=leagueid,league_obj=league) + else: + xbmc.log(msg="[Football Panel] Using cached object for league %s" % (str(leagueid)), level=xbmc.LOGDEBUG) + league = self.cache_object.getcachedLeague(leagueid) + + #team data + update_team_data = True + if self.cache_object.isCachedTeams(leagueid): + update_team_data = abs(t2 - self.cache_object.getCachedTeamsTimeStamp(leagueid)) > datetime.timedelta(hours=hoursList[interval]) + + if update_team_data: + xbmc.log(msg="[Football Panel] Timedelta was reached for teams in league %s new request to be made..." % (str(leagueid)), level=xbmc.LOGDEBUG) + teams_in_league = api.Lookups().Team(leagueid=self.tables[self.table_index]) + self.cache_object.cacheLeagueTeams(leagueid=leagueid,team_obj_list=teams_in_league) + else: + xbmc.log(msg="[Football Panel] Using cached object for teams in league %s" % (str(leagueid)), level=xbmc.LOGDEBUG) + teams_in_league = self.cache_object.getcachedLeagueTeams(leagueid) + #Finnaly set the table + self.table = [] for tableentry in table: try: if show_alternative == "false": item = xbmcgui.ListItem(tableentry.name) - else: - item = xbmcgui.ListItem(tableentry.Team.AlternativeNameFirst) - item.setArt({ 'thumb': tableentry.Team.strTeamBadge }) + + for team in teams_in_league: + if tableentry.teamid == team.idTeam: + item.setArt({ 'thumb': team.strTeamBadge }) + if show_alternative == "true": + item.setLabel(team.AlternativeNameFirst) + item.setProperty('points',str(tableentry.total)) self.table.append(item) - except:pass + except Exception, e: + xbmc.log(msg="[Football Panel] Exception: %s" % (str(e)), level=xbmc.LOGDEBUG) + self.getControl(LEAGUETABLES_LIST_CONTROL).reset() if league.strLogo: @@ -302,11 +343,14 @@ def get_params(): del main else: - ignoreWindow = ignoreleagues.Select( - 'DialogSelect.xml', - addon_path, - 'default', - '', - ) - ignoreWindow.doModal() - del ignoreWindow + if "ignoreleagues" in params: + ignoreWindow = ignoreleagues.Select( + 'DialogSelect.xml', + addon_path, + 'default', + '', + ) + ignoreWindow.doModal() + del ignoreWindow + elif "removecache" in params: + AddonCache.removeCachedData() diff --git a/script.screensaver.football.panel/resources/language/English/strings.po b/script.screensaver.football.panel/resources/language/English/strings.po index bafb1233e6..ace5e53033 100644 --- a/script.screensaver.football.panel/resources/language/English/strings.po +++ b/script.screensaver.football.panel/resources/language/English/strings.po @@ -84,5 +84,24 @@ msgctxt "#32017" msgid "Cancel" msgstr "" +msgctxt "#32018" +msgid "Cache" +msgstr "" + +msgctxt "#32019" +msgid "Only request new team and league logos after..." +msgstr "" + +msgctxt "#32020" +msgid "Remove all cached data" +msgstr "" + +msgctxt "#32021" +msgid "All cached data was removed!" +msgstr "" + +msgctxt "#32022" +msgid "No cached data available to remove!" +msgstr "" diff --git a/script.screensaver.football.panel/resources/language/Portuguese/strings.po b/script.screensaver.football.panel/resources/language/Portuguese/strings.po index 8880e09892..ca2acb0313 100644 --- a/script.screensaver.football.panel/resources/language/Portuguese/strings.po +++ b/script.screensaver.football.panel/resources/language/Portuguese/strings.po @@ -83,3 +83,23 @@ msgstr "OK" msgctxt "#32017" msgid "Cancel" msgstr "Cancelar" + +msgctxt "#32018" +msgid "Cache" +msgstr "Cache" + +msgctxt "#32019" +msgid "Only request new team and league logos after..." +msgstr "Apenas pedir novos logos após" + +msgctxt "#32020" +msgid "Remove all cached data" +msgstr "Remover todos os dados em cache" + +msgctxt "#32021" +msgid "All cached data was removed!" +msgstr "Todos os dados em cache foram removidos" + +msgctxt "#32022" +msgid "No cached data available to remove!" +msgstr "Nao existem dados em cache para remover!" diff --git a/script.screensaver.football.panel/resources/lib/cache.py b/script.screensaver.football.panel/resources/lib/cache.py new file mode 100644 index 0000000000..9d822a4907 --- /dev/null +++ b/script.screensaver.football.panel/resources/lib/cache.py @@ -0,0 +1,97 @@ +# -*- coding: utf-8 -*- +''' + script.screensaver.football.panel - A Football Panel for Kodi + RSS Feeds, Livescores and League tables as a screensaver or + program addon + Copyright (C) 2016 enen92 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +''' + +import thesportsdb +import os +import datetime +import xbmc +from common_addon import * + + +class AddonCache: + + def __init__(self): + #setup cache folders on object instantiation + if not os.path.exists(addon_userdata): + os.mkdir(addon_userdata) + if not os.path.exists(addon_userdata_cached_leagues): + os.mkdir(addon_userdata_cached_leagues) + if not os.path.exists(addon_userdata_cached_teams): + os.mkdir(addon_userdata_cached_teams) + + def isCachedLeague(self,leagueid): + return os.path.exists(os.path.join(addon_userdata_cached_leagues,"%s.txt" % (str(leagueid)))) + + def isCachedTeams(self,leagueid): + return os.path.exists(os.path.join(addon_userdata_cached_teams,"%s.txt" % (str(leagueid)))) + + def getCachedLeagueTimeStamp(self,leagueid): + return datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(addon_userdata_cached_leagues,"%s.txt" % (str(leagueid))))) + + def getCachedTeamsTimeStamp(self,leagueid): + return datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(addon_userdata_cached_teams,"%s.txt" % (str(leagueid))))) + + def cacheLeague(self,leagueid,league_obj): + league_dict = league_obj.__dict__ + filewrite(os.path.join(os.path.join(addon_userdata_cached_leagues,"%s.txt" % (str(leagueid)))),str(league_dict)) + return + + def getcachedLeague(self,leagueid): + league_str = fileread(os.path.join(os.path.join(addon_userdata_cached_leagues,"%s.txt" % (str(leagueid))))) + league = thesportsdb.league.as_league(eval(league_str)) + return league + + def cacheLeagueTeams(self,leagueid,team_obj_list): + team_list = [] + for team in team_obj_list: + team_list.append(team.__dict__) + filewrite(os.path.join(os.path.join(addon_userdata_cached_teams,"%s.txt" % (str(leagueid)))),str(team_list)) + return + + def getcachedLeagueTeams(self,leagueid): + team_list_str = fileread(os.path.join(os.path.join(addon_userdata_cached_teams,"%s.txt" % (str(leagueid))))) + teams_list = [thesportsdb.team.as_team(team) for team in eval(team_list_str)] + return teams_list + + @staticmethod + def removeCachedData(): + has_removed_files = False + cached_leagues = os.listdir(addon_userdata_cached_leagues) + if cached_leagues: + for leaguefile in cached_leagues: + os.remove(os.path.join(addon_userdata_cached_leagues,leaguefile)) + if not has_removed_files: + has_removed_files = True + cached_league_teams = os.listdir(addon_userdata_cached_teams) + if cached_league_teams: + for leagueteamfile in cached_league_teams: + os.remove(os.path.join(addon_userdata_cached_teams,leagueteamfile)) + if not has_removed_files: + has_removed_files = True + if has_removed_files: + xbmc.executebuiltin("XBMC.Notification(%s,%s,3000,%s)" % (translate(32000),translate(32021),os.path.join(addon_path,"icon.png"))) + else: + xbmc.executebuiltin("XBMC.Notification(%s,%s,3000,%s)" % (translate(32000),translate(32022),os.path.join(addon_path,"icon.png"))) + + + + + diff --git a/script.screensaver.football.panel/resources/lib/common_addon.py b/script.screensaver.football.panel/resources/lib/common_addon.py index 865466d6ef..f4db519e1b 100644 --- a/script.screensaver.football.panel/resources/lib/common_addon.py +++ b/script.screensaver.football.panel/resources/lib/common_addon.py @@ -30,6 +30,9 @@ addon_userdata = xbmc.translatePath(addon.getAddonInfo('profile')).decode('utf-8') addon_name = addon.getAddonInfo('name') +addon_userdata_cached_leagues = os.path.join(addon_userdata,"leagues") +addon_userdata_cached_teams = os.path.join(addon_userdata,"teams") + ignored_league_list_file = os.path.join(addon_userdata,"ignored.txt") livescores_update_time = int(addon.getSetting("livescores-update-time")) tables_update_time = int(addon.getSetting("tables-update-time")) @@ -59,3 +62,14 @@ def removeNonAscii(s): def translate(text): return addon.getLocalizedString(text).encode('utf-8') + +def filewrite(_file,contents): + with open(_file, "w") as f: + f.write(contents) + return + +def fileread(_file): + with open(_file,"r") as f: + contents = f.read() + return contents + diff --git a/script.screensaver.football.panel/resources/settings.xml b/script.screensaver.football.panel/resources/settings.xml index 49fd34674b..baf0f69401 100644 --- a/script.screensaver.football.panel/resources/settings.xml +++ b/script.screensaver.football.panel/resources/settings.xml @@ -44,4 +44,8 @@ + + + +