-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utils to locally load/save item data for specified item ids
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
from src.data_utils import save_data | ||
from src.download_utils import download_from_item_data_tracker | ||
|
||
|
||
def get_item_data_filename(item_id, folder_name="data/items/"): | ||
Path(folder_name).mkdir(parents=True, exist_ok=True) | ||
|
||
fname = f"{item_id}.json" | ||
|
||
return folder_name + fname | ||
|
||
|
||
def load_item_data(item_id): | ||
fname = get_item_data_filename(item_id) | ||
|
||
try: | ||
with open(fname, encoding="utf8") as f: | ||
item_data = json.load(f) | ||
except FileNotFoundError: | ||
item_data = download_from_item_data_tracker(item_id) | ||
save_data(item_data, fname) | ||
|
||
return item_data |