pref: remove fallback source

This commit is contained in:
Ou 2024-10-13 11:39:28 +08:00
parent 1f08297e19
commit fcddce3a83
3 changed files with 81 additions and 2 deletions

49
server/sources/douyin.ts Normal file
View File

@ -0,0 +1,49 @@
interface Res {
data?: {
word_list?: {
sentence_id: string
word: string
event_time: string
hot_value: string
}[]
}
}
async function getDyCookies() {
try {
const cookisUrl = "https://www.douyin.com/passport/general/login_guiding_strategy/?aid=6383"
const data = await $fetch.raw(cookisUrl)
const pattern = /passport_csrf_token=(.*); Path/s
const matchResult = data.headers.get("set-cookie")?.[0]?.match(pattern)
if (matchResult && matchResult.length > 1) {
const cookieData = matchResult[1]
return cookieData
}
} catch (error) {
logger.error(`获取抖音 Cookie 出错: ${error}`)
return null
}
}
export default defineSource(async () => {
const url = "https://www.douyin.com/aweme/v1/web/hot/search/list/?device_platform=webapp&aid=6383&channel=channel_pc_web&detail_list=1"
const cookie = await getDyCookies()
const res: Res = await $fetch(url, {
headers: {
Cookie: `passport_csrf_token=${cookie}`,
},
})
if (!res?.data?.word_list || res.data.word_list.length === 0) throw new Error("Cannot fetch data")
return res.data.word_list
.slice(0, 20)
.map((k) => {
return {
id: k.sentence_id,
title: k.word,
extra: {
info: k.hot_value,
},
url: `https://www.douyin.com/hot/${k.sentence_id}`,
}
})
})

View File

@ -9,6 +9,8 @@ import coolapk from "./coolapk"
import sputniknewscn from "./sputniknewscn"
import kr from "./36kr"
import wallstreetcn from "./wallstreetcn"
import douyin from "./douyin"
import toutiao from "./toutiao"
export const sourcesFn = {
weibo,
@ -20,7 +22,7 @@ export const sourcesFn = {
cankaoxiaoxi,
sputniknewscn,
wallstreetcn,
"douyin": defineFallbackSource("douyin"),
"toutiao": defineFallbackSource("toutiao"),
douyin,
toutiao,
"36kr-quick": kr,
} as Record<SourceID, () => Promise<NewsItem[]>>

28
server/sources/toutiao.ts Normal file
View File

@ -0,0 +1,28 @@
interface Res {
data?: {
ClusterIdStr: string
Title: string
HotValue: string
Image: {
url: string
}
}[]
}
export default defineSource(async () => {
const url = "https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc"
const res: Res = await $fetch(url)
if (!res.data || res.data.length === 0) throw new Error("Cannot fetch data")
return res.data
.slice(0, 20)
.map((k) => {
return {
id: k.ClusterIdStr,
title: k.Title,
extra: {
info: k.HotValue,
},
url: `https://www.toutiao.com/trending/${k.ClusterIdStr}/`,
}
})
})