Caching ClearSky requests
Signed-off-by: Markus Birth <markus@birth-online.de>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import configparser
|
||||
import json
|
||||
import requests
|
||||
import os.path
|
||||
import pickle
|
||||
@@ -65,8 +66,15 @@ class ClearSky():
|
||||
url = self.BASE_URL + "/api/v1/" + uri
|
||||
if page > 1:
|
||||
url += f"/{page}"
|
||||
r = requests.get(url).json()
|
||||
sleep(0.20) # Poor man's way of making sure to not go over 5 requests per second
|
||||
cachefile = "cs_" + url[len(self.BASE_URL)+8:].replace("/", "_") + ".json"
|
||||
if os.path.isfile(cachefile):
|
||||
with open(cachefile, "rt") as f:
|
||||
r = json.load(f)
|
||||
else:
|
||||
r = requests.get(url).json()
|
||||
with open(cachefile, "wt") as f:
|
||||
json.dump(r, f)
|
||||
sleep(0.20) # Poor man's way of making sure to not go over 5 requests per second
|
||||
data_len = len(r["data"][data_key]) if data_key else len(r["data"])
|
||||
result += r["data"][data_key] if data_key else r["data"]
|
||||
if data_len < 100:
|
||||
|
||||
Reference in New Issue
Block a user