diff --git a/lib/config.ts b/lib/config.ts index 3a10349fd..0d55e685f 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -166,6 +166,9 @@ export type Config = { username?: string; password?: string; }; + javdb: { + session?: string; + }; lastfm: { api_key?: string; }; @@ -508,6 +511,9 @@ const calculateValue = () => { username: envs.IWARA_USERNAME, password: envs.IWARA_PASSWORD, }, + javdb: { + session: envs.JAVDB_SESSION, + }, lastfm: { api_key: envs.LASTFM_API_KEY, }, diff --git a/lib/routes/javdb/actors.ts b/lib/routes/javdb/actors.ts index 37d121ac6..a4bc97517 100644 --- a/lib/routes/javdb/actors.ts +++ b/lib/routes/javdb/actors.ts @@ -7,7 +7,13 @@ export const route: Route = { example: '/javdb/actors/R2Vg', parameters: { id: '编号,可在演员页 URL 中找到', filter: '过滤,见下表,默认为 `全部`' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'JAVDB_SESSION', + description: 'JavDB登陆后的session值,可在控制台的cookie下查找 `_jdb_session` 的值,即可获取', + optional: true, + }, + ], requirePuppeteer: false, antiCrawler: true, supportBT: false, @@ -46,6 +52,5 @@ async function handler(ctx) { }; const title = `JavDB${filters[filter] === '' ? '' : ` - ${filters[filter]}`} `; - return await utils.ProcessItems(ctx, currentUrl, title); } diff --git a/lib/routes/javdb/makers.ts b/lib/routes/javdb/makers.ts index fbb8165b8..ce61dcc5e 100644 --- a/lib/routes/javdb/makers.ts +++ b/lib/routes/javdb/makers.ts @@ -7,7 +7,13 @@ export const route: Route = { example: '/javdb/makers/7R', parameters: { id: '编号,可在片商页 URL 中找到', filter: '过滤,见下表,默认为 `全部`' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'JAVDB_SESSION', + description: 'JavDB登陆后的session值,可在控制台的cookie下查找 `_jdb_session` 的值,即可获取', + optional: true, + }, + ], requirePuppeteer: false, antiCrawler: true, supportBT: false, diff --git a/lib/routes/javdb/rankings.ts b/lib/routes/javdb/rankings.ts index a798d5f82..5e767ad44 100644 --- a/lib/routes/javdb/rankings.ts +++ b/lib/routes/javdb/rankings.ts @@ -7,7 +7,13 @@ export const route: Route = { example: '/javdb/rankings', parameters: { category: '分类,见下表,默认为 `有碼`', time: '时间,见下表,默认为 `日榜`' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'JAVDB_SESSION', + description: 'JavDB登陆后的session值,可在控制台的cookie下查找 `_jdb_session` 的值,即可获取', + optional: true, + }, + ], requirePuppeteer: false, antiCrawler: true, supportBT: false, diff --git a/lib/routes/javdb/search.ts b/lib/routes/javdb/search.ts index fb194ce0a..be869f49d 100644 --- a/lib/routes/javdb/search.ts +++ b/lib/routes/javdb/search.ts @@ -7,7 +7,13 @@ export const route: Route = { example: '/javdb/search/巨乳', parameters: { keyword: '关键字,默认为空', filter: '过滤,见下表,默认为 `可播放`', sort: '排序,见下表,默认为 `按相关度排序`' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'JAVDB_SESSION', + description: 'JavDB登陆后的session值,可在控制台的cookie下查找 `_jdb_session` 的值,即可获取', + optional: true, + }, + ], requirePuppeteer: false, antiCrawler: true, supportBT: false, diff --git a/lib/routes/javdb/series.ts b/lib/routes/javdb/series.ts index 33d8f8676..10172c5c9 100644 --- a/lib/routes/javdb/series.ts +++ b/lib/routes/javdb/series.ts @@ -7,7 +7,13 @@ export const route: Route = { example: '/javdb/series/1NW', parameters: { id: '编号,可在系列页 URL 中找到', filter: '过滤,见下表,默认为 `全部`' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'JAVDB_SESSION', + description: 'JavDB登陆后的session值,可在控制台的cookie下查找 `_jdb_session` 的值,即可获取', + optional: true, + }, + ], requirePuppeteer: false, antiCrawler: true, supportBT: false, diff --git a/lib/routes/javdb/tags.ts b/lib/routes/javdb/tags.ts index e9232f060..94712a761 100644 --- a/lib/routes/javdb/tags.ts +++ b/lib/routes/javdb/tags.ts @@ -7,7 +7,13 @@ export const route: Route = { example: '/javdb/tags/c2=5&c10=1', parameters: { query: '筛选,默认为 `c10=1`', category: '分类,见下表,默认为 `有碼`' }, features: { - requireConfig: false, + requireConfig: [ + { + name: 'JAVDB_SESSION', + description: 'JavDB登陆后的session值,可在控制台的cookie下查找 `_jdb_session` 的值,即可获取', + optional: true, + }, + ], requirePuppeteer: false, antiCrawler: true, supportBT: false, diff --git a/lib/routes/javdb/utils.ts b/lib/routes/javdb/utils.ts index 0e9016f76..545fe9b05 100644 --- a/lib/routes/javdb/utils.ts +++ b/lib/routes/javdb/utils.ts @@ -3,6 +3,8 @@ import got from '@/utils/got'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; import { config } from '@/config'; +import { Cookie, CookieJar } from 'tough-cookie'; + import ConfigNotFoundError from '@/errors/types/config-not-found'; const allowDomain = new Set(['javdb.com', 'javdb36.com', 'javdb007.com', 'javdb521.com']); @@ -15,9 +17,22 @@ const ProcessItems = async (ctx, currentUrl, title) => { const rootUrl = `https://${domain}`; + const cookieJar = new CookieJar(); + + if (config.javdb.session) { + const cookie = Cookie.fromJSON({ + key: '_jdb_session', + value: config.javdb.session, + domain, + path: '/', + }); + cookie && cookieJar.setCookie(cookie, rootUrl); + } + const response = await got({ method: 'get', url: url.href, + cookieJar, }); const $ = load(response.data); @@ -42,6 +57,7 @@ const ProcessItems = async (ctx, currentUrl, title) => { const detailResponse = await got({ method: 'get', url: item.link, + cookieJar, }); const content = load(detailResponse.data); diff --git a/lib/utils/got.test.ts b/lib/utils/got.test.ts index deb7c49e2..42db54b39 100644 --- a/lib/utils/got.test.ts +++ b/lib/utils/got.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it, vi } from 'vitest'; import { http, HttpResponse } from 'msw'; import got from '@/utils/got'; import { config } from '@/config'; +import { Cookie, CookieJar } from 'tough-cookie'; describe('got', () => { it('headers', async () => { @@ -56,4 +57,20 @@ describe('got', () => { expect(response.body instanceof Buffer).toBe(true); expect(response.data instanceof Buffer).toBe(true); }); + + it('cookieJar', async () => { + const cookieJar = new CookieJar(); + const cookie = Cookie.fromJSON({ + key: 'cookie', + value: 'test', + domain: 'rsshub.test', + path: '/', + }); + cookie && cookieJar.setCookie(cookie, 'http://rsshub.test'); + const { data } = await got.get('http://rsshub.test/headers', { + cookieJar, + }); + + expect(data.cookie).toBe('cookie=test; Domain=rsshub.test; Path=/'); + }); }); diff --git a/lib/utils/got.ts b/lib/utils/got.ts index e7c4b6b80..869a93d3f 100644 --- a/lib/utils/got.ts +++ b/lib/utils/got.ts @@ -53,6 +53,17 @@ const getFakeGot = (defaultOptions?: any) => { delete options.parseResponse; } + if (options.cookieJar) { + const cookies = options.cookieJar.getCookiesSync(request); + if (cookies.length) { + if (!options.headers) { + options.headers = {}; + } + options.headers.cookie = cookies.join('; '); + } + delete options.cookieJar; + } + const response = ofetch(request, options); if (options?.responseType === 'arrayBuffer') {