Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
50 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = `<img src="${$tradeItem | ||
.find('.cover') | ||
.attr('src') | ||
.replace(/\?.+/g, '')}" /><br>`; | ||
|
||
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: `<p>价格: ¥${item.price}</p><p>地址: ${item.address}</p><p>${item.content}</p><img src="${item.cover}" style="max-width: 100%;"/>`, | ||
pubDate: new Date(item.created_at * 1000), | ||
link: `https://www.dgtle.com/sale-${item.id}-1.html`, | ||
})), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 url = 'https://www.dgtle.com/sale'; | ||
|
||
const host = 'http://trade.dgtle.com'; | ||
const baseUrl = `${host}/dgtle_module.php?mod=trade&typeid=${typeId}`; | ||
const res = await got({ | ||
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 = `<img src="${$tradeItem | ||
.find('.cover') | ||
.attr('src') | ||
.replace(/\?.+/g, '')}" /><br>`; | ||
|
||
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: `<p>价格: ¥${item.price}</p><p>地址: ${item.address}</p><p>${item.content}</p><img src="${item.cover}" style="max-width: 100%;"/>`, | ||
pubDate: new Date(item.created_at * 1000), | ||
link: `https://www.dgtle.com/sale-${item.id}-1.html`, | ||
})), | ||
}; | ||
}; |