newsnow/server/utils/source.ts

91 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-10-05 23:55:30 +08:00
import type { NewsItem, RSSHubInfo, SourceID } from "@shared/types"
2024-10-04 15:36:03 +08:00
2024-10-05 17:20:49 +08:00
export function defineSource(source: () => Promise<NewsItem[]>): () => Promise<NewsItem[]> {
return source
2024-10-04 15:36:03 +08:00
}
2024-10-06 00:31:43 +08:00
interface SourceOption {
// default: false
hiddenDate?: boolean
}
2024-10-05 23:55:30 +08:00
interface FallbackRes {
code: number
message: string
name: string
title: string
subtitle: string
total: number
updateTime: string
data: {
title: string
desc: string
time?: string
url: string
mobileUrl: string
}[]
}
2024-10-06 00:31:43 +08:00
export function defineFallbackSource(id: SourceID, option?: SourceOption): () => Promise<NewsItem[]> {
2024-10-05 23:55:30 +08:00
return async () => {
const url = `https://smzdk.top/api/${id}/new`
const res: FallbackRes = await $fetch(url)
if (res.code !== 200 || !res.data) throw new Error(res.message)
2024-10-13 13:59:23 +08:00
return res.data.slice(0, 30).map(item => ({
2024-10-05 23:55:30 +08:00
extra: {
2024-10-06 00:31:43 +08:00
date: !option?.hiddenDate && item.time,
2024-10-05 23:55:30 +08:00
},
id: item.url,
title: item.title,
url: item.url,
mobileUrl: item.mobileUrl,
}))
}
}
2024-10-06 00:31:43 +08:00
export function defineRSSSource(url: string, option?: SourceOption): () => Promise<NewsItem[]> {
2024-10-04 15:36:03 +08:00
return async () => {
2024-10-05 01:22:41 +08:00
const data = await rss2json(url)
if (!data?.items.length) throw new Error("Cannot fetch data")
2024-10-13 13:59:23 +08:00
return data.items.slice(0, 30).map(item => ({
2024-10-05 17:20:49 +08:00
title: item.title,
url: item.link,
id: item.link,
extra: {
2024-10-06 00:31:43 +08:00
date: !option?.hiddenDate && item.created,
2024-10-05 17:20:49 +08:00
},
}))
2024-10-04 15:36:03 +08:00
}
}
2024-10-05 01:22:41 +08:00
2024-10-06 00:31:43 +08:00
interface RSSHubOption {
2024-10-05 01:22:41 +08:00
// default: true
sorted?: boolean
// default: 20
limit?: number
}
2024-10-06 00:31:43 +08:00
export function defineRSSHubSource(route: string, RSSHubOptions?: RSSHubOption, sourceOption?: SourceOption): () => Promise<NewsItem[]> {
2024-10-05 01:22:41 +08:00
return async () => {
2024-10-06 00:31:43 +08:00
// "https://rsshub.pseudoyu.com"
const RSSHubBase = "https://rsshub.rssforever.com"
2024-10-05 01:22:41 +08:00
const url = new URL(route, RSSHubBase)
url.searchParams.set("format", "json")
2024-10-06 00:31:43 +08:00
const defaultRSSHubOption: RSSHubOption = {
2024-10-05 01:22:41 +08:00
sorted: true,
limit: 20,
}
2024-10-06 00:31:43 +08:00
Object.assign(defaultRSSHubOption, RSSHubOptions)
Object.entries(defaultRSSHubOption).forEach(([key, value]) => {
2024-10-05 01:22:41 +08:00
url.searchParams.set(key, value.toString())
})
const data: RSSHubInfo = await $fetch(url)
2024-10-13 13:59:23 +08:00
return data.items.slice(0, 30).map(item => ({
2024-10-05 17:20:49 +08:00
title: item.title,
url: item.url,
id: item.id ?? item.url,
extra: {
2024-10-06 00:31:43 +08:00
date: !sourceOption?.hiddenDate && item.date_published,
2024-10-05 17:20:49 +08:00
},
}))
2024-10-05 01:22:41 +08:00
}
}