From d576f359710f28b997a1b8c5224ea2cfe83c11db Mon Sep 17 00:00:00 2001 From: zhboner Date: Thu, 14 Jun 2018 13:02:29 +1000 Subject: [PATCH] =?UTF-8?q?=E6=8A=93=E5=8F=963dm=E6=96=B0=E9=97=BB?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=20(#292)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add 3dm news center. * Modify router name, fix timezone issue. --- router.js | 1 + routes/3dm/news_center.js | 72 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 routes/3dm/news_center.js diff --git a/router.js b/router.js index 39e7fe884..378e59642 100644 --- a/router.js +++ b/router.js @@ -286,6 +286,7 @@ router.get('/uukanshu/chapter/:uid', require('./routes/uukanshu/chapter')); // 3dm router.get('/3dm/:name/:type', require('./routes/3dm/news')); +router.get('/3dm/news', require('./routes/3dm/news_center')); // 喜马拉雅 router.get('/ximalaya/album/:classify/:id', require('./routes/ximalaya/album')); diff --git a/routes/3dm/news_center.js b/routes/3dm/news_center.js new file mode 100644 index 000000000..e04b782eb --- /dev/null +++ b/routes/3dm/news_center.js @@ -0,0 +1,72 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const config = require('../../config'); + +const sourceTimezoneOffset = -8; +module.exports = async (ctx) => { + const url = 'http://www.3dmgame.com/news/'; + const res = await axios({ + method: 'get', + url: url, + headers: { + 'User-Agent': config.ua, + }, + }); + const data = res.data; + const $ = cheerio.load(data); + const list = $('.QZlisttxt ul li p'); + const out = []; + + for (let i = 0; i < (list.length <= 20 ? list.length : 20); i++) { + let $ = cheerio.load(data); + const item = list[i]; + const itemUrl = $(item) + .find('a:nth-child(2)') + .attr('href'); + const cache = await ctx.cache.get(itemUrl); + if (cache) { + out.push(cache); + continue; + } + const title = $(item) + .find('a:nth-child(2)') + .text(); + let itemRes; + try { + itemRes = await axios({ + method: 'get', + url: itemUrl, + headers: { + 'User-Agent': config.ua, + }, + }); + } catch (e) { + continue; + } + const itemPage = itemRes.data; + $ = cheerio.load(itemPage); + const content = $('.con div:nth-child(2)').html(); + const pageInfo = $('.arctitle>span').text(); + const regex = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}/; + const regRes = regex.exec(pageInfo); + const time = regRes === null ? new Date() : new Date(regRes[0]); + time.setTime(time.getTime() + (sourceTimezoneOffset - time.getTimezoneOffset() / 60) * 60 * 60 * 1000); + const single = { + title: title, + description: content, + pubDate: time.toUTCString(), + link: itemUrl, + guid: itemUrl, + }; + out.push(single); + + ctx.cache.set(itemUrl, single, 24 * 60 * 60); + } + ctx.state.data = { + title: $('title') + .text() + .split('_')[0], + link: url, + item: out, + }; +};