diff --git a/lib/routes/fantia/search.ts b/lib/routes/fantia/search.ts index fe28e5381..15b403e28 100644 --- a/lib/routes/fantia/search.ts +++ b/lib/routes/fantia/search.ts @@ -81,7 +81,13 @@ export const route: Route = { keyword: 'Keyword, empty by default', }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'FANTIA_COOKIE', + optional: true, + description: 'The `cookie` after login can be obtained by viewing the request header in the console, If not filled in will cause some posts that require login to read to get exceptions', + }, + ], requirePuppeteer: false, antiCrawler: false, supportBT: false, diff --git a/lib/routes/fantia/user.ts b/lib/routes/fantia/user.ts index 4847145fe..5244b8954 100644 --- a/lib/routes/fantia/user.ts +++ b/lib/routes/fantia/user.ts @@ -2,7 +2,7 @@ import { config } from '@/config'; import type { Route } from '@/types'; import { ViewType } from '@/types'; import cache from '@/utils/cache'; -import got from '@/utils/got'; +import ofetch from '@/utils/ofetch'; import { parseDate } from '@/utils/parse-date'; export const route: Route = { @@ -12,7 +12,13 @@ export const route: Route = { example: '/fantia/user/3498', parameters: { id: 'User id, can be found in user profile URL' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'FANTIA_COOKIE', + optional: true, + description: 'The `cookie` after login can be obtained by viewing the request header in the console, If not filled in will cause some posts that require login to read to get exceptions', + }, + ], requirePuppeteer: false, antiCrawler: false, supportBT: false, @@ -34,36 +40,32 @@ async function handler(ctx) { const rootUrl = 'https://fantia.jp'; const userUrl = `${rootUrl}/api/v1/fanclubs/${ctx.req.param('id')}`; - const initalResponse = await got({ - method: 'get', - url: rootUrl, + const initalResponse = await ofetch(rootUrl, { headers: { Cookie: config.fantia.cookies ?? '', }, }); - const csrfToken = initalResponse.data.match(/name="csrf-token" content="(.*?)"\s?\/>/)[1]; + const csrfToken = initalResponse.match(/name="csrf-token" content="(.*?)"\s?\/>/)[1]; - const response = await got({ - method: 'get', - url: userUrl, + const response = await ofetch(userUrl, { headers: { Cookie: config.fantia.cookies ?? '', }, }); - const list = response.data.fanclub.recent_posts.map((item) => ({ + const fanClub = response.fanclub; + + const list = response.fanclub.recent_posts.map((item) => ({ title: item.title, link: `${rootUrl}/api/v1/posts/${item.id}`, - description: `

${item.comment}

`, + description: item.comment ? `

${item.comment}

` : '', pubDate: parseDate(item.posted_at), })); const items = await Promise.all( list.map((item) => cache.tryGet(item.link, async () => { - const contentResponse = await got({ - method: 'get', - url: item.link, + const contentResponse = await ofetch(item.link, { headers: { Cookie: config.fantia.cookies ?? '', 'X-CSRF-Token': csrfToken, @@ -73,7 +75,7 @@ async function handler(ctx) { }, }); item.link = item.link.replace('api/v1/', ''); - item.description += ``; + item.description += ``; return item; }) @@ -81,8 +83,10 @@ async function handler(ctx) { ); return { - title: `Fantia - ${response.data.fanclub.fanclub_name_with_creator_name}`, + title: `Fantia - ${fanClub.fanclub_name_with_creator_name}`, + description: fanClub.comment?.replaceAll('\r\n', ' ')?.trim(), link: `${rootUrl}/fanclubs/${ctx.req.param('id')}`, + image: fanClub.icon.original ?? fanClub.icon.main, item: items, }; }