-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
169 lines (155 loc) · 5.7 KB
/
app.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
160
161
162
163
164
165
166
167
168
169
import dotenv from 'dotenv';
import axios from 'axios';
import fs from 'fs';
import mongoose from 'mongoose';
import gridfs from 'gridfs-stream';
import { exit } from 'process';
dotenv.config();
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
});
mongoose.Promise = global.Promise;
gridfs.mongo = mongoose.mongo;
var connection = mongoose.connection;
function sleep(ms) { // Steam servers are paranoid about DDoS, so we wait 10s before each new request to their API
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
var appid = process.argv[2]; // App ID of the target game. CSG0 - 730, DotA 2 - 570, PUBG - 578080. You will only have to change here.
var gameDict = {
'730': 'CSGO',
'570': 'DotA2',
'578080': 'PUBG'
}; // Add any other games here. Format - appid: name
if (Object.keys(gameDict).indexOf(String(appid)) == -1) {
console.log('This game is not supported. Refer the README to find out how to add support.');
exit(127);
}
async function getItemNames() {
var itemNames = [];
var start = 100;
var response = await axios.get('https://steamcommunity.com/market/search/render/?start=0&search_descriptions=0&sort_column=default&sort_dir=desc&appid=' + appid + '&norender=1&count=100');
var results = response.data.results;
var total = response.data.total_count;
for (var i of results)
itemNames.push(i.name)
while (itemNames.length < total) {
try {
var response = await axios.get('https://steamcommunity.com/market/search/render/?start=' + start + '&search_descriptions=0&sort_column=default&sort_dir=desc&appid=' + appid + '&norender=1&count=100')
var results = response.data.results;
for (var i of results) {
itemNames.push(i.hash_name);
console.log(i.hash_name);
}
start += 100;
await sleep(0);
}
catch (error) {
if (error.response !== undefined) {
if (error.response.status === 429) {
console.log("Error 429: retrying after 1 minute...");
await sleep(60000);
}
else console.log(error.response);
}
else console.log("There was an error, retrying...");
}
}
console.log('Fetched: ' + itemNames.length);
fs.writeFileSync(gameDict[appid] + 'Names.json', JSON.stringify(itemNames));
return itemNames;
}
async function getItemPriceHistory(name) {
const opts = {
headers: {
cookie: 'steamLoginSecure=' + process.env.STEAM_LOGIN_SECURE,
}
};
name = name.replace("/", "-").replace("&", "((PERCENTAGE))26").replace("+","((PLUS))");
var URI = 'https://steamcommunity.com/market/pricehistory/?appid=' + appid + '&market_hash_name=' + name;
var encodedURL = encodeURI(URI).replace("((PERCENTAGE))", "%").replace("((PLUS))", "%2B");
var success = false;
while (!success) {
try {
console.log('Now fetching: ' + encodedURL);
var response = await axios.get(encodedURL, opts);
var prices = response.data.prices;
success = true;
return prices;
}
catch (error) {
if (error.response !== undefined)
if (error.response.status === 400) {
console.log("There was an error, retrying now... If this persists, please check your STEAM_LOGIN_SECURE cookie.");
}
else console.log("There was an error fetching that link, trying again...");
}
}
}
function writeToGridFS(appid) {
var gfs = gridfs(connection.db);
var options = {
_id: gameDict[appid],
root: 'steamData'
};
gfs.exist(options, function (err, found) {
if (err) console.log(err);
if (found) {
console.log('Data already exists in GridFS, deleting...');
gfs.remove(options, function (err) {
if (err) console.log(err);
console.log('Deleted existing record.');
});
}
else console.log('Data not found in GridFS, creating new record...');
});
var writestream = gfs.createWriteStream({
_id: gameDict[appid],
filename: gameDict[appid] + '.json',
mode: 'w',
content_type: 'application/json',
root: 'steamData'
});
fs.createReadStream(gameDict[appid] + '.json').pipe(writestream);
writestream.on('close', function (file) {
console.log('File Created : ' + file.filename);
});
}
async function run() {
const names = await getItemNames();
const results = {};
console.log('Total number of items: ' + names.length);
for (var name of names) {
if (name != undefined) {
results[name] = await getItemPriceHistory(name);
await sleep(0);
}
}
console.log(results);
console.log(Object.keys(results).length);
fs.writeFileSync(gameDict[appid] + '.json', JSON.stringify(results));
writeToGridFS(appid);
}
async function runWithoutPhase1(appid) {
var raw = fs.readFileSync(gameDict[appid] + 'Names.json');
var names = JSON.parse(raw);
var results = {};
for (var name of names) {
if (name != undefined) {
results[name] = await getItemPriceHistory(name);
await sleep(0);
}
}
console.log(results);
console.log(Object.keys(results).length);
fs.writeFileSync(gameDict[appid] + '.json', JSON.stringify(results));
writeToGridFS(appid);
}
if (process.argv[3] == 'true') {
console.log("Phase 1 skipped, using local item name list...");
runWithoutPhase1(appid);
}
else run();