Skip to content

Commit

Permalink
Add compression to reduce file size
Browse files Browse the repository at this point in the history
  • Loading branch information
Javex committed Sep 27, 2023
1 parent b467c98 commit cec2a02
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions hotprices_au/output.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import pathlib
import json
import gzip
from datetime import datetime


def save_data(store, categories):
def save_data(store, categories, compression='gzip'):
now = datetime.now()
date_str = now.strftime("%Y-%m-%d")
fname = f"{date_str}.json"
save_dir = pathlib.Path(f"output/{store}")
save_dir.mkdir(parents=True, exist_ok=True)
fpath = save_dir / fname
fpath.write_text(json.dumps(categories))

if compression == 'gzip':
fpath = fpath.with_suffix('.json.gz')
with gzip.open(fpath, 'wt') as fp:
fp.write(json.dumps(categories))
elif compression is None:
fpath.write_text(json.dumps(categories))
else:
raise RuntimeError(f"Unsupported compression '{compression}'")

0 comments on commit cec2a02

Please sign in to comment.