import { z } from 'zod'; import { BaseConnector, type ConnectorMeta, type CrawlContext, type RawRecordInput, type RawRecordLike } from '@rareindex/connectors'; import { normalizeCondition } from '@rareindex/taxonomy'; import { AssetAttributesSchema, NormalizedListingSchema, type NormalizedRecord } from '@rareindex/shared'; import { ShopifyProductSchema, bagHardware, bagMaterial, bagModel, bagSize, caseSize, fetchShopifyPage, moneyNumber, trimShopifyProduct, watchCategory, watchMaterial, watchReferenceFromText } from '../_luxury-lib/index.js'; /** Rebag — Shopify feed of individually graded pre-owned luxury items (USD). */ const BASE = 'https://shop.rebag.com'; const PARSER_VERSION = '1.0.0'; export const PagePayloadSchema = z.object({ kind: z.literal('shopify_page'), url: z.string(), seed: z.string(), page: z.number(), products: z.array(ShopifyProductSchema) }); export type PagePayload = z.infer; const CONDITION_MAP: Record = { pristine: 'pristine', excellent: 'excellent', great: 'very_good', 'very good': 'very_good', good: 'good', fair: 'fair', new: 'new' }; export function categoryFor(productType: string | null | undefined, vendor: string | null | undefined, tags: string[]): string { const t = (productType ?? '').toLowerCase(); const tagStr = tags.join(' ').toLowerCase(); if (/watch/.test(t) || /bc-filter-watches/.test(tagStr)) return watchCategory(vendor); if (/jewel|ring|bracelet|necklace|earring|brooch|pendant|charm/.test(t)) return 'jewelry'; if (/bag|tote|clutch|satchel|backpack|wallet|pouch|luggage|handle|hobo|crossbody|shoulder|belt bag|case|small leather/.test(t)) return 'luxury_handbags'; if (/shoe|sneaker|boot|sandal|heel|pump|loafer|apparel|clothing|scarf|shawl|belt|hat|sunglass|dress|jacket|coat|\btop\b|pant|skirt|sweater/.test(t)) return 'fashion_streetwear'; return 'luxury_handbags'; } /** "Great | Item # 417505/4 / Blue" → { grade: 'Great', color: 'Blue' } */ export function parseVariantTitle(title: string): { grade: string | null; color: string | null } { const parts = title.split('|').map((s) => s.trim()); const grade = parts[0] && !/item/i.test(parts[0]) ? parts[0] : null; const colorPart = parts[parts.length - 1] ?? ''; const color = colorPart.includes('/') ? (colorPart.split('/').pop()?.trim() ?? null) : null; return { grade, color: color || null }; } export function retailFromBody(body: string | null | undefined): number | null { const m = (body ?? '').replace(/<[^>]+>/g, ' ').match(/Estimated Retail Price:\s*\$?\s*([0-9,]+(?:\.\d+)?)/i); return m ? moneyNumber(m[1]) : null; } export class RebagConnector extends BaseConnector { readonly version = '1.0.0'; readonly parserVersion = PARSER_VERSION; protected override minIntervalMs = 1500; override readonly urlPatterns = [/^https?:\/\/(shop\.|www\.)?rebag\.com\/products\/([a-z0-9-]+)/i]; async *crawl(ctx: CrawlContext): AsyncIterable { const seeds = (ctx.options.seeds?.length ? ctx.options.seeds : (this.meta.config.seeds as string[] | undefined)) ?? []; const pages = ctx.options.mode === 'backfill' ? 20 : Number(this.meta.config.pagesPerSeed ?? 2); let count = 0; for (const seed of seeds) { for (let page = 1; page <= pages; page++) { if (ctx.signal?.aborted || this.reached(ctx, count)) return; await this.throttle(); const { products, res } = await fetchShopifyPage(ctx, BASE, `/collections/${seed}/products.json`, page); if (!res.success) { ctx.anomaly('page_fetch_failed', `${seed} p${page}: ${res.error ?? res.httpStatus}`); break; } if (products.length === 0) break; count++; const payload: PagePayload = { kind: 'shopify_page', url: `${BASE}/collections/${seed}/products.json?page=${page}`, seed, page, products: products.map(trimShopifyProduct) }; yield { url: payload.url, externalId: `collection:${seed}:${page}`, kind: 'listing', engine: res.engine, httpStatus: res.httpStatus, payload, fetchedAt: res.fetchedAt }; if (products.length < 250) break; } } } async lookup(url: string, ctx: CrawlContext): Promise { const handle = url.match(this.urlPatterns[0]!)?.[2]; if (!handle) return []; await this.throttle(); const res = await ctx.fetch(`${BASE}/products/${handle}.json`, { engines: ['api'], responseType: 'json', minQuality: 0.2 }); const parsed = z.object({ product: ShopifyProductSchema }).safeParse(res.json); if (!res.success || !parsed.success) return []; const payload: PagePayload = { kind: 'shopify_page', url: `${BASE}/products/${handle}`, seed: `product:${handle}`, page: 1, products: [trimShopifyProduct(parsed.data.product)] }; return [{ url: payload.url, externalId: `product:${handle}`, kind: 'listing', engine: res.engine, httpStatus: res.httpStatus, payload, fetchedAt: res.fetchedAt }]; } async normalize(raw: RawRecordLike): Promise { const p = PagePayloadSchema.parse(raw.payload); const out: NormalizedRecord[] = []; for (const pr of p.products) { const v = pr.variants[0]; if (!v) continue; const price = moneyNumber(v.price); if (!price) continue; const brand = pr.vendor ?? null; const tags = pr.tags ?? []; const categorySlug = categoryFor(pr.product_type, brand, tags); const isWatch = ['rolex', 'patek_philippe', 'audemars_piguet', 'omega', 'other_watches'].includes(categorySlug); const title = pr.title.replace(/\s+/g, ' ').trim(); const { grade, color } = parseVariantTitle(v.title); const conditionRaw = grade; const condition = grade ? (CONDITION_MAP[grade.toLowerCase()] ?? normalizeCondition(categorySlug, grade)) : null; const ref = isWatch ? watchReferenceFromText(`${title} ${pr.body_html ?? ''}`) : null; const retail = retailFromBody(pr.body_html); const attributes = AssetAttributesSchema.parse({ categorySlug, brand, name: brand ? `${brand} ${title}` : title, model: isWatch ? null : bagModel(title), reference: ref, material: isWatch ? watchMaterial(title) : bagMaterial(title), size: isWatch ? caseSize(title) : bagSize(title), color, originalMsrp: retail, originalMsrpCurrency: retail ? 'USD' : null, identifiers: { rebag_item: v.sku ?? String(pr.id), ...(ref ? { reference: ref } : {}) }, metadata: { product_type: pr.product_type, hardware: bagHardware(title), filters: tags.filter((t) => t.startsWith('bc-filter-')).map((t) => t.slice(10)).slice(0, 15) }, }); const accessories = (pr.body_html ?? '').match(/Accessories:\s*([^.]{0,80})/i)?.[1]?.trim() ?? null; const completeness = accessories ? (/no accessories/i.test(accessories) ? 'item_only' : /box/i.test(accessories) && /dust ?bag|card|receipt/i.test(accessories) ? 'full_set' : /dust ?bag/i.test(accessories) ? 'dust_bag' : /box/i.test(accessories) ? 'box' : null) : null; out.push( NormalizedListingSchema.parse({ kind: 'listing', connectorId: this.meta.id, sourceId: this.meta.sourceId, sourceUrl: `${BASE}/products/${pr.handle}`, externalId: String(pr.id), rawTitle: title, description: pr.body_html ?? null, imageUrls: (pr.images ?? []).map((i) => i.src), attributes, condition: { condition, conditionRaw, completeness }, observedAt: raw.fetchedAt, confidence: 0.85, parserVersion: PARSER_VERSION, listingType: 'fixed_price', price, currency: 'USD', seller: 'Rebag', location: 'US', quantity: 1, listedAt: pr.published_at ? new Date(pr.published_at) : null, availability: v.available ? 'available' : 'sold', }), ); } return out; } } export default function createConnector(meta: ConnectorMeta) { return new RebagConnector(meta); }