crawler / server.ts
GitHub Actions Bot
🚀 Auto-deploy: 5d74de4ffce97f8bbd420c866de5e20ac52a3bad
ec4bf22
Raw
History Blame Contribute Delete
2.79 kB
import express from 'express';
import cors from 'cors';
import { runOttoCrawler } from './src/otto-crawler.js';
import { runAmazonCrawler } from './src/amazon-crawler.js';
import { ProductData } from './src/productData.js';
import { log } from 'crawlee';
const app = express();
const PORT = process.env.PORT || 7860;
// ==================== 爬虫映射表 ====================
/**
* 支持的爬虫源类型
*/
type CrawlerPlatform = 'Otto' | 'Amazon';
/**
* 爬虫函数映射表
*/
const crawlerMap: Record<CrawlerPlatform, (urls: string[], options?: any) => Promise<ProductData[]>> = {
'Otto': runOttoCrawler,
'Amazon': runAmazonCrawler
};
// ==================== Express 中间件配置 ====================
app.use(cors());
app.use(express.json({ limit: '10mb' }));
// 1. 健康检查接口
app.get('/health', (req, res) => {
res.json({
status: 'ok',
service: 'smart-search-crawler',
timestamp: new Date().toISOString(),
supportedPlatforms: Object.keys(crawlerMap)
});
});
// 2. 执行爬取任务接口
app.post('/crawl', async (req, res) => {
try {
const { platform, urls, preset = 'stable' } = req.body;
// 参数验证
if (!platform || typeof platform !== 'string') {
return res.status(400).json({
success: false,
error: '请提供有效的 platform 参数 (例如: "Otto")'
});
}
if (!urls || !Array.isArray(urls) || urls.length === 0) {
return res.status(400).json({
success: false,
error: '请提供有效的 URL 数组 (urls)'
});
}
// 检查是否支持该源
if (!(platform in crawlerMap)) {
return res.status(400).json({
success: false,
error: `不支持的爬虫源: ${platform}。支持的源: ${Object.keys(crawlerMap).join(', ')}`
});
}
log.info(`[API] 收到爬取请求: platform=${platform}, ${urls.length} 个 URL, preset=${preset}`);
// 根据 platform 选择对应的爬虫执行
const crawler = crawlerMap[platform as CrawlerPlatform];
const results: ProductData[] = await crawler(urls, { preset });
res.json({
success: true,
platform: platform,
count: results.length,
data: results
});
} catch (error: any) {
log.error('[API] 爬取任务执行失败:', error.message);
res.status(500).json({
success: false,
error: error.message
});
}
});
// 启动服务
app.listen(PORT, () => {
console.log(`\n🚀 Smart Search Crawler API is running on http://localhost:${PORT}`);
console.log(` - Health: GET http://localhost:${PORT}/health`);
console.log(` - Crawl: POST http://localhost:${PORT}/crawl`);
console.log(` - Supported Platforms: ${Object.keys(crawlerMap).join(', ')}`);
console.log('');
});