实现采集器模块
- 添加采集器类型定义 (collector.ts) - 实现采集器基类 (base.collector.ts) - 实现 API 采集器 (api.collector.ts) - 实现 Playwright 网页采集器 (playwright.collector.ts) - 实现 RSS 采集器 (rss.collector.ts) - 创建采集器工厂 (collector.factory.ts) - 实现采集服务 (collector.service.ts) - 安装 rss-parser 依赖 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b8791ee556
commit
fe7714a3a6
10 files changed
+703
No files matched your search
@@ -0,0 +1,156 @@
|
||||
import { BaseCollector } from './base.collector';
|
||||
import {
|
||||
CollectorType,
|
||||
CollectionResult,
|
||||
CollectedArticle,
|
||||
PlaywrightCollectorConfig,
|
||||
} from '../types/collector';
|
||||
import { chromium, Browser, Page } from 'playwright';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
export class PlaywrightCollector extends BaseCollector {
|
||||
private playwrightConfig: PlaywrightCollectorConfig;
|
||||
private browser?: Browser;
|
||||
|
||||
constructor(config: PlaywrightCollectorConfig, sourceId: number) {
|
||||
super(config, sourceId);
|
||||
this.playwrightConfig = config;
|
||||
}
|
||||
|
||||
async collect(): Promise<CollectionResult> {
|
||||
try {
|
||||
logger.info(`Playwright采集器开始采集: ${this.playwrightConfig.url}`);
|
||||
|
||||
// 启动浏览器
|
||||
this.browser = await chromium.launch({
|
||||
headless: true,
|
||||
});
|
||||
|
||||
const page = await this.browser.newPage();
|
||||
|
||||
// 设置 User-Agent
|
||||
await page.setExtraHTTPHeaders({
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||
});
|
||||
|
||||
// 访问页面
|
||||
await page.goto(this.playwrightConfig.url, {
|
||||
waitUntil: 'networkidle',
|
||||
timeout: 30000,
|
||||
});
|
||||
|
||||
// 等待内容加载
|
||||
if (this.playwrightConfig.waitFor) {
|
||||
await page.waitForSelector(this.playwrightConfig.waitFor, {
|
||||
timeout: this.playwrightConfig.waitTimeout || 10000,
|
||||
});
|
||||
}
|
||||
|
||||
// 执行自定义脚本(如果有)
|
||||
if (this.playwrightConfig.script) {
|
||||
await page.evaluate(this.playwrightConfig.script);
|
||||
}
|
||||
|
||||
// 提取数据
|
||||
const articles = await this.extractArticles(page);
|
||||
|
||||
logger.info(`Playwright采集器完成,采集到 ${articles.length} 条资讯`);
|
||||
|
||||
return {
|
||||
articles,
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Playwright采集器失败', error);
|
||||
return {
|
||||
articles: [],
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
};
|
||||
} finally {
|
||||
await this.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
private async extractArticles(page: Page): Promise<CollectedArticle[]> {
|
||||
const items = await page.$$(this.playwrightConfig.selectors.items);
|
||||
const articles: CollectedArticle[] = [];
|
||||
|
||||
for (const item of items) {
|
||||
try {
|
||||
const article: CollectedArticle = {
|
||||
title: '',
|
||||
};
|
||||
|
||||
// 提取标题
|
||||
const titleElement = await item.$(this.playwrightConfig.selectors.title);
|
||||
if (titleElement) {
|
||||
article.title = (await titleElement.textContent())?.trim() || '';
|
||||
}
|
||||
|
||||
// 提取内容
|
||||
if (this.playwrightConfig.selectors.content) {
|
||||
const contentElement = await item.$(this.playwrightConfig.selectors.content);
|
||||
if (contentElement) {
|
||||
article.content = (await contentElement.textContent())?.trim();
|
||||
}
|
||||
}
|
||||
|
||||
// 提取链接
|
||||
if (this.playwrightConfig.selectors.url) {
|
||||
const urlElement = await item.$(this.playwrightConfig.selectors.url);
|
||||
if (urlElement) {
|
||||
const href = await urlElement.getAttribute('href');
|
||||
if (href) {
|
||||
article.url = new URL(href, this.playwrightConfig.url).toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 提取日期
|
||||
if (this.playwrightConfig.selectors.publishedAt) {
|
||||
const dateElement = await item.$(this.playwrightConfig.selectors.publishedAt);
|
||||
if (dateElement) {
|
||||
const dateText = (await dateElement.textContent())?.trim();
|
||||
if (dateText) {
|
||||
article.publishedAt = this.parseDate(dateText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (article.title) {
|
||||
articles.push(article);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn('提取单条资讯失败', error);
|
||||
}
|
||||
}
|
||||
|
||||
return articles;
|
||||
}
|
||||
|
||||
validateConfig(): boolean {
|
||||
return !!(
|
||||
this.playwrightConfig.url &&
|
||||
this.playwrightConfig.selectors &&
|
||||
this.playwrightConfig.selectors.items &&
|
||||
this.playwrightConfig.selectors.title
|
||||
);
|
||||
}
|
||||
|
||||
getType(): CollectorType {
|
||||
return 'playwright';
|
||||
}
|
||||
|
||||
async cleanup(): Promise<void> {
|
||||
if (this.browser) {
|
||||
await this.browser.close();
|
||||
this.browser = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
private parseDate(value: string): Date | undefined {
|
||||
const date = new Date(value);
|
||||
return isNaN(date.getTime()) ? undefined : date;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user