Files
RSSHub/lib/routes/oschina/u.js
T
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

58 lines
1.9 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const id = ctx.params.id;
const res = await got({
method: 'get',
url: `https://my.oschina.net/u/${id}`,
headers: {
Referer: `https://my.oschina.net/u/${id}`,
},
});
const $ = cheerio.load(res.data);
$('div[data-tooltip]').remove();
const author = $('[data-user-name]').attr('data-user-name');
const list = $('#newestBlogList').find('.blog-item');
const count = [];
for (let i = 0; i < Math.min(list.length, 10); i++) {
count.push(i);
}
const resultItem = await Promise.all(
count.map(async (i) => {
const each = $(list[i]);
const originalUrl = each.find('a.header', '.content').attr('href');
const item = {
title: each.find('a.header', '.content').text(),
description: each.find('p', '.description').text(),
link: encodeURI(originalUrl),
};
const key = 'oschina' + item.link;
const value = await ctx.cache.get(key);
if (value) {
item.description = value;
} else {
const detail = await got({
method: 'get',
url: item.link,
headers: {
Referer: item.link,
},
});
const content = cheerio.load(detail.data);
content('#articleContent .ad-wrap').remove();
item.description = content('#articleContent').html();
ctx.cache.set(key, item.description);
}
return Promise.resolve(item);
})
);
ctx.state.data = {
title: author + '的博客',
link: `https://my.oschina.net/u/${id}`,
item: resultItem,
};
};