-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
278 lines (251 loc) · 8.66 KB
/
Copy pathserver.js
File metadata and controls
278 lines (251 loc) · 8.66 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import express from 'express';
import morgan from 'morgan';
import dotenv from 'dotenv';
import path from 'path';
import fs from 'fs/promises';
import { fileURLToPath } from 'url';
import { fetch } from 'undici';
import { parse } from 'csv-parse/sync';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3000;
app.use(morgan('dev'));
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// Utilities
const toRad = (deg) => (deg * Math.PI) / 180;
function haversineKm(lat1, lon1, lat2, lon2) {
const R = 6371; // km
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
function walkingMinutes(km) {
const avgKmPerMin = 5 / 60; // 5 km/h walking speed
return Math.round((km / avgKmPerMin));
}
async function geocode(address) {
if (!address) return null;
try {
const url = new URL('https://nominatim.openstreetmap.org/search');
url.searchParams.set('q', address);
url.searchParams.set('format', 'json');
url.searchParams.set('limit', '1');
const res = await fetch(url, {
headers: { 'User-Agent': 'VibeEvent/0.1 (demo)' }
});
if (!res.ok) return null;
const data = await res.json();
if (data && data.length > 0) {
return { lat: parseFloat(data[0].lat), lng: parseFloat(data[0].lon) };
}
return null;
} catch (e) {
return null;
}
}
// Data sources
async function fetchLumaEvents() {
const apiKey = process.env.LUMA_API_KEY;
if (!apiKey) return [];
try {
// Minimal example: fetch attending events; adjust endpoint per your org needs
const res = await fetch('https://public-api.luma.com/api/v1/events', {
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (!res.ok) {
console.warn('Luma API error', res.status);
return [];
}
const payload = await res.json();
// Attempt common shapes; fallback to []
const items = payload?.data || payload?.events || [];
return items.map((e) => ({
source: 'luma',
raw: e
}));
} catch (e) {
console.warn('Luma fetch failed', e.message);
return [];
}
}
async function readLumaCSV() {
try {
const csvPath = path.join(__dirname, 'data', 'luma_food_events_with_acceptance.csv');
const content = await fs.readFile(csvPath, 'utf-8');
const records = parse(content, {
columns: true,
skip_empty_lines: true,
trim: true
});
return records.map((row) => ({
source: 'luma_csv',
raw: {
name: row.Description || '',
hostedBy: row['Hosted by'] || '',
dateStr: row.Date || '',
location: row.Location || '',
acceptance: row.Acceptance || 'Pending'
}
}));
} catch (e) {
console.warn('Failed to read CSV:', e.message);
return [];
}
}
async function readPartifulMock() {
try {
const f = await fs.readFile(path.join(__dirname, 'data', 'partiful.mock.json'), 'utf-8');
const json = JSON.parse(f);
return (json.events || []).map((e) => ({ source: 'partiful', raw: e }));
} catch (e) {
return [];
}
}
function parseStatus(tagsOrStatus) {
if (!tagsOrStatus) return 'pending';
const s = (Array.isArray(tagsOrStatus) ? tagsOrStatus.join(' ') : String(tagsOrStatus)).toLowerCase();
if (s.includes('approved') || s.includes('accepted') || s.includes('rsvp:yes') || s.includes('going')) return 'accepted';
if (s.includes('not approved')) return 'rejected';
if (s.includes('waitlist') || s.includes('pending')) return 'pending';
return 'pending';
}
function parseDateString(dateStr) {
// Parse "Oct 9, Thursday, 6:00 PM" format
if (!dateStr) return null;
try {
// Extract month, day, time
const match = dateStr.match(/(\w+)\s+(\d+),\s+\w+,\s+(.+)/);
if (!match) return null;
const [, month, day, time] = match;
const year = 2025; // Assuming 2025 for now
const dateTimeStr = `${month} ${day}, ${year} ${time}`;
const parsed = new Date(dateTimeStr);
return isNaN(parsed.getTime()) ? null : parsed.toISOString();
} catch (e) {
return null;
}
}
async function normalizeEvent(item) {
if (item.source === 'luma_csv') {
const e = item.raw;
const name = e.name || 'Event';
const start = parseDateString(e.dateStr);
const category = 'Food & Social'; // All CSV events are food-related
const locationText = e.location || '';
const status = parseStatus(e.acceptance);
let lat = null;
let lng = null;
if (locationText) {
const geo = await geocode(locationText + ', San Francisco Bay Area');
if (geo) { lat = geo.lat; lng = geo.lng; }
}
return { name, start, category, location: locationText, status, lat, lng, hostedBy: e.hostedBy };
}
if (item.source === 'luma') {
const e = item.raw;
const name = e?.name || e?.title || 'Luma Event';
const start = e?.start_at || e?.starts_at || e?.start || null;
const category = Array.isArray(e?.tags) ? e.tags.join(', ') : (e?.category || '');
const locationText = e?.location?.name || e?.location?.address || e?.location || '';
const status = parseStatus(e?.rsvp_status || e?.attendance_status || e?.tags);
let lat = parseFloat(e?.location?.lat);
let lng = parseFloat(e?.location?.lng);
if (!(Number.isFinite(lat) && Number.isFinite(lng)) && locationText) {
const geo = await geocode(locationText);
if (geo) { lat = geo.lat; lng = geo.lng; }
}
return { name, start, category, location: locationText, status, lat, lng };
}
if (item.source === 'partiful') {
const e = item.raw;
const name = e.name;
const start = e.start;
const category = e.category || '';
const locationText = e.location || e.city || '';
const status = e.status || 'pending';
let { lat, lng } = e;
if (!(Number.isFinite(lat) && Number.isFinite(lng)) && locationText) {
const geo = await geocode(locationText);
if (geo) { lat = geo.lat; lng = geo.lng; }
}
return { name, start, category, location: locationText, status, lat, lng };
}
return null;
}
function withDistance(events, userLat, userLng) {
if (!(Number.isFinite(userLat) && Number.isFinite(userLng))) return events;
return events.map((ev) => {
let distance_km = null;
if (Number.isFinite(ev.lat) && Number.isFinite(ev.lng)) {
distance_km = Math.round(haversineKm(userLat, userLng, ev.lat, ev.lng) * 10) / 10;
}
return { ...ev, distance_km };
});
}
app.get('/api/events', async (req, res) => {
try {
const userLat = parseFloat(req.query.lat);
const userLng = parseFloat(req.query.lng);
const [csvItems, lumaItems, partifulItems] = await Promise.all([
readLumaCSV(),
fetchLumaEvents(),
readPartifulMock()
]);
const normalized = (await Promise.all([...csvItems, ...lumaItems, ...partifulItems].map(normalizeEvent)))
.filter(Boolean);
const enriched = withDistance(normalized, userLat, userLng)
.map((e) => ({
name: e.name,
start: e.start,
category: e.category,
location: e.location,
status: e.status,
lat: e.lat,
lng: e.lng,
distance_km: e.distance_km
}));
// Sort by start then distance
enriched.sort((a, b) => {
const at = a.start ? Date.parse(a.start) : 0;
const bt = b.start ? Date.parse(b.start) : 0;
if (at !== bt) return at - bt;
const ad = a.distance_km ?? Infinity;
const bd = b.distance_km ?? Infinity;
return ad - bd;
});
res.json({ events: enriched });
} catch (e) {
console.error(e);
res.status(500).json({ error: 'Failed to load events' });
}
});
app.get('/api/distance', (req, res) => {
const fromLat = parseFloat(req.query.fromLat);
const fromLng = parseFloat(req.query.fromLng);
const toLat = parseFloat(req.query.toLat);
const toLng = parseFloat(req.query.toLng);
if (![fromLat, fromLng, toLat, toLng].every(Number.isFinite)) {
return res.status(400).json({ error: 'fromLat, fromLng, toLat, toLng are required as numbers' });
}
const km = haversineKm(fromLat, fromLng, toLat, toLng);
const minutes = walkingMinutes(km);
res.json({ distance_km: Math.round(km * 10) / 10, walking_minutes: minutes });
});
app.get('/health', (req, res) => res.json({ ok: true }));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});