抓取3dm新闻中心 (#292)

* Add 3dm news center.

* Modify router name, fix timezone issue.
This commit is contained in:
zhboner
2018-06-14 13:02:29 +10:00
committed by DIYgod
parent a3d11e01dd
commit d576f35971
2 changed files with 73 additions and 0 deletions

View File

@@ -286,6 +286,7 @@ router.get('/uukanshu/chapter/:uid', require('./routes/uukanshu/chapter'));
// 3dm // 3dm
router.get('/3dm/:name/:type', require('./routes/3dm/news')); 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')); router.get('/ximalaya/album/:classify/:id', require('./routes/ximalaya/album'));

72
routes/3dm/news_center.js Normal file
View File

@@ -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,
};
};