You've already forked tf2wikipricing
refactor: replace prices.tf with pricedb.io
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { defindex_key, storage_priceprefix } from "./config"
|
||||
import { priceUsingPricesTF } from "./pricing/pricestf"
|
||||
import { priceUsingPricedb } from "./pricing/pricedb"
|
||||
import { getStorageValue, setStorageValue } from "./storage"
|
||||
import { logDebug } from "./utils/log"
|
||||
declare const __ENV_WEBEXTENSION: boolean;
|
||||
@@ -21,7 +21,7 @@ export class ItemPriceData {
|
||||
scmPrice: number
|
||||
|
||||
toString(): string {
|
||||
return `Price for ${this.sku}, fetched ${new Date(this.update)} (expires ${new Date(this.update + this.ttl)})\n` +
|
||||
return `Price for ${this.sku}, fetched ${new Date(this.update)} (expires ${new Date(this.update + this.ttl)})\n` +
|
||||
JSON.stringify({
|
||||
keys: this.keys,
|
||||
metal: this.metal,
|
||||
@@ -31,17 +31,16 @@ export class ItemPriceData {
|
||||
}
|
||||
|
||||
|
||||
export async function fetchKeyPrice(token: string) {
|
||||
return fetchPrice(token, `${defindex_key};6`, new Date(), 86400000)
|
||||
export async function fetchKeyPrice() {
|
||||
return fetchPrice(`${defindex_key};6`, new Date(), 86400000)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a price for a given SKU, using cached values if available.
|
||||
* @param token prices.tf access token.
|
||||
* @param update Date retrieved.
|
||||
* @param ttl Time to cache results in milliseconds. 30 minutes by default.
|
||||
*/
|
||||
export async function fetchPrice(token: string, sku: string, update: Date = new Date(), ttl: number = 30 * 60 * 1000): Promise<ItemPriceData> {
|
||||
export async function fetchPrice(sku: string, update: Date = new Date(), ttl: number = 30 * 60 * 1000): Promise<ItemPriceData> {
|
||||
let data: ItemPriceData | null
|
||||
|
||||
const cached: ItemPriceData = await getStorageValue(storage_priceprefix + sku, null)
|
||||
@@ -51,9 +50,6 @@ export async function fetchPrice(token: string, sku: string, update: Date = new
|
||||
|
||||
if (!data || data.sku != sku || 'update' in data && 'ttl' in data && Date.now() > (new Date(data.update).getTime() + data.ttl)) {
|
||||
logDebug(`Fetching price data for ${sku}`)
|
||||
if(!token || token === '') {
|
||||
throw new Error('No token provided')
|
||||
}
|
||||
data = new ItemPriceData()
|
||||
data.sku = sku
|
||||
data.update = update.getTime()
|
||||
@@ -62,9 +58,9 @@ export async function fetchPrice(token: string, sku: string, update: Date = new
|
||||
try {
|
||||
let response: PricesResponse
|
||||
if(__ENV_USERSCRIPT) {
|
||||
response = await priceUsingPricesTF(token, sku)
|
||||
response = await priceUsingPricedb(sku)
|
||||
} else {
|
||||
response = await chrome.runtime.sendMessage({contentScriptQuery: "priceSKU", service: "prices.tf", sku: sku, token: token});
|
||||
response = await chrome.runtime.sendMessage({contentScriptQuery: "priceSKU", sku: sku});
|
||||
}
|
||||
if (!response || response instanceof Error) {
|
||||
throw new Error(`Bad response: ${response}`)
|
||||
@@ -72,7 +68,7 @@ export async function fetchPrice(token: string, sku: string, update: Date = new
|
||||
data.keys = response.keys
|
||||
data.metal = response.metal
|
||||
} catch (error) {
|
||||
throw new Error(`Received "${error}" error while pricing ${sku} using prices.tf`)
|
||||
throw new Error(`Received "${error}" error while pricing ${sku} using pricedb.io`)
|
||||
}
|
||||
|
||||
if ('metal' in data && 'keys' in data) {
|
||||
@@ -82,4 +78,4 @@ export async function fetchPrice(token: string, sku: string, update: Date = new
|
||||
logDebug(`Using cached price data for ${sku}`)
|
||||
}
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user