Created
December 23, 2016 00:57
-
-
Save Teemu/5639e9d90449fdf0fb91d5c10c22360e to your computer and use it in GitHub Desktop.
Request JSON data from API and cache the result so you won't abuse the servers when developing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cache_get(url): | |
"""Request from API and cache the result""" | |
import hashlib | |
import json | |
import os | |
import requests | |
domain = url.split('/')[2].split('.')[-2] | |
path = os.path.join( | |
'.cache', | |
'%s_%s' % ( | |
domain, | |
hashlib.sha256(url.encode('utf-8')).hexdigest()[0:32] | |
) | |
) | |
try: | |
os.mkdir('.cache') | |
except FileExistsError: | |
pass | |
try: | |
with open(path, 'r') as fp: | |
return json.loads(fp.read()) | |
except FileNotFoundError: | |
data = requests.get(url).json() | |
with open(path, 'w') as fp: | |
fp.write(json.dumps(data, indent=2)) | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment