From 9afc600e634598dce59c8416e2115c219a1ef67d Mon Sep 17 00:00:00 2001 From: Thad Miller Date: Thu, 28 May 2026 21:15:29 -0400 Subject: [PATCH] [bugfix] remove leading zeros from number --- scripts/pokemon-helper.ts | 4 ++-- scripts/preload-tcgplayer.ts | 29 ++++++++++++++++++++++++----- tsconfig.json | 2 +- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/scripts/pokemon-helper.ts b/scripts/pokemon-helper.ts index 82558c1..7fbc0ef 100644 --- a/scripts/pokemon-helper.ts +++ b/scripts/pokemon-helper.ts @@ -219,7 +219,7 @@ from ( coalesce(o.product_line_name, t.product_line_name) as product_line_name, coalesce(o.product_url_name, t.product_url_name) as product_url_name, coalesce(o.rarity_name, t.rarity_name) as rarity_name, coalesce(o.sealed, t.sealed) as sealed, coalesce(o.set_id, t.set_id) as set_id, coalesce(o.card_type, t.card_type) as card_type, coalesce(o.energy_type, t.energy_type) as energy_type, - coalesce(o.number, t.number) as number, coalesce(o.artist, t.artist) as artist + coalesce(o.number, regexp_replace(t.number,'^0+','')) as number, coalesce(o.artist, t.artist) as artist from tcg_cards t join (select distinct product_id, variant from skus) b on t.product_id = b.product_id left join tcg_overrides o on t.product_id = o.product_id @@ -239,7 +239,7 @@ select t.product_id, b.variant, coalesce(o.product_name, regexp_replace(regexp_replace(coalesce(nullif(t.product_name, ''), t.product_url_name),' \\\\(.*\\\\)',''),' - .*$','')) as product_name, coalesce(o.product_line_name, t.product_line_name) as product_line_name, coalesce(o.product_url_name, t.product_url_name) as product_url_name, coalesce(o.rarity_name, t.rarity_name) as rarity_name, coalesce(o.sealed, t.sealed) as sealed, coalesce(o.set_id, t.set_id) as set_id, coalesce(o.card_type, t.card_type) as card_type, -coalesce(o.energy_type, t.energy_type) as energy_type, coalesce(o.number, t.number) as number, coalesce(o.artist, t.artist) as artist +coalesce(o.energy_type, t.energy_type) as energy_type, coalesce(o.number, regexp_replace(t.number,'^0+','')) as number, coalesce(o.artist, t.artist) as artist from tcg_cards t join (select distinct product_id, variant from skus) b on t.product_id = b.product_id left join tcg_overrides o on t.product_id = o.product_id diff --git a/scripts/preload-tcgplayer.ts b/scripts/preload-tcgplayer.ts index 1ad5b7a..3725828 100644 --- a/scripts/preload-tcgplayer.ts +++ b/scripts/preload-tcgplayer.ts @@ -76,11 +76,30 @@ const syncProductLine = async ( log(` - ${item.productName} (ID: ${item.productId})`); - const detailResponse = await fetch(`https://mp-search-api.tcgplayer.com/v2/product/${item.productId}/details`, { - method: 'GET', - }); - if (!detailResponse.ok) { - throw new Error(`Error fetching product details for ${item.productId}: ${detailResponse.statusText}`); + let detailResponse: Response | null = null; + let lastError: unknown = null; + for (let attempt = 1; attempt <= 3; attempt++) { + try { + const r = await fetch(`https://mp-search-api.tcgplayer.com/v2/product/${item.productId}/details`, { + method: 'GET', + }); + if (r.ok) { + detailResponse = r; + break; + } + lastError = new Error(`Error fetching product details for ${item.productId}: ${r.statusText}`); + } catch (err) { + lastError = err; + } + if (attempt < 3) { + log(` retry ${attempt}/2 for product ${item.productId} in 5s...`); + await helper.Sleep(5000); + } + } + if (!detailResponse) { + throw lastError instanceof Error + ? lastError + : new Error(`Error fetching product details for ${item.productId}: ${String(lastError)}`); } const detailData = await detailResponse.json(); diff --git a/tsconfig.json b/tsconfig.json index 7feccb2..824237a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { "extends": "astro/tsconfigs/strict", - "include": [".astro/types.d.ts", "src/**/*"], + "include": [".astro/types.d.ts", "src/**/*", "scripts/**/*"], "exclude": ["dist"] }