newsnow/server/utils/index.ts

84 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-10-05 01:22:41 +08:00
import { RSSHubBase } from "@shared/consts"
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-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
}[]
}
export function defineFallbackSource(id: SourceID): () => Promise<NewsItem[]> {
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)
return res.data.map(item => ({
extra: {
date: item.time,
},
id: item.url,
title: item.title,
url: item.url,
mobileUrl: item.mobileUrl,
}))
}
}
2024-10-05 17:20:49 +08:00
export function defineRSSSource(url: string): () => 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-05 17:20:49 +08:00
return data.items.slice(0, 20).map(item => ({
title: item.title,
url: item.link,
id: item.link,
extra: {
date: item.created,
},
}))
2024-10-04 15:36:03 +08:00
}
}
2024-10-05 01:22:41 +08:00
interface Option {
// default: true
sorted?: boolean
// default: 20
limit?: number
}
2024-10-05 17:20:49 +08:00
export function defineRSSHubSource(route: string, option?: Option): () => Promise<NewsItem[]> {
2024-10-05 01:22:41 +08:00
return async () => {
const url = new URL(route, RSSHubBase)
url.searchParams.set("format", "json")
const defaultOption: Option = {
sorted: true,
limit: 20,
}
Object.assign(defaultOption, option)
Object.entries(defaultOption).forEach(([key, value]) => {
url.searchParams.set(key, value.toString())
})
const data: RSSHubInfo = await $fetch(url)
2024-10-05 17:20:49 +08:00
return data.items.slice(0, 20).map(item => ({
title: item.title,
url: item.url,
id: item.id ?? item.url,
extra: {
date: item.date_published,
},
}))
2024-10-05 01:22:41 +08:00
}
}