Desynth page and improved item info api. Added string substitution to utils.
This commit is contained in:
@@ -82,6 +82,7 @@ async def ensure_inventory_table(conn: asyncpg.Connection) -> None:
|
||||
storage_type TEXT NOT NULL,
|
||||
item_name TEXT NOT NULL,
|
||||
quantity INT NOT NULL,
|
||||
item_id INT,
|
||||
last_updated TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
"""
|
||||
@@ -93,7 +94,13 @@ async def truncate_inventory(conn: asyncpg.Connection) -> None:
|
||||
await conn.execute("TRUNCATE TABLE inventory;")
|
||||
|
||||
|
||||
async def copy_csv_to_db(conn: asyncpg.Connection, rows: List[Tuple[str, str, str, int]]) -> None:
|
||||
async def fetch_item_ids(conn: asyncpg.Connection, item_names: List[str]) -> Dict[str, int]:
|
||||
"""Fetch item IDs from the database."""
|
||||
rows = await conn.fetch("SELECT id, name FROM all_items WHERE name = ANY($1::text[])", item_names)
|
||||
return {row["name"]: row["id"] for row in rows}
|
||||
|
||||
|
||||
async def copy_csv_to_db(conn: asyncpg.Connection, rows: List[Tuple[str, str, str, int, int, _dt.datetime]]) -> None:
|
||||
"""Bulk copy the parsed CSV rows into the DB using ``copy_records_to_table``."""
|
||||
await conn.copy_records_to_table(
|
||||
"inventory",
|
||||
@@ -102,6 +109,7 @@ async def copy_csv_to_db(conn: asyncpg.Connection, rows: List[Tuple[str, str, st
|
||||
"character_name",
|
||||
"storage_type",
|
||||
"item_name",
|
||||
"item_id",
|
||||
"quantity",
|
||||
"last_updated",
|
||||
],
|
||||
@@ -130,15 +138,24 @@ async def load_inventory(csv_path: pathlib.Path) -> None:
|
||||
await truncate_inventory(conn)
|
||||
|
||||
# Parse CSV
|
||||
rows: List[Tuple[str, str, str, int]] = []
|
||||
rows: List[Tuple[str, str, str, int, int]] = []
|
||||
with csv_path.open(newline="", encoding="utf-8") as f:
|
||||
reader = csv.DictReader(f, delimiter=";", quotechar='"')
|
||||
names_set = set()
|
||||
for r in reader:
|
||||
names_set.add(r["item"].strip())
|
||||
# fetch ids
|
||||
id_rows = await conn.fetch("SELECT id,name FROM all_items WHERE name = ANY($1::text[])", list(names_set))
|
||||
id_map = {row["name"]: row["id"] for row in id_rows}
|
||||
f.seek(0)
|
||||
next(reader) # skip header again
|
||||
for r in reader:
|
||||
char = r["char"].strip()
|
||||
storage = r["storage"].strip()
|
||||
item = r["item"].strip()
|
||||
qty = int(r["quantity"].strip()) if r["quantity"].strip() else 0
|
||||
rows.append((char, storage, item, qty, _dt.datetime.utcnow()))
|
||||
item_id = id_map.get(item)
|
||||
rows.append((char, storage, item, item_id, qty, _dt.datetime.utcnow()))
|
||||
|
||||
await copy_csv_to_db(conn, rows)
|
||||
print(f"Inserted {len(rows)} inventory rows.")
|
||||
|
||||
Reference in New Issue
Block a user