This commit is contained in:
2026-03-18 20:43:42 +08:00
parent 92021eaa41
commit cf3422b1ca
8 changed files with 155 additions and 45 deletions

View File

@@ -3,6 +3,7 @@ import { sleep_ms } from '../flow_utils.js';
import { amazon_product, amazon_search_item, amazon_review } from '../../../models/index.js';
import { safe_json_stringify } from '../../json_utils.js';
import { close_browser } from '../../puppeteer/puppeteer_runner.js';
import { Op } from 'sequelize';
function build_batch_key(prefix) {
return `${prefix}_${Date.now()}_${Math.random().toString().slice(2, 8)}`;
@@ -14,6 +15,24 @@ function pick_asin_from_url(url) {
return m && m[1] ? m[1].toUpperCase() : null;
}
function get_today_start() {
const d = new Date();
d.setHours(0, 0, 0, 0);
return d;
}
async function has_detail_fetched_today(asin) {
if (!asin) return false;
const row = await amazon_product.findOne({
attributes: ['asin', 'updated_at'],
where: {
asin,
updated_at: { [Op.gte]: get_today_start() }
}
});
return !!row;
}
function unwrap_action_result(res) {
// 插件返回通常是 { ..., result: { stage, items... } }
if (res && typeof res === 'object' && res.result && typeof res.result === 'object') {
@@ -192,26 +211,25 @@ export async function run_amazon_search_detail_reviews_flow(flow_payload) {
if (gap_ms > 0) {
await sleep_ms(gap_ms);
}
const detail_res = await execute_action_and_record({
action_name: 'amazon_product_detail',
action_payload: { product_url: url },
const asin = pick_asin_from_url(url);
const skip_detail = asin ? await has_detail_fetched_today(asin) : false;
const res = await execute_action_and_record({
action_name: 'amazon_product_detail_reviews',
action_payload: { product_url: url, limit: reviews_limit, skip_detail },
source: 'cron'
});
await persist_detail(detail_res);
const r = unwrap_action_result(res);
const detail_part = r && r.detail ? r.detail : null;
const reviews_part = r && r.reviews ? r.reviews : null;
if (gap_ms > 0) {
await sleep_ms(gap_ms);
if (detail_part) {
await persist_detail(detail_part);
}
if (reviews_part) {
await persist_reviews(reviews_part);
}
const reviews_res = await execute_action_and_record({
action_name: 'amazon_product_reviews',
action_payload: { product_url: url, limit: reviews_limit },
source: 'cron',
keep_browser_open: true
});
await persist_reviews(reviews_res);
}