feat: add Amazon seller and buy box share metrics to product analysis
- Introduced `amazonIsSeller` and `amazonBuyboxSharePct90d` fields in KeepaData type. - Updated database schema and queries to store Amazon seller status and buy box share percentage. - Enhanced product analysis results with new metrics from Keepa API. - Modified frontend components to display Amazon seller status and buy box share percentage. - Implemented reanalysis functionality for products to refresh Amazon-related metrics.
This commit is contained in:
119
src/keepa.ts
119
src/keepa.ts
@@ -3,6 +3,8 @@ import type { KeepaData } from "./types.ts";
|
||||
|
||||
const KEEPA_BASE = "https://api.keepa.com";
|
||||
const MAX_ASINS_PER_REQUEST = 100;
|
||||
const AMAZON_US_SELLER_ID = "ATVPDKIKX0DER";
|
||||
const KEEPA_MINUTES_OFFSET = 21_564_000;
|
||||
|
||||
// Token-based rate limiting: Keepa Pro = 1 token/min regeneration.
|
||||
// Each product request costs 1 token regardless of ASIN count (up to 100).
|
||||
@@ -44,7 +46,7 @@ export async function fetchKeepaDataBatch(
|
||||
await waitForToken();
|
||||
|
||||
const asinParam = chunk.join(",");
|
||||
const url = `${KEEPA_BASE}/product?key=${config.keepaApiKey}&domain=1&asin=${asinParam}&stats=90`;
|
||||
const url = `${KEEPA_BASE}/product?key=${config.keepaApiKey}&domain=1&asin=${asinParam}&stats=90&buybox=1&days=90`;
|
||||
|
||||
console.log(
|
||||
`Keepa: fetching ${chunk.length} ASINs (tokens left: ${tokensLeft})...`,
|
||||
@@ -97,6 +99,14 @@ function parseKeepaProduct(product: Record<string, any>): KeepaData {
|
||||
const monthlySold =
|
||||
pickKeepaNumber(product.monthlySold, stats?.monthlySold) ??
|
||||
salesRankDrops30;
|
||||
const amazonIsSeller = resolveAmazonIsSeller(product, stats, csv);
|
||||
const amazonBuyboxSharePct90d =
|
||||
extractAmazonBuyboxSharePct90d(product, stats) ??
|
||||
computeAmazonBuyBoxSharePctFromHistory(
|
||||
product.buyBoxSellerIdHistory,
|
||||
90,
|
||||
new Set([AMAZON_US_SELLER_ID]),
|
||||
);
|
||||
|
||||
return {
|
||||
currentPrice: extractCurrentPrice(csv),
|
||||
@@ -108,6 +118,8 @@ function parseKeepaProduct(product: Record<string, any>): KeepaData {
|
||||
salesRankDrops30,
|
||||
salesRankDrops90,
|
||||
sellerCount: stats?.current?.[11] ?? null,
|
||||
amazonIsSeller,
|
||||
amazonBuyboxSharePct90d,
|
||||
buyBoxSeller: product.buyBoxSellerId ?? null,
|
||||
buyBoxPrice: stats?.current?.[18] != null ? stats.current[18] / 100 : null,
|
||||
monthlySold,
|
||||
@@ -116,6 +128,111 @@ function parseKeepaProduct(product: Record<string, any>): KeepaData {
|
||||
};
|
||||
}
|
||||
|
||||
function resolveAmazonIsSeller(
|
||||
product: Record<string, any>,
|
||||
stats: Record<string, any> | undefined,
|
||||
csv: number[][] | undefined,
|
||||
): boolean | null {
|
||||
if (typeof product.isAmazonSeller === "boolean") {
|
||||
return product.isAmazonSeller;
|
||||
}
|
||||
|
||||
if (typeof product.availabilityAmazon === "number") {
|
||||
if (product.availabilityAmazon >= 0) return true;
|
||||
if (
|
||||
product.availabilityAmazon === -1 ||
|
||||
product.availabilityAmazon === -2
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (stats?.buyBoxIsAmazon === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (typeof stats?.current?.[0] === "number") {
|
||||
if (stats.current[0] > 0) return true;
|
||||
if (stats.current[0] === -1 || stats.current[0] === -2) return false;
|
||||
}
|
||||
|
||||
const latestAmazonPrice = extractLatestPositivePrice(csv?.[0]);
|
||||
if (latestAmazonPrice != null) return true;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function extractAmazonBuyboxSharePct90d(
|
||||
product: Record<string, any>,
|
||||
stats: Record<string, any> | undefined,
|
||||
): number | null {
|
||||
const candidates: unknown[] = [
|
||||
product.buyBoxStatsAmazon90,
|
||||
stats?.buyBoxStatsAmazon90,
|
||||
product.buyBoxStats?.amazon90,
|
||||
product.buyBoxStats?.amazon?.[90],
|
||||
product.buyBoxStats?.amazon?.["90"],
|
||||
product.buyBoxStats?.[AMAZON_US_SELLER_ID]?.[90],
|
||||
product.buyBoxStats?.[AMAZON_US_SELLER_ID]?.["90"],
|
||||
];
|
||||
|
||||
for (const value of candidates) {
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) continue;
|
||||
if (value < 0 || value > 100) continue;
|
||||
return Math.round(value * 100) / 100;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function computeAmazonBuyBoxSharePctFromHistory(
|
||||
history: unknown,
|
||||
windowDays: number,
|
||||
amazonSellerIds: Set<string>,
|
||||
): number | null {
|
||||
if (!Array.isArray(history) || history.length < 2) return null;
|
||||
|
||||
const nowKeepaMinutes =
|
||||
Math.floor(Date.now() / 60_000) - KEEPA_MINUTES_OFFSET;
|
||||
const windowStart = nowKeepaMinutes - windowDays * 24 * 60;
|
||||
let qualifiedMinutes = 0;
|
||||
let amazonMinutes = 0;
|
||||
|
||||
for (let i = 0; i < history.length - 1; i += 2) {
|
||||
const startMinute = Number.parseInt(String(history[i]), 10);
|
||||
const sellerId = String(history[i + 1] ?? "").toUpperCase();
|
||||
const nextRaw = i + 2 < history.length ? history[i + 2] : nowKeepaMinutes;
|
||||
const endMinute = Number.parseInt(String(nextRaw), 10);
|
||||
|
||||
if (!Number.isFinite(startMinute) || !Number.isFinite(endMinute)) continue;
|
||||
if (endMinute <= startMinute) continue;
|
||||
|
||||
const intervalStart = Math.max(startMinute, windowStart);
|
||||
const intervalEnd = Math.min(endMinute, nowKeepaMinutes);
|
||||
if (intervalEnd <= intervalStart) continue;
|
||||
|
||||
if (sellerId === "-1" || sellerId === "-2") continue;
|
||||
|
||||
const minutes = intervalEnd - intervalStart;
|
||||
qualifiedMinutes += minutes;
|
||||
if (amazonSellerIds.has(sellerId)) {
|
||||
amazonMinutes += minutes;
|
||||
}
|
||||
}
|
||||
|
||||
if (qualifiedMinutes === 0) return null;
|
||||
return Math.round((amazonMinutes / qualifiedMinutes) * 10_000) / 100;
|
||||
}
|
||||
|
||||
function extractLatestPositivePrice(series: unknown): number | null {
|
||||
if (!Array.isArray(series) || series.length < 2) return null;
|
||||
const last = series[series.length - 1];
|
||||
if (typeof last !== "number" || !Number.isFinite(last) || last <= 0) {
|
||||
return null;
|
||||
}
|
||||
return last / 100;
|
||||
}
|
||||
|
||||
function pickKeepaNumber(...values: unknown[]): number | null {
|
||||
for (const value of values) {
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) continue;
|
||||
|
||||
Reference in New Issue
Block a user