@daiyu-5577/quickbuild
Version:
front-end build service
160 lines (159 loc) • 7.7 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { CronJob } from 'cron';
import path from 'path';
import axios from 'axios';
import { autoClearFiles } from "./fileLog.js";
import dayjs from 'dayjs';
import controller_io from '../command/commandServer/controller.io.js';
import { formatMsg } from '../command/commandBuild/type.js';
import { getWriteStream } from "../utils/fileLog.js";
import { catchBuildLogPath, catchImagePath, baseRoute, catchWebDistPath } from '../command/config.js';
import { generateUid } from '../database/index.js';
import { getFromatTime } from '../utils/time.js';
import { IdType } from '../command/commandBuild/type.js';
const __dirname = new URL('.', import.meta.url).pathname;
const clearFileCron = new CronJob('0 0 12 * * 1', () => {
try {
console.log(`${dayjs().format('YYYY-MM-DD HH:mm:ss')} run clearFileCron`);
autoClearFiles(catchBuildLogPath, 50);
autoClearFiles(catchWebDistPath, 50);
}
catch (error) {
console.log(error);
}
}, null, false);
const getDailyCron = new CronJob('0 0 10-19/1 * * 1-5', () => __awaiter(void 0, void 0, void 0, function* () {
var _a;
try {
console.log(`${dayjs().format('YYYY-MM-DD HH:mm:ss')} run getDailyCron`);
const res = yield axios('https://api.kuleu.com/api/MP4_xiaojiejie?type=json');
const mp4_video = (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.mp4_video;
if (!mp4_video)
return;
const url = new URL(mp4_video);
const fileParse = path.parse(url.pathname);
yield axios({
url: mp4_video,
method: 'GET',
responseType: 'arraybuffer',
})
.then((res) => __awaiter(void 0, void 0, void 0, function* () {
if ((res === null || res === void 0 ? void 0 : res.status) !== 200)
return;
const data = res === null || res === void 0 ? void 0 : res.data;
if (!Buffer.isBuffer(data))
return;
const { writeStream, localFileName } = yield getWriteStream(catchImagePath, `getDailyCron_${dayjs().format('YYYY-MM-DD_HH-mm-ss')}${fileParse.ext}`);
writeStream.end(data);
controller_io.sendMsg('on:msg-chat', formatMsg('on:msg-chat', {
id: generateUid(IdType.M),
socketId: '',
msg: '轻松一刻',
name: '系统消息',
files: [`${baseRoute}/api/showLog?type=image&name=${localFileName}`],
time: getFromatTime(),
timestamp: +dayjs(),
ctxType: 'ctx:imgs'
}));
}));
}
catch (error) {
console.error('GetDailyCron Error:', error);
}
}), null, false);
const getWeatherCron = new CronJob('0 0 10 * * 1-5', () => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g;
try {
console.log(`${dayjs().format('YYYY-MM-DD HH:mm:ss')} run getDailyCron`);
const res = yield axios('https://aider.meizu.com/app/weather/listWeather?cityIds=101200101');
const data = (_b = (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b[0];
if (!data)
return;
const msgArr = [
`${dayjs().format('YYYY年MM月DD日')} ${data.provinceName}${data.city}天气`,
`天气:${(_c = data === null || data === void 0 ? void 0 : data.realtime) === null || _c === void 0 ? void 0 : _c.weather}`,
`温度:${(_d = data === null || data === void 0 ? void 0 : data.realtime) === null || _d === void 0 ? void 0 : _d.temp}度`,
`风向:${(_e = data === null || data === void 0 ? void 0 : data.realtime) === null || _e === void 0 ? void 0 : _e.wD} ${(_f = data === null || data === void 0 ? void 0 : data.realtime) === null || _f === void 0 ? void 0 : _f.wS}`,
`空气质量:${(_g = data === null || data === void 0 ? void 0 : data.pm25) === null || _g === void 0 ? void 0 : _g.quality}`,
`\n------ 温馨小贴士 ------`,
...(data.indexes || []).map((v) => {
return `${v.content}`;
}),
`------ 未来天气 ------`,
...(data.weathers || []).slice(0, -1).map((v) => {
return `${v.date} ${v.week} ${v.weather} 白天最高${v.temp_day_c}度,夜晚最低${v.temp_night_c}度`;
})
];
controller_io.sendMsg('on:msg-chat', formatMsg('on:msg-chat', {
id: generateUid(IdType.M),
socketId: '',
name: '天气小助手',
msg: msgArr.join('\n'),
time: getFromatTime(),
timestamp: +dayjs(),
ctxType: 'ctx:txt'
}));
}
catch (error) {
console.error('GetWeatherCron Error:', error);
}
}), null, false);
const getNewsCron = new CronJob('0 50 9 * * 1-5', () => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
try {
console.log(`${dayjs().format('YYYY-MM-DD HH:mm:ss')} run getNewsCron`);
const res = yield axios({
method: 'post',
url: 'https://gateway.36kr.com/api/mis/nav/home/nav/rank/hot',
headers: {
Origin: 'https://m.36kr.com/',
Referer: 'https://m.36kr.com/',
},
data: {
partner_id: 'wap',
timestamp: +dayjs(),
param: { siteId: 1, platformId: 2 }
}
});
const list = ((_b = (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.hotRankList) || [];
if (!(list === null || list === void 0 ? void 0 : list.length))
return;
const msgArr = [
`${dayjs().format('YYYY年MM月DD日')} 今日头条`,
...list.map((v, i) => {
var _a, _b;
return `${i} ${v === null || v === void 0 ? void 0 : v.templateMaterial.widgetTitle}\n${getFromatTime((_a = v === null || v === void 0 ? void 0 : v.templateMaterial) === null || _a === void 0 ? void 0 : _a.publishTime)} 热度:${(_b = v === null || v === void 0 ? void 0 : v.templateMaterial) === null || _b === void 0 ? void 0 : _b.statRead}`;
})
];
controller_io.sendMsg('on:msg-chat', formatMsg('on:msg-chat', {
id: generateUid(IdType.M),
socketId: '',
name: '36氪热榜',
msg: msgArr.join('\n\n'),
time: getFromatTime(),
timestamp: +dayjs(),
ctxType: 'ctx:txt'
}));
}
catch (error) {
console.error('GetWeatherCron Error:', error);
}
}), null, false);
const cronList = [
clearFileCron,
getWeatherCron,
getNewsCron,
];
export const startCron = () => {
cronList.forEach((cron) => {
cron.start();
});
};