feat(source): add kuaishou (#7)

* feat(source): add  kuaishou

* Update kuaishou.ts

* Update sources.ts

---------

Co-authored-by: Ou <orongxing@gmail.com>
This commit is contained in:
onlylhf 2024-10-26 10:26:17 +08:00 committed by GitHub
parent 769f127a78
commit 9d2dc242d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 71 additions and 0 deletions

1
server/glob.d.ts vendored
View File

@ -13,6 +13,7 @@ declare module 'glob:./sources/{*.ts,**/index.ts}' {
export const hackernews: typeof import('./sources/hackernews')
export const ithome: typeof import('./sources/ithome')
export const kaopu: typeof import('./sources/kaopu')
export const kuaishou: typeof import('./sources/kuaishou')
export const producthunt: typeof import('./sources/producthunt')
export const solidot: typeof import('./sources/solidot')
export const sputniknewscn: typeof import('./sources/sputniknewscn')

View File

@ -0,0 +1,63 @@
interface KuaishouRes {
defaultClient: {
ROOT_QUERY: {
"visionHotRank({\"page\":\"home\"})": {
type: string
id: string
typename: string
}
[key: string]: any
}
[key: string]: any
}
}
interface HotRankData {
result: number
pcursor: string
webPageArea: string
items: {
type: string
generated: boolean
id: string
typename: string
}[]
}
export default defineSource(async () => {
// 获取快手首页HTML
const html = await $fetch("https://www.kuaishou.com/?isHome=1")
// 提取window.__APOLLO_STATE__中的数据
const matches = (html as string).match(/window\.__APOLLO_STATE__\s*=\s*(\{.+?\});/)
if (!matches) {
throw new Error("无法获取快手热榜数据")
}
// 解析JSON数据
const data: KuaishouRes = JSON.parse(matches[1])
// 获取热榜数据ID
const hotRankId = data.defaultClient.ROOT_QUERY["visionHotRank({\"page\":\"home\"})"].id
// 获取热榜列表数据
const hotRankData = data.defaultClient[hotRankId] as HotRankData
// 转换数据格式
return hotRankData.items.map((item) => {
// 从id中提取实际的热搜词
const hotSearchWord = item.id.replace("VisionHotRankItem:", "")
// 获取具体的热榜项数据
const hotItem = data.defaultClient[item.id]
return {
id: hotSearchWord,
title: hotItem.name,
url: `https://www.kuaishou.com/search/video?searchKey=${encodeURIComponent(hotItem.name)}`,
extra: hotItem.iconUrl
? {
icon: `/api/proxy?img=${encodeURIComponent(hotItem.iconUrl)}`,
}
: {},
}
})
})

View File

@ -246,6 +246,13 @@ export const originSources = {
},
},
},
"kuaishou": {
name: "快手",
type: "hottest",
column: "china",
color: "orange",
home: "https://www.kuaishou.com",
},
"kaopu": {
name: "靠谱新闻",
column: "world",