From 0c2832ddf0480084d6c295e5a0ab38fcdab1bb9b Mon Sep 17 00:00:00 2001 From: Dmitri Sotnikov Date: Sat, 18 Aug 2018 13:25:48 -0400 Subject: [PATCH] fixed accessing multiple tumblr accounts, added config options --- README.md | 9 ++++++++- mastodon-bot.cljs | 45 +++++++++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 60b045d..fbd52e8 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,20 @@ If you get a [permission failure](https://github.com/anmonteiro/lumo/issues/206) :consumer_secret "XXXX" :access_token_key "XXXX" :access_token_secret "XXXX"} - :accounts ["arstechnica" "WIRED"]} ;; accounts you wish to mirror + ;; optional, defaults to false + :include-replies? false + ;; optional, defaults to false + :include-rts? false + ;; accounts you wish to mirror + :accounts ["arstechnica" "WIRED"]} ;; add Tumblr config to mirror Tumblr accounts :tumblr {:access-keys {:consumer_key "XXXX" :consumer_secret "XXXX" :token "XXXX" :token_secret "XXXX"} + ;; optional limit for number of posts to retrieve, default: 5 + :limit 10 :accounts ["cyberpunky.tumblr.com" "scipunk.tumblr.com"]} ;; add RSS config to follow feeds :rss {"Hacker News" "https://hnrss.org/newest" diff --git a/mastodon-bot.cljs b/mastodon-bot.cljs index 5d5b59d..dfc4d4f 100755 --- a/mastodon-bot.cljs +++ b/mastodon-bot.cljs @@ -13,6 +13,10 @@ ["tumblr" :as tumblr] ["twitter" :as twitter])) +(defn exit-with-error [error] + (js/console.error error) + (js/process.exit 1)) + (defn find-config [] (or (first *command-line-args*) (-> js/process .-env .-MASTODON_BOT_CONFIG) @@ -21,9 +25,7 @@ (def config (-> (find-config) (fs/readFileSync #js {:encoding "UTF-8"}) edn/read-string)) (def mastodon-client (or (some-> config :mastodon clj->js mastodon.) - (do - (js/console.error "missing Mastodon client configuration!") - (js/process.exit 1)))) + (exit-with-error "missing Mastodon client configuration!"))) (def content-filter-regexes (mapv re-pattern (-> config :mastodon :content-filters))) @@ -32,7 +34,7 @@ (def max-post-length (-> config :mastodon :max-post-length)) (defn blocked-content? [text] - (boolean (some #(re-matches % text) content-filter-regexes))) + (boolean (some #(re-find % text) content-filter-regexes))) (defn js->edn [data] (js->clj data :keywordize-keys true)) @@ -138,20 +140,39 @@ {:created-at (js/Date. (or isoDate pubDate)) :text (str (trim-text title) "\n\n" (strip-utm link))}))))) +(defn twitter-client [access-keys] + (try + (twitter. (clj->js access-keys)) + (catch js/Error e + (exit-with-error + (str "failed to connect to Twitter: " (.-message e)))))) + +(defn tumblr-client [access-keys account] + (try + (tumblr/Blog. account (clj->js access-keys)) + (catch js/Error e + (exit-with-error + (str "failed to connect to Tumblr account " account ": " (.-message e)))))) + (get-mastodon-timeline (fn [timeline] (let [last-post-time (-> timeline first :created_at (js/Date.))] ;;post from Twitter - (when-let [twitter-client (some-> config :twitter :access-keys clj->js twitter.)] - (doseq [account (-> config :twitter :accounts)] - (.get twitter-client + (when-let [twitter-config (:twitter config)] + (let [{:keys [access-keys accounts include-replies? include-rts?]} twitter-config + client (twitter-client access-keys)] + (doseq [account accounts] + (.get client "statuses/user_timeline" - #js {:screen_name account :include_rts false :exclude_replies true} - (post-tweets last-post-time)))) + #js {:screen_name account + :include_rts (boolean include-replies?) + :exclude_replies (boolean include-rts?)} + (post-tweets last-post-time))))) ;;post from Tumblr - (when-let [tumblr-oauth (some-> config :tumblr :access-keys clj->js)] - (when-let [tumblr-client (some-> config :tumblr :accounts first (tumblr/Blog. tumblr-oauth))] - (.posts tumblr-client #js {:limit 5} (post-tumblrs last-post-time)))) + (when-let [{:keys [accounts limit tumblr-oauth]} (:tumblr config)] + (doseq [account accounts] + (let [client (tumblr-client access-keys account)] + (.posts client #js {:limit (or limit 5)} (post-tumblrs last-post-time))))) ;;post from RSS (when-let [feeds (some-> config :rss)] (let [parser (rss.)]