fix(route): Restore Steam store route (#14834)

This updates the deprecated Steam store search route. The CSS stuff is
mostly untouched, just some small changes for bundle and discount detection.
`URLSearchParams` is now used instead of external query packages.

Implement PR feedback
This commit is contained in:
moppman
2024-03-19 15:42:05 +08:00
committed by GitHub
parent f68e2b607d
commit e91927843d
4 changed files with 71 additions and 58 deletions
+1 -1
View File
@@ -442,7 +442,7 @@ router.get('/cpu/yjsy', lazyloadRouteHandler('./routes/universities/cpu/yjsy'));
router.get('/zimuku/:type?', lazyloadRouteHandler('./routes/zimuku/index'));
// Steam
router.get('/steam/search/:params', lazyloadRouteHandler('./routes/steam/search'));
// router.get('/steam/search/:params', lazyloadRouteHandler('./routes/steam/search'));
// Steamgifts
router.get('/steamgifts/discussions/:category?', lazyloadRouteHandler('./routes/steam/steamgifts/discussions'));
-57
View File
@@ -1,57 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const qs = require('querystring');
const queryString = require('query-string');
module.exports = async (ctx) => {
const { params } = ctx.params;
const query = qs.parse(params);
const { data: html } = await got.get(`https://store.steampowered.com/search/results`, {
searchParams: queryString.stringify(query),
});
const $ = cheerio.load(html);
ctx.state.data = {
title: 'Steam search result',
description: `Query: ${qs.stringify(query)}`,
link: /g_strUnfilteredURL\s=\s'(.*)'/.exec(html)[1],
item: $('#search_result_container a')
.toArray()
.map((a) => {
const $el = $(a);
const isBundle = !!$el.attr('data-ds-bundle-data');
const isDiscounted = $el.find('.search_price.discounted').length > 0;
const hasReview = $el.find('.search_review_summary').length > 0;
let desc = '';
if (isBundle) {
const bundle = JSON.parse($el.attr('data-ds-bundle-data'));
desc += 'Bundle\n';
if (bundle.m_bRestrictGifting) {
desc += 'Restrict gifting\n';
}
desc += `Items count: ${bundle.m_rgItems.length}\n`;
}
if (isDiscounted) {
desc += `Discount: ${$el.find('.search_discount span').text().trim()}\n`;
desc += `Original price: ${$el.find('.search_price strike').text().trim()}\n`;
desc += `Discounted price: ${$el
.find('.search_price')
.contents()
.filter((i, e) => e.nodeType === 3)
.text()
.trim()}\n`;
} else {
desc += `Price: ${$el.find('.search_price').text().trim()}\n`;
}
if (hasReview) {
desc += $el.find('.search_review_summary').attr('data-tooltip-html');
}
return {
title: $el.find('span.title').text(),
link: $el.attr('href'),
description: desc.replaceAll('\n', '<br>'),
};
})
.filter((it) => it.title),
};
};
+6
View File
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: 'Steam',
url: 'store.steampowered.com',
};
+64
View File
@@ -0,0 +1,64 @@
import type { Route } from '@/types';
import got from '@/utils/got';
import { load } from 'cheerio';
export const route: Route = {
path: '/search/:params',
categories: ['game'],
example: '/steam/search/sort_by=Released_DESC&tags=492&category1=10&os=linux',
parameters: { params: 'Query parameters for a Steam Store search.' },
radar: [
{
source: ['store.steampowered.com', 'store.steampowered.com/search/:params'],
},
],
name: 'Store Search',
maintainers: ['moppman'],
handler,
};
async function handler(ctx) {
const query = new URLSearchParams(ctx.req.param('params'));
const { data: html } = await got('https://store.steampowered.com/search/', {
searchParams: query,
});
const $ = load(html);
return {
title: 'Steam search result',
description: `Query: ${query.toString()}`,
link: /g_strUnfilteredURL\s=\s'(.*)'/.exec(html)[1],
item: $('#search_result_container a')
.toArray()
.map((a) => {
const $el = $(a);
const isBundle = !!$el.attr('data-ds-bundle-data');
const isDiscounted = $el.find('.discount_original_price').length > 0;
const hasReview = $el.find('.search_review_summary').length > 0;
let desc = '';
if (isBundle) {
const bundle = JSON.parse($el.attr('data-ds-bundle-data'));
desc += 'Bundle\n';
if (bundle.m_bRestrictGifting) {
desc += 'Restrict gifting\n';
}
desc += `Items count: ${bundle.m_rgItems.length}\n`;
}
if (isDiscounted) {
desc += `Discount: ${$el.find('.discount_pct').text().trim()}\n`;
desc += `Original price: ${$el.find('.discount_original_price').text().trim()}\n`;
desc += `Discounted price: ${$el.find('.discount_final_price').text().trim()}\n`;
} else {
desc += `Price: ${$el.find('.discount_final_price').text().trim()}\n`;
}
if (hasReview) {
desc += $el.find('.search_review_summary').attr('data-tooltip-html');
}
return {
title: $el.find('span.title').text(),
link: $el.attr('href'),
description: desc.replaceAll('\n', '<br>'),
};
})
.filter((it) => it.title),
};
}