From fcddce3a83204dfb4710505ee0f3d10b9cd668c7 Mon Sep 17 00:00:00 2001 From: Ou Date: Sun, 13 Oct 2024 11:39:28 +0800 Subject: [PATCH] pref: remove fallback source --- server/sources/douyin.ts | 49 +++++++++++++++++++++++++++++++++++++++ server/sources/index.ts | 6 +++-- server/sources/toutiao.ts | 28 ++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 server/sources/douyin.ts create mode 100644 server/sources/toutiao.ts diff --git a/server/sources/douyin.ts b/server/sources/douyin.ts new file mode 100644 index 0000000..9ccfebf --- /dev/null +++ b/server/sources/douyin.ts @@ -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}`, + } + }) +}) diff --git a/server/sources/index.ts b/server/sources/index.ts index 99f5208..c054f37 100644 --- a/server/sources/index.ts +++ b/server/sources/index.ts @@ -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 Promise> diff --git a/server/sources/toutiao.ts b/server/sources/toutiao.ts new file mode 100644 index 0000000..10eeaa0 --- /dev/null +++ b/server/sources/toutiao.ts @@ -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}/`, + } + }) +})