egg_server/schedule/fmMonitor.ts

60 lines
1.8 KiB
TypeScript

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("观棋有语监控开始")
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,
title,
"fmMonitor 节目更新"
)
logger.info("观棋有语监控成功")
} catch (error: any) {
logger.info("观棋有语监控失败", { error: error.message })
const errorMessage = `fmMonitor error: ${error}`
await larkService.message.sendText2Chat(appInfo.errChatId, errorMessage)
}
}
export default fmMonitor