Files
RSSHub/lib/v2/pianyuan/search.js
T
jerry1119andRongrong 0c3ca1bb11 fix(route): fix pianyuan, add pianyuan/search (#9721)
* fix route pianyuan, add route pianyuan/search

* update doc

* changes for reviews

* Update lib/routes/pianyuan/utils.js

Co-authored-by: Rongrong <Rongronggg9@outlook.com>

* changes for reviews

* refactor: migrate to v2

Co-authored-by: Rongrong <Rongronggg9@outlook.com>
2022-05-09 01:00:29 +08:00

46 lines
1.4 KiB
JavaScript

const cheerio = require('cheerio');
const utils = require('./utils');
module.exports = async (ctx) => {
const link_base = 'https://pianyuan.org/';
const description = '搜索';
// 适配jackett 搜索api, eg: https://rsshub.app/pianyuan/indexers/pianyuan/results/search/api?t=test&q=halo
const searchKey = ctx.originalUrl.split('&q=')[1];
const link = link_base + `search?q=${searchKey}`;
const response = await utils.request(link, ctx.cache);
const $ = cheerio.load(response.data);
// 只获取第一页的搜索结果
const searchLinks = $('.nomt > a')
.get()
.map((a) => $(a).attr('href'));
if (searchLinks.length === 0) {
throw 'pianyuan 搜索失败';
}
const detailLinks = [];
await Promise.all(
searchLinks.map(async (e) => {
const link = new URL(e, link_base);
const single = await ctx.cache.tryGet(link, async () => {
const res = await utils.request(link, ctx.cache);
const content = cheerio.load(res.data);
content('.ico.ico_bt')
.get()
.map((a) => detailLinks.push($(a).attr('href')));
});
return single;
})
);
const items = await utils.ProcessFeed(detailLinks, ctx.cache);
ctx.state.data = {
title: '片源网',
description,
link: link_base,
item: items,
};
};