Merge branch 'add-rapidapi-support' into 'master'

add rapidapi support

Closes #30

See merge request jjwiseman/advisory-circular!5
This commit is contained in:
John Wiseman 2020-09-23 21:38:36 +00:00
commit 2321468260
3 changed files with 34 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,19 @@
(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} ;; RapidAPI expects a trailing slash.
(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 +721,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 +760,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 +833,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