Skip to content

Commit

Permalink
fix API request
Browse files Browse the repository at this point in the history
  • Loading branch information
iakat committed Aug 20, 2024
1 parent 48bddd0 commit 3320540
Showing 1 changed file with 18 additions and 47 deletions.
65 changes: 18 additions & 47 deletions lua/inject_meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ local args = ngx.req.get_uri_args()
if not args["icao"] then
return
end
-- get icao arg, but escape it.
local icao = ngx.escape_uri(args["icao"])
local unsafe_icao = args["icao"]

local aircraft_types = require "aircraft_types"
local icao_ranges = require "icao_ranges"
Expand All @@ -27,69 +26,42 @@ function find_country_by_hex(hex)
return "??"
end

if icao then
if unsafe_icao then
ngx.log(ngx.ERR, "icao: ", unsafe_icao)
local http = require "resty.http"
local httpc = http.new()
httpc:set_timeout(100)

local res, err = httpc:request_uri("http://reapi-readsb.adsblol.svc.cluster.local:30152/?find_hex=" .. icao, {
local res, err = httpc:request_uri("http://reapi-readsb.adsblol.svc.cluster.local:30152/?find_hex=" .. unsafe_icao, {
ssl_verify = false
})

if err then
ngx.log(ngx.ERR, "API request failed: ", err)
res, err = httpc:request_uri("https://re-api.adsb.lol/?find_hex=" .. icao, {
res, err = httpc:request_uri("https://re-api.adsb.lol/?find_hex=" .. unsafe_icao, {
ssl_verify = false
})
end

local description = "Check out this aircraft."
local max_groundspeed = -1;
local min_lat, min_lon, max_lat, max_lon = false, false, false, false
local safe_icao = ""
ngx.log(ngx.ERR, "res: " .. res.body)
if res and res.status == 200 then
local cjson = require "cjson"
local data = cjson.decode(res.body)
local n_aircraft = #data["aircraft"]
if data["aircraft"] and n_aircraft > 0 then
ngx.log(ngx.ERR, "safe_icao: ", safe_icao)
local aircraft_descriptions = {}
for i, aircraft in ipairs(data["aircraft"]) do
safe_icao = safe_icao .. aircraft["hex"] .. ","
local callsign = aircraft["flight"] or ""
callsign = callsign:gsub("%s+", "")
local reg = aircraft["r"] or ""
local reg_country = find_country_by_hex(aircraft["hex"] or "")
reg = reg_country .. reg

aircraft["lat"] = tonumber(aircraft["lat"]) or false
aircraft["lon"] = tonumber(aircraft["lon"]) or false

local tentative_gs = tonumber(aircraft["gs"]) or -1
if tentative_gs > max_groundspeed then
max_groundspeed = tentative_gs
end

if aircraft["lat"] and aircraft["lon"] then
if not min_lat or not min_lon or not max_lat or not max_lon then
ngx.log(ngx.ERR, "first valid aircraft")
min_lat = aircraft["lat"]
min_lon = aircraft["lon"]
max_lat = aircraft["lat"]
max_lon = aircraft["lon"]
else
ngx.log(ngx.ERR, "not first aircraft")
min_lat = math.min(min_lat, aircraft["lat"])
min_lon = math.min(min_lon, aircraft["lon"])
max_lat = math.max(max_lat, aircraft["lat"])
max_lon = math.max(max_lon, aircraft["lon"])
end
else
ngx.log(ngx.ERR, "invalid lat/lon for aircraft ", aircraft["hex"])
end

ngx.log(ngx.ERR, "groundspeed: ", groundspeed)
ngx.log(ngx.ERR, "ac lat: ", aircraft["lat"])
ngx.log(ngx.ERR, "ac lon: ", aircraft["lon"])


local type = aircraft["t"] or ""
type = aircraft_types[type] and aircraft_types[type][1] or type

Expand Down Expand Up @@ -136,20 +108,19 @@ if icao then
else
ngx.log(ngx.ERR, "API request failed: ", err)
end
if not safe_icao then
ngx.log(ngx.ERR, "No safe_icao found")
return
end
-- otherwise, remove , from the end of safe_icao
safe_icao = safe_icao:gsub(",$", "")

local open_graph_tags = ngx.shared.open_graph_tags
local cache_key = icao
local cache_key = safe_icao


local image_url = "https://api-dev.adsb.lol/0/screenshot/" .. icao .. "?"
if max_groundspeed > 0 then
image_url = image_url .. "gs=" .. max_groundspeed .. "&"
end
if min_lat and min_lon and max_lat and max_lon then
image_url = image_url .. "min_lat=" .. min_lat .. "&min_lon=" .. min_lon .. "&max_lat=" .. max_lat .. "&max_lon=" .. max_lon .. "&"
end
ngx.log(ngx.ERR, "lat/lons: ", min_lat, min_lon, max_lat, max_lon)
-- remove image_url trailing & if found
open_graph_tags:set(cache_key .. ":image", image_url:gsub("&$", ""))
open_graph_tags:set(cache_key .. ":image", "https://api-dev.adsb.lol/0/screenshot/" .. safe_icao)
open_graph_tags:set(cache_key .. ":description", description)
else
ngx.log(ngx.ERR, "No icao parameter in URL")
Expand Down

0 comments on commit 3320540

Please sign in to comment.