import fs = require ( 'fs-extra' ) ; import { ajax } from '@/utils/ajax' ; import { jsonDeepParse , timeFormat , sendPrivateMsg , getImageUrl } from '@/utils' ; import { RssChannel , RssItem } from '@/models' ; import { CQImage } from 'cq-websocket' ; import { getUsernameFromUID } from './helper' ; import { USE_AV } from '@/config' ; class CardItem { desc : any ; card : any ; extend_json : any ; extra : any ; display : any ; } export async function getBiliDynamic ( uid , pushType ? ) { const result = await ajax ( 'https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history' , { host_uid : uid , } , { } , 'GET' , { Referer : `https://space.bilibili.com/${ uid }/` , } ) ; const cards = jsonDeepParse ( result ?. data ?. data ?. cards ) ; if ( ! cards ) { return ; } const uname = cards [ 0 ] ?. desc ?. user_profile ?. info ?. uname || '' ; return new RssChannel ( { title : `${ uname } 的 bilibili 动态` , link : `https://space.bilibili.com/${ uid }/#/dynamic` , description : `${ uname } 的 bilibili 动态` , item : cards . map ( ( item ) => { const card = item . card ; const data = card . item || card ; const origin = card . origin ; let images = [ ] ; const getImgs = ( data ) => { const imgs = [ ] ; if ( data . pictures ) { for ( let i = 0 ; i < data . pictures . length ; i ++ ) { imgs . push ( data . pictures [ i ] . img_src ) ; } } if ( data . image_urls ) { for ( let i = 0 ; i < data . image_urls . length ; i ++ ) { imgs . push ( data . image_urls [ i ] ) ; } } if ( data . pic ) { imgs . push ( data . pic ) ; } if ( data . cover ) { imgs . push ( data . cover ) ; } if ( data ?. sketch ?. cover_url ) { imgs . push ( data . sketch . cover_url ) ; } return imgs ; } ; images = images . concat ( getImgs ( data ) ) ; if ( origin ) { images = images . concat ( getImgs ( origin . item || origin ) ) ; } let link = '' ; if ( data . dynamic_id_str ) { link = `https://t.bilibili.com/${ data . dynamic_id_str }` ; } else if ( item ?. desc ?. dynamic_id_str ) { link = `https://t.bilibili.com/${ item . desc . dynamic_id_str }` ; } const getTitle = ( data ) => data . title || '' ; const getDes = ( data ) => { if ( ! data ) { return '' ; } let des = data . desc || data . description || data . content || data . summary || ( data ?. vest ?. content ? data . vest . content : '' ) + ( data ?. sketch ? `\n${ data . sketch ?. title }\n${ data . sketch ?. desc_text }` : '' ) || data . intro || data . update_info || '' ; if ( item ?. display ?. emoji_info ) { const emoji = item ?. display ?. emoji_info ?. emoji_details ; emoji ?. forEach ( ( e ) => { switch ( pushType ) { case 'dingtalk' : des = des . replace ( new RegExp ( `\\${ e . text }` , 'g' ) , `![](${ e . url })` ) ; break ; default : des = des . replace ( new RegExp ( `\\${ e . text }` , 'g' ) , new CQImage ( `${ e . url }@40w_1e_1c.png` ) . toString ( ) ) ; } } ) ; } return des ; } ; const getOriginDes = ( data ) => { if ( ! data ) { return '' ; } let text = '' ; if ( data ?. apiSeasonInfo ?. title ) { text += `\n//转发自: ${ data . apiSeasonInfo . title }` ; } if ( data ?. index_title ) { text += `\n${ data . index_title }` ; } return text ; } ; const getOriginName = ( data ) => data . uname || data . author ?. name || data . upper || data . user ?. uname || data . user ?. name || data ?. owner ?. name || data ?. up_info ?. name || '' ; const getOriginTitle = ( data ) => { if ( ! data ) { return '' ; } let title = '' ; if ( data ?. title ) { title += `${ data . title }\n` ; } if ( data ?. subtitle ) { title += `${ data . subtitle }\n` ; } return title ; } ; const getUrl = ( data ) => { if ( ! data ) { return '' ; } const type = item ?. desc ?. type ; if ( data . aid ) { if ( USE_AV ) { return `\n视频地址：https://www.bilibili.com/video/av${ data ?. aid }` ; } const bvid = item ?. desc ?. bvid || item ?. desc ?. origin ?. bvid ; return `\n视频地址：https://www.bilibili.com/video/${ bvid }` ; } if ( data . image_urls ) { return `\n专栏地址：https://www.bilibili.com/read/cv${ data ?. id }` ; } if ( data . upper ) { return `\n音频地址：https://www.bilibili.com/audio/au${ data ?. id }` ; } if ( data . roomid ) { return `\n直播间地址：https://live.bilibili.com/${ data ?. roomid }` ; } if ( data . sketch ) { return `\n活动地址：${ data ?. sketch ?. target_url }` ; } if ( data . url ) { return `\n地址：${ data ?. url }` ; } return '' ; } ; return new RssItem ( { title : getTitle ( data ) , link , description : `${ getDes ( data ) }${ origin && getOriginName ( origin ) ? `\n//@${ getOriginName ( origin ) }: ${ getOriginTitle ( origin . item || origin ) }${ getDes ( origin . item || origin ) }` : `${ getOriginDes ( origin ) }` }${ getUrl ( data ) }${ getUrl ( origin ) }` , images , pubDate : new Date ( item . desc . timestamp * 1000 ) , } ) ; } ) , } ) ; } author CaoMeiYouRen
 *  date export param { string } userName param { RssItem } dynamic param { string } pushType returns export function biliDynamicFormat ( userName , dynamic , pushType ? ) { let text = `检测到您关注的B站up主 ${ userName } 发布了新的动态\n` ; if ( dynamic . title && ! dynamic . description . startsWith ( dynamic . title ) ) { text += `${ dynamic . title }\n` ; } text += `${ dynamic . description }\n` ; if ( dynamic . images ?. length ) { text += `${ dynamic . images ?. map ( ( e ) => { switch ( pushType ) { case 'dingtalk' : return `![](${ e })` ; default : { if ( ! e . includes ( '@' ) ) { e += '@518w_1e_1c.png' ; } return new CQImage ( e ) . toString ( ) ; } } } ) . join ( '' ) }\n` ; } text += `动态链接：${ dynamic . link }\n` ; text += `发布时间：${ timeFormat ( dynamic . pubDate ) }` ; text = text . replace ( /(\n[\s|\t]*\r*\n)/g , '\n' ) ; switch ( pushType ) { case 'dingtalk' : { const urls = getImageUrl ( text ) ; urls . forEach ( ( e ) => { text = text . replace ( e . cq , `![](${ e . url })` ) ; } ) ; return text . replace ( /\n/g , '\n\n' ) ; } default : return text ; } } by