buy.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import annotations
  2. import collections
  3. import dataclasses
  4. import typing
  5. import cache
  6. from config import config
  7. import supply
  8. if typing.TYPE_CHECKING:
  9. import market
  10. def main() -> None:
  11. raw_prices: typing.Mapping[str, market.RawPrice] = {p['MaterialTicker']: p
  12. for p in cache.get('https://refined-prun.github.io/refined-prices/all.json') if p['ExchangeCode'] == 'IC1'}
  13. planets = [supply.Planet(fio_burn) for fio_burn in cache.get('https://rest.fnar.net/fioweb/burn/user/' + config.username,
  14. headers={'Authorization': config.fio_api_key})]
  15. buy: dict[str, int] = collections.defaultdict(int)
  16. for planet in planets:
  17. for mat, amount in planet.buy_for_target(7).items():
  18. buy[mat] += amount
  19. materials: list[Material] = []
  20. for mat, amount in buy.items():
  21. price = raw_prices[mat]
  22. if price['Bid'] is None or price['Ask'] is None:
  23. print(mat, 'has no bid/ask')
  24. continue
  25. spread = price['Ask'] - price['Bid']
  26. materials.append(Material(mat, amount=amount, spread=spread, total=spread * amount))
  27. materials.sort(reverse=True)
  28. print('mat amount total')
  29. for m in materials:
  30. print(f'{m.ticker:4} {m.amount:>6} {m.total:6.0f}')
  31. @dataclasses.dataclass(eq=False, slots=True)
  32. class Material:
  33. ticker: str
  34. amount: int
  35. spread: float
  36. total: float
  37. def __lt__(self, o: Material) -> bool:
  38. return self.total < o.total
  39. if __name__ == '__main__':
  40. main()