fix: refactor dgtle trade (#4074)
This commit is contained in:
parent
b4432725d0
commit
9678a640fd
|
|
@ -688,7 +688,7 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性
|
|||
|
||||
### 闲置(分类)
|
||||
|
||||
<Route author="xyqfer" example="/dgtle/trade/111" path="/dgtle/trade/:typeId?" :paramsDesc="['分类 id,默认为全部']">
|
||||
<Route author="xyqfer hoilc" example="/dgtle/trade/111" path="/dgtle/trade/:typeId?" :paramsDesc="['分类 id,默认为全部']">
|
||||
|
||||
| 全部 | 电脑 | 手机 | 平板 | 相机 | 影音 | 外设 | 生活 | 公告 |
|
||||
| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
|
||||
|
|
@ -698,7 +698,7 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性
|
|||
|
||||
### 闲置(关键词)
|
||||
|
||||
<Route author="gaoliang" example="/dgtle/trade/search/ipad" path="/dgtle/trade/search/:keyword" :paramsDesc="['搜索关键词']"/>
|
||||
<Route author="gaoliang hoilc" example="/dgtle/trade/search/ipad" path="/dgtle/trade/search/:keyword" :paramsDesc="['搜索关键词']"/>
|
||||
|
||||
### 鲸图(分类)
|
||||
|
||||
|
|
|
|||
|
|
@ -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`,
|
||||
})),
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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 = `<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`,
|
||||
})),
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue