import { filestore } from '@/db' ; import { ajax } from '@/utils/ajax' ; import { RssChannel , BiliRoom } from '@/models' ; import { BiliLive } from '@/models/BiliLive' ; import { getRoomIdFromUID } from './helper' ; import { CQImage } from 'cq-websocket' ; import { timeFormat , removeHtmlTag } from '@/utils' ; author CaoMeiYouRen
 *  date export param { number } roomid export async function getBiliLiveFromRoomid ( roomid ) { const result = await ajax ( 'https://api.live.bilibili.com/room/v1/Room/get_info' , { room_id : roomid , from : 'room' , } , { } , 'GET' , { Referer : `https://live.bilibili.com/${ roomid }` , } ) ; return result . data ?. data ; } export async function getBiliLive ( roomid ) { const result = await ajax ( 'https://api.live.bilibili.com/room/v1/Room/get_info' , { room_id : roomid , from : 'room' , } , { } , 'GET' , { Referer : `https://live.bilibili.com/${ roomid }` , } ) ; const data = result . data ?. data ; if ( ! data ) { return ; } return new RssChannel ( { title : data . title , link : `https://live.bilibili.com/${ roomid }` , description : data . description , lastBuildDate : data . live_time !== '0000-00-00 00:00:00' ? new Date ( data . live_time ) : undefined , item : [ ] , } ) ; } author CaoMeiYouRen
 *  date export param { number } uid export async function isLive ( uid ) { const result = await ajax ( 'https://api.live.bilibili.com/room/v1/Room/getRoomInfoOld' , { mid : uid , } , { } , 'GET' , { Referer : `https://space.bilibili.com/${ uid }/` , } ) ; const data = result . data ?. data ; if ( ! data ) { return false ; } const key = `bili-roomid-from-uid-${ uid }` ; const roomid = data . roomid ; await filestore . set ( key , roomid ) ; return Boolean ( data . liveStatus ) ; } author CaoMeiYouRen
 *  date export param { number } uid export async function isNewLive ( uid ) { if ( ! await isLive ( uid ) ) { return false ; } const roomid = await getRoomIdFromUID ( uid ) ; if ( ! roomid ) { return false ; } const key = `bili-room-from-roomid-${ roomid }` ; const newRoom = await getBiliLiveFromRoomid ( roomid ) ; if ( ! newRoom ) { return false ; } const oldRoom = await filestore . get ( key , 'live' ) ; if ( ! oldRoom || oldRoom . live_time !== newRoom . live_time ) { const { live_time } = newRoom ; await filestore . set ( key , { live_time } , 'live' ) ; return newRoom ; } return false ; } export function biliLiveFormat ( userName , live , pushType ? ) { let text = `检测到您关注的B站up主 ${ userName } 开播了\n` ; text += `${ removeHtmlTag ( live . title ) }\n` ; text += `${ removeHtmlTag ( live . description ) }\n` ; let cover = live . user_cover ; switch ( pushType ) { case 'dingtalk' : text += `![](${ cover })\n` ; break ; default : { if ( ! cover . includes ( '@' ) ) { cover += '@518w_1e_1c.png' ; } text += `${ new CQImage ( cover ) . toString ( ) }\n` ; } } text += `直播间地址：https://live.bilibili.com/${ live . room_id }\n` ; text += `开播时间：${ timeFormat ( live . live_time ) }` ; text = text . replace ( /(\n[\s|\t]*\r*\n)/g , '\n' ) ; switch ( pushType ) { case 'dingtalk' : { return text . replace ( /\n/g , '\n\n' ) ; } default : return text ; } }