From 5ba524e226a34709d9e83c49ddd9b212cd480b19 Mon Sep 17 00:00:00 2001 From: Ou Date: Sun, 6 Oct 2024 00:31:43 +0800 Subject: [PATCH] chore: minor changes --- server/sources/index.ts | 4 +++- server/utils/{index.ts => source.ts} | 31 +++++++++++++++++----------- shared/consts.ts | 3 --- shared/utils.ts | 7 ++++--- 4 files changed, 26 insertions(+), 19 deletions(-) rename server/utils/{index.ts => source.ts} (63%) diff --git a/server/sources/index.ts b/server/sources/index.ts index 6529b9a..89ca97f 100644 --- a/server/sources/index.ts +++ b/server/sources/index.ts @@ -3,7 +3,9 @@ import weibo from "./weibo" import zaobao from "./zaobao" export const sourcesFn = { - "peopledaily": defineRSSSource("https://feedx.net/rss/people.xml"), + "peopledaily": defineRSSSource("https://feedx.net/rss/people.xml", { + hiddenDate: true, + }), weibo, "douyin": defineFallbackSource("douyin"), zaobao, diff --git a/server/utils/index.ts b/server/utils/source.ts similarity index 63% rename from server/utils/index.ts rename to server/utils/source.ts index 6bf5806..2151a69 100644 --- a/server/utils/index.ts +++ b/server/utils/source.ts @@ -1,10 +1,14 @@ -import { RSSHubBase } from "@shared/consts" import type { NewsItem, RSSHubInfo, SourceID } from "@shared/types" export function defineSource(source: () => Promise): () => Promise { return source } +interface SourceOption { + // default: false + hiddenDate?: boolean +} + interface FallbackRes { code: number message: string @@ -21,14 +25,14 @@ interface FallbackRes { mobileUrl: string }[] } -export function defineFallbackSource(id: SourceID): () => Promise { +export function defineFallbackSource(id: SourceID, option?: SourceOption): () => Promise { 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 => ({ + return res.data.slice(0, 20).map(item => ({ extra: { - date: item.time, + date: !option?.hiddenDate && item.time, }, id: item.url, title: item.title, @@ -37,7 +41,8 @@ export function defineFallbackSource(id: SourceID): () => Promise { })) } } -export function defineRSSSource(url: string): () => Promise { + +export function defineRSSSource(url: string, option?: SourceOption): () => Promise { return async () => { const data = await rss2json(url) if (!data?.items.length) throw new Error("Cannot fetch data") @@ -46,28 +51,30 @@ export function defineRSSSource(url: string): () => Promise { url: item.link, id: item.link, extra: { - date: item.created, + date: !option?.hiddenDate && item.created, }, })) } } -interface Option { +interface RSSHubOption { // default: true sorted?: boolean // default: 20 limit?: number } -export function defineRSSHubSource(route: string, option?: Option): () => Promise { +export function defineRSSHubSource(route: string, RSSHubOptions?: RSSHubOption, sourceOption?: SourceOption): () => Promise { return async () => { + // "https://rsshub.pseudoyu.com" + const RSSHubBase = "https://rsshub.rssforever.com" const url = new URL(route, RSSHubBase) url.searchParams.set("format", "json") - const defaultOption: Option = { + const defaultRSSHubOption: RSSHubOption = { sorted: true, limit: 20, } - Object.assign(defaultOption, option) - Object.entries(defaultOption).forEach(([key, value]) => { + Object.assign(defaultRSSHubOption, RSSHubOptions) + Object.entries(defaultRSSHubOption).forEach(([key, value]) => { url.searchParams.set(key, value.toString()) }) const data: RSSHubInfo = await $fetch(url) @@ -76,7 +83,7 @@ export function defineRSSHubSource(route: string, option?: Option): () => Promis url: item.url, id: item.id ?? item.url, extra: { - date: item.date_published, + date: !sourceOption?.hiddenDate && item.date_published, }, })) } diff --git a/shared/consts.ts b/shared/consts.ts index b390571..b3ff3e5 100644 --- a/shared/consts.ts +++ b/shared/consts.ts @@ -6,6 +6,3 @@ export const TTL = 30 * 60 * 1000 * 默认刷新间隔 */ export const Interval = 10 * 60 * 1000 - -export const RSSHubBase = "https://rsshub.rssforever.com" -// export const RSSHubBase = "https://rsshub.pseudoyu.com" diff --git a/shared/utils.ts b/shared/utils.ts index d4195c4..35ab356 100644 --- a/shared/utils.ts +++ b/shared/utils.ts @@ -1,13 +1,14 @@ export function relativeTime(timestamp: string | number) { + if (!timestamp) return undefined const date = new Date(timestamp) + if (Number.isNaN(date.getDay())) return undefined + const now = new Date() const diffInSeconds = (now.getTime() - date.getTime()) / 1000 const diffInMinutes = diffInSeconds / 60 const diffInHours = diffInMinutes / 60 - if (Number.isNaN(date.getDay())) { - return undefined - } else if (diffInSeconds < 60) { + if (diffInSeconds < 60) { return "刚刚" } else if (diffInMinutes < 60) { const minutes = Math.floor(diffInMinutes)