import { z } from 'zod'; import { BaseConnector, html as H, type ConnectorMeta, type CrawlContext, type RawRecordInput, type RawRecordLike } from '@rareindex/connectors'; import { AssetAttributesSchema, NormalizedListingSchema, parsePrice, type NormalizedRecord } from '@rareindex/shared'; const BASE = 'https://www.fossilera.com'; const PARSER_VERSION = '1.0.0'; export const SpecimenSchema = z.object({ item: z.string(), url: z.string(), title: z.string(), price: z.number().nullable(), oldPrice: z.number().nullable(), sold: z.boolean(), image: z.string().nullable(), species: z.string().nullable().default(null), age: z.string().nullable().default(null), location: z.string().nullable().default(null), formation: z.string().nullable().default(null), size: z.string().nullable().default(null), category: z.string().nullable().default(null), subCategory: z.string().nullable().default(null), }); export type Specimen = z.infer; export const PagePayloadSchema = z.object({ kind: z.enum(['category_page', 'specimen_page']), url: z.string(), categorySlug: z.string(), items: z.array(SpecimenSchema) }); export type PagePayload = z.infer; const abs = (u: string | null | undefined) => (u ? (u.startsWith('//') ? `https:${u}` : u.startsWith('http') ? u : BASE + u) : null); export function parseCategoryPage(htmlText: string, pageUrl: string, categorySlug: string): PagePayload { const $ = H.load(htmlText); const items: Specimen[] = []; $('a[href^="/fossils/"], a[href^="/minerals/"], a[href^="/meteorites/"]').each((_, el) => { const a = $(el); if (!a.find('.info').length) return; const url = BASE + a.attr('href')!; const alt = a.find('img').attr('alt') ?? ''; const item = alt.match(/#(\d+)\s*$/)?.[1] ?? url.split('/').pop()!; const info = a.find('.info').clone(); const priceEl = info.find('.price'); const oldPrice = parsePrice(H.text(priceEl.find('.old-price')), 'USD')?.amount ?? null; priceEl.find('.old-price').remove(); const priceText = H.text(priceEl) ?? ''; const sold = /sold/i.test(priceText); const price = sold ? null : (parsePrice(priceText, 'USD')?.amount ?? null); priceEl.remove(); const title = H.text(info) ?? alt.replace(/\s*#\d+\s*$/, ''); if (!title) return; items.push({ item, url, title, price, oldPrice, sold, image: abs(a.find('img').attr('src')), species: null, age: null, location: null, formation: null, size: null, category: null, subCategory: null }); }); return { kind: 'category_page', url: pageUrl, categorySlug, items }; } export function parseSpecimenPage(htmlText: string, url: string, categorySlug: string): PagePayload | null { const $ = H.load(htmlText); const title = H.text($('h1').first()); if (!title) return null; const detail = (label: string) => { let v: string | null = null; $('[class*="detail"]').each((_, el) => { const t = $(el).text().replace(/\s+/g, ' ').trim(); const m = t.match(new RegExp(`^${label}\\s+(.+)$`, 'i')); if (m && !v) v = m[1]!.trim(); }); return v; }; const body = $('body').text().replace(/\s+/g, ' '); const item = body.match(/ITEM\s*#\s*(\d+)/i)?.[1] ?? url.split('/').pop()!; const priceBox = $('.price').first(); const oldPrice = parsePrice(H.text(priceBox.find('.old-price')), 'USD')?.amount ?? null; const priceText = priceBox.clone().find('.old-price').remove().end().text(); const sold = /this (specimen|item) (has been|was) sold|sold out/i.test(body); const price = sold ? null : (parsePrice(priceText, 'USD')?.amount ?? null); return { kind: 'specimen_page', url, categorySlug, items: [{ item, url, title, price, oldPrice, sold, image: abs($('meta[property="og:image"]').attr('content')), species: detail('SPECIES'), age: detail('AGE'), location: detail('LOCATION'), formation: detail('FORMATION'), size: detail('SIZE'), category: detail('CATEGORY'), subCategory: detail('SUB CATEGORY') }], }; } export class FossilEraConnector extends BaseConnector { readonly version = '1.0.0'; readonly parserVersion = PARSER_VERSION; protected override minIntervalMs = 1500; override readonly urlPatterns = [/^https?:\/\/(www\.)?fossilera\.com\/(fossils|minerals|meteorites)\/[a-z0-9-]+/i]; async *crawl(ctx: CrawlContext): AsyncIterable { const seeds = (this.meta.config.seeds as Array<{ path: string; categorySlug: string }> | undefined) ?? []; const pages = Number(this.meta.config.pagesPerSeed ?? 1); const cap = ctx.options.limit; let count = 0; const start = ctx.options.mode === 'incremental' ? Number(ctx.options.cursor?.seedIndex ?? 0) : 0; for (let i = start; i < seeds.length; i++) { const seed = seeds[i]!; for (let page = 1; page <= pages; page++) { if (ctx.signal?.aborted || (cap !== undefined && count >= cap)) return; const url = `${BASE}${seed.path}${page > 1 ? `?page=${page}` : ''}`; await this.throttle(); const res = await ctx.fetch(url, { engines: ['api', 'firecrawl'], responseType: 'text', expect: ['title', 'price'], parse: (r) => { const p = r.html ? parseCategoryPage(r.html, url, seed.categorySlug) : null; return p?.items.length ? { title: 'ok', price: p.items.some((x) => x.price) ? 1 : null } : null; }, }); const payload = res.success && res.html ? parseCategoryPage(res.html, url, seed.categorySlug) : null; if (!payload?.items.length) { ctx.anomaly('page_fetch_failed', `${url}: ${res.error ?? res.httpStatus ?? 'no specimen cards'}`); break; } count++; yield { url, externalId: `${seed.path}|p${page}`, kind: 'listing', engine: res.engine, httpStatus: res.httpStatus, payload, fetchedAt: res.fetchedAt }; } await ctx.setCursor({ seedIndex: i + 1 >= seeds.length ? 0 : i + 1, updatedAt: new Date().toISOString() }); } } async lookup(url: string, ctx: CrawlContext): Promise { const m = url.match(this.urlPatterns[0]!); if (!m) return []; const slug = m[2]!.toLowerCase() === 'minerals' ? 'minerals' : m[2]!.toLowerCase() === 'meteorites' ? 'meteorites' : 'fossils'; const res = await ctx.fetch(url, { engines: ['api', 'firecrawl'], responseType: 'text', minQuality: 0 }); const payload = res.success && res.html ? parseSpecimenPage(res.html, url, slug) : null; if (!payload) return []; return [{ url, externalId: payload.items[0]!.item, 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 s of p.items) { const loc = s.location ?? s.title.match(/ - ([A-Z][A-Za-z .]+)$/)?.[1] ?? null; const attributes = AssetAttributesSchema.parse({ categorySlug: p.categorySlug, name: s.title, size: s.size ?? s.title.match(/(\d+(?:\.\d+)?")/)?.[1] ?? null, country: loc, identifiers: { fossilera_item: s.item }, metadata: { species: s.species, geological_age: s.age, formation: s.formation, locality: s.location, category: s.category, sub_category: s.subCategory, previous_price_usd: s.oldPrice, dealer_guarantee: 'FossilEra authenticity guarantee (dealer statement)', unique_specimen: true }, }); const base = { connectorId: this.meta.id, sourceId: this.meta.sourceId, sourceUrl: s.url, rawTitle: s.title, imageUrls: s.image ? [s.image] : [], attributes, observedAt: raw.fetchedAt, parserVersion: PARSER_VERSION }; out.push(NormalizedListingSchema.parse({ kind: 'listing', ...base, externalId: s.item, confidence: 0.85, listingType: 'fixed_price', price: s.price, currency: 'USD', seller: 'FossilEra', location: 'US', availability: s.sold ? 'sold' : s.price ? 'available' : 'unknown' })); } return out; } } export default function createConnector(meta: ConnectorMeta) { return new FossilEraConnector(meta); }