import { z } from 'zod'; import { BaseConnector, type ConnectorMeta, type CrawlContext, type RawRecordInput, type RawRecordLike } from '@rareindex/connectors'; import type { NormalizedRecord, NormalizedSale } from '@rareindex/shared'; import { dateMDY, makeSale, md, money, splitMarkdownItems, vehicleAttributes } from '../_carlib/index.js'; const BASE = 'https://carsandbids.com'; const PARSER_VERSION = '1.0.0'; export const ItemSchema = z.object({ id: z.string(), url: z.string(), title: z.string(), statusText: z.string(), ended: z.string().nullable(), subtitle: z.string().nullable(), image: z.string().nullable(), }); export const PagePayloadSchema = z.object({ kind: z.literal('past_page'), page: z.number(), items: z.array(ItemSchema) }); export type PagePayload = z.infer; /** Parse the Firecrawl markdown of /past-auctions/ into compact items. */ export function parsePastPage(markdown: string, page: number): PagePayload { const chunks = splitMarkdownItems(markdown, /^- \[!\[/m); const items: z.infer[] = []; for (const c of chunks) { const link = md.link(c, /https:\/\/carsandbids\.com\/auctions\/([A-Za-z0-9]+)\/[a-z0-9-]+/); if (!link) continue; const id = link.href.match(/\/auctions\/([A-Za-z0-9]+)\//)?.[1]; if (!id) continue; const title = c.match(/\]\(https:\/\/carsandbids\.com\/auctions\/[A-Za-z0-9]+\/[a-z0-9-]+\s+"([^"]+)"\)/)?.[1] ?? link.text; const statusText = c.match(/-\s*((?:Sold for|Bid to)\s+[^\]\n]+)\]/)?.[1]?.trim() ?? ''; const ended = c.match(/Ended\s+(\d{1,2}\/\d{1,2}\/\d{2,4})/)?.[1] ?? null; const lines = c.split('\n').map((l) => l.trim()).filter(Boolean); const titleLineIdx = lines.findIndex((l) => l.startsWith(`[${title}](`) || l.startsWith(`[${title.replace(/"/g, '')}]`)); const subtitle = titleLineIdx >= 0 ? lines.slice(titleLineIdx + 1).find((l) => !/^Ended|^Featured|^Watch|^!\[|^- \[/.test(l) && !l.startsWith('[')) ?? null : null; if (!statusText) continue; items.push({ id, url: link.href, title: md.clean(title), statusText, ended, subtitle: subtitle ? md.clean(subtitle) : null, image: md.image(c) }); } return { kind: 'past_page', page, items }; } export class CarsAndBidsConnector extends BaseConnector { readonly version = '1.0.0'; readonly parserVersion = PARSER_VERSION; protected override minIntervalMs = 2000; async *crawl(ctx: CrawlContext): AsyncIterable { const pages = Number(this.meta.config.pagesPerRun ?? 10); const backfill = ctx.options.mode === 'backfill'; const start = backfill ? Number(ctx.options.cursor?.nextPage ?? 1) : 1; const newest = !backfill && typeof ctx.options.cursor?.newestEnded === 'string' ? new Date(ctx.options.cursor.newestEnded as string) : null; let count = 0; let maxEnded: Date | null = newest; for (let page = start; page < start + pages; page++) { if (ctx.signal?.aborted || this.reached(ctx, count)) break; const url = `${BASE}/past-auctions/${page > 1 ? `?page=${page}` : ''}`; await this.throttle(); const res = await ctx.fetch(url, { expect: ['title', 'price', 'date', 'status'], parse: (r) => { const p = r.markdown ? parsePastPage(r.markdown, page) : null; const f = p?.items[0]; return f ? { title: f.title, price: money(f.statusText, 'USD')?.amount ?? null, date: f.ended, status: f.statusText } : null; }, }); if (!res.success || !res.markdown) { ctx.anomaly('page_fetch_failed', `${url}: ${res.error ?? res.httpStatus}`); break; } const payload = parsePastPage(res.markdown, page); if (payload.items.length === 0) { ctx.anomaly('empty_page', url); break; } count++; yield { url, externalId: `past:${page}:${payload.items[0]!.id}`, kind: 'sale', engine: res.engine, httpStatus: res.httpStatus, payload, fetchedAt: res.fetchedAt }; const dates = payload.items.map((i) => dateMDY(i.ended)).filter((d): d is Date => Boolean(d)); for (const d of dates) if (!maxEnded || d > maxEnded) maxEnded = d; const oldest = dates.length ? new Date(Math.min(...dates.map((d) => d.getTime()))) : null; if (backfill) await ctx.setCursor({ nextPage: page + 1, updatedAt: new Date().toISOString() }); else if (newest && oldest && oldest < newest) break; } if (!backfill && maxEnded) await ctx.setCursor({ newestEnded: maxEnded.toISOString(), updatedAt: new Date().toISOString() }); } async normalize(raw: RawRecordLike): Promise { const p = PagePayloadSchema.parse(raw.payload); const out: NormalizedSale[] = []; for (const it of p.items) { if (!/^Sold for/i.test(it.statusText)) continue; const m = money(it.statusText, 'USD'); const saleDate = dateMDY(it.ended); if (!m || !saleDate) continue; const attributes = vehicleAttributes(it.title, { identifiers: { carsandbids_id: it.id }, metadata: { highlights: it.subtitle } }); out.push(makeSale({ meta: this.meta, sourceUrl: it.url, externalId: it.id, rawTitle: it.title, attributes, price: m.amount, currency: m.currency, saleDate, buyerPremiumIncluded: false, auctionHouse: 'Cars & Bids', imageUrls: it.image ? [it.image] : [], description: it.subtitle, observedAt: raw.fetchedAt, parserVersion: PARSER_VERSION })); } return out; } } export default (meta: ConnectorMeta) => new CarsAndBidsConnector(meta);