From 8080038e01c10eca3908d6dbbb7e96a842b6da43 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Wed, 13 Feb 2019 04:16:04 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=87=82=E7=90=83=E5=B8=9D?= =?UTF-8?q?=E6=97=A9=E6=8A=A5=E9=83=A8=E5=88=86=E9=93=BE=E6=8E=A5=20404=20?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20(#1498)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复懂球帝早报部分链接 404 的问题 * refactor: revert back to axios --- lib/routes/dongqiudi/daily.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/routes/dongqiudi/daily.js b/lib/routes/dongqiudi/daily.js index 6cd9c6bd0..a89c47946 100644 --- a/lib/routes/dongqiudi/daily.js +++ b/lib/routes/dongqiudi/daily.js @@ -1,6 +1,7 @@ const axios = require('../../utils/axios'); const cheerio = require('cheerio'); const utils = require('./utils'); +const date = require('../../utils/date'); module.exports = async (ctx) => { const response = await axios.get('https://www.dongqiudi.com/special/48'); @@ -10,7 +11,7 @@ module.exports = async (ctx) => { const host = 'https://www.dongqiudi.com'; const list = $('.detail.special ul li h3') - .slice(0, 10) + .slice(0, 5) .get(); const proList = []; @@ -21,18 +22,19 @@ module.exports = async (ctx) => { const title = $('a').text(); const itemUrl = host + $('a').attr('href'); + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const single = { title, link: itemUrl, }; - try { - const es = axios.get(itemUrl); - proList.push(es); - return Promise.resolve(single); - } catch (err) { - console.log(`${title}: ${itemUrl} -- ${err.response.status}: ${err.response.statusText}`); - } + const es = axios.get(itemUrl); + proList.push(es); + return Promise.resolve(single); }) ); @@ -43,11 +45,19 @@ module.exports = async (ctx) => { out[i].description = utils.ProcessHref(full.find('div:nth-of-type(1)')).html(); out[i].author = full.find('span.name').text(); - out[i].pubDate = new Date(full.find('span.time').text()).toUTCString(); + out[i].pubDate = date( + full + .find('span.time') + .text() + .trim(), + 8 + ); + + ctx.cache.set(out[i].itemUrl, JSON.stringify(out[i]), 24 * 60 * 60); } ctx.state.data = { title: '懂球帝早报', link: 'http://www.dongqiudi.com/special/48', - item: out, + item: out.filter((e) => e !== undefined), }; };