integration.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from __future__ import annotations
  2. import collections
  3. import re
  4. import sys
  5. import typing
  6. import cache
  7. if typing.TYPE_CHECKING:
  8. import roi
  9. def main() -> None:
  10. mat = sys.argv[1]
  11. recipes: list[roi.Recipe] = cache.get('https://api.prunplanner.org/data/recipes/')
  12. companies = pmmg_monthly_report()
  13. print(mat, '→')
  14. wrought = (recipe for recipe in recipes
  15. if mat in (i['material_ticker'] for i in recipe['inputs']) and len(recipe['outputs']) == 1)
  16. output_mats = {}
  17. for recipe in sorted(wrought, key=lambda r: r['outputs'][0]['material_ticker']):
  18. (output,) = recipe['outputs']
  19. (input,) = (i for i in recipe['inputs'] if i['material_ticker'] == mat)
  20. ratio = input['material_amount'] / output['material_amount']
  21. output_mats[output['material_ticker']] = ratio
  22. print(f'\t{output["material_ticker"]:3}:', ratio)
  23. companies_produced = companies_consumed = companies_consumed_only = companies_consumed_own = companies_consumed_80_own = 0
  24. into = dict.fromkeys(output_mats.keys(), 0)
  25. for company in companies.values():
  26. if (co_production := company.get(mat)) is not None:
  27. companies_produced += 1
  28. consumed = 0
  29. for output_mat, per_run_consumption in output_mats.items():
  30. if co_consumption := company.get(output_mat):
  31. total_consumption = co_consumption['amount'] * per_run_consumption
  32. consumed += total_consumption
  33. into[output_mat] += total_consumption
  34. if consumed > 0:
  35. companies_consumed += 1
  36. if co_production is None:
  37. companies_consumed_only += 1
  38. else:
  39. companies_consumed_own += 1
  40. if consumed > 0.8 * co_production['amount']:
  41. companies_consumed_80_own += 1
  42. print(f'{companies_produced} companies producing')
  43. print(f'{companies_consumed} companies consuming')
  44. print(f'{companies_consumed_only} companies consuming without producing any')
  45. print(f'{companies_consumed_own} companies consuming their own production')
  46. print(f'{companies_consumed_80_own} companies consuming 80%+ of their own production')
  47. recipes_per_output = collections.defaultdict(int)
  48. for recipe in recipes:
  49. for output in recipe['outputs']:
  50. if output['material_ticker'] in into:
  51. recipes_per_output[output['material_ticker']] += 1
  52. for output_mat, total in sorted(into.items(), key=lambda kv: kv[1], reverse=True):
  53. alt = ''
  54. if (recipe_count := recipes_per_output[output_mat]) != 1:
  55. alt = f' ({recipe_count} recipes)'
  56. print(f'{output_mat:3}: {total:8,.0f}{alt}')
  57. def pmmg_monthly_report() -> dict[str, dict[str, CompanyOutput]]:
  58. report_constants = cache.get(
  59. 'https://raw.githubusercontent.com/PMMG-Products/pmmg-products.github.io/main/reports/src/staticData/constants.ts',
  60. json=False, expiry=cache.ONE_DAY)
  61. # export const months = ["mar25", "apr25", ..., "dec25", "jan26"];
  62. match = re.search(r'export const months = \[(.*?)\];', report_constants)
  63. assert match
  64. months_str = match.group(1)
  65. months = [m.strip().strip('"') for m in months_str.split(',')]
  66. last_month = months[-1]
  67. print('getting report for', last_month)
  68. return cache.get(f'https://pmmg-products.github.io/reports/data/company-data-{last_month}.json', expiry=cache.ONE_DAY)['individual']
  69. class CompanyOutput(typing.TypedDict):
  70. amount: int
  71. if __name__ == '__main__':
  72. main()