Update router/api.js
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
sora-ext 2025-02-24 15:12:46 +01:00
parent 8a46f473eb
commit 1e10109bbe

View File

@ -1,37 +1,23 @@
const axios = require("axios");
module.exports = (fastify, opts, done) => { module.exports = (fastify, opts, done) => {
fastify.get("/flight/:id", async (req, reply) => { fastify.get("/flight/:id", async (req, reply) => {
const ENDPOINT = "https://www.flightradar24.com/v1/search/web/find";
const FORMAT = "-";
if (req.params.id) { if (req.params.id) {
axios const url = new URL("https://www.flightradar24.com/v1/search/web/find");
.get(ENDPOINT, { url.searchParams.append('format', '-')
params: { url.searchParams.append('query', req.params.id)
format: FORMAT, url.searchParams.append('limit', 16)
query: req.params.id, url.searchParams.append('type', 'schedule')
limit: 16, fetch(url).then((res) => reply.send(res.data));
type: "schedule",
},
})
.then((res) => reply.send(res.data));
} else { } else {
return reply.send([]); return reply.send([]);
} }
return reply; return reply;
}); });
fastify.get("/place/:id", async (req, reply) => { fastify.get("/place/:id", async (req, reply) => {
const ENDPOINT = "https://nominatim.openstreetmap.org/";
const FORMAT = "jsonv2";
if (req.params.id) { if (req.params.id) {
axios const url = new URL("https://nominatim.openstreetmap.org/");
.get(ENDPOINT, { url.searchParams.append('format', 'jsonv2')
params: { url.searchParams.append('q', req.params.id)
format: FORMAT, fetch(url).then((res) => reply.send(res.data));
q: req.params.id,
},
})
.then((res) => reply.send(res.data));
} else { } else {
return reply.send([]); return reply.send([]);
} }
@ -49,18 +35,18 @@ module.exports = (fastify, opts, done) => {
} else { } 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">' 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 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 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","...") const esc_str = (str) => (str || "Undefined").replace('"', "&quot;").replace("'", "&apos;").replace("<", "&lt;").replace(">", "&gt;").replace("&", "&amp;").replace("\n", "...")
data.main.forEach(a => { data.main.forEach(a => {
file+= gen_wpt(esc_str(a.hotel.name), esc_str(a.hotel.notes), a.hotel.latlon, icon="Hotel"); file += gen_wpt(esc_str(a.hotel.name), esc_str(a.hotel.notes), a.hotel.latlon, icon = "Hotel");
a.places.restaurants.forEach(b => { a.places.restaurants.forEach(b => {
file+= gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, icon="Restaurant"); file += gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, icon = "Restaurant");
}); });
a.places.activities.forEach(b => { a.places.activities.forEach(b => {
file+= gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, icon="Tree"); file += gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, icon = "Tree");
}); });
}); });
file+="</gpx>"; file += "</gpx>";
reply.header('Content-Type', 'application/gpx+xml'); reply.header('Content-Type', 'application/gpx+xml');
reply.header('Content-Disposition', `attachment; filename=${req.params.id}.gpx`); reply.header('Content-Disposition', `attachment; filename=${req.params.id}.gpx`);
reply.send(file); reply.send(file);