first commit
This commit is contained in:
33
src/config.ts
Normal file
33
src/config.ts
Normal file
File diff suppressed because one or more lines are too long
42
src/db/index.ts
Normal file
42
src/db/index.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { JSONFilePreset, } from 'lowdb/node'
|
||||
import { Low } from 'lowdb'
|
||||
|
||||
// 定义数据结构:setting 是对象,不是数组
|
||||
type Data = {
|
||||
setting: {
|
||||
count: string | number;
|
||||
time: string;
|
||||
[key: string]: any;
|
||||
};
|
||||
};
|
||||
|
||||
class DbJson {
|
||||
private defaultData: Data = {
|
||||
setting: {
|
||||
count: 0,
|
||||
time: ''
|
||||
}
|
||||
}
|
||||
|
||||
db!: Low<Data>;
|
||||
|
||||
constructor () {
|
||||
this.init();
|
||||
}
|
||||
|
||||
async init () {
|
||||
this.db = await JSONFilePreset<Data>('db.json', this.defaultData)
|
||||
}
|
||||
|
||||
async set(params: Data['setting']) {
|
||||
this.db.data.setting = params
|
||||
await this.db.write()
|
||||
}
|
||||
|
||||
// 读取当前保存的 setting
|
||||
async get() {
|
||||
return this.db.data.setting;
|
||||
}
|
||||
}
|
||||
|
||||
export default new DbJson
|
||||
68
src/index.ts
Normal file
68
src/index.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import puppeteer from 'puppeteer-core';
|
||||
import config from "./config";
|
||||
import db from "./db/index";
|
||||
import mailer from "./utils/mailer";
|
||||
import schedule from 'node-schedule';
|
||||
|
||||
// 把你的逻辑封装成函数
|
||||
async function startTask() {
|
||||
console.log('');
|
||||
console.log('====================================');
|
||||
console.log('⏰ 定时任务开始执行:', new Date().toLocaleString());
|
||||
console.log('====================================');
|
||||
console.log('');
|
||||
|
||||
try {
|
||||
// 1. 启动无头浏览器
|
||||
const browser = await puppeteer.launch(config.launchConfig);
|
||||
const page = await browser.newPage();
|
||||
await page.setExtraHTTPHeaders(config.httpHeaders);
|
||||
|
||||
// 访问页面
|
||||
await page.goto(config.page, {
|
||||
waitUntil: 'networkidle2',
|
||||
timeout: <number>config.pageTimeOut
|
||||
});
|
||||
|
||||
await page.setViewport({ width: 1080, height: 1024 });
|
||||
|
||||
// 抓取数量
|
||||
const number = await page.$eval(
|
||||
'#semiTabpost h2 span:last-child',
|
||||
el => el.textContent.trim()
|
||||
);
|
||||
console.log('1 抓取到主播最新作品数:', number);
|
||||
|
||||
// 获取旧值
|
||||
const { count } = await db.get();
|
||||
console.log('2 数据库里的旧值:', count);
|
||||
|
||||
// 判断是否更新
|
||||
if (String(count) !== String(number)) {
|
||||
console.log('✅ 主播更新作品了');
|
||||
await mailer.send();
|
||||
await db.set({
|
||||
count: number,
|
||||
time: new Date().toLocaleString()
|
||||
});
|
||||
console.log('✅ 已更新到数据库');
|
||||
} else {
|
||||
console.log('❌ 主播没有更新作品');
|
||||
}
|
||||
|
||||
await browser.close();
|
||||
console.log('✅ 本次任务执行完成\n');
|
||||
|
||||
} catch (err) {
|
||||
console.error('❌ 任务执行出错:', err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
console.log('✅ 程序已启动,执行任务,定时规则:', config.cron);
|
||||
|
||||
schedule.scheduleJob(config.cron, async () => {
|
||||
await startTask();
|
||||
});
|
||||
|
||||
// startTask()
|
||||
41
src/utils/mailer.ts
Normal file
41
src/utils/mailer.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
import config from '../config';
|
||||
|
||||
|
||||
class Mailer {
|
||||
transporter: any;
|
||||
|
||||
constructor() {
|
||||
this.transporter = nodemailer.createTransport(config.mailer);
|
||||
this.verify();
|
||||
}
|
||||
|
||||
|
||||
async verify() {
|
||||
try {
|
||||
await this.transporter.verify();
|
||||
console.log("✅ 邮件服务已经初始化成功...");
|
||||
} catch (err) {
|
||||
console.error("❌ 邮件服务初始化失败: ", err);
|
||||
}
|
||||
}
|
||||
|
||||
async send() {
|
||||
console.log('准备发送邮件通知!');
|
||||
try {
|
||||
const result = await this.transporter.sendMail({
|
||||
from: '2271608011@qq.com', // sender address
|
||||
to: "2271608011@qq.com", // list of recipients
|
||||
subject: "小🐰碎碎念 - 更新了!", // subject line
|
||||
html: `您关注的博主,更新啦!<a href="${config.page}">抖音主页</a>`
|
||||
});
|
||||
|
||||
console.log('✅ 邮件发送成功:', result);
|
||||
} catch (err) {
|
||||
console.error('❌ 邮件发送失败:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new Mailer
|
||||
Reference in New Issue
Block a user