Merge branch 'add-nhentai-torrent' of git://github.com/hoilc/RSSHub into hoilc-add-nhentai-torrent

This commit is contained in:
DIYgod
2020-02-09 20:20:23 +08:00
7 changed files with 110 additions and 12 deletions
+6
View File
@@ -402,4 +402,10 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式
- `LASTFM_API_KEY`: Last.fm API Key
- 北大未名 BBS 全站十大
- `PKUBBS_COOKIE`: BBS 注册用户登录后的 Cookie 值,获取方式:1.登录后打开论坛首页 2. 打开控制台 3. 刷新 4. 找到 <https://bbs.pku.edu.cn/v2/home.php> 请求 5. 找到请求头中的 Cookie
- nhentai torrent 下载: [申请地址](https://nhentai.net/register/)
- `NHENTAI_USERNAME`: nhentai 用户名或邮箱
- `NHENTAI_PASSWORD`: nhentai 密码
+2 -2
View File
@@ -77,11 +77,11 @@ pageClass: routes
### 分类筛选
<Route author="MegrezZhu" example="/nhentai/language/chinese" path="/nhentai/:key/:keyword/:mode?" :paramsDesc="['筛选条件,可选: parody, character, tag, artist, group, language, category','筛选值', '模式,`simple`为仅封面,`detail`会包括本子每一页,但对服务器负载大。默认为`simple`']" anticrawler="1" />
<Route author="MegrezZhu hoilc" example="/nhentai/language/chinese" path="/nhentai/:key/:keyword/:mode?" :paramsDesc="['筛选条件,可选: parody, character, tag, artist, group, language, category','筛选值', '模式,`simple`为仅封面,`detail`会包括本子每一页,但对服务器负载大,`torrent`会包括磁力链接,需要登录,参见[部分 RSS 模块配置](/install/#bu-fen-rss-mo-kuai-pei-zhi)。默认为`simple`']" anticrawler="1" supportBT="1" />
### 高级搜索
<Route author="MegrezZhu" example="/nhentai/search/language%3Ajapanese+-scat+-yaoi+-guro+-%22mosaic+censorship%22" path="/nhentai/search/:keyword/:mode?" :paramsDesc="['用于搜索的关键词。可在原网站搜索后复制 q= 后面的内容,也可直接输入。用法详见[官网](https://nhentai.net/info/)', '模式,`simple`为仅封面,`detail`会包括本子每一页,但对服务器负载大。默认为`simple`']" anticrawler="1"/>
<Route author="MegrezZhu hoilc" example="/nhentai/search/language%3Ajapanese+-scat+-yaoi+-guro+-%22mosaic+censorship%22" path="/nhentai/search/:keyword/:mode?" :paramsDesc="['用于搜索的关键词。可在原网站搜索后复制 q= 后面的内容,也可直接输入。用法详见[官网](https://nhentai.net/info/)', '模式,`simple`为仅封面,`detail`会包括本子每一页,但对服务器负载大,`torrent`会包括磁力链接,需要登录,参见[部分 RSS 模块配置](/install/#bu-fen-rss-mo-kuai-pei-zhi)。默认为`simple`']" anticrawler="1" supportBT="1" />
## Tits Guru
+4
View File
@@ -113,6 +113,10 @@ const calculateValue = () => {
pkubbs: {
cookie: envs.PKUBBS_COOKIE,
},
nhentai: {
username: envs.NHENTAI_USERNAME,
password: envs.NHENTAI_PASSWORD,
},
};
};
calculateValue();
+6 -5
View File
@@ -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,
};
};
+6 -5
View File
@@ -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,
};
};
+85
View File
@@ -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 <a href="https://docs.rsshub.app/install/#bu-fen-rss-mo-kuai-pei-zhi">relevant config</a>';
}
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);
+1
View File
@@ -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",