fix: 使用官方rss源,避免因为文章置顶和取消置顶造成的文章重复 (#2383)

This commit is contained in:
Cloud
2019-06-13 11:38:30 +08:00
committed by DIYgod
parent 1c6e71cfb7
commit cbcfc03207
5 changed files with 112 additions and 86 deletions

View File

@@ -524,6 +524,12 @@ type 为 all 时category 参数不支持 cost 和 free
<Route author="xapool" example="/aqicn/beijing" path="/aqicn/:city" :paramsDesc="['城市拼音或地区 ID详见[aqicn.org](http://aqicn.org/city/)']"/>
## 快科技(原驱动之家)
### 最新新闻
<Route author="kt286" example="/kkj/news" path="/kkj/news"/>
## 老司机
### 首页

View File

@@ -1409,4 +1409,7 @@ router.get('/eastday/sh', require('./routes/eastday/sh'));
// Metacritic
router.get('/metacritic/release/:platform/:type/:sort?', require('./routes/metacritic/release'));
// 快科技(原驱动之家)
router.get('/kkj/news', require('./routes/kkj/news'));
module.exports = router;

View File

@@ -1,27 +1,59 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const util = require('./utils');
const parser = require('@/utils/rss-parser');
module.exports = async (ctx) => {
const response = await got({
method: 'get',
url: 'https://www.cnbeta.com/',
headers: {
Referer: 'https://www.cnbeta.com/',
},
});
const feed = await parser.parseURL('https://rss.cnbeta.com/');
const data = response.data;
const ProcessFeed = (data) => {
const $ = cheerio.load(data);
const $ = cheerio.load(data);
const list = $('.items-area [id^="J_all_item_"]').get();
// 还原图片地址
$('img').each((index, elem) => {
const $elem = $(elem);
const src = $elem.attr('data-original');
if (src && src !== '') {
$elem.attr('src', `${src}`);
}
$elem.attr('referrerpolicy', 'no-referrer');
});
const result = await util.ProcessFeed(list, ctx.cache);
// 提取内容
return $('.article-summary p').html() + '<br>' + $('.article-content').html();
};
const items = await Promise.all(
feed.items
.filter((item) => item.author !== 'adam')
.map(async (item) => {
const cache = await ctx.cache.get(item.link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got({
method: 'get',
url: item.link,
});
const description = ProcessFeed(response.data);
const single = {
title: item.title,
description,
pubDate: item.pubDate,
link: item.link,
author: item.author,
};
ctx.cache.set(item.link, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: 'cnBeta',
link: 'https://www.cnbeta.com/',
description: $('meta[name="description"]').attr('content'),
item: result,
description: feed.description,
item: items,
};
};

View File

@@ -1,72 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
// 加载文章页
async function load(link) {
const response = await got.get(link);
const $ = cheerio.load(response.data);
// 解析日期
const date = new Date(
$('.meta>span:first-child')
.text()
.match(/\d{4}.\d{2}.\d{2}. \d{2}:\d{2}/)[0]
.replace(/年|月/g, '-')
.replace(/日/g, '')
);
const timeZone = 8;
const serverOffset = date.getTimezoneOffset() / 60;
const pubDate = new Date(date.getTime() - 60 * 60 * 1000 * (timeZone + serverOffset)).toUTCString();
// 还原图片地址
$('img').each((index, elem) => {
const $elem = $(elem);
const src = $elem.attr('data-original');
if (src && src !== '') {
$elem.attr('src', `${src}`);
}
$elem.attr('referrerpolicy', 'no-referrer');
});
// 提取内容
const description = $('.article-summary p').html() + '<br>' + $('.article-content').html();
// 稿源
const author = $('.source a span').text();
return { description, pubDate, author };
}
const ProcessFeed = async (list, caches) => {
const host = 'https://www.cnbeta.com/';
return await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const $title = $('dl dt a');
// 还原相对链接为绝对链接
const itemUrl = url.resolve(host, $title.attr('href'));
// 列表上提取到的信息
const single = {
title: $title.text(),
link: itemUrl,
// author: $('.nickname').text(),
guid: itemUrl,
};
// 使用tryGet方法从缓存获取内容。
// 当缓存中无法获取到链接内容的时候则使用load方法加载文章内容。
const other = await caches.tryGet(itemUrl, async () => await load(itemUrl));
// 合并解析后的结果集作为该篇文章最终的输出结果
return Promise.resolve(Object.assign({}, single, other));
})
);
};
module.exports = {
ProcessFeed,
};

57
lib/routes/kkj/news.js Normal file
View File

@@ -0,0 +1,57 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const parser = require('@/utils/rss-parser');
module.exports = async (ctx) => {
const feed = await parser.parseURL('http://rss.mydrivers.com/rss.aspx?Tid=1');
const ProcessFeed = (data) => {
const $ = cheerio.load(data);
$('img').each((index, elem) => {
const $elem = $(elem);
$elem.attr('referrerpolicy', 'no-referrer');
});
// 移除广告等内容
$('.weixin ~ div').remove();
$('.weixin').remove();
$('script').remove();
// 提取内容
return $('.news_info').html();
};
const items = await Promise.all(
feed.items.map(async (item) => {
const cache = await ctx.cache.get(item.link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got({
method: 'get',
url: item.link,
});
const description = ProcessFeed(response.data);
const single = {
title: item.title,
description,
pubDate: item.pubDate,
link: item.link,
author: item.author,
};
ctx.cache.set(item.link, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: feed.title,
link: feed.link,
description: feed.description,
item: items,
};
};