From f0a0c363a7a96fb44ec4b6d662f8e8822b4e8316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=89=E5=87=89?= Date: Thu, 18 Oct 2018 17:17:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=EF=BC=9A=E8=85=BE=E8=AE=AF?= =?UTF-8?q?=E6=B8=B8=E6=88=8F=E5=BC=80=E5=8F=91=E8=80=85=E7=A4=BE=E5=8C=BA?= =?UTF-8?q?=20(#918)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现 #704 - [源页面](http://gameinstitute.qq.com/community/)支持按时间和热度排序,目前只做了按时间输出内容,后续如有需要再加参数控制 - 路由命名和文档写的稍有些拗口,麻烦看下怎么修改比较好(光速逃 --- docs/README.md | 14 ++++++++ router.js | 3 ++ routes/gameinstitute/community.js | 60 +++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 routes/gameinstitute/community.js diff --git a/docs/README.md b/docs/README.md index c40198dd0..d0e4e5012 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1920,3 +1920,17 @@ IATA 国际航空运输协会机场代码, 参见[维基百科 国际航空运 ### 腾讯大家 + +### 腾讯游戏开发者社区 + +::: warning 注意 + +有部分输出全文带有未进行样式处理的代码内容,显示效果不佳,建议跳转原文阅读 + +::: + + + +| 热门 | 策划 | 程序 | 技术前沿 | 音频 | 项目管理 | 游戏运营 | 游戏测试 | +| ---- | ---- | ------- | -------- | ----- | -------- | -------- | -------- | +| hot | plan | program | tech | audio | project | yunying | test | diff --git a/router.js b/router.js index 8130288e5..279bbbfb5 100644 --- a/router.js +++ b/router.js @@ -665,4 +665,7 @@ router.get('/dajia/index', require('./routes/dajia/')); // 甩甩尾巴 router.get('/dgtle/trade/:typeId?', require('./routes/dgtle/trade')); +// 腾讯游戏开发者社区 +router.get('/gameinstitute/community/:tag?', require('./routes/gameinstitute/community')); + module.exports = router; diff --git a/routes/gameinstitute/community.js b/routes/gameinstitute/community.js new file mode 100644 index 000000000..69452f0cd --- /dev/null +++ b/routes/gameinstitute/community.js @@ -0,0 +1,60 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const { tag = 'hot' } = ctx.params; + + const listRes = await axios({ + method: 'get', + url: `http://gameinstitute.qq.com/community/${tag}/new?p=1&ps=20&ob=new&cid=0&tag=${tag}`, + headers: { + 'X-Requested-With': 'XMLHttpRequest', + }, + }); + + const storyList = listRes.data.archives; + const resultItem = await Promise.all( + storyList.map(async (story) => { + const url = `http://gameinstitute.qq.com/article/detail/${story.id}`; + const item = { + title: story.title, + description: '', + link: url, + author: story.user_name, + pubDate: new Date(story.publish_time).toUTCString(), + }; + const key = `gameinstitute-community: ${url}`; + const value = await ctx.cache.get(key); + + if (value) { + item.description = value; + } else { + const storyDetail = await axios({ + method: 'get', + url: url, + }); + const $ = cheerio.load(storyDetail.data); + $('.art-cont img').each(function(_, item) { + $(item).attr('referrerpolicy', 'no-referrer'); + }); + + // 处理图片左边被截断问题 + $('.art-cont .image-package').each(function(_, item) { + $(item).css('margin', '0'); + }); + + item.description = $('.art-cont').html(); + ctx.cache.set(key, item.description, 24 * 60 * 60); + } + + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: '游戏开发者社区-腾讯游戏学院', + description: '腾讯游戏学院是专门为游戏行业从业者服务的游戏开发者社区,这里有关于游戏开发相关的文章教程、游戏策划、美术作品、还有新鲜的前沿科技资讯。', + link: `http://gameinstitute.qq.com/community/${tag}`, + item: resultItem, + }; +};