add rapidapi support

This commit is contained in:
Michael Crenshaw 2020-09-15 18:14:37 +00:00
parent 3264a87c02
commit 0b4a825917
3 changed files with 33 additions and 14 deletions

3
.gitignore vendored
View File

@ -3,4 +3,7 @@ node_modules
out
.DS_Store
screenshots
config.yaml
secrets.yaml
build
advisorycircular.json

View File

@ -1,6 +1,7 @@
(ns lemondronor.advisorycircular
(:require
["canvas" :as canvas]
[cemerick.url :as c-url]
["commander" :as commander]
["fs" :as fs]
["tmp-promise" :as tmp]
@ -107,15 +108,18 @@
(js->clj (parse-json json-str))
"ac"))})
(defn get-adsbexchange-live-data [{:keys [url lat lon radius-nm api-key api-whitelist]}]
(let [url (->> [url
"lat" lat
"lon" lon
"dist" (.toFixed radius-nm 1)]
(map str)
(string/join "/"))
headers (cond-> {:api-auth api-key}
(defn get-adsbexchange-live-data [{:keys [url lat lon radius-nm api-key api-whitelist rapid-api?]}]
(let [url (str
(->> [url
"lat" lat
"lon" lon
"dist" (.toFixed radius-nm (if rapid-api? 0 1))]
(map str)
(string/join "/"))
(if rapid-api? "/" ""))
headers (cond-> (if rapid-api?
{:x-rapidapi-key api-key :x-rapidapi-host (:host (c-url/url url)) :useQueryString true}
{:auth-key api-key})
api-whitelist (assoc :ADSBX-WL api-whitelist))]
(p/let [http-result (util/http-get url {:headers headers})]
(let [result (parse-adsbexchange-live-data http-result)]
@ -716,7 +720,8 @@
:minimum-airport-distance-km 3.5
:history-db-path "advisorycircular.json"
:aircraft-info-db-path "aircraft-info.sqb"
:twitter {:enabled? true}})
:twitter {:enabled? true}
:rapid-api? false})
(defn build-config-from-commander [commander]
@ -754,7 +759,8 @@
[:aircraft-info-db-path]
[:adsbx :url]
[:adsbx :api-key]
[:pelias :url]]
[:pelias :url]
[:rapid-api?]]
present (util/nested-keys config)
missing1 (set/difference (set required) (set present))
missing2 (when (get-in config [:twitter :enabled?])
@ -826,7 +832,8 @@
(merge (:adsbx config)
{:lat (:lat config)
:lon (:lon config)
:radius-nm (* (:radius-km config) 0.539957)}))
:radius-nm (* (:radius-km config) 0.539957)
:rapid-api? (:rapid-api? config)}))
filtered-api-data (remove-blocked-icaos api-data (get config :icao-blocklist '()))
now (current-time)
[new-db potential-circles] (-> db

View File

@ -1,6 +1,7 @@
(ns lemondronor.advisorycircular.util
(:require [cemerick.url :as c-url]
["fs" :as fs]
[goog.string :as gstring]
[kitchen-async.promise :as p]
[lemondronor.advisorycircular.logging :as logging]
["request-promise-native" :as request]
@ -11,6 +12,9 @@
(def fs-promises (.-promises fs))
(defn ^boolean ends-with?
[s substr]
(gstring/endsWith s substr))
;; Reads a file, returns a promise resolving to the file contents.
@ -51,8 +55,13 @@
([url options]
(let [query (or (:query options) {})
options (dissoc options :query)
url (-> (c-url/url (str url))
(assoc :query query))]
parsed-url (-> (c-url/url (str url))
(assoc :query query))
;; Work around c-url bug to re-append any stripped trailing slash.
;; https://github.com/cemerick/url/issues/24
url (if (ends-with? (str url) "/")
(str parsed-url "/")
parsed-url)]
(p/do
(log-verbose "Fetching %s" url)
(p/let [result (.get request