Skip to content
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
2 changes: 1 addition & 1 deletion script.screensaver.football.panel/addon.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.screensaver.football.panel" name="Football Panel" version="1.3.1" provider-name="enen92">
<addon id="script.screensaver.football.panel" name="Football Panel" version="1.4.0" provider-name="enen92">
<requires>
<import addon="xbmc.python" version="2.20.0"/>
<import addon="script.module.thesportsdb" version="1.0.1"/>
Expand Down
3 changes: 3 additions & 0 deletions script.screensaver.football.panel/changelog.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
72 changes: 58 additions & 14 deletions script.screensaver.football.panel/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""


Original file line number Diff line number Diff line change
Expand Up @@ -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!"
97 changes: 97 additions & 0 deletions script.screensaver.football.panel/resources/lib/cache.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
'''

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")))





14 changes: 14 additions & 0 deletions script.screensaver.football.panel/resources/lib/common_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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

4 changes: 4 additions & 0 deletions script.screensaver.football.panel/resources/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@
<setting id="rss-url" type="text" label="32007" default="http://uefa.com/rssfeed/news/rss.xml"/>
<setting label="32008" type="slider" id="rss-update-time" default="10" range="10,5,120" option="int" />
</category>
<category label="32018">
<setting id="new_request_interval" type="enum" label="32019" values="24h|48h|96h|1w|2w" default="4"/>
<setting label="32020" type="action" action="RunScript(script.screensaver.football.panel, removecache)"/>
</category>
</settings>