-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (135 loc) · 4.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import axios from 'axios';
import dotenv, { parse } from 'dotenv';
dotenv.config();
const API_KEY = process.env.CSINVENTORYAPI_KEY;
if (!API_KEY) {
throw new Error('No API key provided');
}
/**
* @typedef {Object} Inventory
*
* @property {Array} assets
* @property {Array} descriptions
* @property {Number} total_inventory_count
* @property {Number} success
* @property {Number} rwgrsn
*/
/**
* @typedef {Object} ParsedInventoryItem
*
* These are from assets[]
* @property {Number} appid
* @property {String} classid
* @property {String} instanceid
* @property {String} assetid
* @property {String} contextid
*
* These are from descriptions[]
* @property {String} icon_url
* @property {Number} tradable
* @property {String | null} inspect_url
* @property {String} name
* @property {String} market_hash_name
* @property {String} name_color
* You can fetch many more properties from descriptions[]
*
* Float properties
* @property {Number} floatvalue
* @property {Number} paintseed
* @property {Number} paintindex
*/
/**
* Fetches the inventory of a user
*
* @param {string} steamid64 of the user
* @returns {Promise<Inventory>} the user's inventory
*/
const getUserInventory = async (steamid64) => {
const response = await axios.get(
`https://csinventoryapi.com/api/v1/inventory?api_key=${API_KEY}&steamid64=${steamid64}`
);
if (response.data.success !== 1) {
throw new Error('Failed to fetch inventory');
}
if (response.data.total_inventory_count === 0) {
throw new Error('No items found.');
}
if (!response.data.assets || !response.data.descriptions) {
throw new Error('No assets found.');
}
return response.data;
}
/**
*
* @param {Inventory} inventory
* @returns {Promise<ParsedInventoryItem[]>}
*/
const parseInventory = async (inventory) => {
const parsedInventory = [];
for (let i = 0; i < inventory.assets.length; i++) {
const asset = inventory.assets[i];
const description = inventory.descriptions.find(d => d.classid === asset.classid);
if (!description) {
continue;
}
parsedInventory.push({
appid: asset.appid,
classid: asset.classid,
instanceid: asset.instanceid,
assetid: asset.assetid,
contextid: asset.contextid,
icon_url: description.icon_url,
tradable: description.tradable,
inspect_url: description.actions && description.actions[0] && description.actions[0].link || null,
name: description.name,
market_hash_name: description.market_hash_name,
name_color: description.name_color
});
}
return parsedInventory;
}
/**
* Populates the float values of the items in the inventory
* @param {ParsedInventoryItem[]} parsedInventory
* @param {steamid64} steamid64
* @returns {Promise<ParsedInventoryItem[]>}
*/
const addFloatsToParsedInventory = async (parsedInventory, steamid64) => {
const parsedInventoryWithFloats = [];
for (const item of parsedInventory) {
try {
if (!item.inspect_url) {
console.error('No inspect url found for item:', item.market_hash_name);
continue;
}
// replace %owner_steamid% with the steamid64 of the user
// and %assetid% with the assetid of the item
const parsedInspectUrl = item.inspect_url.replace('%owner_steamid%', steamid64).replace('%assetid%', item.assetid);
const response = await axios.get(
'https://csinventoryapi.com/api/v1/inspect?api_key=' + API_KEY + '&url=' + parsedInspectUrl
);
const { floatvalue, paintseed, paintindex } = response.data.iteminfo;
parsedInventoryWithFloats.push({
...item,
inspect_url: parsedInspectUrl,
floatvalue,
paintseed,
paintindex
});
await new Promise(r => setTimeout(r, 10000));
console.log('Fetched float values for item:', item.market_hash_name);
} catch (error) {
console.log(error);
console.error('Failed to fetch float values for item:', item.market_hash_name);
}
}
return parsedInventoryWithFloats;
}
const getInventoryWithFloats = async (steamid64) => {
const inventory = await getUserInventory(steamid64);
const parsedInventory = await parseInventory(inventory);
const inventoryWithFloats = await addFloatsToParsedInventory(parsedInventory, steamid64);
console.log(inventoryWithFloats);
}
// Change the steamid64 to the user you want to fetch the inventory of
getInventoryWithFloats('76561198166295458');