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))
(if (and image-path description)
(do
(p/let [image (util/read-file image-path)]
(if (get-in config [:twitter :enabled?])
(twitter/tweet (twitter/twit (:twitter config))
description
[image-path]
[image]
lat
lon)
(log-warn "Skipping tweeting"))
@ -671,11 +671,16 @@
(.option "--history <path>" "Path to history/state file" "advisorycircular.json")
(.option "--log-prefix <prefix>" "Log prefix to use")
(.option "--airport-geojson" "Generate airport GEOJSON and exit")
(.option "--test")
(.option "--icao <icao>")
(.option "--reg <reg>")
(.parse (.-argv js/process)))
(logging/set-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)]
(util/read-config config-path)
(if (fs/existsSync default-config-path)
@ -685,15 +690,16 @@
config (build-config base-config cli-config {})
geojson (airport-geojson config)]
(println (.stringify js/JSON (clj->js geojson) nil " ")))
(p/catch :default e
(log-error "%s" e)
(log-error "%s" (.-stack e))
(.exit js/process 1)))
(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/try
(println "OMG DOING THIS")
(.-test commander)
(do
(print (.from js/Buffer #js [1 2 3 4]))
(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)]
;; 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)
@ -734,11 +740,11 @@
reverse
(take 3)
(map #(str (:icao %) ":" (.toFixed (:curviness %) 0)))
(string/join " "))))))
(p/catch :default e
(log-error "%s" e)
(log-error "%s" (.-stack e))
(.exit js/process 1))))))
(string/join " "))))))))
(p/catch :default e
(log-error "%s" e)
(log-error "%s" (.-stack e))
(.exit js/process 1))))
;; (.on js/process "unhandledRejection"

View File

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

View File

@ -14,8 +14,11 @@
;; Reads a file, returns a promise resolving to the file contents.
(defn read-file [path options]
(.readFile fs-promises path (clj->js options)))
(defn read-file
([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