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 out
.DS_Store .DS_Store
screenshots screenshots
config.yaml
secrets.yaml secrets.yaml
build
advisorycircular.json

View File

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

View File

@ -1,6 +1,7 @@
(ns lemondronor.advisorycircular.util (ns lemondronor.advisorycircular.util
(:require [cemerick.url :as c-url] (:require [cemerick.url :as c-url]
["fs" :as fs] ["fs" :as fs]
[goog.string :as gstring]
[kitchen-async.promise :as p] [kitchen-async.promise :as p]
[lemondronor.advisorycircular.logging :as logging] [lemondronor.advisorycircular.logging :as logging]
["request-promise-native" :as request] ["request-promise-native" :as request]
@ -11,6 +12,9 @@
(def fs-promises (.-promises fs)) (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. ;; Reads a file, returns a promise resolving to the file contents.
@ -51,8 +55,13 @@
([url options] ([url options]
(let [query (or (:query options) {}) (let [query (or (:query options) {})
options (dissoc options :query) options (dissoc options :query)
url (-> (c-url/url (str url)) parsed-url (-> (c-url/url (str url))
(assoc :query query))] (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 (p/do
(log-verbose "Fetching %s" url) (log-verbose "Fetching %s" url)
(p/let [result (.get request (p/let [result (.get request