vizwall_screensaver/server.js

115 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-04-28 17:40:17 -04:00
const express = require('express');
const http = require('http');
const fs = require('fs');
const fetch = require('isomorphic-fetch');
const path = require('path');
require('dotenv').config();
const msGraphHelper = require('./msGraphHelper');
const settings = require('./settings');
const HTTP_PORT = process.env.HTTP_PORT || 8080;
const app = express();
const slideshowDir = './slideshow';
app.use(express.static(path.join(__dirname, 'public')));
2024-04-28 17:40:17 -04:00
app.use(express.json());
app.use(express.static('public'));
msGraphHelper.initializeGraphForAppOnlyAuth(settings);
2024-04-28 17:40:17 -04:00
const data = fs.readFileSync('./resourceUPNs.json', 'utf8');
const resourceUPNs = JSON.parse(data);
app.get('/api/events', async (req, res) => {
try {
let now = new Date();
let startDateTime = new Date(now);
// startDateTime.setHours(8, 0, 0, 0);
2024-04-28 17:40:17 -04:00
let endDateTime = new Date(now);
endDateTime.setHours(20, 0, 0, 0);
2024-04-28 17:40:17 -04:00
if (startDateTime > endDateTime) {
// Exit early since there are no relevant events to display
return res.json({});
}
let events = {};
2024-04-28 17:40:17 -04:00
for (const resource of resourceUPNs) {
try {
const data =
await msGraphHelper.getCalendarView(resource, startDateTime.toISOString(), endDateTime.toISOString());
const resourceEvents = data;
2024-04-28 17:40:17 -04:00
events[resource] = resourceEvents;
} catch (error) {
console.error(error);
}
}
res.json(events);
} catch (err) {
res.status(500).json({ message: err.message });
}
});
app.get('/api/weather', async (req, res) => {
try {
const lat = 45.49;
const lon = -73.58;
const appId = process.env.OPENWEATHER_APPID;
const url = `https://api.openweathermap.org/data/2.5/weather?units=metric&lat=${lat}&lon=${lon}&appid=${appId}`;
2024-04-28 17:40:17 -04:00
const response = await fetch(url);
if (response.ok) {
const json = await response.json();
res.json(json);
} else {
res.status(500).json({ message: 'Error in /weather' });
}
} catch (err) {
res.status(500).json({ message: err.message });
}
});
app.get('/api/quotes', async (req, res) => {
try {
const url = `https://api.quotable.io/quotes?limit=150&tags=urban|city|technology|future`
const response = await fetch(url);
if (response.ok) {
const json = await response.json();
res.json(json);
} else {
res.status(500).json({ message: 'Error in /quotes' });
}
} catch (err) {
res.status(500).json({ message: err.message });
}
});
app.use('/slideshow', express.static(slideshowDir));
app.get('/api/slideshow', (req, res) => {
fs.readdir(slideshowDir, (err, files) => {
if (err) {
console.error('Failed to list slideshow images: ', err.message);
res.status(500).json({ message: err.message });
return;
} else {
const imagePaths = files
.filter(file => /\.(jpg|jpeg|png|gif)$/i.test(file))
.map(file => `/slideshow/${file}`);
res.json(imagePaths);
}
});
});
const httpServer = http.createServer(app);
httpServer.listen(HTTP_PORT, () => console.log(`HTTP Server listening on http://localhost:${HTTP_PORT}`));