middleware: filter
This commit is contained in:
parent
3398fdec32
commit
4776fa6aba
|
|
@ -13,6 +13,18 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇
|
|||
演示域名为 [rss.now.sh](https://rss.now.sh),缓存时间 5 分钟,可以随意使用
|
||||
:::
|
||||
|
||||
## 内容过滤
|
||||
|
||||
可以使用以下 URL query 过滤出想要的内容,支持正则
|
||||
|
||||
- filter: 过滤标题和描述
|
||||
|
||||
- filter_title: 过滤标题
|
||||
|
||||
- filter_description: 过滤描述
|
||||
|
||||
举例: [https://rss.now.sh/bilibili/user/coin/2267573?filter=微小微|赤九玖|暴走大事件](https://rss.now.sh/bilibili/user/coin/2267573?filter=微小微|赤九玖|暴走大事件)
|
||||
|
||||
## bilibili
|
||||
|
||||
### 番剧
|
||||
|
|
|
|||
6
index.js
6
index.js
|
|
@ -8,6 +8,7 @@ const header = require('./middleware/header.js');
|
|||
const utf8 = require('./middleware/utf8');
|
||||
const memoryCache = require('./middleware/cache.js');
|
||||
const redisCache = require('koa-redis-cache');
|
||||
const filter = require('./middleware/filter.js');
|
||||
|
||||
const router = require('./router');
|
||||
|
||||
|
|
@ -28,11 +29,14 @@ app.use(header);
|
|||
// fix incorrect `utf-8` characters
|
||||
app.use(utf8);
|
||||
|
||||
app.use(filter);
|
||||
|
||||
// cache
|
||||
if (config.cacheType === 'memory') {
|
||||
app.use(
|
||||
memoryCache({
|
||||
expire: config.cacheExpire
|
||||
expire: config.cacheExpire,
|
||||
ignoreQuery: true,
|
||||
})
|
||||
);
|
||||
} else if (config.cacheType === 'redis') {
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ module.exports = function (options = {}) {
|
|||
exclude = [],
|
||||
passParam = '',
|
||||
maxLength = Infinity,
|
||||
ignoreQuery = false,
|
||||
} = options;
|
||||
|
||||
const memoryCache = lru({
|
||||
|
|
@ -23,7 +24,7 @@ module.exports = function (options = {}) {
|
|||
return async function cache (ctx, next) {
|
||||
const { url, path } = ctx.request;
|
||||
const resolvedPrefix = typeof prefix === 'function' ? prefix.call(ctx, ctx) : prefix;
|
||||
const key = resolvedPrefix + md5(url);
|
||||
const key = resolvedPrefix + md5(ignoreQuery ? path : url);
|
||||
const tkey = key + ':type';
|
||||
let match = false;
|
||||
let routeExpire = false;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
const cheerio = require('cheerio');
|
||||
|
||||
module.exports = async (ctx, next) => {
|
||||
await next();
|
||||
|
||||
if (ctx.body && ctx.query && (ctx.query.filter || ctx.query.filter_title || ctx.query.filter_description)) {
|
||||
const $ = cheerio.load(ctx.body, {
|
||||
xmlMode: true
|
||||
});
|
||||
$('item').filter((index, item) => {
|
||||
const title = $(item).find('title').text();
|
||||
const description = $(item).find('description').text();
|
||||
return ctx.query.filter && !title.match(ctx.query.filter) && !description.match(ctx.query.filter)
|
||||
|| ctx.query.filter_title && !title.match(ctx.query.filter_title)
|
||||
|| ctx.query.filter_description && !description.match(ctx.query.filter_description);
|
||||
}).remove();
|
||||
|
||||
ctx.body = $.xml();
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue