diff --git a/docs/other.md b/docs/other.md index b0adb0efe1..667fadd6cb 100644 --- a/docs/other.md +++ b/docs/other.md @@ -524,6 +524,12 @@ type 为 all 时,category 参数不支持 cost 和 free +## 快科技(原驱动之家) + +### 最新新闻 + + + ## 老司机 ### 首页 diff --git a/lib/router.js b/lib/router.js index 7cbde07706..90ca867d76 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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; diff --git a/lib/routes/cnbeta/home.js b/lib/routes/cnbeta/home.js index 47f695ae42..6a77e5fd1e 100644 --- a/lib/routes/cnbeta/home.js +++ b/lib/routes/cnbeta/home.js @@ -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() + '
' + $('.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, }; }; diff --git a/lib/routes/cnbeta/utils.js b/lib/routes/cnbeta/utils.js deleted file mode 100644 index 3b30efd42f..0000000000 --- a/lib/routes/cnbeta/utils.js +++ /dev/null @@ -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() + '
' + $('.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, -}; diff --git a/lib/routes/kkj/news.js b/lib/routes/kkj/news.js new file mode 100644 index 0000000000..42da9abf95 --- /dev/null +++ b/lib/routes/kkj/news.js @@ -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, + }; +};