Preparing to tweet aircraft photos.

This commit is contained in:
John Wiseman 2020-06-07 14:56:53 -07:00
parent 3910ed2bc7
commit de67d97223
3 changed files with 35 additions and 26 deletions

View File

@ -561,11 +561,11 @@
) )
(log-info "%s: Filtering out because we have insuffucient reverse geo info" (:icao ac)) (log-info "%s: Filtering out because we have insuffucient reverse geo info" (:icao ac))
(if (and image-path description) (if (and image-path description)
(do (p/let [image (util/read-file image-path)]
(if (get-in config [:twitter :enabled?]) (if (get-in config [:twitter :enabled?])
(twitter/tweet (twitter/twit (:twitter config)) (twitter/tweet (twitter/twit (:twitter config))
description description
[image-path] [image]
lat lat
lon) lon)
(log-warn "Skipping tweeting")) (log-warn "Skipping tweeting"))
@ -671,11 +671,16 @@
(.option "--history <path>" "Path to history/state file" "advisorycircular.json") (.option "--history <path>" "Path to history/state file" "advisorycircular.json")
(.option "--log-prefix <prefix>" "Log prefix to use") (.option "--log-prefix <prefix>" "Log prefix to use")
(.option "--airport-geojson" "Generate airport GEOJSON and exit") (.option "--airport-geojson" "Generate airport GEOJSON and exit")
(.option "--test")
(.option "--icao <icao>")
(.option "--reg <reg>")
(.parse (.-argv js/process))) (.parse (.-argv js/process)))
(logging/set-log-prefix! (or (.-logPrefix commander) "")) (logging/set-log-prefix! (or (.-logPrefix commander) ""))
(reset! log-prefix (or (.-logPrefix commander) "")) (reset! log-prefix (or (.-logPrefix commander) ""))
(if (.-airportGeojson commander)
(p/try (p/try
(cond
(.-airportGeojson commander)
(p/let [base-config (if-let [config-path (.-config commander)] (p/let [base-config (if-let [config-path (.-config commander)]
(util/read-config config-path) (util/read-config config-path)
(if (fs/existsSync default-config-path) (if (fs/existsSync default-config-path)
@ -685,15 +690,16 @@
config (build-config base-config cli-config {}) config (build-config base-config cli-config {})
geojson (airport-geojson config)] geojson (airport-geojson config)]
(println (.stringify js/JSON (clj->js geojson) nil " "))) (println (.stringify js/JSON (clj->js geojson) nil " ")))
(p/catch :default e (.-test commander)
(log-error "%s" e) (do
(log-error "%s" (.-stack e)) (print (.from js/Buffer #js [1 2 3 4]))
(.exit js/process 1))) (print (.from js/Buffer (.from js/Buffer #js [1 2 3 4])))
(print (.toString (.from js/Buffer (.from js/Buffer #js [1 2 3 4])) "base64"))
)
:else
(let [start-time (current-time)] (let [start-time (current-time)]
;; If --config-path is specified, definitely try to read that ;; If --config-path is specified, definitely try to read that
;; file. Otherwise, only read config.yaml if it exists. ;; file. Otherwise, only read config.yaml if it exists.
(p/try
(println "OMG DOING THIS")
(p/let [base-config (if-let [config-path (.-config commander)] (p/let [base-config (if-let [config-path (.-config commander)]
(util/read-config config-path) (util/read-config config-path)
(if (fs/existsSync default-config-path) (if (fs/existsSync default-config-path)
@ -734,11 +740,11 @@
reverse reverse
(take 3) (take 3)
(map #(str (:icao %) ":" (.toFixed (:curviness %) 0))) (map #(str (:icao %) ":" (.toFixed (:curviness %) 0)))
(string/join " ")))))) (string/join " "))))))))
(p/catch :default e (p/catch :default e
(log-error "%s" e) (log-error "%s" e)
(log-error "%s" (.-stack e)) (log-error "%s" (.-stack e))
(.exit js/process 1)))))) (.exit js/process 1))))
;; (.on js/process "unhandledRejection" ;; (.on js/process "unhandledRejection"

View File

@ -24,20 +24,20 @@
;; Uploads an image to twitter. Returns a promise that resolves to the ;; Uploads an image to twitter. Returns a promise that resolves to the
;; new media ID of the image. ;; new media ID of the image.
(defn upload-image [twit path] (defn upload-image [twit image]
(log-info "Uploading media to twitter: %s" path) (log-info "Uploading media to twitter")
(p/let [b64content (util/read-file path {:encoding "base64"}) (p/let [b64content (.toString (.from js/Buffer image) "base64")
result (.post twit "media/upload" (clj->js {:media_data b64content})) result (.post twit "media/upload" (clj->js {:media_data b64content}))
media-id (get-in (js->clj result :keywordize-keys true) [:data :media_id_string])] media-id (get-in (js->clj result :keywordize-keys true) [:data :media_id_string])]
(log-info "%s got media ID %s" path media-id) (log-info "%s got media ID %s" media-id)
media-id)) media-id))
;; Posts a tweet with optional multiple media. Returns a promise that ;; Posts a tweet with optional multiple media. Returns a promise that
;; resolves to the response result. ;; resolves to the response result.
(defn tweet [twit status image-paths lat lon] (defn tweet [twit status images lat lon]
(p/then (p/all (map #(upload-image twit %) image-paths)) (p/then (p/all (map #(upload-image twit %) images))
(fn [media-ids] (fn [media-ids]
(log-warn "Tweeting status:'%s' with media: %s" status media-ids) (log-warn "Tweeting status:'%s' with media: %s" status media-ids)
(p/let [result (.post twit "statuses/update" (p/let [result (.post twit "statuses/update"

View File

@ -14,8 +14,11 @@
;; Reads a file, returns a promise resolving to the file contents. ;; Reads a file, returns a promise resolving to the file contents.
(defn read-file [path options] (defn read-file
(.readFile fs-promises path (clj->js options))) ([path]
(read-file path {}))
([path options]
(.readFile fs-promises path (clj->js options))))
;; Writes a file, returns a promise that resolves to no arguments on ;; Writes a file, returns a promise that resolves to no arguments on