"""Import public reference purchase prices from an already downloaded Coriolis data snapshot."""
import datetime, hashlib, json, pathlib, zipfile
root = pathlib.Path(__file__).resolve().parents[1]
archive = root / 'artifacts/coriolis-price-source.zip'
prices, hashes, ambiguous = {}, {}, {}
with zipfile.ZipFile(archive) as data:
prefix = data.namelist()[0].split('/')[0]
for name in data.namelist():
relative = name.split('/', 1)[-1]
if not relative.startswith('modules/') or not relative.endswith('.json'):
continue
source = data.read(name)
hashes[relative] = hashlib.sha256(source).hexdigest()
def visit(value):
if isinstance(value, dict):
symbol, cost = value.get('symbol'), value.get('cost')
if isinstance(symbol, str) and isinstance(cost, (int, float)) and 0 < cost < 10**12:
amount = int(cost)
key = symbol.lower()
if key in ambiguous:
ambiguous[key].add(amount)
elif key in prices and prices[key] != amount:
ambiguous[key] = {prices.pop(key), amount}
else:
prices[key] = amount
for child in value.values():
visit(child)
elif isinstance(value, list):
for child in value:
visit(child)
visit(json.loads(source))
document = dict(source='https://github.com/EDCD/coriolis-data', snapshot=prefix,
importedAt=datetime.datetime.now(datetime.timezone.utc).isoformat(),
archiveSha256=hashlib.sha256(archive.read_bytes()).hexdigest(), files=hashes,
note='Reference base purchase prices in Cr; station discounts and live availability are not included.',
prices=dict(sorted(prices.items())), ambiguous={k: sorted(v) for k, v in ambiguous.items()})
output = root / 'XFEEDToolBox.Core/Outfitting/prices.json'
output.write_text(json.dumps(document, ensure_ascii=False, separators=(',', ':')), encoding='utf-8')
print(json.dumps(dict(prices=len(prices), ambiguous=len(ambiguous), sourceFiles=len(hashes), snapshot=prefix)))
"""Import public reference purchase prices from an already downloaded Coriolis data snapshot."""
import datetime, hashlib, json, pathlib, zipfile
root = pathlib.Path(__file__).resolve().parents[1]
archive = root / 'artifacts/coriolis-price-source.zip'
prices, hashes, ambiguous = {}, {}, {}
with zipfile.ZipFile(archive) as data:
prefix = data.namelist()[0].split('/')[0]
for name in data.namelist():
relative = name.split('/', 1)[-1]
if not relative.startswith('modules/') or not relative.endswith('.json'):
continue
source = data.read(name)
hashes[relative] = hashlib.sha256(source).hexdigest()
def visit(value):
if isinstance(value, dict):
symbol, cost = value.get('symbol'), value.get('cost')
if isinstance(symbol, str) and isinstance(cost, (int, float)) and 0 < cost < 10**12:
amount = int(cost)
key = symbol.lower()
if key in ambiguous:
ambiguous[key].add(amount)
elif key in prices and prices[key] != amount:
ambiguous[key] = {prices.pop(key), amount}
else:
prices[key] = amount
for child in value.values():
visit(child)
elif isinstance(value, list):
for child in value:
visit(child)
visit(json.loads(source))
document = dict(source='https://github.com/EDCD/coriolis-data', snapshot=prefix,
importedAt=datetime.datetime.now(datetime.timezone.utc).isoformat(),
archiveSha256=hashlib.sha256(archive.read_bytes()).hexdigest(), files=hashes,
note='Reference base purchase prices in Cr; station discounts and live availability are not included.',
prices=dict(sorted(prices.items())), ambiguous={k: sorted(v) for k, v in ambiguous.items()})
output = root / 'XFEEDToolBox.Core/Outfitting/prices.json'
output.write_text(json.dumps(document, ensure_ascii=False, separators=(',', ':')), encoding='utf-8')
print(json.dumps(dict(prices=len(prices), ambiguous=len(ambiguous), sourceFiles=len(hashes), snapshot=prefix)))