From 1e10109bbe90f9a3da23a7939f0a53ea2baf62f9 Mon Sep 17 00:00:00 2001 From: sora-ext Date: Mon, 24 Feb 2025 15:12:46 +0100 Subject: [PATCH] Update router/api.js --- router/api.js | 84 +++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/router/api.js b/router/api.js index 1d8e13a..1a8dfe3 100644 --- a/router/api.js +++ b/router/api.js @@ -1,37 +1,23 @@ -const axios = require("axios"); - module.exports = (fastify, opts, done) => { fastify.get("/flight/:id", async (req, reply) => { - const ENDPOINT = "https://www.flightradar24.com/v1/search/web/find"; - const FORMAT = "-"; if (req.params.id) { - axios - .get(ENDPOINT, { - params: { - format: FORMAT, - query: req.params.id, - limit: 16, - type: "schedule", - }, - }) - .then((res) => reply.send(res.data)); + 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.data)); } else { return reply.send([]); } return reply; }); fastify.get("/place/:id", async (req, reply) => { - const ENDPOINT = "https://nominatim.openstreetmap.org/"; - const FORMAT = "jsonv2"; if (req.params.id) { - axios - .get(ENDPOINT, { - params: { - format: FORMAT, - q: req.params.id, - }, - }) - .then((res) => reply.send(res.data)); + const url = new URL("https://nominatim.openstreetmap.org/"); + url.searchParams.append('format', 'jsonv2') + url.searchParams.append('q', req.params.id) + fetch(url).then((res) => reply.send(res.data)); } else { return reply.send([]); } @@ -40,33 +26,33 @@ module.exports = (fastify, opts, done) => { fastify.get("/gpx/:id", async (req, reply) => { if (req.params.id == undefined) - return reply.code(400).send({ error: "No ID query parameter" }); + 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 = '' - const data = JSON.parse(val); - const gen_wpt = (name,desc,latlon,icon="Flag") => `0${name}-${desc}${icon}` - const esc_str = (str) => (str||"Undefined").replace('"',""").replace("'","'").replace("<","<").replace(">",">").replace("&","&").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"); + fastify.level.db.get(req.params.id, (err, val) => { + if (err) { + console.warn(err); + reply.code(500).send(); + } else { + let file = '' + const data = JSON.parse(val); + const gen_wpt = (name, desc, latlon, icon = "Flag") => `0${name}-${desc}${icon}` + const esc_str = (str) => (str || "Undefined").replace('"', """).replace("'", "'").replace("<", "<").replace(">", ">").replace("&", "&").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"); + }); }); - a.places.activities.forEach(b => { - file+= gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, icon="Tree"); - }); - }); - file+=""; - reply.header('Content-Type', 'application/gpx+xml'); - reply.header('Content-Disposition', `attachment; filename=${req.params.id}.gpx`); - reply.send(file); - } - }); - return reply; + file += ""; + 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) => {