-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchAlbion2D.js
More file actions
97 lines (80 loc) · 3.15 KB
/
fetchAlbion2D.js
File metadata and controls
97 lines (80 loc) · 3.15 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
import fetch from 'node-fetch';
import fs from 'fs';
import path from 'path';
import { info, error } from './utils/logger.js';
const DATA_FILE = path.join(process.cwd(), 'data', 'prices2d.json');
const LOCATIONS = ['Caerleon', 'Bridgewatch', 'Lymhurst', 'Martlock', 'Thetford', 'Fort Sterling', 'Brecilien'];
const API_URL = (itemId, quality = 1) =>
`https://www.albion-online-data.com/api/v2/stats/prices/${encodeURIComponent(itemId)}.json?locations=${LOCATIONS.join(',')}&qualities=${quality}`;
function normalizeApi(apiData) {
const result = {};
if (!apiData || !Array.isArray(apiData)) return result;
for (const entry of apiData) {
const city = entry.city || entry.location;
if (!city) continue;
if (!result[city]) result[city] = { sell: [], buy: [], updated: null };
if (entry.sell_price_min && entry.sell_price_min > 0) {
result[city].sell.push({ price: entry.sell_price_min, date: entry.sell_price_min_date || null });
}
if (entry.buy_price_max && entry.buy_price_max > 0) {
result[city].buy.push({ price: entry.buy_price_max, date: entry.buy_price_max_date || null });
}
const dates = [entry.sell_price_min_date, entry.buy_price_max_date].filter(Boolean);
for (const d of dates) {
if (!result[city].updated || new Date(d) > new Date(result[city].updated)) {
result[city].updated = d;
}
}
}
for (const city of Object.keys(result)) {
result[city].sell = result[city].sell
.sort((a, b) => new Date(b.date) - new Date(a.date))
.slice(0, 5);
result[city].buy = result[city].buy
.sort((a, b) => new Date(b.date) - new Date(a.date))
.slice(0, 5);
}
return result;
}
export async function fetchPrices(itemId, quality = 1) {
try {
info(`Fetching backend2 API ${itemId} quality=${quality}`);
const url = API_URL(itemId, quality);
console.log(`Fetching URL: ${url}`);
const r = await fetch(url);
if (!r.ok) throw new Error(`API status ${r.status}`);
const json = await r.json();
console.log('API response length:', json.length);
const norm = normalizeApi(json);
const adapted = {};
for (const [city, val] of Object.entries(norm)) {
adapted[city] = {
orden_venta: val.sell.map(s => ({ precio: s.price, fecha: s.date })),
orden_compra: val.buy.map(b => ({ precio: b.price, fecha: b.date })),
actualizado: val.updated,
};
}
// Guardar cache local
try {
let cache = { updated: new Date().toISOString(), precios: {} };
if (fs.existsSync(DATA_FILE)) {
cache = JSON.parse(fs.readFileSync(DATA_FILE, 'utf8'));
}
cache.precios[itemId] = adapted;
cache.updated = new Date().toISOString();
fs.writeFileSync(DATA_FILE, JSON.stringify(cache, null, 2));
} catch (e) {
error('Error writing cache: ' + e.message);
}
return { updated: new Date().toISOString(), precios: adapted };
} catch (err) {
error('fetchPrices backend2 error: ' + err.message);
// fallback a cache local
try {
const raw = fs.readFileSync(DATA_FILE, 'utf8');
return JSON.parse(raw);
} catch (e) {
return { updated: null, precios: {} };
}
}
}