-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.js
117 lines (106 loc) · 4.45 KB
/
inventory.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
import request from "request";
import Axios from "axios";
var API = {
getInventory: (user_id) => {
return new Promise((resolve, reject) => {
var types = [8,18,19,41,42,43,44,45,46,47];
var left = types.length;
var inventory = [];
var pages = [];
for (var i = 0; i < types.length; i++) {
pages[i] = [];
getPage(types[i], 1);
}
function getPage(type, num, maxpages, tries=1) {
request.get('https://www.roblox.com/Trade/inventoryhandler.ashx?userid=' + user_id + '&page=' + num.toString() + '&itemsPerPage=25&assetTypeId=' + type.toString(), {timeout:5000}, function(err, resp, body) {
if(err) {
if(tries == 5) return complete(type);
return getPage(type, num, maxpages, tries+1);
}
var json = JSON.parse(body);
if(!(json.msg == 'Inventory retreived!'))
return complete(type);
if(num == 1) {
var maxpages = Math.ceil(json.data.totalNumber/25);
if(maxpages == 1)
return push(json, function() { complete(type); });
push(json, function() {
for (var i = 2; i <= maxpages; i++) {
pages[types.indexOf(type)].push(i);
getPage(type, i)
}
});
} else {
push(json, function() {
pageComplete(type, num);
});
}
});
}
function push(json, cb) {
var left_to_process = json.data.InventoryItems.length;
json.data.InventoryItems.forEach(function(item) {
inventory.push({
name: item.Name,
rap: parseInt(item.AveragePrice),
uaid: item.UserAssetID,
link: item.ItemLink
});
if(!--left_to_process) {
cb();
}
});
}
function pageComplete(type, page) {
pages[types.indexOf(type)].splice(pages[types.indexOf(type)].indexOf(page), 1);
if(!pages[types.indexOf(type)].length) { complete(type); }
}
function complete(type) {
if(!--left) { resolve(inventory); }
}
})
},
getAssetOwners: (asset_id) => {
return new Promise((resolve, reject) => {
var owners = [];
function getPage(asset_id, page_cursor, attempts) {
console.log(owners.length);
var url = "https://inventory.roblox.com/v1/assets/"+asset_id.toString()+"/owners?sortOrder=Asc&limit=100" + (page_cursor?"&cursor=" + page_cursor:"");
Axios.get(url).then((response) => {
return (response.data.nextPageCursor == null)
? push(response.data, () => { resolve(owners); })
: push(response.data, () => { getPage(asset_id, response.data.nextPageCursor, 1); });
})
.catch((err) => {
console.log(err);
return (attempts == 3) ? resolve(owners) : getPage(asset_id, page_cursor, attempts+1);
});
}
function push(json, callback) {
var left_to_process = json.data.length;
json.data.forEach(function(item) {
if(item.owner != null)
owners.push({
uaid: item.userAssetID,
serial: item.serialNumber,
owner: item.owner.userId
});
if(!--left_to_process) {
callback();
}
});
}
getPage(asset_id, undefined, 1);
});
},
getLimiteds: () => {
return new Promise((resolve, reject) => {
API.getInventory(1).then((inventory) => {
for(var i = 0; i < inventory.length; i++)
inventory[i].id = parseInt(inventory[i].split('?id=')[1]);
resolve(inventory);
});
});
}
}
module.exports = API;