You've already forked tf2wikipricing
Initial commit
This commit is contained in:
67
src/content/pricing/pricestf.ts
Normal file
67
src/content/pricing/pricestf.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
declare function GM_fetch(input: string | URL | globalThis.Request, init?: RequestInit): Promise<Response>
|
||||
import '../GM_fetch'
|
||||
|
||||
async function getPricesToken(): Promise<string> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
GM_fetch('https://api2.prices.tf/auth/access', {
|
||||
method: 'post',
|
||||
headers: new Headers({
|
||||
'Accept': 'application/json'
|
||||
})
|
||||
})
|
||||
.then((response) => {
|
||||
if (response.status != 200) {
|
||||
reject(response.status)
|
||||
}
|
||||
return response.json()
|
||||
})
|
||||
.then((responseData) => resolve(responseData['accessToken']))
|
||||
})
|
||||
}
|
||||
|
||||
class PricesResponse {
|
||||
keys: number
|
||||
metal: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Price the given item using https://prices.tf
|
||||
* @return
|
||||
*/
|
||||
async function priceUsingPricesTF(token: string, defIndex: number, quality: number): Promise<PricesResponse> {
|
||||
// prices.tf
|
||||
// https://api2.prices.tf/prices/${sku}
|
||||
// Authorization: Bearer ${token}
|
||||
return new Promise(async (resolve, reject) => {
|
||||
if (!token) {
|
||||
reject(401)
|
||||
}
|
||||
const sku = defIndex + ";" + quality;
|
||||
// logDebug(`Making network request to prices.tf for ${sku}`)
|
||||
var response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku)}`, {
|
||||
method: 'get',
|
||||
headers: new Headers({
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
})
|
||||
})
|
||||
if (response.status === 404 && quality === 6) {
|
||||
// Try uncraftable variant
|
||||
response = await GM_fetch(`https://api2.prices.tf/prices/${encodeURIComponent(sku + ';uncraftable')}`, {
|
||||
method: 'get',
|
||||
headers: new Headers({
|
||||
'Accept': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
})
|
||||
})
|
||||
}
|
||||
if (response.status === 200) {
|
||||
const json = await response.json()
|
||||
resolve({ keys: json['sellKeys'], metal: json['sellHalfScrap'] / 18.0 })
|
||||
} else {
|
||||
reject(response.status)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export { getPricesToken, priceUsingPricesTF }
|
||||
Reference in New Issue
Block a user