-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
61 lines (50 loc) · 1.75 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import api
import json
import sys
from os import path
###
# TODO: saved data check; if *.json older than 1 day, discard and get new data
##
def save_data_to_json_file(filename, json_object):
with open(filename, "w") as f:
json_string = json.dumps(json_object)
f.write(json_string)
def load_data_from_json_file(filename):
with open(filename, "r") as f:
json_string = f.read()
return json.loads(json_string)
def get_from_api_or_load_data_from_json_file(
filename, getter_func, getter_func_args=[], allow_saved_data=True
):
data = None
if sys.modules[__name__] == "test":
filename = "test_data/" + filename
if path.exists(filename) and allow_saved_data:
data = load_data_from_json_file(filename)
else:
data = getter_func(*getter_func_args)
save_data_to_json_file(filename, data)
return data
def get_all_products(allow_saved_data=True):
filename = "products.json"
getter_func = api.get_all_products
products = get_from_api_or_load_data_from_json_file(
filename, getter_func, allow_saved_data=allow_saved_data
)
return products
def get_products_from_ids(ids, allow_saved_data=True):
filename = "interesting_items.json"
getter_func = api.get_products_from_ids
getter_func_args = [ids]
products = get_from_api_or_load_data_from_json_file(
filename, getter_func, getter_func_args, allow_saved_data
)
return products
def load_latest_available_items():
# Loads 'available_items_dump.json' into a json object
# which is returned
# NOTE: only used for testing purposes in repl
with open("available_items.json") as f:
json_string = f.read()
available_items = json.loads(json_string)
return available_items