-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
87 lines (73 loc) · 1.87 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
'use strict';
const got = require('got');
let URL;
module.exports = function (user, game, gotOptions) {
if (!user) {
throw new TypeError('Please provide a user');
}
game = game || '730/2/';
if (isNaN(Number(user))) {
URL = `http://steamcommunity.com/id/${user}/inventory/json/${game}`;
} else {
URL = `http://steamcommunity.com/profiles/${user}/inventory/json/${game}`;
}
let response = [];
return got(URL, gotOptions).then(res => {
let items;
let desc;
try {
desc = JSON.parse(res.body).rgDescriptions;
items = JSON.parse(res.body).rgInventory;
} catch (e) {
items = [];
desc = {};
}
if (!items) {
return response;
}
Object.keys(items).forEach(key => {
let temp = items[key];
let item = desc[`${temp.classid}_${temp.instanceid}`];
if (!item) {
return response.push({error: true});
}
let data = {
id: temp.id,
amount: temp.amount,
pos: temp.pos,
name: item.name,
marketHashName: item.market_hash_name,
appid: item.appid,
classid: item.classid,
instanceid: item.instanceid,
tradable: item.tradable,
marketable: item.marketable,
marketTradableRestriction: item.market_tradable_restriction,
link: item.actions ? item.actions[0].link : null,
image: `http://steamcommunity-a.akamaihd.net/economy/image/${item.icon_url_large || item.icon_url}`,
category: null,
type: null,
exterior: null,
quality: null,
raw: item,
};
// To match http://steamcommunity.com/market/ filters names
item.tags.forEach(tag => {
if (tag.category === 'Type') {
data.type = tag.name;
}
if (tag.category === 'Weapon') {
data.weapon = tag.name;
}
if (tag.category === 'Quality') {
data.category = tag.name;
}
if (tag.category === 'Exterior') {
data.exterior = tag.name;
}
});
response.push(data);
});
return response;
});
};