@daiyu-5577/quickbuild
Version:
front-end build service
120 lines (119 loc) • 5.82 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 fs from 'node:fs';
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 } 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 clearBuildLogCron = new CronJob('0 0 12 * * 1', () => {
try {
console.log(`${dayjs().format('YYYY-MM-DD HH:mm:ss')} run clearBuildLogCron`);
if (!fs.existsSync(catchBuildLogPath))
return;
autoClearFiles(catchBuildLogPath, 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 cronList = [
clearBuildLogCron,
getWeatherCron
];
export const startCron = () => {
cronList.forEach((cron) => {
cron.start();
});
};