From a87ffdb8e841eee4c2c44ea8f04e7da2b9de38d8 Mon Sep 17 00:00:00 2001 From: Gao Liang Date: Sat, 20 Jul 2019 12:51:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=A9=E7=94=A9=E5=B0=BE=E5=B7=B4?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=85=B3=E9=94=AE=E8=AF=8D=E7=AD=9B=E9=80=89?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=20(#2649)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/shopping.md | 4 ++ lib/router.js | 1 + lib/routes/dgtle/keyword.js | 74 +++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 lib/routes/dgtle/keyword.js diff --git a/docs/shopping.md b/docs/shopping.md index 4b0c03d71d..9691ea2f68 100644 --- a/docs/shopping.md +++ b/docs/shopping.md @@ -124,6 +124,10 @@ pageClass: routes +### 关键词 + + + ## 淘宝众筹 ### 众筹项目 diff --git a/lib/router.js b/lib/router.js index c7dfd52978..2924f46e43 100644 --- a/lib/router.js +++ b/lib/router.js @@ -793,6 +793,7 @@ router.get('/parcel/hermesuk/:tracking', require('./routes/parcel/hermesuk')); // 甩甩尾巴 router.get('/dgtle/trade/:typeId?', require('./routes/dgtle/trade')); +router.get('/dgtle/trade/search/:keyword', require('./routes/dgtle/keyword')); // 抽屉新热榜 router.get('/chouti/:subject?', require('./routes/chouti')); diff --git a/lib/routes/dgtle/keyword.js b/lib/routes/dgtle/keyword.js new file mode 100644 index 0000000000..b9ae87ec23 --- /dev/null +++ b/lib/routes/dgtle/keyword.js @@ -0,0 +1,74 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const keyword = ctx.params.keyword; + + const host = 'http://trade.dgtle.com'; + const baseUrl = `${host}/dgtle_module.php?mod=trade&searchsort=1&PName=${keyword}`; + const res = await got({ + method: 'get', + url: baseUrl, + }); + const $ = cheerio.load(res.data); + const tradeList = $('.tradebox'); + + const resultItem = await Promise.all( + tradeList + .map(async (_, tradeItem) => { + const $tradeItem = $(tradeItem); + + const url = `${host}${$tradeItem.find('.tradetitle a').attr('href')}`; + const item = { + title: $tradeItem.find('.tradetitle').attr('title'), + description: '', + link: url, + author: $tradeItem.find('.tradeuser').text(), + }; + + const key = `dgtle-trade: ${url}`; + const value = await ctx.cache.get(key); + + if (value) { + item.description = value.description; + item.pubDate = value.pubDate; + } else { + const tradeDetail = await got({ + method: 'get', + url: url, + }); + const $ = cheerio.load(tradeDetail.data); + const pubDate = new Date( + $('.cr_date > em') + .last() + .text() + ).toUTCString(); + let description = `
`; + + description += $tradeItem.find('.tradeprice').text(); + description += $('#trade_info').html(); + description += $('.pcb').html(); + + item.description = description; + item.pubDate = pubDate; + ctx.cache.set(key, { + pubDate, + description, + }); + } + + return Promise.resolve(item); + }) + .get() + ); + + ctx.state.data = { + title: `甩甩尾巴 - ${keyword}`, + description: '纯净,安全的玩家闲置物品交易平台', + link: baseUrl, + item: resultItem, + }; +};