SPB Git forge

spb/rareindex

Public
54commits 1branches 0releases
7.1 MBsize
maindefault branch
10 days agolast push
TypeScript 61.9% HTML 37.2% SQL 0.7%
2.5 KB · 38 lines typescript
Raw Blame History
1/** Live fixture capture for hareruya: search-form cardsets + two unisearch_api pages. Usage: pnpm tsx connectors/api/hareruya/_capture.ts */2import { mkdirSync, writeFileSync } from 'node:fs';3import path from 'node:path';4import { fixtureDir, saveFixture } from '@rareindex/connectors/testing';5import { DocSchema, apiUrl, parseCardsets, type HareruyaPayload } from './index.js';6import { HTML_HEADERS, JSON_HEADERS } from '../_g1-cards-eu-jp-lib/index.js';78const SITE = 'https://www.hareruyamtg.com';9const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));10mkdirSync(fixtureDir('hareruya'), { recursive: true });1112const form = await (await fetch(`${SITE}/en/products/search`, { headers: HTML_HEADERS })).text();13writeFileSync(path.join(fixtureDir('hareruya'), 'search-form.html'), form);14const cardsets = parseCardsets(form);15console.log('cardsets', cardsets.length, cardsets.slice(0, 3));1617async function page(cardset: { id: string; name: string }, pageNo: number, name: string, note: string, keep = 15) {18  await wait(4000);19  const url = apiUrl(cardset.id, pageNo, 60);20  const json = (await (await fetch(url, { headers: { ...JSON_HEADERS, referer: `${SITE}/en/products/search?cardset=${cardset.id}` } })).json()) as { response: { numFound: number; page: number | string; docs: unknown[] } };21  const docs = json.response.docs.map((d) => DocSchema.parse(d)).slice(0, keep);22  const payload: HareruyaPayload = { cardset, page: pageNo, numFound: json.response.numFound, docs };23  saveFixture('hareruya', name, {24    raw: { url, externalId: `${cardset.id}:p${pageNo}`, kind: 'listing', engine: 'api', fetchedAt: new Date(), payload },25    expect: { minCount: Math.min(keep, docs.length), kinds: ['listing'], requiredFields: ['price', 'attributes.name', 'attributes.identifiers.hareruya_product_id'] },26    note: `Live capture ${new Date().toISOString().slice(0, 10)} — ${url} (${note}; numFound ${json.response.numFound}, first ${docs.length} docs kept)`,27  });28  console.log(name, cardset, json.response.numFound, docs[0]);29  return json.response.numFound;30}3132const newest = cardsets[0]!;33await page(newest, 1, 'newest-set-page1', `newest card set in the form: ${newest.name}`);34// a set with several languages/foils: find one with a large numFound among the first options35const big = cardsets.find((c) => /Foundations|Bloomburrow|Duskmourn|Aetherdrift|Tarkir/i.test(c.name)) ?? cardsets[3]!;36const n = await page(big, 1, 'multi-language-page1', `set ${big.name} with EN/JP/foil offers`);37if (n > 60) await page(big, 2, 'multi-language-page2', `page 2 of ${big.name} (pagination)`, 8);38