Added --stats which (for now) just prints the number of twitter followers.

Refactored config handling a bit.
This commit is contained in:
John Wiseman 2020-09-28 17:05:43 -07:00
parent 0f07b705c1
commit c3c384d030
1 changed files with 42 additions and 16 deletions

View File

@ -779,6 +779,41 @@
(def default-config-path "config.yaml")
(defn get-config
([commander]
(get-config commander {}))
([commander options]
(p/let [base-config (if-let [config-path (.-config commander)]
(util/read-config config-path)
(if (fs/existsSync default-config-path)
(util/read-config default-config-path)
{}))
cli-config (build-config-from-commander commander)
secrets (if (:no-secrets? options)
{}
(util/read-config (.-secrets commander)))
config (build-config base-config cli-config secrets)
_ (if (:no-validate? options)
nil
(validate-config config))]
config)))
(defn get-stats [config]
(let [twit (twitter/twit (:twitter config))]
(p/let [followers (.get twit "followers/ids")
num-followers (-> followers
(js->clj :keywordize-keys true)
:data
:ids
count)]
{:num-followers num-followers})))
(defn print-stats [stats]
(println (:num-followers stats) "Twitter followers"))
(defn main [& args]
(-> commander
(.option "--lat <lat>" "Latitude of the circle of region of interest" parse-number)
@ -795,6 +830,7 @@
(.option "--log-prefix <prefix>" "Log prefix to use")
(.option "--airport-geojson" "Generate airport GEOJSON and exit")
(.option "--create-aircraft-info-db-from-json <json path>" "Generate aircraft info DB and exit")
(.option "--stats" "Show bot stats and exit")
(.parse (.-argv js/process)))
(logging/set-log-prefix! (or (.-logPrefix commander) ""))
(reset! log-prefix (or (.-logPrefix commander) ""))
@ -803,28 +839,18 @@
(.-createAircraftInfoDbFromJson commander)
(create-aircraft-info-db (.-createAircraftInfoDbFromJson commander) (.-aircraftInfoDb commander))
(.-airportGeojson commander)
(p/let [base-config (if-let [config-path (.-config commander)]
(util/read-config config-path)
(if (fs/existsSync default-config-path)
(util/read-config default-config-path)
{}))
cli-config (build-config-from-commander commander)
config (build-config base-config cli-config {})
(p/let [config (get-config commander {:no-secrets? true :no-validate? true})
geojson (airport-geojson config)]
(println (.stringify js/JSON (clj->js geojson) nil " ")))
(.-stats commander)
(p/let [config (get-config commander)
stats (get-stats config)]
(print-stats stats))
:else
(let [start-time (current-time)]
;; If --config-path is specified, definitely try to read that
;; file. Otherwise, only read config.yaml if it exists.
(p/let [base-config (if-let [config-path (.-config commander)]
(util/read-config config-path)
(if (fs/existsSync default-config-path)
(util/read-config default-config-path)
{}))
cli-config (build-config-from-commander commander)
secrets (util/read-config (.-secrets commander))
config (build-config base-config cli-config secrets)
_ (validate-config config)
(p/let [config (get-config commander)
history-db-path (:history-db-path config)
_ (when (not (fs/existsSync history-db-path))
(log-info "%s does not exist; creating empty one." history-db-path)