import { Response } from "node-fetch" ; import { readFile } from "fs/promises" ; import { FeedItem , JSONValue } from "./@types/bubo" ; export const getLink = ( obj ) : string => { const link_values = [ "link" , "url" , "guid" , "home_page_url" ] ; const keys = Object . keys ( obj ) ; const link_property = link_values . find ( link_value => keys . includes ( link_value ) ) ; return link_property ? ( obj [ link_property ] as string ) : "" ; } ; export const getTitle = ( obj ) : string => { const title_values = [ "title" , "url" , "link" ] ; const keys = Object . keys ( obj ) ; const title_property = title_values . find ( title_value => keys . includes ( title_value ) && obj [ title_value ] ) ; return title_property ? ( obj [ title_property ] as string ) : "" ; } ; export const getTimestamp = ( obj ) : string => { const dateString = ( obj . pubDate || obj . isoDate || obj . date || obj . date_published ) . toString ( ) ; const timestamp = new Date ( dateString ) . getTime ( ) ; return isNaN ( timestamp ) ? dateString : timestamp . toString ( ) ; } ; export async function parseFeed ( response ) { const contentType = response . headers . get ( "content-type" ) ?. split ( ";" ) [ 0 ] ; if ( ! contentType ) return { } ; const rssFeed = [ contentType ] . map ( item => [ "application/atom+xml" , "application/rss+xml" , "application/xml" , "text/xml" , "text/html" ] . includes ( item ) ? response . text ( ) : false ) . filter ( _ => _ ) [ 0 ] ; const jsonFeed = [ contentType ] . map ( item => [ "application/json" , "application/feed+json" ] . includes ( item ) ? ( response . json ( ) as Promise < JSONValue > ) : false ) . filter ( _ => _ ) [ 0 ] ; return ( rssFeed && rssFeed ) || ( jsonFeed && jsonFeed ) || { } ; } export const getFeedList = async ( ) : Promise < JSONValue > => { return JSON . parse ( ( await readFile ( new URL ( "../config/feeds.json" , import . meta . url ) ) ) . toString ( ) ) ; } ; export const getBuboInfo = async ( ) : Promise < JSONValue > => { return JSON . parse ( ( await readFile ( new URL ( "../package.json" , import . meta . url ) ) ) . toString ( ) ) ; } ;