From 080282fc01c245483d6b6c755dac4eba432f615a Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Thu, 9 Sep 2021 16:51:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E5=85=A8=E7=8E=B0=E5=9C=A8?= =?UTF-8?q?=E9=A6=96=E9=A1=B5=20(#8142)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/allnow/column.js | 47 ++------------------------ lib/routes/allnow/index.js | 46 ++----------------------- lib/routes/allnow/tag.js | 46 ++----------------------- lib/routes/allnow/user.js | 46 ++----------------------- lib/routes/allnow/utils.js | 67 +++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 174 deletions(-) create mode 100644 lib/routes/allnow/utils.js diff --git a/lib/routes/allnow/column.js b/lib/routes/allnow/column.js index d7f779cf4e..afb29d1278 100644 --- a/lib/routes/allnow/column.js +++ b/lib/routes/allnow/column.js @@ -1,48 +1,7 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); +const utils = require('./utils'); module.exports = async (ctx) => { - const rootUrl = 'https://www.allnow.com'; - const currentUrl = `${rootUrl}/column/${ctx.params.id}`; - const response = await got({ - method: 'get', - url: currentUrl, - }); + const id = ctx.params.id; - const $ = cheerio.load(response.data); - const list = $('.post-list .post') - .slice(0, 10) - .map((_, item) => { - item = $(item); - return { - link: `${rootUrl}${item.attr('href')}`, - }; - }) - .get(); - - const items = await Promise.all( - list.map((item) => - ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - const content = cheerio.load(detailResponse.data); - - item.title = content('p.title').text(); - item.author = content('a.single-name').eq(0).text(); - item.description = content('#article-content').html(); - item.pubDate = new Date(detailResponse.data.match(/time:"(.*?)"\}/)[1] + ' GMT+8').toUTCString(); - - return item; - }) - ) - ); - - ctx.state.data = { - title: `${$('p.name').text()} - 全现在`, - link: rootUrl, - item: items, - description: $('p.desc').text(), - }; + ctx.state.data = await utils.processItems(ctx, `${utils.rootUrl}/column/${id}`); }; diff --git a/lib/routes/allnow/index.js b/lib/routes/allnow/index.js index d228fdb8c6..16d1f8f0e9 100644 --- a/lib/routes/allnow/index.js +++ b/lib/routes/allnow/index.js @@ -1,47 +1,5 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); +const utils = require('./utils'); module.exports = async (ctx) => { - const rootUrl = 'https://www.allnow.com'; - const response = await got({ - method: 'get', - url: rootUrl, - }); - - const $ = cheerio.load(response.data); - const list = $('.post-list .post') - .slice(0, 10) - .map((_, item) => { - item = $(item); - return { - link: `${rootUrl}${item.attr('href')}`, - }; - }) - .get(); - - const items = await Promise.all( - list.map((item) => - ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - const content = cheerio.load(detailResponse.data); - - item.title = content('p.title').text(); - item.author = content('a.single-name').eq(0).text(); - item.description = content('#article-content').html(); - item.pubDate = new Date(detailResponse.data.match(/time:"(.*?)"\}/)[1] + ' GMT+8').toUTCString(); - - return item; - }) - ) - ); - - ctx.state.data = { - title: '首页 - 全现在', - link: rootUrl, - item: items, - description: $('p.desc').text(), - }; + ctx.state.data = await utils.processItems(ctx, utils.rootUrl); }; diff --git a/lib/routes/allnow/tag.js b/lib/routes/allnow/tag.js index dd22bd37a3..f27a72c28b 100644 --- a/lib/routes/allnow/tag.js +++ b/lib/routes/allnow/tag.js @@ -1,47 +1,7 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); +const utils = require('./utils'); module.exports = async (ctx) => { - const rootUrl = 'https://www.allnow.com'; - const currentUrl = `${rootUrl}/tag/${ctx.params.id}`; - const response = await got({ - method: 'get', - url: currentUrl, - }); + const id = ctx.params.id; - const $ = cheerio.load(response.data); - const list = $('.post-list .post') - .slice(0, 10) - .map((_, item) => { - item = $(item); - return { - link: `${rootUrl}${item.attr('href')}`, - }; - }) - .get(); - - const items = await Promise.all( - list.map((item) => - ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - const content = cheerio.load(detailResponse.data); - - item.title = content('p.title').text(); - item.author = content('a.single-name').eq(0).text(); - item.description = content('#article-content').html(); - item.pubDate = new Date(detailResponse.data.match(/time:"(.*?)"\}/)[1] + ' GMT+8').toUTCString(); - - return item; - }) - ) - ); - - ctx.state.data = { - title: `${$('p.title').eq(0).text()} - 全现在`, - link: currentUrl, - item: items, - }; + ctx.state.data = await utils.processItems(ctx, `${utils.rootUrl}/tag/${id}`); }; diff --git a/lib/routes/allnow/user.js b/lib/routes/allnow/user.js index 3ab0e14b33..8b4b80eb8f 100644 --- a/lib/routes/allnow/user.js +++ b/lib/routes/allnow/user.js @@ -1,47 +1,7 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); +const utils = require('./utils'); module.exports = async (ctx) => { - const rootUrl = 'https://www.allnow.com'; - const currentUrl = `${rootUrl}/user/${ctx.params.id}`; - const response = await got({ - method: 'get', - url: currentUrl, - }); + const id = ctx.params.id; - const $ = cheerio.load(response.data); - const list = $('.post-list .post') - .slice(0, 10) - .map((_, item) => { - item = $(item); - return { - link: `${rootUrl}${item.attr('href')}`, - }; - }) - .get(); - - const items = await Promise.all( - list.map((item) => - ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - const content = cheerio.load(detailResponse.data); - - item.title = content('p.title').text(); - item.author = content('a.single-name').eq(0).text(); - item.description = content('#article-content').html(); - item.pubDate = new Date(detailResponse.data.match(/time:"(.*?)"\}/)[1] + ' GMT+8').toUTCString(); - - return item; - }) - ) - ); - - ctx.state.data = { - title: `${$('p.title').eq(0).text()} - 全现在`, - link: currentUrl, - item: items, - }; + ctx.state.data = await utils.processItems(ctx, `${utils.rootUrl}/user/${id}`); }; diff --git a/lib/routes/allnow/utils.js b/lib/routes/allnow/utils.js new file mode 100644 index 0000000000..5311c4a104 --- /dev/null +++ b/lib/routes/allnow/utils.js @@ -0,0 +1,67 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +const rootUrl = 'https://www.allnow.com'; + +module.exports = { + rootUrl, + + processItems: async (ctx, currentUrl) => { + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('.post-list .post') + .slice(0, ctx.params.limit ? parseInt(ctx.params.limit) : 15) + .map((_, item) => { + item = $(item); + + return { + link: `${rootUrl}${item.attr('href')}`, + }; + }) + .get(); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + item.author = detailResponse.data.match(/authorName:"(.*?)",avatar/)[1]; + item.title = content('title') + .text() + .replace(/-全现在官方网站/, ''); + item.pubDate = timezone(parseDate(detailResponse.data.match(/time:"(.*)",type/)[1]), +8); + item.category = detailResponse.data + .match(/tags:\[(.*)\],columns/)[1] + .split('},') + .map((category) => category.match(/title:"(.*)"/)[1]); + item.description = content('#article-content').html() ?? content('.summary').html() ?? ''; + + content('video').each(function () { + item.description += ``; + }); + + return item; + }) + ) + ); + + return { + title: $('title').text(), + link: currentUrl, + item: items, + description: $('.desc').eq(0).text(), + }; + }, +};