OTM/router/api.js
soraefir 0cc9d235ed
All checks were successful
continuous-integration/drone/push Build is passing
Better nominatim
2025-02-25 01:38:34 +01:00

100 lines
3.7 KiB
JavaScript

module.exports = (fastify, opts, done) => {
fastify.get("/flight/:id", async (req, reply) => {
if (req.params.id) {
const url = new URL("https://www.flightradar24.com/v1/search/web/find");
url.searchParams.append('format', '-')
url.searchParams.append('query', req.params.id)
url.searchParams.append('limit', 16)
url.searchParams.append('type', 'schedule')
fetch(url).then((res) => reply.send(res.json()));
} else {
return reply.send([]);
}
return reply;
});
fastify.get("/place/:id", async (req, reply) => {
if (req.params.id) {
const url = new URL("https://nominatim.openstreetmap.org/search");
url.searchParams.append('format', 'jsonv2')
url.searchParams.append('q', req.params.id)
let bb = JSON.parse(req.query.bb)
if(bb){
url.searchParams.append('viewbox', `${bb[0][0]},${bb[0][1]},${bb[1][0]},${bb[1][1]}`)
url.searchParams.append('bounded', 1)
}
fetch(url).then((res) => {
if( !res.ok) throw new Error("Nominatim Error")
return res.json()
}).then(res=>{
reply.send(res)});
} else {
return reply.send([]);
}
return reply;
});
fastify.get("/gpx/:id", async (req, reply) => {
if (req.params.id == undefined)
return reply.code(400).send({ error: "No ID query parameter" });
fastify.level.db.get(req.params.id, (err, val) => {
if (err) {
console.warn(err);
reply.code(500).send();
} else {
let file = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="EMTAC BTGPS Trine II DataLog Dump 1.0 - http://www.ayeltd.biz" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">'
const data = JSON.parse(val);
const gen_wpt = (name, desc, latlon, icon = "Flag") => `<wpt lat="${latlon[0]}" lon="${latlon[1]}"><ele>0</ele><name>${name}</name><cmt>-</cmt><desc>${desc}</desc><sym>${icon}</sym></wpt>`
const esc_str = (str) => (str || "Undefined").replace('"', "&quot;").replace("'", "&apos;").replace("<", "&lt;").replace(">", "&gt;").replace("&", "&amp;").replace("\n", "...")
data.main.forEach(a => {
file += gen_wpt(esc_str(a.hotel.name), esc_str(a.hotel.notes), a.hotel.latlon, icon = "Hotel");
a.places.restaurants.forEach(b => {
file += gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, icon = "Restaurant");
});
a.places.activities.forEach(b => {
file += gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, icon = "Tree");
});
});
file += "</gpx>";
reply.header('Content-Type', 'application/gpx+xml');
reply.header('Content-Disposition', `attachment; filename=${req.params.id}.gpx`);
reply.send(file);
}
});
return reply;
});
fastify.get("/:id", async (req, reply) => {
if (req.params.id == undefined)
return reply.code(400).send({ error: "No ID query parameter" });
fastify.level.db.get(req.params.id, (err, val) => {
if (err) {
console.warn(err);
reply.code(500).send();
} else {
reply.send(JSON.parse(val));
}
});
return reply;
});
fastify.post("/:id", async (req, reply) => {
if (req.params.id == undefined)
return reply.code(400).send({ error: "No ID query parameter" });
fastify.level.db.put(req.params.id, req.body, (err) => {
if (err) {
console.warn(err);
reply.code(500).send({ error: "Error with DB" });
} else {
reply.send({ content: "ok" });
}
});
return reply;
});
done();
};