More stability & defaults
This commit is contained in:
48
src/geocode.js
Normal file
48
src/geocode.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const https = require('https');
|
||||
|
||||
/**
|
||||
* Geocode city to lat/lon using Nominatim (OpenStreetMap)
|
||||
* @param {string} cityQuery - City name to geocode
|
||||
* @returns {Promise<{lat: number, lon: number, displayName: string}>}
|
||||
*/
|
||||
async function geocodeCity(cityQuery) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const encodedQuery = encodeURIComponent(cityQuery);
|
||||
const url = `https://nominatim.openstreetmap.org/search?q=${encodedQuery}&format=json&limit=1`;
|
||||
|
||||
const options = {
|
||||
headers: {
|
||||
'User-Agent': 'webpage-to-hls-streaming-app/1.0'
|
||||
}
|
||||
};
|
||||
|
||||
https.get(url, options, (res) => {
|
||||
let data = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const results = JSON.parse(data);
|
||||
if (results && results.length > 0) {
|
||||
resolve({
|
||||
lat: parseFloat(results[0].lat),
|
||||
lon: parseFloat(results[0].lon),
|
||||
displayName: results[0].display_name
|
||||
});
|
||||
} else {
|
||||
reject(new Error('No results found'));
|
||||
}
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
}).on('error', (error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { geocodeCity };
|
||||
Reference in New Issue
Block a user