chore: minor changes

This commit is contained in:
Ou 2024-10-06 00:31:43 +08:00
parent a4cf74b9d8
commit 5ba524e226
4 changed files with 26 additions and 19 deletions

View File

@ -3,7 +3,9 @@ import weibo from "./weibo"
import zaobao from "./zaobao" import zaobao from "./zaobao"
export const sourcesFn = { export const sourcesFn = {
"peopledaily": defineRSSSource("https://feedx.net/rss/people.xml"), "peopledaily": defineRSSSource("https://feedx.net/rss/people.xml", {
hiddenDate: true,
}),
weibo, weibo,
"douyin": defineFallbackSource("douyin"), "douyin": defineFallbackSource("douyin"),
zaobao, zaobao,

View File

@ -1,10 +1,14 @@
import { RSSHubBase } from "@shared/consts"
import type { NewsItem, RSSHubInfo, SourceID } from "@shared/types" import type { NewsItem, RSSHubInfo, SourceID } from "@shared/types"
export function defineSource(source: () => Promise<NewsItem[]>): () => Promise<NewsItem[]> { export function defineSource(source: () => Promise<NewsItem[]>): () => Promise<NewsItem[]> {
return source return source
} }
interface SourceOption {
// default: false
hiddenDate?: boolean
}
interface FallbackRes { interface FallbackRes {
code: number code: number
message: string message: string
@ -21,14 +25,14 @@ interface FallbackRes {
mobileUrl: string mobileUrl: string
}[] }[]
} }
export function defineFallbackSource(id: SourceID): () => Promise<NewsItem[]> { export function defineFallbackSource(id: SourceID, option?: SourceOption): () => Promise<NewsItem[]> {
return async () => { return async () => {
const url = `https://smzdk.top/api/${id}/new` const url = `https://smzdk.top/api/${id}/new`
const res: FallbackRes = await $fetch(url) const res: FallbackRes = await $fetch(url)
if (res.code !== 200 || !res.data) throw new Error(res.message) if (res.code !== 200 || !res.data) throw new Error(res.message)
return res.data.map(item => ({ return res.data.slice(0, 20).map(item => ({
extra: { extra: {
date: item.time, date: !option?.hiddenDate && item.time,
}, },
id: item.url, id: item.url,
title: item.title, title: item.title,
@ -37,7 +41,8 @@ export function defineFallbackSource(id: SourceID): () => Promise<NewsItem[]> {
})) }))
} }
} }
export function defineRSSSource(url: string): () => Promise<NewsItem[]> {
export function defineRSSSource(url: string, option?: SourceOption): () => Promise<NewsItem[]> {
return async () => { return async () => {
const data = await rss2json(url) const data = await rss2json(url)
if (!data?.items.length) throw new Error("Cannot fetch data") if (!data?.items.length) throw new Error("Cannot fetch data")
@ -46,28 +51,30 @@ export function defineRSSSource(url: string): () => Promise<NewsItem[]> {
url: item.link, url: item.link,
id: item.link, id: item.link,
extra: { extra: {
date: item.created, date: !option?.hiddenDate && item.created,
}, },
})) }))
} }
} }
interface Option { interface RSSHubOption {
// default: true // default: true
sorted?: boolean sorted?: boolean
// default: 20 // default: 20
limit?: number limit?: number
} }
export function defineRSSHubSource(route: string, option?: Option): () => Promise<NewsItem[]> { export function defineRSSHubSource(route: string, RSSHubOptions?: RSSHubOption, sourceOption?: SourceOption): () => Promise<NewsItem[]> {
return async () => { return async () => {
// "https://rsshub.pseudoyu.com"
const RSSHubBase = "https://rsshub.rssforever.com"
const url = new URL(route, RSSHubBase) const url = new URL(route, RSSHubBase)
url.searchParams.set("format", "json") url.searchParams.set("format", "json")
const defaultOption: Option = { const defaultRSSHubOption: RSSHubOption = {
sorted: true, sorted: true,
limit: 20, limit: 20,
} }
Object.assign(defaultOption, option) Object.assign(defaultRSSHubOption, RSSHubOptions)
Object.entries(defaultOption).forEach(([key, value]) => { Object.entries(defaultRSSHubOption).forEach(([key, value]) => {
url.searchParams.set(key, value.toString()) url.searchParams.set(key, value.toString())
}) })
const data: RSSHubInfo = await $fetch(url) const data: RSSHubInfo = await $fetch(url)
@ -76,7 +83,7 @@ export function defineRSSHubSource(route: string, option?: Option): () => Promis
url: item.url, url: item.url,
id: item.id ?? item.url, id: item.id ?? item.url,
extra: { extra: {
date: item.date_published, date: !sourceOption?.hiddenDate && item.date_published,
}, },
})) }))
} }

View File

@ -6,6 +6,3 @@ export const TTL = 30 * 60 * 1000
* *
*/ */
export const Interval = 10 * 60 * 1000 export const Interval = 10 * 60 * 1000
export const RSSHubBase = "https://rsshub.rssforever.com"
// export const RSSHubBase = "https://rsshub.pseudoyu.com"

View File

@ -1,13 +1,14 @@
export function relativeTime(timestamp: string | number) { export function relativeTime(timestamp: string | number) {
if (!timestamp) return undefined
const date = new Date(timestamp) const date = new Date(timestamp)
if (Number.isNaN(date.getDay())) return undefined
const now = new Date() const now = new Date()
const diffInSeconds = (now.getTime() - date.getTime()) / 1000 const diffInSeconds = (now.getTime() - date.getTime()) / 1000
const diffInMinutes = diffInSeconds / 60 const diffInMinutes = diffInSeconds / 60
const diffInHours = diffInMinutes / 60 const diffInHours = diffInMinutes / 60
if (Number.isNaN(date.getDay())) { if (diffInSeconds < 60) {
return undefined
} else if (diffInSeconds < 60) {
return "刚刚" return "刚刚"
} else if (diffInMinutes < 60) { } else if (diffInMinutes < 60) {
const minutes = Math.floor(diffInMinutes) const minutes = Math.floor(diffInMinutes)