-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-prices.js
More file actions
executable file
·232 lines (187 loc) · 6.82 KB
/
sync-prices.js
File metadata and controls
executable file
·232 lines (187 loc) · 6.82 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env node
/**
* Daily Price Sync Script
* Syncs prices from Skin.Broker for all tracked items
* Run via cron: 0 3 * * * /usr/bin/node /var/www/csfloat-api/sync-prices.js
*/
const winston = require('winston');
const CONFIG = require('./config.js');
const Postgres = require('./lib/postgres');
const gameData = require('./lib/game_data');
winston.level = 'info';
const postgres = new Postgres(CONFIG.database_url, false);
const API_KEY = CONFIG.skinbroker_api_key || 'sbv1UvaYyGLofmw26Huqqadjkqi3TwvwTzPn6mDaRQD3Fta6hUOc';
// Rate limiting: 1000 requests/hour = 1 request per 3.6 seconds
const RATE_LIMIT_MS = 4000; // 4 seconds to be safe
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function syncPricesForItem(marketHashName) {
try {
const url = `https://skin.broker/api/v1/item?marketHashName=${encodeURIComponent(marketHashName)}&key=${API_KEY}`;
const response = await fetch(url);
if (!response.ok) {
winston.warn(`Failed to fetch ${marketHashName}: HTTP ${response.status}`);
return false;
}
const data = await response.json();
if (!data.success) {
winston.warn(`No data for ${marketHashName}`);
return false;
}
// Store prices for each market
const markets = [
{ key: 'buff', name: 'buff163' },
{ key: 'skinport', name: 'skinport' },
{ key: 'marketCsgo', name: 'marketcsgo' },
{ key: 'csfloat', name: 'csfloat' },
{ key: 'steam', name: 'steam' }
];
for (const market of markets) {
if (data.price[market.key]) {
const priceData = data.price[market.key];
await postgres.storePriceSnapshot(
marketHashName,
market.name,
priceData.converted.price,
priceData.count,
{
priceOriginal: priceData.original.price,
currency: priceData.original.currency
}
);
}
}
winston.info(`✓ Synced prices for ${marketHashName}`);
return true;
} catch (e) {
winston.error(`Error syncing ${marketHashName}:`, e.message);
return false;
}
}
async function syncRecentSalesForItem(marketHashName) {
try {
const url = `https://skin.broker/api/v1/item/sales?marketHashName=${encodeURIComponent(marketHashName)}&key=${API_KEY}`;
const response = await fetch(url);
if (!response.ok) {
return false;
}
const data = await response.json();
if (!Array.isArray(data) || data.length === 0) {
return false;
}
// Store recent sales (limit to 10 most recent)
for (const sale of data.slice(0, 10)) {
await postgres.storeRecentSale(marketHashName, {
price: sale.price,
float: sale.float,
pattern: sale.pattern,
stickers: sale.stickers,
date: sale.time,
market: sale.market.name
});
}
winston.info(`✓ Synced ${data.length} sales for ${marketHashName}`);
return true;
} catch (e) {
winston.error(`Error syncing sales for ${marketHashName}:`, e.message);
return false;
}
}
async function getUniqueItemNames() {
// Get unique items from database
const result = await postgres.pool.query(`
SELECT DISTINCT
defindex,
paintindex,
stattrak,
souvenir,
props
FROM items
ORDER BY defindex, paintindex
LIMIT 800
`);
// Convert to market hash names
const itemNames = new Set();
for (const row of result.rows) {
try {
// Reconstruct item name from game data
const item = {
defindex: row.defindex,
paintindex: row.paintindex,
stattrak: row.stattrak,
souvenir: row.souvenir
};
// This requires game data to be loaded
// For now, we'll query items table for existing full names
} catch (e) {
winston.warn(`Failed to process item: ${JSON.stringify(row)}`);
}
}
// Alternative: Get from price_cache table (items already checked)
const cacheResult = await postgres.pool.query(`
SELECT DISTINCT market_hash_name
FROM price_cache
ORDER BY cached_at DESC
LIMIT 500
`);
for (const row of cacheResult.rows) {
itemNames.add(row.market_hash_name);
}
return Array.from(itemNames);
}
async function main() {
winston.info('='.repeat(60));
winston.info('Starting Price Sync...');
winston.info(`Time: ${new Date().toISOString()}`);
winston.info('='.repeat(60));
try {
await postgres.connect();
winston.info('✓ Database connected');
// Get list of items to sync
const itemNames = await getUniqueItemNames();
winston.info(`Found ${itemNames.length} unique items to sync`);
if (itemNames.length === 0) {
winston.warn('No items found to sync. Price cache may be empty.');
winston.info('Items will be added as users check floats.');
process.exit(0);
}
let successCount = 0;
let failCount = 0;
for (let i = 0; i < itemNames.length; i++) {
const itemName = itemNames[i];
winston.info(`[${i + 1}/${itemNames.length}] Processing: ${itemName}`);
// Sync prices
const priceSuccess = await syncPricesForItem(itemName);
if (priceSuccess) {
successCount++;
} else {
failCount++;
}
// Rate limit
await sleep(RATE_LIMIT_MS);
// Sync sales (every 10th item to save API calls)
if (i % 10 === 0) {
await syncRecentSalesForItem(itemName);
await sleep(RATE_LIMIT_MS);
}
// Progress update every 50 items
if ((i + 1) % 50 === 0) {
winston.info(`Progress: ${i + 1}/${itemNames.length} (${successCount} success, ${failCount} failed)`);
}
}
winston.info('='.repeat(60));
winston.info('Price Sync Complete!');
winston.info(`Total: ${itemNames.length} items`);
winston.info(`Success: ${successCount}`);
winston.info(`Failed: ${failCount}`);
winston.info(`Success Rate: ${(successCount / itemNames.length * 100).toFixed(2)}%`);
winston.info('='.repeat(60));
process.exit(0);
} catch (e) {
winston.error('Fatal error during sync:', e);
process.exit(1);
}
}
// Run the sync
main();