From 9678a640fd26f8778aa6f65bb4510cb264553853 Mon Sep 17 00:00:00 2001 From: hoilc Date: Tue, 25 Feb 2020 00:15:33 +0800 Subject: [PATCH] fix: refactor dgtle trade (#4074) --- docs/social-media.md | 4 +- lib/routes/dgtle/keyword.js | 79 +++++++----------------------- lib/routes/dgtle/trade.js | 97 ++++++++++++------------------------- 3 files changed, 50 insertions(+), 130 deletions(-) diff --git a/docs/social-media.md b/docs/social-media.md index 5653739e5..c0c883659 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -688,7 +688,7 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性 ### 闲置(分类) - + | 全部 | 电脑 | 手机 | 平板 | 相机 | 影音 | 外设 | 生活 | 公告 | | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | @@ -698,7 +698,7 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性 ### 闲置(关键词) - + ### 鲸图(分类) diff --git a/lib/routes/dgtle/keyword.js b/lib/routes/dgtle/keyword.js index 752da7f99..2d512ae31 100644 --- a/lib/routes/dgtle/keyword.js +++ b/lib/routes/dgtle/keyword.js @@ -1,74 +1,29 @@ 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({ + const url = `https://www.dgtle.com/search?search_word=${encodeURIComponent(keyword)}`; + + const response = await got({ method: 'get', - url: baseUrl, + url: `https://www.dgtle.com/search/sale?search_word=${encodeURIComponent(keyword)}&page=1`, + headers: { + Referer: url, + }, }); - 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() - ); + const list = response.data.data.dataList; ctx.state.data = { - title: `甩甩尾巴 - ${keyword}`, - description: '纯净,安全的玩家闲置物品交易平台', - link: baseUrl, - item: resultItem, + title: `数字尾巴 - 闲置 - ${keyword}`, + link: url, + item: list.map((item) => ({ + title: item.title.replace(/<.*?>/g, ''), + author: item.author.username, + description: `

价格: ¥${item.price}

地址: ${item.address}

${item.content}

`, + pubDate: new Date(item.created_at * 1000), + link: `https://www.dgtle.com/sale-${item.id}-1.html`, + })), }; }; diff --git a/lib/routes/dgtle/trade.js b/lib/routes/dgtle/trade.js index 7c672f069..7170f7cad 100644 --- a/lib/routes/dgtle/trade.js +++ b/lib/routes/dgtle/trade.js @@ -1,77 +1,42 @@ const got = require('@/utils/got'); -const cheerio = require('cheerio'); + +const type_names = { + 0: '全部', + 109: '手机', + 110: '平板', + 111: '电脑', + 112: '生活', + 113: '相机', + 114: '影音', + 115: '外设', + 116: '公告', +}; module.exports = async (ctx) => { - let { typeId = '' } = ctx.params; - if (typeId === 0) { - typeId = ''; - } + const typeId = ctx.params.typeId ? ctx.params.typeId : '0'; - const host = 'http://trade.dgtle.com'; - const baseUrl = `${host}/dgtle_module.php?mod=trade&typeid=${typeId}`; - const res = await got({ + const url = 'https://www.dgtle.com/sale'; + + const response = await got({ method: 'get', - url: baseUrl, + url: `https://www.dgtle.com/sale/getList/${typeId}?page=1&last_id=0`, + headers: { + Referer: url, + }, }); - 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() - ); + const type_name = type_names[typeId] ? type_names[typeId] : typeId; + const list = response.data.data.dataList; ctx.state.data = { - title: '甩甩尾巴', - description: '纯净,安全的玩家闲置物品交易平台', - link: baseUrl, - item: resultItem, + title: `数字尾巴 - 闲置 - ${type_name}`, + link: url, + item: list.map((item) => ({ + title: item.title, + author: item.user.username, + description: `

价格: ¥${item.price}

地址: ${item.address}

${item.content}

`, + pubDate: new Date(item.created_at * 1000), + link: `https://www.dgtle.com/sale-${item.id}-1.html`, + })), }; };