From 3a68965f11f3ba88cea716d09a2baee1e8b29cb5 Mon Sep 17 00:00:00 2001 From: John Wiseman Date: Thu, 15 Oct 2020 22:12:00 -0700 Subject: [PATCH] Added --oauth flag to authorize a bot for another account. --- package-lock.json | 13 ++++++ package.json | 1 + src/main/lemondronor/advisorycircular.cljs | 50 +++++++++++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 334c59c..bc1ee69 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1273,6 +1273,11 @@ "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" }, + "oauth": { + "version": "0.9.15", + "resolved": "https://registry.npmjs.org/oauth/-/oauth-0.9.15.tgz", + "integrity": "sha1-vR/vr2hslrdUda7VGWQS/2DPucE=" + }, "oauth-sign": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz", @@ -1923,6 +1928,14 @@ } } }, + "twitter-pin": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/twitter-pin/-/twitter-pin-1.0.0.tgz", + "integrity": "sha1-nVnamDwASzLG/rCFvPE1781ZNQ4=", + "requires": { + "oauth": "^0.9.13" + } + }, "typedarray": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", diff --git a/package.json b/package.json index 7c0c2c3..caaab5c 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "sqlite": "^3.0.3", "tmp-promise": "^3.0.2", "twit": "^2.2.11", + "twitter-pin": "^1.0.0", "winston": "^3.3.3" } } diff --git a/src/main/lemondronor/advisorycircular.cljs b/src/main/lemondronor/advisorycircular.cljs index 5cb7174..f28220f 100644 --- a/src/main/lemondronor/advisorycircular.cljs +++ b/src/main/lemondronor/advisorycircular.cljs @@ -17,7 +17,8 @@ [lemondronor.advisorycircular.pelias :as pelias] [lemondronor.advisorycircular.twitter :as twitter] [lemondronor.advisorycircular.util :as util] - ["sqlite" :as sqlite])) + ["sqlite" :as sqlite] + ["twitter-pin" :as twitter-pin])) (declare logger log-debug log-verbose log-info log-warn log-error) (logging/deflog "advisorycircular" logger) @@ -848,6 +849,50 @@ (println (:num-followers stats) "Twitter followers")) +(defn read-line [] + (p/promise [resolve] + (.once js/process.stdin + "data" + #(resolve (.trim (str %)))))) + + +(defn twitter-pin-get-url [tp] + (p/promise [resolve reject] + (.getUrl tp (fn [err url] + (if err + (reject err) + (resolve url)))))) + + +(defn twitter-pin-authorize [tp pin] + (p/promise [resolve reject] + (.authorize tp pin (fn [err result] + (when err + (reject err)) + (resolve + {:user-id (.-user_id result) + :screen-name (.-screen_name result) + :token (.-token result) + :secret (.-secret result)}))))) + + +(defn do-oauth-authorization [] + (p/let [_ (println "Enter the bot app's consumer key:") + consumer-key (read-line) + _ (println "Enter the bot app's consumer secret:") + consumer-secret (read-line) + _ (println consumer-key consumer-secret twitter-pin) + pin-getter (twitter-pin consumer-key consumer-secret) + auth-url (twitter-pin-get-url pin-getter) + _ (println "1. Open this URL and get the PIN:" auth-url) + _ (println "2. Enter the PIN you got from the URL above:") + pin (read-line) + authorization (twitter-pin-authorize pin-getter (.trim (str pin)))] + (println "Authorizing account:" (:screen-name authorization)) + (println "Token:" (:token authorization)) + (println "Secret:" (:secret authorization)))) + + (defn main [& args] (-> commander (.option "--lat " "Latitude of the circle of region of interest" parse-number) @@ -865,6 +910,7 @@ (.option "--airport-geojson" "Generate airport GEOJSON and exit") (.option "--create-aircraft-info-db-from-json " "Generate aircraft info DB and exit") (.option "--stats" "Show bot stats and exit") + (.option "--oauth" "Run the oauth authorization process for the bot and a twitter account") (.parse (.-argv js/process))) (logging/set-log-prefix! (or (.-logPrefix commander) "")) (reset! log-prefix (or (.-logPrefix commander) "")) @@ -880,6 +926,8 @@ (p/let [config (get-config commander) stats (get-stats config)] (print-stats stats)) + (.-oauth commander) + (do-oauth-authorization) :else (let [start-time (current-time)] ;; If --config-path is specified, definitely try to read that