Explorar el Código

switch to PUNoted /user endpoints

the non-/user endpoints show all users in all datagroups,
even with a ptk
raylu hace 2 semanas
padre
commit
917a1a0adc
Se han modificado 2 ficheros con 30 adiciones y 42 borrados
  1. 8 14
      punoted/storage.py
  2. 22 28
      ts/production.ts

+ 8 - 14
punoted/storage.py

@@ -8,22 +8,16 @@ from config import config
 
 def main():
 	items = frozenset(i.upper() for i in sys.argv[1:])
-	users: typing.Sequence[UserStore] = httpx.get('https://api.punoted.net/v1/storages/',
+	storages: typing.Sequence[Storage] = httpx.get('https://api.punoted.net/v1/storages/user',
 			headers={'X-Data-Token': config.punoted_api_key}).raise_for_status().json()
 
-	for user in users:
-		print(user['Username'])
-		for storage in user['Storages']:
-			for item in storage['StorageItems']:
-				if item['MaterialTicker'] in items:
-					name = storage['Name']
-					if name == 'null':
-						name = storage['Location']
-					print(f'\t{name}: {item["MaterialAmount"]}')
-
-class UserStore(typing.TypedDict):
-	Username: str
-	Storages: typing.Sequence[Storage]
+	for storage in storages:
+		for item in storage['StorageItems']:
+			if item['MaterialTicker'] in items:
+				name = storage['Name']
+				if name == 'null':
+					name = storage['Location']
+				print(f'{name}: {item["MaterialAmount"]} {item["MaterialTicker"]}')
 
 class Storage(typing.TypedDict):
 	Name: str

+ 22 - 28
ts/production.ts

@@ -418,35 +418,33 @@ async function fetchPrices(): Promise<Record<string, RawPrice>> {
 async function fetchSites(): Promise<Record<string, Set<string>>> {
 	if (!apiKey.value)
 		return {};
-	const userSites: PUNUserSite[] = await fetch('https://api.punoted.net/v1/sites/?include_buildings=true',
+	const sites: PUNSite[] = await fetch('https://api.punoted.net/v1/sites/user?include_buildings=true',
 			{headers: {'X-Data-Token': apiKey.value}}).then((r) => r.json());
 
 	const buildingPlanets: Record<string, Set<string>> = {};
-	for (const user of userSites)
-		for (const site of user.Sites)
-			for (const building of site.Buildings) {
-				if (!buildingPlanets[building.BuildingTicker])
-					buildingPlanets[building.BuildingTicker] = new Set();
-				buildingPlanets[building.BuildingTicker].add(site.PlanetName);
-			}
+	for (const site of sites)
+		for (const building of site.Buildings) {
+			if (!buildingPlanets[building.BuildingTicker])
+				buildingPlanets[building.BuildingTicker] = new Set();
+			buildingPlanets[building.BuildingTicker].add(site.PlanetName);
+		}
 	return buildingPlanets;
 }
 
 async function fetchStorage(): Promise<Storage> {
 	if (!apiKey.value)
 		return {allItems: {}, planetItems: {}};
-	const userStores: PUNUserStore[] = await fetch('https://api.punoted.net/v1/storages/',
+	const storages: PUNStorage[] = await fetch('https://api.punoted.net/v1/storages/user',
 			{headers: {'X-Data-Token': apiKey.value}}).then((r) => r.json());
 
 	const allItems: Record<string, number> = {};
 	const planetItems: Record<string, Record<string, number>> = {};
-	for (const user of userStores)
-		for (const storage of user.Storages)
-			for (const item of storage.StorageItems) {
-				allItems[item.MaterialTicker] = (allItems[item.MaterialTicker] ?? 0) + item.MaterialAmount;
-				const planetStorage = planetItems[storage.Location] ??= {};
-				planetStorage[item.MaterialTicker] = (planetStorage[item.MaterialTicker] ?? 0) + item.MaterialAmount;
-			}
+	for (const storage of storages)
+		for (const item of storage.StorageItems) {
+			allItems[item.MaterialTicker] = (allItems[item.MaterialTicker] ?? 0) + item.MaterialAmount;
+			const planetStorage = planetItems[storage.Location] ??= {};
+			planetStorage[item.MaterialTicker] = (planetStorage[item.MaterialTicker] ?? 0) + item.MaterialAmount;
+		}
 	return {allItems, planetItems};
 }
 
@@ -518,21 +516,17 @@ interface Building {
 	scientists: number
 }
 
-interface PUNUserStore {
-	Storages: Array<{
-		Location: string
-		StorageItems: Array<{
-			MaterialTicker: string
-			MaterialAmount: number
-		}>
+interface PUNStorage {
+	Location: string
+	StorageItems: Array<{
+		MaterialTicker: string
+		MaterialAmount: number
 	}>
 }
 
-interface PUNUserSite {
-	Sites: Array<{
-		PlanetName: string
-		Buildings: Array<{BuildingTicker: string}>
-	}>
+interface PUNSite {
+	PlanetName: string
+	Buildings: Array<{BuildingTicker: string}>
 }
 
 type Production = Record<string, Record<string, number>>;