raylu 2 өдөр өмнө
parent
commit
b0981fc09a
1 өөрчлөгдсөн 34 нэмэгдсэн , 23 устгасан
  1. 34 23
      buy.py

+ 34 - 23
buy.py

@@ -1,6 +1,7 @@
 from __future__ import annotations
 
 import collections
+import concurrent.futures
 import dataclasses
 import typing
 
@@ -12,29 +13,15 @@ if typing.TYPE_CHECKING:
 	import market
 
 def main() -> None:
-	raw_prices: typing.Mapping[str, market.RawPrice] = {p['MaterialTicker']: p
-			for p in cache.get('https://refined-prun.github.io/refined-prices/all.json') if p['ExchangeCode'] == 'IC1'}
-
-	# what we need to buy
-	planets = [supply.Planet(fio_burn) for fio_burn in cache.get('https://rest.fnar.net/fioweb/burn/user/' + config.username,
-			headers={'Authorization': config.fio_api_key})]
-	buy: dict[str, int] = collections.defaultdict(int)
-	for planet in planets:
-		for mat, amount in planet.supply_for_days(7).items():
-			buy[mat] += amount
-
-	# what we have
-	warehouse = supply.warehouse_inventory()
-
-	# what we already are bidding for
-	orders: typing.Sequence[market.ExchangeOrder] = cache.get('https://rest.fnar.net/cxos/' + config.username,
-			headers={'Authorization': config.fio_api_key})
-	orders = [order for order in orders
-			if order['OrderType'] == 'BUYING' and order['Status'] != 'FILLED' and order['ExchangeCode'] == 'IC1']
-
-	bids = collections.defaultdict(int)
-	for order in orders:
-		bids[order['MaterialTicker']] += order['Amount']
+	with concurrent.futures.ThreadPoolExecutor() as executor:
+		futures = [
+			executor.submit(get_raw_prices),
+			executor.submit(get_total_buy), # what we need to buy
+			executor.submit(supply.warehouse_inventory), # what we have
+			executor.submit(get_bids), # what we already are bidding for
+		]
+		raw_prices, buy, warehouse, (bids, orders) = (f.result() for f in futures)
+		executor.shutdown()
 
 	# what's left to buy
 	materials: list[Material] = []
@@ -61,6 +48,30 @@ def main() -> None:
 	for order in orders:
 		print(f"{order['MaterialTicker']:4} {order['Limit'] * order['Amount']:7,.0f}")
 
+def get_raw_prices() -> typing.Mapping[str, market.RawPrice]:
+	return {p['MaterialTicker']: p
+			for p in cache.get('https://refined-prun.github.io/refined-prices/all.json') if p['ExchangeCode'] == 'IC1'}
+
+def get_total_buy() -> typing.Mapping[str, int]:
+	planets = [supply.Planet(fio_burn) for fio_burn in cache.get('https://rest.fnar.net/fioweb/burn/user/' + config.username,
+			headers={'Authorization': config.fio_api_key})]
+	buy: dict[str, int] = collections.defaultdict(int)
+	for planet in planets:
+		for mat, amount in planet.supply_for_days(7).items():
+			buy[mat] += amount
+	return buy
+
+def get_bids() -> tuple[typing.Mapping[str, int], list[market.ExchangeOrder]]:
+	orders: typing.Sequence[market.ExchangeOrder] = cache.get('https://rest.fnar.net/cxos/' + config.username,
+			headers={'Authorization': config.fio_api_key})
+	orders = [order for order in orders
+			if order['OrderType'] == 'BUYING' and order['Status'] != 'FILLED' and order['ExchangeCode'] == 'IC1']
+
+	bids = collections.defaultdict(int)
+	for order in orders:
+		bids[order['MaterialTicker']] += order['Amount']
+	return bids, orders
+
 @dataclasses.dataclass(eq=False, slots=True)
 class Material:
 	ticker: str