feat: 添加fmMonitor定时任务,监控节目更新并发送通知;优化sendZhongNotify逻辑,增强错误处理
This commit is contained in:
parent
dacdd812fd
commit
feb6b1efaf
@ -3,7 +3,7 @@ import { RecordModel } from "pocketbase"
|
|||||||
|
|
||||||
import pbClient from "../db/pbClient"
|
import pbClient from "../db/pbClient"
|
||||||
|
|
||||||
interface ConfigModel extends RecordModel {
|
export interface ConfigModel extends RecordModel {
|
||||||
key: string
|
key: string
|
||||||
value: string
|
value: string
|
||||||
desc: string
|
desc: string
|
||||||
|
59
schedule/fmMonitor.ts
Normal file
59
schedule/fmMonitor.ts
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import { ConfigModel } from "../constant/config"
|
||||||
|
import pbClient from "../db/pbClient"
|
||||||
|
import { genContextManually } from "../utils/genContext"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取最新的节目标题
|
||||||
|
* @returns {Promise<string>} 返回最新的节目标题
|
||||||
|
*/
|
||||||
|
const getLatestTitle = async (): Promise<string> => {
|
||||||
|
const response = await fetch("https://www.qingting.fm/channels/387255/").then(
|
||||||
|
(res) => res.text()
|
||||||
|
)
|
||||||
|
const match = response.match(/"title":"([^"]+)"/)
|
||||||
|
if (match?.[1]) return match[1]
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 比较并更新数据库中的节目标题
|
||||||
|
* @param {string} title - 最新的节目标题
|
||||||
|
* @returns {Promise<boolean>} 返回是否有更新
|
||||||
|
*/
|
||||||
|
const diffPbContent = async (title: string): Promise<boolean> => {
|
||||||
|
const id = "7364o2nt8iq7j1n"
|
||||||
|
const current = await pbClient.collection<ConfigModel>("env").getOne(id)
|
||||||
|
if (current.value === title) return false
|
||||||
|
await pbClient.collection<ConfigModel>("env").update(id, { value: title })
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监控节目更新并发送通知
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
const fmMonitor = async (): Promise<void> => {
|
||||||
|
const { larkService, logger, appInfo } = await genContextManually()
|
||||||
|
logger.info("fmMonitor start")
|
||||||
|
try {
|
||||||
|
const title = await getLatestTitle()
|
||||||
|
if (!title) throw new Error("getLatestTitle empty")
|
||||||
|
const diff = await diffPbContent(title)
|
||||||
|
if (!diff) {
|
||||||
|
logger.info("fmMonitor no diff")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await larkService.message.sendText2Chat(
|
||||||
|
appInfo.errChatId,
|
||||||
|
`<b>fmMonitor 节目更新</b>
|
||||||
|
${title}`
|
||||||
|
)
|
||||||
|
logger.info("fmMonitor success")
|
||||||
|
} catch (error) {
|
||||||
|
const errorMessage = `fmMonitor error: ${error}`
|
||||||
|
logger.error(errorMessage)
|
||||||
|
await larkService.message.sendText2Chat(appInfo.errChatId, errorMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default fmMonitor
|
@ -1,6 +1,7 @@
|
|||||||
import schedule from "node-schedule"
|
import schedule from "node-schedule"
|
||||||
|
|
||||||
import report from "../controller/groupAgent/report"
|
import report from "../controller/groupAgent/report"
|
||||||
|
import fmMonitor from "./fmMonitor"
|
||||||
import sendZhongNotify from "./zhongNotify"
|
import sendZhongNotify from "./zhongNotify"
|
||||||
|
|
||||||
export const initSchedule = async () => {
|
export const initSchedule = async () => {
|
||||||
@ -12,4 +13,7 @@ export const initSchedule = async () => {
|
|||||||
|
|
||||||
// 定时任务,每天早上 6 点 0 分 0 秒执行
|
// 定时任务,每天早上 6 点 0 分 0 秒执行
|
||||||
schedule.scheduleJob("0 6 * * *", sendZhongNotify)
|
schedule.scheduleJob("0 6 * * *", sendZhongNotify)
|
||||||
|
|
||||||
|
// 定时任务,每5分钟执行一次
|
||||||
|
schedule.scheduleJob("*/5 * * * *", fmMonitor)
|
||||||
}
|
}
|
||||||
|
@ -17,12 +17,21 @@ const getScheduleStatus = () => {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
const sendZhongNotify = async () => {
|
const sendZhongNotify = async () => {
|
||||||
const ctx = await genContextManually()
|
const { larkService, logger, appInfo } = await genContextManually()
|
||||||
const status = getScheduleStatus()
|
logger.info("sendZhongNotify start")
|
||||||
await ctx.larkService.message.sendText2Chat(
|
try {
|
||||||
ctx.appInfo.errChatId,
|
const status = getScheduleStatus()
|
||||||
`钟哥今天是${status}哦~`
|
await larkService.message.sendText2Chat(
|
||||||
)
|
appInfo.errChatId,
|
||||||
|
`<b>zhongNotify 每日提醒</b>
|
||||||
|
钟哥今天是${status}哦~`
|
||||||
|
)
|
||||||
|
logger.info("sendZhongNotify success")
|
||||||
|
} catch (error) {
|
||||||
|
const errorMessage = `sendZhongNotify error: ${error}`
|
||||||
|
logger.error(errorMessage)
|
||||||
|
await larkService.message.sendText2Chat(appInfo.errChatId, errorMessage)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default sendZhongNotify
|
export default sendZhongNotify
|
||||||
|
Loading…
x
Reference in New Issue
Block a user