-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (72 loc) · 2.93 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
import * as items from './lib/items.js';
import * as rolimons from "./lib/rolimons.js";
import db from "./lib/db.js";
import {sleep, repeat, print} from "./lib/globals.js";
import {Worker} from 'node:worker_threads';
const getAllItems = async () => (await db.get('items')) || {};
const updateMissingItems = async () => {
const itemsStored = Object.keys(await getAllItems());
const ollieItems = Object.values(items.getOllieData());
const missingItems = ollieItems
.filter(ollieItem => !itemsStored.find(item => Number(item) === ollieItem.id));
for (const missingItem of missingItems) {
print(`Scraping missing data for "${missingItem.name}" (${missingItem.id})...`);
// fetch all points for the item from rolimons
const allPoints = await rolimons.fetchItemSales(missingItem.id);
if (!allPoints) {
print(`Failed to scrape data for "${missingItem.name}" (${missingItem.id})`);
continue;
}
// condense each array into a single array of objects
const mappedPoints = allPoints.timestamp_list.map((point, index) => ({
saleId: allPoints.sale_id_list[index],
timestamp: point,
rap: allPoints.sale_rap_list[index],
// value: allPoints.sale_value_list[index],
price: allPoints.sale_price_list[index],
}));
// store the data for later use
await db.set(`items.${missingItem.id}.points`, mappedPoints);
print(`Saved ${mappedPoints.length} points for "${missingItem.name}" (${missingItem.id})...`);
// courtesy sleep yw rolimon
await sleep(4 * 1000);
}
};
const handleNewSales = async () => {
const newSales = await rolimons.fetchAllRecentSales();
if (!newSales) return;
for (const saleData of newSales) {
const [
timestamp,
_unknown,
itemId,
oldRap,
newRap,
saleId,
] = saleData;
const itemSales = await db.get(`items.${itemId}.points`);
if (!itemSales) {
print(`Found sale for ${itemId}, but previous data hasn't been scraped yet`);
continue;
}
// check if sale is a duplicate
if (itemSales.find(sale => sale.saleId === saleId)) continue;
const estimatedPrice = oldRap + (newRap - oldRap) * 10;
await db.push(`items.${itemId}.points`, {
saleId,
timestamp,
rap: newRap,
price: estimatedPrice,
});
print(`Found new sale for ${itemId} (${oldRap.toLocaleString()} -> ${newRap.toLocaleString()} | approx. ${estimatedPrice.toLocaleString()})`);
}
};
const main = async () => {
print(`Fetching data from api.danny.ink...`);
await items.refreshOllieData();
repeat(items.refreshOllieData, 60 * 1000);
repeat(updateMissingItems, 5 * 60 * 1000);
repeat(handleNewSales, 20 * 1000);
}
main();
new Worker('./lib/server.js')