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, yearFrom } from '../_luxury-lib/index.js'; /** * FASHIONPHILE — one Shopify product = one authenticated pre-owned item. Listings (asking prices in * USD) plus sold state (`available:false` on a published product = the site shows "SOLD"). */ const BASE = 'https://www.fashionphile.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; export function categoryFor(productType: string | null | undefined, vendor: string | null | undefined, title: string): string { const t = (productType ?? '').toLowerCase(); const l = title.toLowerCase(); if (/watch/.test(t) || /\bwatch\b/.test(l)) return watchCategory(vendor); if (/jewel|ring|bracelet|necklace|earring|brooch|pendant/.test(t)) return 'jewelry'; if (/bag|wallet|clutch|tote|backpack|pouch|luggage|small leather/.test(t)) return 'luxury_handbags'; if (/shoe|sneaker|boot|sandal|heel|pump|loafer|apparel|clothing|scarf|belt|hat|sunglass|jacket|coat|dress/.test(t)) return 'fashion_streetwear'; if (/\b(sandals?|pumps?|loafers?|sneakers?|boots?|heels?|mules?|flats?|espadrilles?|slides?)\b/.test(l) || /\b(muffler|scarf|shawl|stole|belt|sunglasses|hat|cap|jacket|coat|dress|sweater|cardigan|t-shirt|shirt)\b/.test(l)) return 'fashion_streetwear'; return 'luxury_handbags'; } export class FashionphileConnector extends BaseConnector { readonly version = '1.0.0'; readonly parserVersion = PARSER_VERSION; protected override minIntervalMs = 1500; override readonly urlPatterns = [/^https?:\/\/(www\.)?fashionphile\.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 categorySlug = categoryFor(pr.product_type, brand, pr.title); const isWatch = categorySlug.endsWith('watches') || categorySlug === 'rolex' || categorySlug === 'patek_philippe' || categorySlug === 'audemars_piguet' || categorySlug === 'omega'; const title = pr.title.replace(/\s+/g, ' ').trim(); const ref = isWatch ? watchReferenceFromText(title) : null; const model = isWatch ? null : bagModel(title); const attributes = AssetAttributesSchema.parse({ categorySlug, brand, name: brand ? `${brand} ${title}` : title, model, reference: ref, year: yearFrom(pr.body_html ?? '') ?? null, material: isWatch ? watchMaterial(title) : bagMaterial(title), size: isWatch ? caseSize(title) : bagSize(title), color: null, identifiers: { fashionphile_sku: v.sku ?? String(pr.id), ...(ref ? { reference: ref } : {}) }, metadata: { product_type: pr.product_type, hardware: bagHardware(title), tags: pr.tags?.slice(0, 12) ?? [], compare_at_price: moneyNumber(v.compare_at_price) }, }); const sourceUrl = `${BASE}/products/${pr.handle}`; const availability = pr.published_at ? (v.available ? 'available' : 'sold') : 'removed'; const listedAt = pr.published_at ? new Date(pr.published_at) : null; out.push( NormalizedListingSchema.parse({ kind: 'listing', connectorId: this.meta.id, sourceId: this.meta.sourceId, sourceUrl, externalId: String(pr.id), rawTitle: title, description: pr.body_html ?? null, imageUrls: (pr.images ?? []).map((i) => i.src), attributes, condition: { condition: normalizeCondition(categorySlug, null), conditionRaw: null, completeness: null }, observedAt: raw.fetchedAt, confidence: 0.85, parserVersion: PARSER_VERSION, listingType: 'fixed_price', price, currency: 'USD', seller: 'FASHIONPHILE', location: 'US', quantity: 1, listedAt: listedAt && !Number.isNaN(listedAt.getTime()) ? listedAt : null, availability, }), ); } return out; } } export default function createConnector(meta: ConnectorMeta) { return new FashionphileConnector(meta); }