From 943e72e99c520e19230b61f2b9062ab89820f229 Mon Sep 17 00:00:00 2001 From: KotoriK <52659125+KotoriK@users.noreply.github.com> Date: Wed, 30 Dec 2020 01:35:15 +0800 Subject: [PATCH 1/6] feat:game grape --- docs/new-media.md | 10 ++++++ lib/router.js | 3 ++ lib/routes/gamegrape/index.js | 57 +++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 lib/routes/gamegrape/index.js diff --git a/docs/new-media.md b/docs/new-media.md index dea896d573..8ec0839b80 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2109,3 +2109,13 @@ QueryString: ### 全文 + +## 游戏葡萄 +无文章正文,仅有目录索引。 +### 全部文章 + + + +### 分类 +例子对应[深度分类](http://youxiputao.com/article/index/id/13) + diff --git a/lib/router.js b/lib/router.js index 5bb0f656c3..40e64ff11b 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3780,4 +3780,7 @@ router.get('/nace/blog/:sort?', require('./routes/nace/blog')); // Caixin Latest router.get('/caixin/latest', require('./routes/caixin/latest')); +//游戏葡萄 +router.get('/gamegrape/:id?', require('./routes/gamegrape/index')); + module.exports = router; diff --git a/lib/routes/gamegrape/index.js b/lib/routes/gamegrape/index.js new file mode 100644 index 0000000000..15408bc136 --- /dev/null +++ b/lib/routes/gamegrape/index.js @@ -0,0 +1,57 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const BASE_URL = 'http://youxiputao.com' + +function resolveRelativeTime(relativeTime) { + const reg = /\S* · (\d{1,2}天)?(\d{1,2}小时)?(\d{1,2}分钟)?/ + const result = reg.exec(str) + let day, hour, min + + if (result[0]) { + day = result[0] + + } + if (result[1]) { + hour = result[1] + + } + if (result[2]) { + min = result[2] + } + + return ((((day ?? 0) * 24) + (hour ?? 0)) * 60 + (min ?? 0)) * 60 * 1000 +} +module.exports = async (ctx) => { + const { id } = ctx.params; + const feed_url = `${BASE_URL}/article/${id ? `/index/id/${id}` : ''}` + const resp = await got({ + method: 'get', + url: feed_url, + }); + const { data } = resp; + const $ = cheerio.load(data); + const list = $('.news-info-box') + .map((_, item) => { + item = $(item); + const txt_author_pubDate = $(item.find('div > p > span')[1]).text() + const author = `${txt_author_pubDate}`.replace(/ · \S*$/, ''); + const link = BASE_URL + item.find('a.cover').attr('href'); + const title = item.parent().find('h4').text(); + const description = $(item.find('div > p')[0]).text() + const pubDate = Date.now() - resolveRelativeTime(txt_author_pubDate) + return { + title, + link, + author, + description, pubDate + }; + }) + .get(); + const feed_title = $('title').text() + + ctx.state.data = { + title: feed_title, + link: feed_url, + item: list, + }; +}; From 6e6ebc076faba1eeb4129a815aef9a6b82545ace Mon Sep 17 00:00:00 2001 From: KotoriK <52659125+KotoriK@users.noreply.github.com> Date: Wed, 30 Dec 2020 01:44:42 +0800 Subject: [PATCH 2/6] remove ecma2020 feature --- lib/routes/gamegrape/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/routes/gamegrape/index.js b/lib/routes/gamegrape/index.js index 15408bc136..750da88d13 100644 --- a/lib/routes/gamegrape/index.js +++ b/lib/routes/gamegrape/index.js @@ -3,23 +3,23 @@ const cheerio = require('cheerio'); const BASE_URL = 'http://youxiputao.com' function resolveRelativeTime(relativeTime) { - const reg = /\S* · (\d{1,2}天)?(\d{1,2}小时)?(\d{1,2}分钟)?/ - const result = reg.exec(str) - let day, hour, min + const reg = /\S* · (\d{1,2}天)?(\d{1,2}小时)?(\d{1,2}分钟)?/; + const result = reg.exec(relativeTime); + let day, hour, min; if (result[0]) { - day = result[0] + day = result[0]; } if (result[1]) { - hour = result[1] + hour = result[1]; } if (result[2]) { - min = result[2] + min = result[2]; } - return ((((day ?? 0) * 24) + (hour ?? 0)) * 60 + (min ?? 0)) * 60 * 1000 + return ((((day || 0) * 24) + (hour || 0)) * 60 + (min || 0)) * 60 * 1000; } module.exports = async (ctx) => { const { id } = ctx.params; From ab16878ba34773a60fcfad3b7aff43db061043d7 Mon Sep 17 00:00:00 2001 From: KotoriK <52659125+KotoriK@users.noreply.github.com> Date: Wed, 30 Dec 2020 01:46:12 +0800 Subject: [PATCH 3/6] format --- lib/router.js | 2 +- lib/routes/gamegrape/index.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/router.js b/lib/router.js index 40e64ff11b..c45ff27113 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3780,7 +3780,7 @@ router.get('/nace/blog/:sort?', require('./routes/nace/blog')); // Caixin Latest router.get('/caixin/latest', require('./routes/caixin/latest')); -//游戏葡萄 +// 游戏葡萄 router.get('/gamegrape/:id?', require('./routes/gamegrape/index')); module.exports = router; diff --git a/lib/routes/gamegrape/index.js b/lib/routes/gamegrape/index.js index 750da88d13..9990082312 100644 --- a/lib/routes/gamegrape/index.js +++ b/lib/routes/gamegrape/index.js @@ -1,6 +1,6 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const BASE_URL = 'http://youxiputao.com' +const BASE_URL = 'http://youxiputao.com'; function resolveRelativeTime(relativeTime) { const reg = /\S* · (\d{1,2}天)?(\d{1,2}小时)?(\d{1,2}分钟)?/; @@ -23,7 +23,7 @@ function resolveRelativeTime(relativeTime) { } module.exports = async (ctx) => { const { id } = ctx.params; - const feed_url = `${BASE_URL}/article/${id ? `/index/id/${id}` : ''}` + const feed_url = `${BASE_URL}/article/${id ? `/index/id/${id}` : ''}`; const resp = await got({ method: 'get', url: feed_url, @@ -33,12 +33,12 @@ module.exports = async (ctx) => { const list = $('.news-info-box') .map((_, item) => { item = $(item); - const txt_author_pubDate = $(item.find('div > p > span')[1]).text() + const txt_author_pubDate = $(item.find('div > p > span')[1]).text(); const author = `${txt_author_pubDate}`.replace(/ · \S*$/, ''); const link = BASE_URL + item.find('a.cover').attr('href'); const title = item.parent().find('h4').text(); - const description = $(item.find('div > p')[0]).text() - const pubDate = Date.now() - resolveRelativeTime(txt_author_pubDate) + const description = $(item.find('div > p')[0]).text(); + const pubDate = Date.now() - resolveRelativeTime(txt_author_pubDate); return { title, link, @@ -47,7 +47,7 @@ module.exports = async (ctx) => { }; }) .get(); - const feed_title = $('title').text() + const feed_title = $('title').text(); ctx.state.data = { title: feed_title, From 32cdd5661bff0d49a7d81f8ef058380c3e8bb370 Mon Sep 17 00:00:00 2001 From: KotoriK <52659125+KotoriK@users.noreply.github.com> Date: Wed, 30 Dec 2020 01:51:50 +0800 Subject: [PATCH 4/6] fix --- lib/routes/gamegrape/index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/routes/gamegrape/index.js b/lib/routes/gamegrape/index.js index 9990082312..ae1fafdc86 100644 --- a/lib/routes/gamegrape/index.js +++ b/lib/routes/gamegrape/index.js @@ -9,21 +9,19 @@ function resolveRelativeTime(relativeTime) { if (result[0]) { day = result[0]; - } if (result[1]) { hour = result[1]; - } if (result[2]) { min = result[2]; } - return ((((day || 0) * 24) + (hour || 0)) * 60 + (min || 0)) * 60 * 1000; + return (((day || 0) * 24 + (hour || 0)) * 60 + (min || 0)) * 60 * 1000; } module.exports = async (ctx) => { const { id } = ctx.params; - const feed_url = `${BASE_URL}/article/${id ? `/index/id/${id}` : ''}`; + const feed_url = `${BASE_URL}/article/${id ? `index/id/${id}` : ''}`; const resp = await got({ method: 'get', url: feed_url, @@ -43,7 +41,8 @@ module.exports = async (ctx) => { title, link, author, - description, pubDate + description, + pubDate, }; }) .get(); From b40838cff68ed86a681dc20f3cde98feec7b0028 Mon Sep 17 00:00:00 2001 From: KotoriK <52659125+KotoriK@users.noreply.github.com> Date: Wed, 30 Dec 2020 12:41:29 +0800 Subject: [PATCH 5/6] fix:pubDate; format --- lib/routes/gamegrape/index.js | 71 ++++++++++++++++------------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/lib/routes/gamegrape/index.js b/lib/routes/gamegrape/index.js index ae1fafdc86..801c7d1719 100644 --- a/lib/routes/gamegrape/index.js +++ b/lib/routes/gamegrape/index.js @@ -1,53 +1,46 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const BASE_URL = 'http://youxiputao.com'; - -function resolveRelativeTime(relativeTime) { - const reg = /\S* · (\d{1,2}天)?(\d{1,2}小时)?(\d{1,2}分钟)?/; - const result = reg.exec(relativeTime); - let day, hour, min; - - if (result[0]) { - day = result[0]; +function getNum(str) { + const result = str.match(/(\d{1,2}).*/); + if (result) { + return parseInt(result[1]); } +} +function resolveRelativeTime(relativeTime) { + const result = /\S* · (\d{1,2}天)?(\d{1,2}小时)?(\d{1,2}分钟)?/.exec(relativeTime); + let day, hour, min; if (result[1]) { - hour = result[1]; + day = getNum(result[1]); } if (result[2]) { - min = result[2]; + hour = getNum(result[2]); + } + if (result[3]) { + min = getNum(result[3]); } - return (((day || 0) * 24 + (hour || 0)) * 60 + (min || 0)) * 60 * 1000; } module.exports = async (ctx) => { - const { id } = ctx.params; - const feed_url = `${BASE_URL}/article/${id ? `index/id/${id}` : ''}`; - const resp = await got({ - method: 'get', - url: feed_url, - }); - const { data } = resp; - const $ = cheerio.load(data); - const list = $('.news-info-box') - .map((_, item) => { - item = $(item); - const txt_author_pubDate = $(item.find('div > p > span')[1]).text(); - const author = `${txt_author_pubDate}`.replace(/ · \S*$/, ''); - const link = BASE_URL + item.find('a.cover').attr('href'); - const title = item.parent().find('h4').text(); - const description = $(item.find('div > p')[0]).text(); - const pubDate = Date.now() - resolveRelativeTime(txt_author_pubDate); - return { - title, - link, - author, - description, - pubDate, - }; - }) - .get(); - const feed_title = $('title').text(); - + const { id } = ctx.params, + feed_url = `${BASE_URL}/article/${id ? `index/id/${id}` : ''}`, + resp = await got({ url: feed_url }), + { data } = resp, + $ = cheerio.load(data), + feed_title = $('title').text(), + list = $('.news-info-box') + .map((_, item) => { + item = $(item); + const txt_author_pubDate = $(item.find('div > p > span')[1]).text(); + return { + title: item.parent().find('h4').text(), + link: BASE_URL + item.find('a.cover').attr('href'), + author: `${txt_author_pubDate}`.replace(/ · \S*$/, ''), + description: $(item.find('div > p')[0]).text(), + pubDate: Date.now() - resolveRelativeTime(txt_author_pubDate), + }; + }) + .get(); ctx.state.data = { title: feed_title, link: feed_url, From cb14668374b2be6792060663418960a9fa02442b Mon Sep 17 00:00:00 2001 From: KotoriK <52659125+KotoriK@users.noreply.github.com> Date: Sun, 10 Jan 2021 21:12:31 +0800 Subject: [PATCH 6/6] fix:avoid using comma to separate expression --- lib/routes/gamegrape/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/routes/gamegrape/index.js b/lib/routes/gamegrape/index.js index 801c7d1719..5601a8722e 100644 --- a/lib/routes/gamegrape/index.js +++ b/lib/routes/gamegrape/index.js @@ -22,13 +22,13 @@ function resolveRelativeTime(relativeTime) { return (((day || 0) * 24 + (hour || 0)) * 60 + (min || 0)) * 60 * 1000; } module.exports = async (ctx) => { - const { id } = ctx.params, - feed_url = `${BASE_URL}/article/${id ? `index/id/${id}` : ''}`, - resp = await got({ url: feed_url }), - { data } = resp, - $ = cheerio.load(data), - feed_title = $('title').text(), - list = $('.news-info-box') + const { id } = ctx.params; + const feed_url = `${BASE_URL}/article/${id ? `index/id/${id}` : ''}`; + const resp = await got({ url: feed_url }); + const { data } = resp; + const $ = cheerio.load(data); + const feed_title = $('title').text(); + const list = $('.news-info-box') .map((_, item) => { item = $(item); const txt_author_pubDate = $(item.find('div > p > span')[1]).text();