refactor: replace prices.tf with pricedb.io

This commit is contained in:
2026-05-02 15:38:46 -04:00
parent 0879ede4b8
commit 1b6e2b0bd7
8 changed files with 175 additions and 245 deletions

View File

@@ -27,55 +27,35 @@ chrome.runtime.onMessage.addListener(
}
);
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.contentScriptQuery == "getPricesTFToken") {
fetch('https://api2.prices.tf/auth/access', {
method: 'post',
headers: new Headers({
'Accept': 'application/json'
})
})
.then(response => response.json())
.then(json => sendResponse(json['accessToken']))
.catch(error => {
console.error("Failed to get access token", error);
})
return true;
}
}
)
class PricesResponse {
keys: number
metal: number
}
async function priceUsingPricesTF(token: string, sku: string, retries: number = 3): Promise<PricesResponse> {
const url = `https://api2.prices.tf/prices/${encodeURIComponent(sku)}`;
async function priceUsingPricedb(sku: string, retries: number = 3): Promise<PricesResponse> {
const url = `https://pricedb.io/api/item/${encodeURIComponent(sku)}`;
const response = await fetch(url, {
method: 'get',
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${token}`,
}
})
if (response.status === 404 && sku.includes(';') && !sku.includes(';uncraftable')) {
const quality: number = parseInt(sku.split(';')[1], 10);
if(quality === 6) {
// Try uncraftable variant if unique weapon
return priceUsingPricesTF(token, sku + ';uncraftable');
return priceUsingPricedb(sku + ';uncraftable');
}
}
if(response.status === 503) {
// Happens if we send too many requests in a short period of time
if(response.status === 429) {
// Happens if we send too many requests (rate limit: 180 req/min)
// Retry after a few seconds
if(retries >= 0) {
console.log(`Cloudflare rate limit exceeded, trying again after 1 second, ${retries} retries left`)
console.log(`Rate limit exceeded, trying again after 1 second, ${retries} retries left`)
await new Promise(resolve => setTimeout(resolve, 1000));
return priceUsingPricesTF(token, sku, retries - 1);
return priceUsingPricedb(sku, retries - 1);
} else {
throw new Error(`Cloudflare rate limit exceeded, stopping`)
throw new Error(`Rate limit exceeded, stopping`)
}
}
if (!response.ok) {
@@ -83,8 +63,8 @@ async function priceUsingPricesTF(token: string, sku: string, retries: number =
}
const data = await response.json();
const prices = new PricesResponse();
prices.keys = data['sellKeys']
prices.metal = data['sellHalfScrap'] / 18.0;
prices.keys = data.sell.keys
prices.metal = data.sell.metal
return prices;
}
@@ -92,24 +72,14 @@ chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.contentScriptQuery == "priceSKU") {
const sku: string = request.sku
const service: string = request.service
const token: string = request.token
if(token === "" || !token) {
sendResponse(new Error("No token provided"))
priceUsingPricedb(sku)
.then((response) => sendResponse(response))
.catch(error => {
console.error(`Received "${error}" error while pricing ${sku} using pricedb.io`)
sendResponse(null);
return false;
}
switch (service) {
case "prices.tf": {
priceUsingPricesTF(token, sku)
.then((response) => sendResponse(response))
.catch(error => {
console.error(`Received "${error}" error while pricing ${sku} using prices.tf`)
sendResponse(null);
return false;
})
}
}
return true;
})
}
return true;
}
);
);