Skip to content

Commit 88fb65a

Browse files
committed
Cache github api query
1 parent 6514a5a commit 88fb65a

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

command_line.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import plistlib
3737
import codecs
3838
import ssl
39+
from datetime import datetime, timedelta
3940

4041
from io import StringIO
4142

@@ -262,13 +263,37 @@ def get_default_nwjs_branch(self):
262263
Get the default nwjs branch to search for
263264
the changelog from github.
264265
"""
265-
github_url = self.settings['version_info']['github_api_url']
266+
query_api = False
267+
nwjs_branch_path = get_data_file_path(config.NW_BRANCH_FILE)
266268

267-
resp = utils.urlopen(github_url)
268-
json_string = resp.read().decode('utf-8')
269-
data = json.loads(json_string)
269+
if os.path.exists(nwjs_branch_path):
270+
mod_time = os.path.getmtime(nwjs_branch_path)
271+
mod_time = datetime.fromtimestamp(mod_time)
272+
hour_ago = datetime.now() - timedelta(hours=1)
270273

271-
return data['default_branch']
274+
if mod_time <= hour_ago:
275+
query_api = True
276+
else:
277+
query_api = True
278+
279+
branch = None
280+
281+
if query_api:
282+
github_url = self.settings['version_info']['github_api_url']
283+
284+
resp = utils.urlopen(github_url)
285+
json_string = resp.read().decode('utf-8')
286+
data = json.loads(json_string)
287+
branch = data['default_branch']
288+
289+
with codecs.open(nwjs_branch_path, 'w', encoding='utf-8') as f:
290+
f.write(branch)
291+
292+
else:
293+
with codecs.open(nwjs_branch_path, 'r', encoding='utf-8') as f:
294+
branch = f.read().strip()
295+
296+
return branch
272297

273298
def get_versions(self):
274299
"""Get the versions from the NW.js Github changelog"""

0 commit comments

Comments
 (0)