Skip to content

Commit

Permalink
🔨 update getPricelist code for prices.tf
Browse files Browse the repository at this point in the history
  • Loading branch information
idinium96 committed Nov 2, 2023
1 parent 13300a9 commit f27183c
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions src/lib/pricer/pricestf/prices-tf-pricer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import timersPromises from 'timers/promises';
import Currencies from '@tf2autobot/tf2-currencies';
import PricesTfSocketManager from './prices-tf-socket-manager';
import IPricer, {
Expand All @@ -8,12 +7,14 @@ import IPricer, {
PricerOptions,
RequestCheckResponse
} from '../../../classes/IPricer';
import PricesTfApi, { PricesTfItem, PricesTfItemMessageEvent } from './prices-tf-api';
import PricesTfApi, { PricesTfGetPricesResponse, PricesTfItem, PricesTfItemMessageEvent } from './prices-tf-api';
import log from '../../logger';

export default class PricesTfPricer implements IPricer {
private socketManager: PricesTfSocketManager;

private attempts = 0;

public constructor(private api: PricesTfApi) {
this.socketManager = new PricesTfSocketManager(api);
}
Expand Down Expand Up @@ -42,35 +43,56 @@ export default class PricesTfPricer implements IPricer {
}

async getPricelist(): Promise<GetPricelistResponse> {
try {
const pricelist = await PricesTfApi.apiRequest(
'GET',
'/json/pricelist-array',
undefined,
undefined,
undefined,
'https://autobot.tf'
);

return pricelist;
} catch (err) {
log.error('Failed to get pricelist from autobot.tf: ', err);
if (JSON.parse(process.env.DEV) === true) {
try {
const pricelist = await PricesTfApi.apiRequest(
'GET',
'/json/pricelist-array',
undefined,
undefined,
undefined,
'https://autobot.tf'
);

return pricelist;
} catch (err) {
log.error('Failed to get pricelist from autobot.tf: ', err);
}
}

let prices: PricesTfItem[] = [];
let currentPage = 1;
let totalPages = 0;

let delay = 0;
const minDelay = 200;
const minDelay = 100;
let response: PricesTfGetPricesResponse;

log.debug('Requesting pricelist pages...');

do {
await timersPromises.setTimeout(delay);
await new Promise(resolve => setTimeout(resolve, delay));
const start = new Date().getTime();
log.debug('Requesting pricelist pages...');
const response = await this.api.getPricelistPage(currentPage);
totalPages = response.meta.totalPages;
currentPage++;

try {
log.debug(`Getting page ${currentPage}${totalPages === 0 ? '' : ` of ${totalPages}`}...`);
response = await this.api.getPricelistPage(currentPage);
currentPage++;
totalPages = response.meta.totalPages;
} catch (e) {
if (currentPage > 1) {
await new Promise(resolve => setTimeout(resolve, 60 * 1000));
continue;
} else {
if (this.attempts < 3) {
this.attempts++;
return this.getPricelist();
}

this.attempts = 0;
throw e;
}
}

prices = prices.concat(response.items);
const time = new Date().getTime() - start;
Expand Down

0 comments on commit f27183c

Please sign in to comment.