Przeglądaj źródła

integration: always get latest pmmg monthly report

raylu 1 miesiąc temu
rodzic
commit
3215ad3703
2 zmienionych plików z 36 dodań i 7 usunięć
  1. 22 5
      cache.py
  2. 14 2
      integration.py

+ 22 - 5
cache.py

@@ -13,21 +13,38 @@ if typing.TYPE_CHECKING:
 CACHE_DIR = pathlib.Path(__file__).parent / 'cache'
 client = httpx.Client(transport=httpx.HTTPTransport(http2=True, retries=2), timeout=10)
 
-def get(url: str, *, headers: httpx._types.HeaderTypes|None=None) -> typing.Any:
+@typing.overload
+def get(url: str, *, json: typing.Literal[True]=True, headers: httpx._types.HeaderTypes|None=None) -> typing.Any:
+	...
+@typing.overload
+def get(url: str, *, json: typing.Literal[False], headers: httpx._types.HeaderTypes|None=None) -> str:
+	...
+def get(url: str, *, json=True, headers=None) -> typing.Any:
 	parsed = urllib.parse.urlparse(url)
 	assert parsed.hostname is not None
-	cache_path = CACHE_DIR / parsed.hostname / (urllib.parse.quote(parsed.path.removeprefix('/'), safe='') + '.cbor.xz')
+	cache_filename = urllib.parse.quote(parsed.path.removeprefix('/'), safe='')
+	if json:
+		cache_filename += '.cbor'
+	cache_filename += '.xz'
+	cache_path = CACHE_DIR / parsed.hostname / cache_filename
 
 	try:
 		if cache_path.stat().st_mtime > time.time() - 600: # less than 10 minutes old
 			with lzma.open(cache_path, 'rb') as f:
-				return cbor2.load(f)
+				if json:
+					return cbor2.load(f)
+				else:
+					return f.read().decode('utf-8')
 	except FileNotFoundError:
 		pass # fall through
 
 	r = client.get(url, headers=headers).raise_for_status()
 	cache_path.parent.mkdir(parents=True, exist_ok=True)
 	with lzma.open(cache_path, 'wb') as f:
-		data = r.json()
-		cbor2.dump(data, f)
+		if json:
+			data = r.json()
+			cbor2.dump(data, f)
+		else:
+			data = r.text
+			f.write(data.encode('utf-8'))
 	return data

+ 14 - 2
integration.py

@@ -1,6 +1,7 @@
 from __future__ import annotations
 
 import collections
+import re
 import sys
 import typing
 
@@ -10,8 +11,7 @@ def main() -> None:
 	mat = sys.argv[1]
 
 	recipes: list[Recipe] = cache.get('https://api.prunplanner.org/data/recipes')
-	companies: dict[str, dict[str, CompanyOutput]] = cache.get(
-			'https://pmmg-products.github.io/reports/data/company-data-dec25.json')['individual']
+	companies = pmmg_monthly_report()
 
 	print(mat, '→')
 	wrought = (recipe for recipe in recipes if mat in (i['Ticker'] for i in recipe['Inputs']) and len(recipe['Outputs']) == 1)
@@ -54,6 +54,18 @@ def main() -> None:
 			alt = f' ({recipe_count} recipes)'
 		print(f'{output_mat:3}: {total:8,.0f}{alt}')
 
+def pmmg_monthly_report() -> dict[str, dict[str, CompanyOutput]]:
+	report_constants = cache.get(
+			'https://raw.githubusercontent.com/PMMG-Products/pmmg-products.github.io/main/reports/src/staticData/constants.ts', json=False)
+	# export const months = ["mar25", "apr25", ..., "dec25", "jan26"];
+	match = re.search(r'export const months = \[(.*?)\];', report_constants)
+	assert match
+	months_str = match.group(1)
+	months = [m.strip().strip('"') for m in months_str.split(',')]
+	last_month = months[-1]
+	print('getting report for', last_month)
+	return cache.get(f'https://pmmg-products.github.io/reports/data/company-data-{last_month}.json')['individual']
+
 class CompanyOutput(typing.TypedDict):
 	amount: int