From 979bac08beb4af910bf6f028764cb99023ec39f3 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 14 Mar 2021 14:24:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E6=B8=B8=E6=88=8F=E8=91=A1?= =?UTF-8?q?=E8=90=84=E8=8E=B7=E5=8F=96=E6=96=87=E7=AB=A0=E5=85=A8=E6=96=87?= =?UTF-8?q?=20(#7114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 13 ++++---- lib/routes/gamegrape/index.js | 57 +++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/docs/new-media.md b/docs/new-media.md index c313bd8af..6adcd411b 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2231,16 +2231,15 @@ column 为 third 时可选的 category: ## 游戏葡萄 -无文章正文,仅有目录索引。 +### 文章 -### 全部文章 + - +| 全部 | 深度 | 资讯 | DemoWall | 酷玩 | 海外 | 专栏 | 葡萄观察 | +| ---- | ---- | ---- | -------- | ---- | ---- | ---- | -------- | +| | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -### 分类 - -例子对应[深度分类](http://youxiputao.com/article/index/id/13) - + ## 鱼塘热榜 diff --git a/lib/routes/gamegrape/index.js b/lib/routes/gamegrape/index.js index 418540b75..cdd4827ab 100644 --- a/lib/routes/gamegrape/index.js +++ b/lib/routes/gamegrape/index.js @@ -1,6 +1,6 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const BASE_URL = 'http://youxiputao.com'; + function getNum(str) { const result = str.match(/(\d{1,2}).*/); if (result) { @@ -21,29 +21,54 @@ function resolveRelativeTime(relativeTime) { } return (((day || 0) * 24 + (hour || 0)) * 60 + (min || 0)) * 60 * 1000; } + module.exports = async (ctx) => { - const { id } = ctx.params; - const feed_url = `${BASE_URL}/article/${id ? `index/id/${id}` : ''}`; - const resp = await got({ url: feed_url }); - const { data } = resp; - const $ = cheerio.load(data); - const feed_title = $('title').text(); + const id = ctx.params.id || ''; + + const rootUrl = 'http://youxiputao.com'; + const currentUrl = `${rootUrl}/article${id ? `/index/id/${id}` : ''}`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + const list = $('.news-info-box') + .slice(0, 15) .map((_, item) => { item = $(item); - const txt_author_pubDate = $(item.find('div > p > span')[1]).text(); + const a = item.find('a[title]'); + return { - title: item.parent().find('h4').text(), - link: BASE_URL + item.find('a.cover').attr('href'), - author: `${txt_author_pubDate}`.replace(/ · \S*$/, ''), - description: $(item.find('div > p')[0]).text(), - pubDate: Date.now() - resolveRelativeTime(txt_author_pubDate), + title: a.text(), + link: `${rootUrl}${a.attr('href')}`, + pubDate: Date.now() - resolveRelativeTime(item.find('.pull-right').text()), }; }) .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + item.description = content('.info').html(); + item.author = content('.users-info h4').text(); + + return item; + }) + ) + ); + ctx.state.data = { - title: feed_title, - link: feed_url, - item: list, + title: $('title').text(), + link: currentUrl, + item: items, }; };