Skip to content

Commit

Permalink
Adds Fetch for GetTradeHistory if Possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Step7750 committed Apr 11, 2024
1 parent c3ee855 commit 085ac56
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
79 changes: 74 additions & 5 deletions src/lib/alarms/trade_history.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Trade} from '../types/float_market';
import {TradeHistoryStatus} from '../bridge/handlers/trade_history_status';
import cheerio from 'cheerio';
import {AppId} from '../types/steam_constants';

export async function pingTradeHistory(pendingTrades: Trade[]) {
const history = await getTradeHistory();
Expand All @@ -13,7 +14,9 @@ export async function pingTradeHistory(pendingTrades: Trade[]) {

// We only want to send history that is relevant to verifying trades on CSFloat
const historyForCSFloat = history.filter((e) => {
return !![...e.received_asset_ids, ...e.given_asset_ids].find((e) => {
const received_ids = e.received_assets.map((e) => e.asset_id);
const given_ids = e.given_assets.map((e) => e.asset_id);
return !![...received_ids, ...given_ids].find((e) => {
return assetsToFind[e];
});
});
Expand All @@ -33,19 +36,85 @@ async function getTradeHistory(): Promise<TradeHistoryStatus[]> {
});

const body = await resp.text();
const webAPIToken = /data-loyalty_webapi_token=\"&quot;([a-zA-Z0-9\_\.-]+)&quot;\"/.exec(body);
if (webAPIToken && webAPIToken.length > 1) {
try {
const history = await getTradeHistoryFromAPI(webAPIToken[1]);
if (history.length > 0) {
// Hedge in case this endpoint gets killed, only return if there are results, fallback to HTML parser
return history;
}
} catch (e) {
console.error(e);
}

// Fallback to HTML parsing
}

if (body.includes('too many requests')) {
throw 'Too many requests';
}

return parseTradeHistoryHTML(body);
}

interface HistoryAsset {
assetid: string;
appid: AppId;
new_assetid: string;
}

interface TradeHistoryAPIResponse {
response: {
trades: {
tradeid: string;
steamid_other: string;
status: number;
assets_given: HistoryAsset[];
assets_received: HistoryAsset[];
}[];
};
}

async function getTradeHistoryFromAPI(accessToken: string): Promise<TradeHistoryStatus[]> {
// This only works if they have granted permission for https://api.steampowered.com
const resp = await fetch(
`https://api.steampowered.com/IEconService/GetTradeHistory/v1/?access_token=${accessToken}&max_trades=50`,
{
credentials: 'include',
// Expect redirect since we're using `me` above
redirect: 'follow',
}
);

if (resp.status !== 200) {
throw new Error('invalid status');
}

const data = (await resp.json()) as TradeHistoryAPIResponse;
return data.response.trades.map((e) => {
return {
other_party_url: `https://steamcommunity.com/profiles/${e.steamid_other}`,
received_assets: e.assets_received.map((e) => {
return {asset_id: e.assetid, new_asset_id: e.new_assetid};
}),
given_assets: e.assets_given.map((e) => {
return {asset_id: e.assetid, new_asset_id: e.new_assetid};
}),
} as TradeHistoryStatus;
});
}

function parseTradeHistoryHTML(body: string): TradeHistoryStatus[] {
const doc = cheerio.load(body);

const statuses = doc('.tradehistoryrow .tradehistory_event_description a')
.toArray()
.map((row) => {
return {
other_party_url: doc(row).attr('href'),
received_asset_ids: [],
given_asset_ids: [],
received_assets: [],
given_assets: [],
} as TradeHistoryStatus;
});

Expand All @@ -56,9 +125,9 @@ async function getTradeHistory(): Promise<TradeHistoryStatus[]> {
const [text, index, type, assetId] = match;
const tradeIndex = parseInt(index);
if (type === 'received') {
statuses[tradeIndex].received_asset_ids.push(assetId);
statuses[tradeIndex].received_assets.push({asset_id: assetId});
} else if (type === 'given') {
statuses[tradeIndex].given_asset_ids.push(assetId);
statuses[tradeIndex].given_assets.push({asset_id: assetId});
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/lib/alarms/trade_offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export async function pingSentTradeOffers(pendingTrades: Trade[]) {
await TradeOfferStatus.handleRequest({sent_offers: offersForCSFloat}, {});
}

interface TradeOffersAPIResponse {
response: {
trade_offers_sent: {
tradeofferid: string;
accountid_other: string;
trade_offer_state: TradeOfferState;
}[];
};
}

async function getEnglishSentTradeOffersHTML(): Promise<cheerio.Root> {
const resp = await fetch(`https://steamcommunity.com/id/me/tradeoffers/sent`, {
credentials: 'include',
Expand Down
9 changes: 7 additions & 2 deletions src/lib/bridge/handlers/trade_history_status.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {SimpleHandler} from './main';
import {RequestType} from './types';

export interface TradeHistoryAsset {
asset_id: string;
new_asset_id?: string;
}

export interface TradeHistoryStatus {
other_party_url: string;
received_asset_ids: string[];
given_asset_ids: string[];
received_assets: TradeHistoryAsset[];
given_assets: TradeHistoryAsset[];
}

export interface TradeHistoryStatusRequest {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/page_scripts/trade_offers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {init} from './utils';
import {inPageContext} from '../utils/snips';
import {pingTradeHistory} from '../alarms/trade_history';

init('src/lib/page_scripts/trade_offers.js', main);

function main() {}

if (!inPageContext()) {
pingTradeHistory([])
.then((r) => console.log(r))
.catch((e) => console.log(e));
}

0 comments on commit 085ac56

Please sign in to comment.