-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
162 lines (147 loc) · 5.85 KB
/
server.js
File metadata and controls
162 lines (147 loc) · 5.85 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
const express = require('express');
const path = require('path');
const cron = require('node-cron');
const { initDB, insertPrice, getPriceHistory } = require('./db');
const { fetchPrices, cards } = require('./scraper');
const app = express();
const PORT = process.env.PORT || 5000;
// Set EJS as the templating engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Card images (public domain or placeholder)
const cardImages = {
'Charizard Pokemon Japanese Expansion Pack 1996': '/images/japanese-expansion-pack-1996.jpg',
'Charizard Pokemon Base Set 2': '/images/base-set-2.jpg',
'Charizard Pokemon Base Set': '/images/base-set.jpg',
'Charizard Pokemon Shadowless': '/images/shadowless.jpg',
'Charizard Pokemon First Edition': '/images/first-edition.jpg',
};
// Helper function for logging with timestamp and ANSI-colored level tag.
// Falls back to plain text when stdout isn't a TTY (e.g. PM2 file logs).
const ANSI = process.stdout.isTTY ? {
reset: '\x1b[0m', dim: '\x1b[2m',
green: '\x1b[32m', cyan: '\x1b[36m', red: '\x1b[31m'
} : { reset: '', dim: '', green: '', cyan: '', red: '' };
function logWithTimestamp(type, message) {
const now = new Date().toISOString().replace('T', ' ').replace(/\..+/, '') + ' UTC';
let tag = ' ';
if (type === 'success') tag = `${ANSI.green} OK ${ANSI.reset}`;
else if (type === 'check') tag = `${ANSI.cyan} RUN ${ANSI.reset}`;
else if (type === 'error') tag = `${ANSI.red} ERR ${ANSI.reset}`;
console.log(`${ANSI.dim}[${now}]${ANSI.reset} ${tag} ${message}`);
}
// Helper function to make card names concise for logs
function shortCardName(cardName) {
if (cardName === 'Charizard Pokemon Japanese Expansion Pack 1996') return 'Japanese 1996';
return cardName.replace('Charizard Pokemon ', '');
}
// Initialize DB
initDB();
logWithTimestamp('check', 'Cron job scheduled!');
cron.schedule('0 * * * *', async () => {
try {
logWithTimestamp('check', 'Running scheduled price scrape...');
const prices = await fetchPrices();
prices.forEach(({ card_name, grade, price }) => {
if (!isNaN(price)) {
insertPrice(card_name, grade, price);
logWithTimestamp('success', `${shortCardName(card_name)} ${grade}: ${price}`);
}
});
} catch (err) {
logWithTimestamp('error', `Scheduled scrape failed: ${err}`);
}
});
// Run once on startup, but only if the most recent scrape is older than
// STARTUP_SCRAPE_MAX_AGE_MS. Avoids hammering pricecharting on PM2 restarts.
const STARTUP_SCRAPE_MAX_AGE_MS = 30 * 60 * 1000;
(async () => {
const lastTs = await new Promise(resolve => {
getPriceHistory(cards[0].name, '10', 1, (err, rows) => {
if (err || !rows || rows.length === 0) return resolve(null);
resolve(new Date(rows[0].timestamp + ' UTC').getTime());
});
});
if (lastTs && Date.now() - lastTs < STARTUP_SCRAPE_MAX_AGE_MS) {
logWithTimestamp('check', `Skipping startup scrape — last run ${Math.round((Date.now() - lastTs)/60000)}m ago.`);
return;
}
const prices = await fetchPrices();
prices.forEach(({ card_name, grade, price }) => {
if (!isNaN(price)) {
insertPrice(card_name, grade, price);
logWithTimestamp('success', `${shortCardName(card_name)} ${grade}: ${price}`);
}
});
})();
// Helper to get chart data for all cards
function getAllCardData(callback, limit) {
const grades = ['9', '9.5', '10'];
let results = new Array(cards.length);
let pending = cards.length;
cards.forEach((card, idx) => {
let cardData = {
name: card.name,
image: cardImages[card.name] || '/images/placeholder.jpg',
prices: {},
history: []
};
let gradePending = grades.length;
let allHistories = {};
grades.forEach(grade => {
getPriceHistory(card.name, grade, limit, (err, rows) => {
if (rows && rows.length > 0) {
cardData.prices['grade_' + grade.replace('.', '_')] = `$${Math.round(rows[rows.length-1].price).toLocaleString()}`;
allHistories[grade] = rows.map(r => ({ price: r.price, timestamp: r.timestamp }));
} else {
cardData.prices['grade_' + grade.replace('.', '_')] = 'N/A';
allHistories[grade] = [];
}
gradePending--;
if (gradePending === 0) {
cardData.history9 = allHistories['9'];
cardData.history95 = allHistories['9.5'];
cardData.history10 = allHistories['10'];
results[idx] = cardData;
pending--;
if (pending === 0) callback(results);
}
});
});
});
}
app.get('/', (req, res) => {
// Get time horizon from query (?range=1w|1m|3m|6m|1y|max)
const rangeMap = { '1w': 24*7, '1m': 24*30, '3m': 24*90, '6m': 24*180, '1y': 24*365, 'max': 999999 };
const range = req.query.range || '1m';
const limit = rangeMap[range] || 24*30;
getAllCardData(cards => {
// lastUpdated = max timestamp across all series we already loaded
let lastUpdated = null;
cards.forEach(card => {
['history9', 'history95', 'history10'].forEach(key => {
const rows = card[key];
if (rows && rows.length > 0) {
const ts = rows[rows.length - 1].timestamp;
if (!lastUpdated || ts > lastUpdated) lastUpdated = ts;
}
});
});
res.render('index', { cards, range, lastUpdated });
}, limit);
});
app.get('/healthz', (req, res) => {
res.json({ ok: true, uptime: process.uptime() });
});
// Lightweight endpoint for the client poll — just the latest scrape time.
app.get('/api/last-updated', (req, res) => {
getPriceHistory(cards[0].name, '10', 1, (err, rows) => {
if (err) return res.status(500).json({ error: 'db' });
res.json({ lastUpdated: rows && rows[0] ? rows[0].timestamp : null });
});
});
app.listen(PORT, () => {
logWithTimestamp('success', `Server running on http://localhost:${PORT}`);
});