osm-restaurants: fix caching, convert center to lat/lon

This commit is contained in:
2022-10-12 10:36:08 +02:00
parent c7a1256040
commit 66b2068d54

View File

@@ -36,9 +36,13 @@ const argv = yargs
type: "number", type: "number",
default: 500, default: 500,
}) })
.option("center", { .option("latitude", {
alias: "c", description: "The latitude coordinates to search around",
description: "The OSM ID to search around", type: "number",
demandOption: true,
})
.option("longitude", {
description: "The latitude coordinates to search around",
type: "number", type: "number",
demandOption: true, demandOption: true,
}) })
@@ -57,10 +61,9 @@ const argv = yargs
const overpassQuery = ` const overpassQuery = `
[out:json]; [out:json];
node(id:${argv.center})->.center;
( (
node(around.center:'${argv.radius}')[amenity=fast_food]; node(around:${argv.radius},${argv.latitude},${argv.longitude})[amenity=fast_food];
node(around.center:'${argv.radius}')[amenity=restaurant]; node(around:${argv.radius},${argv.latitude},${argv.longitude})[amenity=restaurant];
); );
out; out;
`; `;
@@ -83,10 +86,11 @@ const withRestaurants = (callback) => {
const cachePath = getCachePath(overpassQuery); const cachePath = getCachePath(overpassQuery);
if ( if (
fs.existsSync(cachePath) && fs.existsSync(cachePath) &&
new Date() - fs.statSync(cachePath).mtime > CACHE_AGE new Date() - fs.statSync(cachePath).mtime < CACHE_AGE
) )
return JSON.parse(fs.readFileSync(cachePath)); callback(JSON.parse(fs.readFileSync(cachePath)));
else else {
console.log("fetching");
return axios return axios
.post(argv.endpoint, overpassQuery) .post(argv.endpoint, overpassQuery)
.then((response) => { .then((response) => {
@@ -95,6 +99,7 @@ const withRestaurants = (callback) => {
callback(restaurants); callback(restaurants);
}) })
.catch(console.error); .catch(console.error);
}
}; };
withRestaurants((restaurants) => { withRestaurants((restaurants) => {