Skip to content

Commit 63387a8

Browse files
authored
Merge pull request #25 from AmineI/steam_games_cache
Allow offline support through a caching mechanism of the Steam games lists
2 parents 7a6eee6 + 1481720 commit 63387a8

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

resources/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def run(appid):
122122

123123
@plugin.route('/delete_cache')
124124
def delete_cache():
125+
steam.delete_cache()
125126
arts.delete_cache()
126127

127128

resources/settings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<setting id="steam-args" type="text" label="Arguments to pass to your Steam executable"/>
99
<setting id="version" type="text" label="Internal version number, do not modify" visible="false"/>
1010
<setting id="enable-art-fallback" type="bool" default="true" label="Fallback to another art type if a game has missing art. First launch may be longer for large libraries."/>
11+
<setting id="games-expire-after-minutes" type="number" default="4320" label="Number of minutes before expiration of the games lists cache"/>
1112
<setting id="arts-expire-after-months" type="number" default="2" label="Number of months before expiration of the arts availability cache"/>
1213
<setting id="delete-cache" type="action" label="Clean available games and arts cache" action="RunPlugin(plugin://plugin.program.steam.library/delete_cache)"/>
1314
</settings>

resources/steam.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1+
import os
2+
import sys
13
import shlex
24
import subprocess
5+
6+
import xbmc
7+
import xbmcaddon
8+
import xbmcplugin
9+
310
import requests
11+
import requests_cache
412

513
from util import log
614

15+
__addon__ = xbmcaddon.Addon()
16+
# define the cache file to reside in the ..\Kodi\userdata\addon_data\(your addon)
17+
addonUserDataFolder = xbmc.translatePath(__addon__.getAddonInfo('profile'))
18+
STEAM_GAMES_CACHE_FILE = xbmc.translatePath(os.path.join(addonUserDataFolder, 'requests_cache_games'))
19+
minutesBeforeGamesListsExpiration = int(xbmcplugin.getSetting(int(sys.argv[1]), "games-expire-after-minutes")) # Default is 3 days
20+
21+
# cache expires after: 86400=1 day 604800=7 days
22+
cached_requests = requests_cache.core.CachedSession(STEAM_GAMES_CACHE_FILE, backend='sqlite',
23+
expire_after=60 * minutesBeforeGamesListsExpiration,
24+
old_data_on_error=True)
25+
726

827
def install(steam_exe_path, appid):
928
"""
@@ -29,7 +48,7 @@ def run(steam_exe_path, steam_launch_args, appid):
2948
log('executing ' + steam_exe_path + ' ' + steam_launch_args + ' steam://rungameid/' + appid)
3049

3150
# https://developer.valvesoftware.com/wiki/Steam_browser_protocol
32-
subprocess.call([steam_exe_path] + user_args + ['steam://rungameid/' + appid]) #Concatenate the arrays into one
51+
subprocess.call([steam_exe_path] + user_args + ['steam://rungameid/' + appid]) # Concatenate the arrays into one
3352

3453

3554
def get_user_games(steam_api_key, steam_user_id, recent_only=False):
@@ -73,13 +92,17 @@ def get_user_games(steam_api_key, steam_user_id, recent_only=False):
7392
api_url = 'https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/'
7493

7594
# We send a request to the API, including needed parameters and "include_appinfo" so that game details are returned with the response.
76-
response = requests.get(url=api_url,
77-
params={'key': steam_api_key,
78-
'steamid': steam_user_id,
79-
'include_appinfo': 1,
80-
'format': 'json'},
81-
timeout=10)
95+
response = cached_requests.get(url=api_url,
96+
params={'key': steam_api_key,
97+
'steamid': steam_user_id,
98+
'include_appinfo': 1,
99+
'format': 'json'},
100+
timeout=5)
82101

83102
response.raise_for_status() # If the status code indicates an error, raise a HTTPError, which is itself a RequestException, based on the builtin IOError
84103
response_data = response.json().get('response', {})
85104
return response_data.get('games', {})
105+
106+
107+
def delete_cache():
108+
os.remove(STEAM_GAMES_CACHE_FILE + ".sqlite")

0 commit comments

Comments
 (0)