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"
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,

View File

@ -1,10 +1,14 @@
import { RSSHubBase } from "@shared/consts"
import type { NewsItem, RSSHubInfo, SourceID } from "@shared/types"
export function defineSource(source: () => Promise<NewsItem[]>): () => Promise<NewsItem[]> {
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<NewsItem[]> {
export function defineFallbackSource(id: SourceID, option?: SourceOption): () => 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 => ({
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<NewsItem[]> {
}))
}
}
export function defineRSSSource(url: string): () => Promise<NewsItem[]> {
export function defineRSSSource(url: string, option?: SourceOption): () => Promise<NewsItem[]> {
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<NewsItem[]> {
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<NewsItem[]> {
export function defineRSSHubSource(route: string, RSSHubOptions?: RSSHubOption, sourceOption?: SourceOption): () => Promise<NewsItem[]> {
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,
},
}))
}

View File

@ -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"

View File

@ -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)