From 94160fa0f445ea8470d75e20cd3dad92d03e7801 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 17 Jun 2025 17:48:08 +0800 Subject: [PATCH] fix(bsky): keyword --- lib/config.ts | 6 ++++++ lib/routes/bsky/keyword.ts | 43 ++++++++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/lib/config.ts b/lib/config.ts index 2b2796921..6995f33c5 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -415,6 +415,9 @@ export type Config = { smzdm: { cookie?: string; }; + bsky: { + authorization?: string; + }; }; const value: Config | Record = {}; @@ -870,6 +873,9 @@ const calculateValue = () => { smzdm: { cookie: envs.SMZDM_COOKIE, }, + bsky: { + authorization: envs.BSKY_AUTHORIZATION, + }, }; for (const name in _value) { diff --git a/lib/routes/bsky/keyword.ts b/lib/routes/bsky/keyword.ts index 924a35225..02ae01587 100644 --- a/lib/routes/bsky/keyword.ts +++ b/lib/routes/bsky/keyword.ts @@ -1,5 +1,7 @@ +import { config } from '@/config'; +import ConfigNotFoundError from '@/errors/types/config-not-found'; import { Route } from '@/types'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; export const route: Route = { path: '/keyword/:keyword', @@ -7,7 +9,13 @@ export const route: Route = { example: '/bsky/keyword/hello', parameters: { keyword: 'N' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'BSKY_AUTHORIZATION', + description: 'The authorization token for the Bluesky API', + optional: false, + }, + ], requirePuppeteer: false, antiCrawler: false, supportBT: false, @@ -15,22 +23,35 @@ export const route: Route = { supportScihub: false, }, name: 'Keywords', - maintainers: ['untitaker'], + maintainers: ['untitaker', 'DIYgod'], handler, }; async function handler(ctx) { + if (!config.bsky.authorization) { + throw new ConfigNotFoundError('BSKY_AUTHORIZATION is not set'); + } + const keyword = ctx.req.param('keyword'); - const apiLink = `https://search.bsky.social/search/posts?q=${encodeURIComponent(keyword)}`; - const { data } = await got(apiLink); + const data = await ofetch(`https://stinkhorn.us-west.host.bsky.network/xrpc/app.bsky.feed.searchPosts?q=${encodeURIComponent(keyword)}&limit=25&sort=latest`, { + headers: { + Authorization: `Bearer ${config.bsky.authorization}`, + }, + }); - const items = data.map((item) => ({ - title: item.post.text, - link: `https://bsky.app/profile/${item.user.handle}/post/${item.tid.split('/')[1]}`, - description: item.post.text, - pubDate: new Date(item.post.createdAt / 1_000_000), - author: item.user.handle, + const items = data.posts.map((post) => ({ + title: post.record.text, + link: `https://bsky.app/profile/${post.author.handle}/post/${post.uri.split('/').pop()}`, + description: post.record.text, + pubDate: new Date(post.record.createdAt), + author: [ + { + name: post.author.displayName, + url: `https://bsky.app/profile/${post.author.handle}`, + avatar: post.author.avatar, + }, + ], })); return {