Skip to content

Commit

Permalink
fix(route/xiaoheihe): 修复小黑盒 - 游戏折扣数据抓取失败 (DIYgod#17520)
Browse files Browse the repository at this point in the history
* fix(route/xiaoheihe): 修复小黑盒 - 游戏折扣数据抓取失败

* refactor(route/xiaoheihe): 小黑盒游戏折扣券后价格式化显示
  • Loading branch information
ueiu authored Nov 10, 2024
1 parent 5f256bf commit 173ae71
Showing 1 changed file with 72 additions and 28 deletions.
100 changes: 72 additions & 28 deletions lib/routes/xiaoheihe/discount.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { calculate } from './util';

export const route: Route = {
path: '/discount/:platform',
Expand All @@ -23,20 +24,59 @@ export const route: Route = {
};

const PLATFORM_MAP = {
pc: 'PC',
switch: 'Switch',
psn: 'PSN',
xbox: 'Xbox',
pc: {
key: 'pc',
desc: 'PC',
},
switch: {
key: 'switch',
desc: 'Switch',
},
psn: {
key: 'ps4',
desc: 'PSN',
},
xbox: {
key: 'xbox',
desc: 'Xbox',
},
};

function getDiscountDesc(discount) {
return `${(100 - discount) / 10}折`;
}

function getLowestDesc(priceInfo, isSuperLowest = false) {
if (!('is_lowest' in priceInfo) || priceInfo.is_lowest === 0) {
return '';
} else if (isSuperLowest) {
return '[超史低]';
} else if (priceInfo.is_lowest && priceInfo.is_lowest === 1 && priceInfo.new_lowest && priceInfo.new_lowest === 1) {
return '[新史低]';
} else if (priceInfo.is_lowest && priceInfo.is_lowest === 1) {
return '[史低]';
}
}

function getHeyboxPriceDesc(heyboxPriceInfo) {
if (heyboxPriceInfo.coupon_info) {
let discountPrice = heyboxPriceInfo.cost_coin / 1000;
discountPrice = discountPrice - heyboxPriceInfo.coupon_info.max_reduce;
const formatPrice = Number.isInteger(discountPrice) ? discountPrice.toFixed(0) : discountPrice.toFixed(2);
return `| 券后价: ${formatPrice} [${heyboxPriceInfo.coupon_info.coupon_desc}]`;
} else {
return '';
}
}

async function handler(ctx) {
const platform = ctx.req.param('platform');
const platformInfo = PLATFORM_MAP[ctx.req.param('platform')];

const response = await got({
method: 'get',
url: `https://api.xiaoheihe.cn/game/get_game_list_v3?sort_type=discount&filter_head=${platform}&offset=0&limit=30&os_type=web`,
});
const data = response.data.result.games;
const dataUrl = calculate(
`https://api.xiaoheihe.cn/game/get_game_list_v3/?filter_head=${platformInfo.key}&offset=0&limit=30&os_type=web&app=heybox&client_type=mobile&version=999.0.3&x_client_type=web&x_os_type=Mac&x_app=heybox&heybox_id=-1&include_filter=-1`
);
const response = await ofetch(dataUrl);
const data = response.result.games;

const items = data.map((item) => {
const title = `${item.name}${item.name_en ? '/' + item.name_en : ''}`;
Expand All @@ -47,44 +87,48 @@ async function handler(ctx) {
for (const platform of item.platform_infos) {
if (platform.price) {
if (platform.key) {
description += `平台: ${platform.key}<br/>>`;
description += `平台: ${platform.key.toUpperCase()}<br/>`;
}
if (platform.price.current) {
description += `当前价格: ${platform.price.current}${platform.price.discount === platform.price.lowest_discount ? '[史低]' : ''}<br/>`;
description += `当前价格: ${platform.price.current} ${getLowestDesc(platform.price)}<br/>`;
}
if (platform.price.initial) {
description += `原价: ${platform.price.initial}<br/>`;
}
if (platform.price.discount_desc) {
description += `折扣力度: ${platform.price.discount_desc}<br/>`;
if (platform.price.discount && platform.price.discount > 0) {
description += `折扣力度: ${getDiscountDesc(platform.price.discount)}<br/>`;
}
if (platform.price.deadline_date) {
description += `截止时间: ${platform.price.deadline_date}<br/>`;
}
description += '<br/>';
}
}
} else {
if (item.price) {
description += `平台: ${platform.toUpperCase()}<br/>`;
if (item.price.discount) {
description += `折扣力度: ${(100 - item.price.discount) / 10}折<br/>`;
}
if (item.price.initial && item.price.discount) {
const current = Math.round((item.price.initial * (100 - item.price.discount)) / 100);
description += `当前价格: ${current}${item.price.discount === item.price.lowest_discount ? '[史低]' : ''}&nbsp;&nbsp;`;
description += `平台: ${platformInfo.desc}<br/>`;
if (item.heybox_price) {
description += `当前价格: ${item.price.current} ${getHeyboxPriceDesc(item.heybox_price)} ${getLowestDesc(item.price, item.heybox_price.super_lowest)}<br/>`;
} else if (item.price.current) {
description += `当前价格: ${item.price.current} ${getLowestDesc(item.price)}<br/>`;
}
if (item.price.initial) {
description += `原价: ${item.price.initial}<br/>`;
}
if (item.score) {
description += `评分: ${item.score}<br/>`;
if (item.price.discount && item.price.discount > 0) {
description += `折扣力度: ${getDiscountDesc(item.price.discount)}<br/>`;
}
if (item.price.deadline_date) {
description += `截止时间: ${item.price.deadline_date}<br/>`;
}
description += '<br/>';
}
}
if (item.score) {
description += `评分: ${item.score}<br/>`;
}
description += '<br/>';

let link = `https://api.xiaoheihe.cn/game/share_game_detail?appid=${item.steam_appid}`;
if (platform === 'pc') {
if (platformInfo.key === 'pc') {
link = `https://store.steampowered.com/app/${item.steam_appid}`;
}
return {
Expand All @@ -95,7 +139,7 @@ async function handler(ctx) {
});

return {
title: `小黑盒 ${PLATFORM_MAP[platform]} 游戏折扣`,
title: `小黑盒 ${platformInfo.desc} 游戏折扣`,
link: `https://xiaoheihe.cn`,
item: items,
};
Expand Down

0 comments on commit 173ae71

Please sign in to comment.