feat: server routes

This commit is contained in:
Ou 2024-10-05 01:22:41 +08:00
parent 976825c0e2
commit b1fb9d7285
9 changed files with 88 additions and 18 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

View File

@ -1,3 +1,3 @@
import { defineRSSSource } from "#/utils"
import { defineRSSHubSource } from "#/utils"
export default defineRSSSource("https://rsshub.rssforever.com/36kr/newsflashes")
export default defineRSSHubSource("/36kr/newsflashes")

5
server/sources/36kr.ts Normal file
View File

@ -0,0 +1,5 @@
import { defineRSSHubSource } from "#/utils"
export default defineRSSHubSource("/36kr/hot-list", {
sorted: false,
})

View File

@ -2,7 +2,9 @@ import type { SourceID, SourceInfo } from "@shared/types"
import peopledaily from "./peopledaily"
import weibo from "./weibo"
import zaobao from "./zaobao"
import kr from "./36kr-quick"
import krQ from "./36kr-quick"
import wallstreetcn from "./wallstreetcn"
// import kr from "./36kr"
export { fallback } from "./fallback"
@ -10,5 +12,7 @@ export const sources = {
peopledaily,
weibo,
zaobao,
"36kr-quick": kr,
wallstreetcn,
"36kr-quick": krQ,
// "36kr": kr,
} as Record<SourceID, () => Promise<SourceInfo>>

View File

@ -0,0 +1,3 @@
import { defineRSSHubSource } from "#/utils"
export default defineRSSHubSource("/wallstreetcn/live")

View File

@ -1,4 +1,5 @@
import type { NewsItem, SourceInfo } from "@shared/types"
import { RSSHubBase } from "@shared/consts"
import type { NewsItem, RSSHubInfo, SourceInfo } from "@shared/types"
export function defineSource(source: () => Promise<NewsItem[]>): () => Promise<SourceInfo> {
return async () => ({
@ -9,11 +10,11 @@ export function defineSource(source: () => Promise<NewsItem[]>): () => Promise<S
export function defineRSSSource(url: string): () => Promise<SourceInfo> {
return async () => {
const source = await rss2json(url)
if (!source?.items.length) throw new Error("Cannot fetch data")
const data = await rss2json(url)
if (!data?.items.length) throw new Error("Cannot fetch data")
return {
updatedTime: source.updatedTime ?? Date.now(),
items: source.items.slice(0, 20).map(item => ({
updatedTime: data.updatedTime ?? Date.now(),
items: data.items.slice(0, 20).map(item => ({
title: item.title,
url: item.link,
id: item.link,
@ -24,3 +25,36 @@ export function defineRSSSource(url: string): () => Promise<SourceInfo> {
}
}
}
interface Option {
// default: true
sorted?: boolean
// default: 20
limit?: number
}
export function defineRSSHubSource(route: string, option?: Option): () => Promise<SourceInfo> {
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)
return {
updatedTime: Date.now(),
items: data.items.slice(0, 20).map(item => ({
title: item.title,
url: item.url,
id: item.id ?? item.url,
extra: {
date: item.date_published,
},
})),
}
}
}

View File

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

View File

@ -3,12 +3,18 @@ import type { Metadata } from "./types"
export const sectionIds = ["focus", "social", "china", "world", "digital"] as const
export const sources = {
"36kr": {
name: "36氪",
type: "人气榜",
"wallstreetcn": {
name: "华尔街见闻",
interval: 10,
home: "https://36kr.com",
home: "https://wallstreetcn.com/",
type: "快讯",
},
// "36kr": {
// name: "36氪",
// type: "人气榜",
// interval: 10,
// home: "https://36kr.com",
// },
"36kr-quick": {
name: "36氪",
type: "快讯",
@ -83,19 +89,19 @@ export const metadata: Metadata = {
sourceList: [],
},
social: {
name: "社交媒体",
sourceList: ["douyin", "hupu", "tieba", "weibo"],
name: "实时",
sourceList: ["douyin", "weibo", "36kr-quick", "wallstreetcn", "zaobao"],
},
china: {
name: "国内",
sourceList: ["peopledaily", "36kr", "toutiao", "36kr-quick"],
sourceList: ["peopledaily", "toutiao"],
},
world: {
name: "国外",
sourceList: ["zaobao"],
sourceList: [],
},
digital: {
name: "数码",
sourceList: ["36kr"],
sourceList: [],
},
}

View File

@ -52,3 +52,18 @@ export interface CacheInfo {
updated: number
expires: number
}
export interface RSSHubInfo {
title: string
home_page_url: string
description: string
items: RSSHubItem[]
}
export interface RSSHubItem {
id: string
url: string
title: string
content_html: string
date_published: string
}