Skip to content

Commit

Permalink
fix: refactor dgtle trade (#4074)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoilc committed Feb 24, 2020
1 parent b443272 commit 9678a64
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 130 deletions.
4 changes: 2 additions & 2 deletions docs/social-media.md
Expand Up @@ -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,默认为全部']">

| 全部 | 电脑 | 手机 | 平板 | 相机 | 影音 | 外设 | 生活 | 公告 |
| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
Expand All @@ -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="['搜索关键词']"/>

### 鲸图(分类)

Expand Down
79 changes: 17 additions & 62 deletions 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 = `<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`,
})),
};
};
97 changes: 31 additions & 66 deletions 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 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`,
})),
};
};

0 comments on commit 9678a64

Please sign in to comment.