diff --git a/docs/install/README.md b/docs/install/README.md index cd5395745..9217c77ae 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -402,4 +402,10 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式 - `LASTFM_API_KEY`: Last.fm API Key - 北大未名 BBS 全站十大 + - `PKUBBS_COOKIE`: BBS 注册用户登录后的 Cookie 值,获取方式:1.登录后打开论坛首页 2. 打开控制台 3. 刷新 4. 找到 请求 5. 找到请求头中的 Cookie + +- nhentai torrent 下载: [申请地址](https://nhentai.net/register/) + + - `NHENTAI_USERNAME`: nhentai 用户名或邮箱 + - `NHENTAI_PASSWORD`: nhentai 密码 diff --git a/docs/picture.md b/docs/picture.md index 21a04578c..753c64067 100644 --- a/docs/picture.md +++ b/docs/picture.md @@ -77,11 +77,11 @@ pageClass: routes ### 分类筛选 - + ### 高级搜索 - + ## Tits Guru diff --git a/lib/config.js b/lib/config.js index 3e639ad4a..fd7e96935 100644 --- a/lib/config.js +++ b/lib/config.js @@ -113,6 +113,10 @@ const calculateValue = () => { pkubbs: { cookie: envs.PKUBBS_COOKIE, }, + nhentai: { + username: envs.NHENTAI_USERNAME, + password: envs.NHENTAI_PASSWORD, + }, }; }; calculateValue(); diff --git a/lib/routes/nhentai/other.js b/lib/routes/nhentai/other.js index 4a9cd777a..bd0736055 100644 --- a/lib/routes/nhentai/other.js +++ b/lib/routes/nhentai/other.js @@ -1,10 +1,9 @@ -const { getSimple, getDetails } = require('./util'); +const { getSimple, getDetails, getTorrents } = require('./util'); const supportedKeys = ['parody', 'character', 'tag', 'artist', 'group', 'language', 'category']; module.exports = async (ctx) => { const { keyword, key, mode } = ctx.params; - const isSimple = mode !== 'detail'; if (supportedKeys.indexOf(key) === -1) { ctx.state.data = { @@ -13,12 +12,14 @@ module.exports = async (ctx) => { return; } - const simples = await getSimple(`https://nhentai.net/${key}/${keyword.toLowerCase().replace(' ', '-')}`); + const url = `https://nhentai.net/${key}/${keyword.toLowerCase().replace(' ', '-')}/`; + + const simples = await getSimple(url); ctx.state.data = { title: `nHentai - ${key} - ${keyword}`, - link: 'https://nhentai.net', + link: url, description: 'hentai', - item: isSimple ? simples : await getDetails(ctx.cache, simples), + item: mode === 'detail' ? await getDetails(ctx.cache, simples) : mode === 'torrent' ? await getTorrents(ctx.cache, simples) : simples, }; }; diff --git a/lib/routes/nhentai/search.js b/lib/routes/nhentai/search.js index 40e4915d0..1804cd7bf 100644 --- a/lib/routes/nhentai/search.js +++ b/lib/routes/nhentai/search.js @@ -1,15 +1,16 @@ -const { getSimple, getDetails } = require('./util'); +const { getSimple, getDetails, getTorrents } = require('./util'); module.exports = async (ctx) => { const { keyword, mode } = ctx.params; - const isSimple = mode !== 'detail'; - const simples = await getSimple(`https://nhentai.net/search/?q=${keyword}`); + const url = `https://nhentai.net/search/?q=${keyword}`; + + const simples = await getSimple(url); ctx.state.data = { title: `nHentai - search - ${keyword}`, - link: 'https://nhentai.net', + link: url, description: 'hentai', - item: isSimple ? simples : await getDetails(ctx.cache, simples), + item: mode === 'detail' ? await getDetails(ctx.cache, simples) : mode === 'torrent' ? await getTorrents(ctx.cache, simples) : simples, }; }; diff --git a/lib/routes/nhentai/util.js b/lib/routes/nhentai/util.js index fdec16018..9c4b31dfe 100644 --- a/lib/routes/nhentai/util.js +++ b/lib/routes/nhentai/util.js @@ -1,6 +1,66 @@ const { resolve } = require('url'); const cheerio = require('cheerio'); const got = require('@/utils/got'); +const config = require('@/config').value; +const parseTorrent = require('parse-torrent'); + +const getCookie = async (username, password, cache) => { + const login_url = 'https://nhentai.net/login/'; + const cache_key = 'nhentai-cookie'; + + const cached_cookie = await cache.get(cache_key); + if (cached_cookie) { + const { cookie, time } = JSON.parse(cached_cookie); + const now = new Date().getTime(); + if (now - time < 86400 * 3 * 1000) { + // 不考虑缓存过期的话,有效期最多允许3天 + return cookie; + } + } + + const { data, headers } = await got.get(login_url); + const csrf_token_middleware = data.match(/name="csrfmiddlewaretoken" value="(.*?)"/)[1]; + const csrf_token_cookie = headers['set-cookie'].map((c) => c.split(';')[0]).join('; '); + + const login = await got({ + method: 'post', + url: login_url, + headers: { + referer: login_url, + cookie: csrf_token_cookie, + }, + form: { + csrfmiddlewaretoken: csrf_token_middleware, + username_or_email: username, + password: password, + next: '', + }, + followRedirect: false, + }); + + if (login.statusCode !== 302) { + cache.set( + cache_key, + JSON.stringify({ + cookie: '', + time: new Date().getTime(), + }) + ); + return ''; + } + + const user_token_cookie = login.headers['set-cookie'].map((c) => c.split(';')[0]).join('; '); + + cache.set( + cache_key, + JSON.stringify({ + cookie: user_token_cookie, + time: new Date().getTime(), + }) + ); + + return user_token_cookie; +}; exports.getSimple = async (url) => { const { data } = await got.get(url); @@ -14,6 +74,19 @@ exports.getSimple = async (url) => { const MAX_DETAIL = 5; exports.getDetails = async (cache, simples) => Promise.all(simples.slice(0, MAX_DETAIL).map((simple) => cache.tryGet(simple.link, () => getDetail(simple)))); +const MAX_TORRENT = 5; +exports.getTorrents = async (cache, simples) => { + if (!config.nhentai || !config.nhentai.username || !config.nhentai.password) { + throw 'nhentai RSS with torrents is disabled due to the lack of relevant config'; + } + const cookie = await getCookie(config.nhentai.username, config.nhentai.password, cache); + if (!cookie) { + throw 'Invalid username (or email) or password for nhentai torrent download'; + } + return await getTorrentWithCookie(cache, simples, cookie); +}; +const getTorrentWithCookie = async (cache, simples, cookie) => Promise.all(simples.slice(0, MAX_TORRENT).map((simple) => cache.tryGet(simple.link + 'download', () => getTorrent(simple, cookie)))); + const parseSimpleDetail = ($ele) => { const link = resolve('https://nhentai.net', $ele.attr('href')); const thumb = $ele.children('img'); @@ -27,6 +100,18 @@ const parseSimpleDetail = ($ele) => { }; }; +const getTorrent = async (simple, cookie) => { + const { link } = simple; + const response = await got.get(link + 'download', { followRedirect: false, responseType: 'buffer', headers: { Cookie: cookie } }); + const info = parseTorrent(response.data); + const uri = info ? parseTorrent.toMagnetURI(info) : ''; + return { + ...simple, + enclosure_url: uri, + enclosure_type: 'application/x-bittorrent', + }; +}; + const getDetail = async (simple) => { const { link } = simple; const { data } = await got.get(link); diff --git a/package.json b/package.json index 41ef05c70..05d728236 100644 --- a/package.json +++ b/package.json @@ -90,6 +90,7 @@ "mailparser": "2.7.7", "markdown-it": "10.0.0", "module-alias": "2.2.2", + "parse-torrent": "7.0.1", "pidusage": "2.0.17", "plist": "3.0.1", "puppeteer": "2.1.1",