| 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 |
| }; |
|
|
| |
|
|
| app.use(cors()); |
| app.use(express.json({ limit: '10mb' })); |
|
|
| |
| app.get('/health', (req, res) => { |
| res.json({ |
| status: 'ok', |
| service: 'smart-search-crawler', |
| timestamp: new Date().toISOString(), |
| supportedPlatforms: Object.keys(crawlerMap) |
| }); |
| }); |
|
|
| |
| 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}`); |
|
|
| |
| 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(''); |
| }); |
|
|