1
0

Major Log Refactor & Cleanup

This commit is contained in:
2025-11-17 09:09:49 -05:00
parent cb35e05754
commit 96cb36663e
7 changed files with 153 additions and 95 deletions

View File

@@ -1,7 +1,7 @@
const express = require('express');
const { streamHandler } = require('./src/streamHandler');
const { geocodeCity } = require('./src/geocode');
const { getAllMusicFiles } = require('./src/musicPlaylist');
const { getAllMusicFiles, initializeSharedPlaylist } = require('./src/musicPlaylist');
const app = express();
const PORT = process.env.PORT || 3000;
@@ -79,9 +79,11 @@ function buildWeatherUrl(latitude, longitude, settings) {
// Basic stream endpoint (no music)
app.get('/stream', (req, res) => {
const startTime = Date.now();
streamHandler(req, res, {
useMusic: false,
musicPath: MUSIC_PATH
musicPath: MUSIC_PATH,
startTime
});
});
@@ -133,38 +135,38 @@ app.get('/weather', async (req, res) => {
let initialUrl = 'data:text/html,<html><body style="margin:0;padding:0;background:#000"></body></html>';
if (city && city !== 'Toronto, ON, CAN') {
// Try quick geocode first
const geocodePromise = Promise.race([
geocodeCity(city),
// Start geocoding (only call once)
const geocodePromise = geocodeCity(city);
// Try to use quick result if available within 1 second
const quickResult = Promise.race([
geocodePromise,
new Promise((_, reject) => setTimeout(() => reject(new Error('Geocoding timeout')), 1000))
]).then(geoResult => {
console.log(`Geocoded: ${city} -> ${geoResult.displayName}`);
const finalUrl = buildWeatherUrl(geoResult.lat, geoResult.lon, weatherSettings);
console.log(`URL: ${finalUrl}`);
return { url: finalUrl, lat: geoResult.lat, lon: geoResult.lon };
}).catch(error => {
// Continue geocoding in background
return geocodeCity(city).then(geoResult => {
const finalUrl = buildWeatherUrl(geoResult.lat, geoResult.lon, weatherSettings);
console.log(`Geocoding completed: ${geoResult.displayName} (${geoResult.lat}, ${geoResult.lon})`);
console.log(`Final URL: ${finalUrl}`);
return { url: finalUrl, lat: geoResult.lat, lon: geoResult.lon, isLate: true };
}).catch(err => {
console.warn(`Geocoding failed: ${err.message}`);
// Fallback to Toronto
const fallbackUrl = buildWeatherUrl(43.6532, -79.3832, weatherSettings);
return { url: fallbackUrl, lat: 43.6532, lon: -79.3832, isLate: true };
});
]).catch(() => null); // Timeout = null, will use late result
// Build URL from quick result if available
const urlPromise = quickResult.then(geoResult => {
if (geoResult) {
// Got quick result
return buildWeatherUrl(geoResult.lat, geoResult.lon, weatherSettings);
}
return null; // Will use initial black screen
});
// Late geocode promise (reuses the same geocode call)
lateGeocodePromise = geocodePromise.then(geoResult => {
return buildWeatherUrl(geoResult.lat, geoResult.lon, weatherSettings);
}).catch(err => {
console.warn(`Geocoding failed for ${city}, using fallback`);
// Fallback to Toronto
return buildWeatherUrl(43.6532, -79.3832, weatherSettings);
});
lateGeocodePromise = geocodePromise.then(result => result.url);
} else {
// Toronto default
initialUrl = buildWeatherUrl(43.6532, -79.3832, weatherSettings);
console.log(`URL: ${initialUrl}`);
}
console.log(`Stream starting: ${city}`);
const startTime = Date.now();
// Update request query for stream handler
req.query.url = initialUrl;
@@ -177,7 +179,8 @@ app.get('/weather', async (req, res) => {
return streamHandler(req, res, {
useMusic: true,
musicPath: MUSIC_PATH,
lateGeocodePromise
lateGeocodePromise,
startTime
});
});
@@ -189,18 +192,23 @@ app.get('/health', (req, res) => {
// Start server
app.listen(PORT, () => {
console.log(`Webpage to HLS server running on port ${PORT}`);
console.log(`WS4KP weather service on port ${WS4KP_PORT}`);
if (process.env.WS4KP_EXTERNAL_PORT) {
console.log(`WS4KP weather service on port ${process.env.WS4KP_EXTERNAL_PORT}`);
}
console.log(`Usage: http://localhost:${PORT}/stream?url=http://example.com`);
console.log(`Weather: http://localhost:${PORT}/weather?city=YourCity`);
// Pre-validate music files on startup to cache results
if (MUSIC_PATH) {
console.log(`\n🎵 Pre-validating music library at ${MUSIC_PATH}...`);
console.log(`\nInitializing music library at ${MUSIC_PATH}...`);
const validFiles = getAllMusicFiles(MUSIC_PATH);
if (validFiles.length > 0) {
console.log(`Music library ready: ${validFiles.length} valid tracks cached\n`);
console.log(`Music library ready: ${validFiles.length} tracks validated`);
// Initialize shared playlist at startup
initializeSharedPlaylist(MUSIC_PATH);
console.log('');
} else {
console.log(`⚠️ No valid music files found in ${MUSIC_PATH}\n`);
console.log(`Warning: No valid music files found in ${MUSIC_PATH}\n`);
}
}
});