-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcross-platform-price-comparison.js
More file actions
189 lines (168 loc) Β· 5.83 KB
/
Copy pathcross-platform-price-comparison.js
File metadata and controls
189 lines (168 loc) Β· 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Cross-Platform Price Comparison
*
* Runs all three China wholesale scrapers (Yiwugo, DHgate, Made-in-China)
* for the same keyword, merges results, and outputs a comparison table.
*
* Usage:
* APIFY_TOKEN=your_token node examples/cross-platform-price-comparison.js "bluetooth speaker"
*
* Requirements:
* npm install apify-client
*/
const { ApifyClient } = require('apify-client');
const ACTORS = {
yiwugo: {
id: 'jungle_intertwining/yiwugo-scraper',
name: 'Yiwugo',
input: (kw) => ({ searchKeywords: [kw], maxPages: 2 }),
},
dhgate: {
id: 'jungle_intertwining/dhgate-scraper',
name: 'DHgate',
input: (kw) => ({ searchKeywords: [kw], maxPages: 2, shipTo: 'us' }),
},
mic: {
id: 'jungle_intertwining/made-in-china-scraper',
name: 'Made-in-China',
input: (kw) => ({ searchKeywords: [kw], maxPages: 2 }),
},
};
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Extract the lowest numeric USD price from a price string. */
function parseMinPrice(raw) {
if (!raw) return null;
const s = String(raw).replace(/,/g, '');
// "US$12.50-18.00" β 12.50 | "Β₯8.5" β 8.5 (CNY, ~1.2 USD rough)
const usd = s.match(/\$\s*([\d.]+)/);
if (usd) return parseFloat(usd[1]);
const cny = s.match(/([\d.]+)/);
if (cny) return parseFloat(cny[1]) * 0.14; // rough CNYβUSD
return null;
}
/** Truncate a string to `len` chars. */
function trunc(s, len = 40) {
if (!s) return 'β';
return s.length > len ? s.slice(0, len - 1) + 'β¦' : s;
}
/** Pad/align for console table. */
function pad(s, w) {
s = String(s);
return s.length >= w ? s.slice(0, w) : s + ' '.repeat(w - s.length);
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
const keyword = process.argv[2];
if (!keyword) {
console.error('Usage: node cross-platform-price-comparison.js "<keyword>"');
process.exit(1);
}
const token = process.env.APIFY_TOKEN;
if (!token) {
console.error('Set APIFY_TOKEN environment variable first.');
process.exit(1);
}
const client = new ApifyClient({ token });
console.log(`\nπ Searching "${keyword}" across 3 platforms...\n`);
// Run all three scrapers in parallel
const runs = await Promise.allSettled(
Object.entries(ACTORS).map(async ([key, actor]) => {
console.log(` β³ Starting ${actor.name}...`);
const run = await client.actor(actor.id).call(actor.input(keyword), {
waitSecs: 120,
});
const { items } = await client
.dataset(run.defaultDatasetId)
.listItems({ limit: 50 });
console.log(` β
${actor.name}: ${items.length} products`);
return { platform: actor.name, items };
})
);
// Merge results
const merged = [];
for (const r of runs) {
if (r.status !== 'fulfilled') continue;
const { platform, items } = r.value;
for (const item of items) {
merged.push({
platform,
name: item.productName || item.title || '',
price: item.price || '',
minPrice: parseMinPrice(item.price),
moq: item.moq || item.minOrder || 'β',
supplier: item.supplierName || item.seller || 'β',
url: item.productUrl || item.url || '',
});
}
}
if (merged.length === 0) {
console.log('\nNo results found across any platform.');
return;
}
// Sort by price (cheapest first), nulls last
merged.sort((a, b) => {
if (a.minPrice == null && b.minPrice == null) return 0;
if (a.minPrice == null) return 1;
if (b.minPrice == null) return -1;
return a.minPrice - b.minPrice;
});
// Print comparison table
const cols = { platform: 15, name: 42, price: 20, moq: 14, supplier: 28 };
const sep =
'-'.repeat(cols.platform) + '+-' +
'-'.repeat(cols.name) + '+-' +
'-'.repeat(cols.price) + '+-' +
'-'.repeat(cols.moq) + '+-' +
'-'.repeat(cols.supplier);
console.log(`\nπ Price Comparison: "${keyword}" (${merged.length} products)\n`);
console.log(
pad('Platform', cols.platform) + '| ' +
pad('Product', cols.name) + '| ' +
pad('Price', cols.price) + '| ' +
pad('MOQ', cols.moq) + '| ' +
pad('Supplier', cols.supplier)
);
console.log(sep);
for (const p of merged.slice(0, 30)) {
console.log(
pad(p.platform, cols.platform) + '| ' +
pad(trunc(p.name, cols.name - 1), cols.name) + '| ' +
pad(trunc(p.price, cols.price - 1), cols.price) + '| ' +
pad(trunc(String(p.moq), cols.moq - 1), cols.moq) + '| ' +
pad(trunc(p.supplier, cols.supplier - 1), cols.supplier)
);
}
if (merged.length > 30) {
console.log(` ... and ${merged.length - 30} more products`);
}
// Summary stats per platform
console.log('\nπ Summary by Platform:\n');
for (const plat of ['Yiwugo', 'DHgate', 'Made-in-China']) {
const items = merged.filter((p) => p.platform === plat);
const prices = items.map((p) => p.minPrice).filter(Boolean);
if (items.length === 0) {
console.log(` ${plat}: no results`);
continue;
}
const avg = prices.length
? (prices.reduce((a, b) => a + b, 0) / prices.length).toFixed(2)
: 'β';
const min = prices.length ? Math.min(...prices).toFixed(2) : 'β';
const max = prices.length ? Math.max(...prices).toFixed(2) : 'β';
console.log(
` ${plat}: ${items.length} products | Price range: $${min} β $${max} | Avg: $${avg}`
);
}
// Export to JSON
const outFile = `comparison-${keyword.replace(/\s+/g, '-')}.json`;
require('fs').writeFileSync(outFile, JSON.stringify(merged, null, 2));
console.log(`\nπΎ Full results saved to ${outFile}\n`);
}
main().catch((err) => {
console.error('Error:', err.message);
process.exit(1);
});