200 lines
6.1 KiB
JavaScript
200 lines
6.1 KiB
JavaScript
import axios from "axios";
|
||
import fs from "fs";
|
||
import path from "path";
|
||
import {
|
||
timestampToDate,
|
||
loopCall,
|
||
keywordsInclude,
|
||
getYiqiNoticeUrl,
|
||
parseToGgDetailsParams,
|
||
// addToMessageQueue,
|
||
} from "./utils.js";
|
||
import config from "./config.js";
|
||
import * as cheerio from "cheerio";
|
||
import { SQLiteMessageQueue } from "./sqlite.js";
|
||
// import { messageQueue } from "./msgManager.js";
|
||
|
||
class YiQi {
|
||
constructor() {
|
||
// this.filepath = path.resolve("yiqi.json");
|
||
this.info = [];
|
||
console.log("一汽 爬虫启动...");
|
||
this.queue = new SQLiteMessageQueue();
|
||
this.start();
|
||
}
|
||
|
||
async start() {
|
||
try {
|
||
await this.init();
|
||
} catch (err) {
|
||
console.error("启动失败:", err);
|
||
}
|
||
}
|
||
async init() {
|
||
let announcements = this.queue.getAnnouncementsBySpider("一汽");
|
||
if (announcements.length > 0) {
|
||
await this.increment();
|
||
} else {
|
||
await this.fullFetch();
|
||
}
|
||
// if (fs.existsSync(this.filepath)) {
|
||
// let data = fs.readFileSync(this.filepath, "utf-8");
|
||
// this.info = data ? JSON.parse(data) : [];
|
||
// if (this.info.length > 0) {
|
||
// await this.increment();
|
||
// } else {
|
||
// await this.fullFetch();
|
||
// }
|
||
// } else {
|
||
// console.log("历史文件不存在,开始全量爬取");
|
||
// await this.fullFetch();
|
||
// }
|
||
}
|
||
// 全量爬取
|
||
async fullFetch() {
|
||
console.log("开始全量爬取...");
|
||
try {
|
||
await loopCall(this.getInfo.bind(this), {
|
||
time: config.fullFetchTime,
|
||
pagenumber: 1,
|
||
stopWhen: (pagenumber, result) => {
|
||
return (
|
||
pagenumber >= result.pages || pagenumber >= config.pageNumberLimit
|
||
);
|
||
},
|
||
readyForNext: (pagenumber, result) => {
|
||
this.info.push(...result.info);
|
||
return pagenumber + 1;
|
||
},
|
||
complete: (result) => {
|
||
this.info.push(...result.info);
|
||
console.log(`爬取完成,共获取 ${this.info.length} 条有效数据`);
|
||
try {
|
||
this.queue.saveAnnouncements("一汽", this.info);
|
||
// this.writeFile(this.info);
|
||
this.queue.addMessage("一汽", this.info);
|
||
} catch (error) {
|
||
console.error("数据库操作失败:", error);
|
||
}
|
||
},
|
||
});
|
||
} catch (error) {
|
||
console.error("全量爬取失败:", error);
|
||
}
|
||
console.log("开始增量爬取...");
|
||
this.increment();
|
||
}
|
||
|
||
// 增量爬取
|
||
async increment() {
|
||
console.log("开始增量爬取模式,每5分钟检查一次新数据...");
|
||
try {
|
||
await loopCall(this.getInfo.bind(this), {
|
||
time: config.incrementFetchTime, // 5分钟间隔
|
||
pagenumber: 1,
|
||
readyForNext: (pagenumber, result) => {
|
||
try {
|
||
let newInfo = this.queue.filterNewAnnouncements(
|
||
"一汽",
|
||
result.info
|
||
);
|
||
// let newInfo = result.info.filter(
|
||
// (item) => !this.info.some((info) => info.id === item.id)
|
||
// );
|
||
// 存在新数据
|
||
if (newInfo.length > 0) {
|
||
console.log(`发现 ${newInfo.length} 条新数据`);
|
||
// this.info.push(...newInfo);
|
||
this.queue.saveAnnouncements("一汽", newInfo);
|
||
// this.writeFile(this.info);
|
||
this.queue.addMessage("一汽", newInfo);
|
||
// 全是新数据,继续下一页
|
||
if (newInfo.length === result.info.length) {
|
||
return pagenumber + 1;
|
||
} else {
|
||
// 有部分重复数据,重新从第一页开始
|
||
return 1;
|
||
}
|
||
} else {
|
||
console.log("没有发现新数据,继续监控...");
|
||
return 1; // 重新从第一页开始
|
||
}
|
||
} catch (error) {
|
||
console.error("数据库操作失败:", error);
|
||
}
|
||
},
|
||
});
|
||
} catch (error) {
|
||
console.error("增量爬取失败:", error);
|
||
}
|
||
}
|
||
async getInfo(pagenumber = 1) {
|
||
let info = [];
|
||
console.log(`正在获取第 ${pagenumber} 页数据...`);
|
||
let result = await this.getHtml(pagenumber);
|
||
if (result[0]) {
|
||
// 出错, 记录错误日志
|
||
console.error("获取页面数据失败:", result[0]);
|
||
return { pages: 30, info: [] };
|
||
} else {
|
||
let pages = 30;
|
||
let html = result[1];
|
||
const $ = cheerio.load(html);
|
||
let noticeEl = $(".zl-list-main .zl-col-6");
|
||
noticeEl.each((index, element) => {
|
||
let id = $(element).find(".zl-desc-item:contains('项目编号')").text();
|
||
let name = $(element).find(".title").text();
|
||
let publishTime = $(element)
|
||
.find(".zl-desc-item:contains('发布时间')")
|
||
.text();
|
||
let endTime = $(element).find(".daojishi").attr("data-time");
|
||
// 获取生产链接的参数
|
||
let funcStr = $(element).find(".jump").attr("onclick");
|
||
|
||
let funcArgs = parseToGgDetailsParams(funcStr);
|
||
// 公告未过期 && 命中关键词
|
||
if (endTime && keywordsInclude(name)) {
|
||
let noticeUrl = getYiqiNoticeUrl(...funcArgs);
|
||
info.push({
|
||
id: id.replace("项目编号:", ""),
|
||
name: name.trim(),
|
||
publishTime: publishTime.replace("发布时间:", "").trim(),
|
||
endTime: timestampToDate(Number(endTime)),
|
||
urls: noticeUrl,
|
||
});
|
||
}
|
||
});
|
||
|
||
return { pages, info };
|
||
}
|
||
}
|
||
// 分页获取数据
|
||
getHtml(pagenumber) {
|
||
return axios({
|
||
url: "https://etp.faw.cn/gg/allJYTypeGGList?hangYeType=-1&xmLeiXing=&ggStartTimeEnd=&gongGaoType=5&isNew=1",
|
||
data: {
|
||
searchType: "",
|
||
searchText: "",
|
||
currentPage: pagenumber,
|
||
},
|
||
headers: {
|
||
"Content-Type": "application/x-www-form-urlencoded",
|
||
},
|
||
method: "post",
|
||
})
|
||
.then((res) => {
|
||
let result = res.data;
|
||
return [null, result];
|
||
})
|
||
.catch((err) => {
|
||
return [err, null];
|
||
});
|
||
}
|
||
|
||
// writeFile(info) {
|
||
// fs.writeFileSync(this.filepath, JSON.stringify(info), "utf-8");
|
||
// }
|
||
}
|
||
|
||
new YiQi();
|