Files
RSSHub/lib/routes/qdaily/index.js
Henry Wang d09affa703 feat: add addNoReferrer in common-utils (#2571)
* fix: no-referrer tag

* chore: merge qdaily routes

* feat: add addNoReferrer in common-utils

This adds no-referrer attribute to images.
Parameters:

- add no-referrer attribute to images
- $: cheerio selector
- source: source selector, string
- target: target attribute name, typically for lazyload imgs, string
- srcPrefix: prefix for src, string
- removeAttr: attributes to remove, array: ['attrA','attrB']

* test: add test case

* doc: update
2019-07-08 11:45:02 +08:00

83 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { addNoReferrer } = require('@/utils/common-utils');
module.exports = async (ctx) => {
let type;
switch (ctx.params.type) {
case 'tag':
type = 'tags';
break;
case 'category':
type = 'categories';
break;
case 'column':
type = 'special_columns';
break;
}
const url = `http://www.qdaily.com/${type}/${ctx.params.id}.html`;
const res = await got.get(url);
const $ = cheerio.load(res.data);
const list = $('.article').get();
const proList = [];
const indexList = [];
const out = await Promise.all(
list.map(async (item, i) => {
const $ = cheerio.load(item);
let time = $('.smart-date').attr('data-origindate');
let title = $('.title').text();
if (!title) {
title = $('.smart-dotdotdot').text();
}
if (!title) {
const mmTitle = $('.makemoney-title').text();
const description = $('.makemoney-discription').text();
title = mmTitle + '' + description;
time = new Date();
}
const itemUrl = $('a').attr('href');
const allUrl = `http://www.qdaily.com${itemUrl}`;
const cache = await ctx.cache.get(allUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const single = {
title,
pubDate: new Date(time).toUTCString(),
link: allUrl,
guid: allUrl,
};
proList.push(got.get(allUrl));
indexList.push(i);
return Promise.resolve(single);
})
);
const responses = await got.all(proList);
for (let i = 0; i < responses.length; i++) {
const res = responses[i];
const $ = cheerio.load(res.data);
$('.article-detail-bd .author-share').remove();
$('.article-detail-ft').remove();
addNoReferrer($, '.main .com-article-detail', 'data-src');
if (out[indexList[i]].link.match('/weeklies/')) {
out[indexList[i]].description = $('.packery-container').html();
} else {
out[indexList[i]].description = $('.main .com-article-detail').html();
}
ctx.cache.set(out[indexList[i]].link, JSON.stringify(out[i]));
}
ctx.state.data = {
title: $('title').text(),
link: url,
item: out,
};
};