Env Vars & Geocode Data Caching
This commit is contained in:
@@ -1,4 +1,59 @@
|
||||
const https = require('https');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const CACHE_DIR = path.join(__dirname, '..', 'cache');
|
||||
|
||||
// Ensure cache directory exists
|
||||
if (!fs.existsSync(CACHE_DIR)) {
|
||||
fs.mkdirSync(CACHE_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a safe filename from a city query
|
||||
* @param {string} cityQuery - City name
|
||||
* @returns {string} Safe filename
|
||||
*/
|
||||
function getCacheFileName(cityQuery) {
|
||||
const hash = crypto.createHash('md5').update(cityQuery.toLowerCase().trim()).digest('hex');
|
||||
return `geocode_${hash}.json`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached geocode data if available
|
||||
* @param {string} cityQuery - City name
|
||||
* @returns {Object|null} Cached data or null
|
||||
*/
|
||||
function getCachedGeocode(cityQuery) {
|
||||
try {
|
||||
const cacheFile = path.join(CACHE_DIR, getCacheFileName(cityQuery));
|
||||
if (fs.existsSync(cacheFile)) {
|
||||
const data = fs.readFileSync(cacheFile, 'utf8');
|
||||
const cached = JSON.parse(data);
|
||||
console.log(`Using cached geocode for: ${cityQuery}`);
|
||||
return cached;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Cache read error for ${cityQuery}:`, error.message);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save geocode data to cache
|
||||
* @param {string} cityQuery - City name
|
||||
* @param {Object} data - Geocode data to cache
|
||||
*/
|
||||
function saveCachedGeocode(cityQuery, data) {
|
||||
try {
|
||||
const cacheFile = path.join(CACHE_DIR, getCacheFileName(cityQuery));
|
||||
fs.writeFileSync(cacheFile, JSON.stringify(data, null, 2), 'utf8');
|
||||
console.log(`Cached geocode for: ${cityQuery}`);
|
||||
} catch (error) {
|
||||
console.warn(`Cache write error for ${cityQuery}:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Geocode city to lat/lon using Nominatim (OpenStreetMap)
|
||||
@@ -6,6 +61,11 @@ const https = require('https');
|
||||
* @returns {Promise<{lat: number, lon: number, displayName: string}>}
|
||||
*/
|
||||
async function geocodeCity(cityQuery) {
|
||||
// Check cache first
|
||||
const cached = getCachedGeocode(cityQuery);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const encodedQuery = encodeURIComponent(cityQuery);
|
||||
const url = `https://nominatim.openstreetmap.org/search?q=${encodedQuery}&format=json&limit=1`;
|
||||
@@ -27,11 +87,14 @@ async function geocodeCity(cityQuery) {
|
||||
try {
|
||||
const results = JSON.parse(data);
|
||||
if (results && results.length > 0) {
|
||||
resolve({
|
||||
const geocodeResult = {
|
||||
lat: parseFloat(results[0].lat),
|
||||
lon: parseFloat(results[0].lon),
|
||||
displayName: results[0].display_name
|
||||
});
|
||||
};
|
||||
// Save to cache
|
||||
saveCachedGeocode(cityQuery, geocodeResult);
|
||||
resolve(geocodeResult);
|
||||
} else {
|
||||
reject(new Error('No results found'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user