mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-19 03:09:14 +08:00
45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import type { AllSourceID } from "@shared/types"
|
|
import defu from "defu"
|
|
import type { RSSHubOption, RSSHubInfo as RSSHubResponse, SourceGetter, SourceOption } from "#/types"
|
|
|
|
type X = SourceGetter | Partial<Record<AllSourceID, SourceGetter>>
|
|
export function defineSource<T extends X>(source: T): T {
|
|
return source
|
|
}
|
|
|
|
export function defineRSSSource(url: string, option?: SourceOption): SourceGetter {
|
|
return async () => {
|
|
const data = await rss2json(url)
|
|
if (!data?.items.length) throw new Error("Cannot fetch rss data")
|
|
return data.items.map(item => ({
|
|
title: item.title,
|
|
url: item.link,
|
|
id: item.link,
|
|
pubDate: !option?.hiddenDate ? item.created : undefined,
|
|
}))
|
|
}
|
|
}
|
|
|
|
export function defineRSSHubSource(route: string, RSSHubOptions?: RSSHubOption, sourceOption?: SourceOption): SourceGetter {
|
|
return async () => {
|
|
// "https://rsshub.pseudoyu.com"
|
|
const RSSHubBase = "https://rsshub.rssforever.com"
|
|
const url = new URL(route, RSSHubBase)
|
|
url.searchParams.set("format", "json")
|
|
RSSHubOptions = defu<RSSHubOption, RSSHubOption[]>(RSSHubOptions, {
|
|
sorted: true,
|
|
})
|
|
|
|
Object.entries(RSSHubOptions).forEach(([key, value]) => {
|
|
url.searchParams.set(key, value.toString())
|
|
})
|
|
const data: RSSHubResponse = await $fetch(url)
|
|
return data.items.map(item => ({
|
|
title: item.title,
|
|
url: item.url,
|
|
id: item.id ?? item.url,
|
|
pubDate: !sourceOption?.hiddenDate ? item.date_published : undefined,
|
|
}))
|
|
}
|
|
}
|