This code efficiently retrieves geolocation data for a list of destinations, leveraging a cache to speed up subsequent requests and reduce API calls.
npm run import -- "cache locations nearby"
var fs = require('fs');
var unidecode = require('unidecode');
var importer = require('../Core');
var placesNearby = importer.import("places nearby api");
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var PROJECT_PATH = PROFILE_PATH + '/Collections/timeline';
var nearbyCache;
// cache resturants and locations that don't change often, saves geolocation api calls
try {
nearbyCache = JSON.parse(fs.readFileSync(PROJECT_PATH + '/geolocations.json').toString());
} catch (e) {
nearbyCache = {};
}
function getNearby(destinations) {
return importer.runAllPromises(destinations
.filter(d => !d.traveling)
.map(d => resolve => {
if (typeof nearbyCache[d.name + ', ' + d.location] !== 'undefined') {
return resolve(nearbyCache[d.name + ', ' + d.location]);
}
console.log(d.name + ', ' + d.location);
return placesNearby(
unidecode(d.name + ', ' + d.location),
{lat: d.averageLat, lng: d.averageLon})
.then(result => {
if (result.length === 0) {
console.warn('No match for ' + JSON.stringify(d))
resolve();
} else {
nearbyCache[d.name + ', ' + d.location] = result[0];
resolve(Object.assign(d, result[0]))
}
})
.catch(e => resolve(e))
}))
.then(r => {
fs.writeFileSync(
PROJECT_PATH + '/geolocations.json',
JSON.stringify(nearbyCache, null, 4));
return r.filter(l => typeof l !== 'undefined');
})
}
module.exports = getNearby;
const fs = require('fs').promises;
const unidecode = require('unidecode');
const importer = require('../Core');
const placesNearby = importer.import('places nearby api');
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const PROJECT_PATH = `${PROFILE_PATH}/Collections/timeline`;
let nearbyCache;
try {
nearbyCache = JSON.parse(await fs.readFile(`${PROJECT_PATH}/geolocations.json`, 'utf8'));
} catch (e) {
nearbyCache = {};
}
/**
* Get nearby locations for destinations.
*
* @param {Array
This code fetches nearby places for a list of destinations, caching the results to improve performance.
Here's a breakdown:
Dependencies:
fs
: Node.js built-in module for file system operations.unidecode
: Library for converting Unicode characters to ASCII.importer
: Custom module likely responsible for importing other functions or modules.placesNearby
: Function imported from importer
to fetch nearby places using the Google Places API.Configuration:
PROFILE_PATH
: Path to the user's home directory.PROJECT_PATH
: Path to a directory where the code stores cached geolocation data.nearbyCache
: An object to store cached geolocation data. It's loaded from a JSON file if it exists, otherwise, it's initialized as an empty object.getNearby
Function:
destinations
as input, each destination having name
, location
, averageLat
, and averageLon
properties.traveling
.placesNearby
to fetch the geolocation data from the API.nearbyCache
and resolves with the destination object augmented with the geolocation data.undefined
.importer.runAllPromises
to execute all the promises concurrently.nearbyCache
to the JSON file.undefined
values from the results and returns the array of destinations with geolocation data.Export:
getNearby
function is exported as a module, making it available for use in other parts of the application.Let me know if you have any other code snippets you'd like me to explain!