From 2f46c6520ac7d74ad9c8e04b180ad5d2f5731fb6 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Tue, 22 Jun 2021 20:19:07 +0000 Subject: [PATCH] Improve console log --- package.json | 2 +- project.clj | 2 +- src/main/cljs/mastodon_bot/infra.cljs | 19 +++-- src/main/cljs/mastodon_bot/rss_api.cljs | 9 +-- src/main/cljs/mastodon_bot/transform.cljs | 82 ++++++++++----------- src/main/cljs/mastodon_bot/twitter_api.cljs | 18 ++--- 6 files changed, 66 insertions(+), 66 deletions(-) diff --git a/package.json b/package.json index 5fb55d1..7227a86 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "mastodon-bot", "description": "Bot to publish twitter, tumblr or rss posts to an mastodon account.", "author": "Dmitri Sotnikov", - "version": "1.11.1-SNAPSHOT", + "version": "1.12.0-SNAPSHOT", "homepage": "https://github.com/yogthos/mastodon-bot", "repository": "https://www.npmjs.com/package/mastodon-bot", "license": "MIT", diff --git a/project.clj b/project.clj index c20cb18..3bad80e 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject dda/mastodon-bot "1.11.1-SNAPSHOT" +(defproject dda/mastodon-bot "1.12.0-SNAPSHOT" :description "Bot to publish twitter, tumblr or rss posts to an mastodon account." :url "https://github.com/yogthos/mastodon-bot" :author "Dmitri Sotnikov" diff --git a/src/main/cljs/mastodon_bot/infra.cljs b/src/main/cljs/mastodon_bot/infra.cljs index 567f9f5..9908881 100755 --- a/src/main/cljs/mastodon_bot/infra.cljs +++ b/src/main/cljs/mastodon_bot/infra.cljs @@ -6,11 +6,15 @@ ["deasync" :as deasync] ["node-fetch" :as fetch])) -(defn debug [item] +(defn log-error [item] + (js/console.error item) + item) + +(defn log [item] (js/console.log item) item) -(defn debug-first [item] +(defn log-first [item] (js/console.log (first item)) item) @@ -47,10 +51,13 @@ (defn resolve-promise [promise result-on-error] (let [done (atom false) - result (atom nil) - promise (-> promise - (.then #(do (reset! result %) (reset! done true))) - (.catch #(do (reset! result result-on-error) (reset! done true))))] + result (atom nil)] + (-> promise + (.then #(do (reset! result %) (reset! done true))) + (.catch #(do + (log-error %) + (reset! result result-on-error) + (reset! done true)))) (.loopWhile deasync (fn [] (not @done))) @result)) diff --git a/src/main/cljs/mastodon_bot/rss_api.cljs b/src/main/cljs/mastodon_bot/rss_api.cljs index a34b771..6bcebce 100755 --- a/src/main/cljs/mastodon_bot/rss_api.cljs +++ b/src/main/cljs/mastodon_bot/rss_api.cljs @@ -2,6 +2,7 @@ (:require [orchestra.core :refer-macros [defn-spec]] [mastodon-bot.rss-domain :as rd] + [mastodon-bot.infra :as infra] [clojure.spec.alpha :as s] ["rss-parser" :as rss])) @@ -20,9 +21,5 @@ link)})) (defn-spec get-feed map? - [url string? - callback fn?] - (print url) - (-> (.parseURL (rss-client) url) - (.then callback) - (.catch #(js/console.log %)))) + [url string?] + (infra/resolve-promise (.parseURL (rss-client) url) [])) diff --git a/src/main/cljs/mastodon_bot/transform.cljs b/src/main/cljs/mastodon_bot/transform.cljs index 0422274..7fe1f4f 100644 --- a/src/main/cljs/mastodon_bot/transform.cljs +++ b/src/main/cljs/mastodon_bot/transform.cljs @@ -43,38 +43,33 @@ (update input :text #(reduce-kv string/replace % (:replacements transformation)))) (defn-spec post-tweets-to-mastodon any? - [mastodon-auth md/mastodon-auth? + [tweets any? + mastodon-auth md/mastodon-auth? transformation ::trd/transformation last-post-time any?] (let [{:keys [source target resolve-urls?]} transformation] - (fn [error tweets response] - (if error - (infra/exit-with-error error) - (->> (infra/js->edn tweets) - (map twa/parse-tweet) - (filter #(> (:created-at %) last-post-time)) - (remove #(blocked-content? transformation (:text %))) - (map #(intermediate-resolve-urls resolve-urls? %)) - (map #(twa/nitter-url source %)) - (map #(perform-replacements transformation %)) - (map #(ma/intermediate-to-mastodon target %)) - (ma/post-items mastodon-auth target)))))) + (->> (infra/js->edn tweets) + (map twa/parse-tweet) + (filter #(> (:created-at %) last-post-time)) + (remove #(blocked-content? transformation (:text %))) + (map #(intermediate-resolve-urls resolve-urls? %)) + (map #(twa/nitter-url source %)) + (map #(perform-replacements transformation %)) + (map #(ma/intermediate-to-mastodon target %)) + (ma/post-items mastodon-auth target)))) (defn-spec tweets-to-mastodon any? [mastodon-auth md/mastodon-auth? twitter-auth twd/twitter-auth? transformation ::trd/transformation last-post-time any?] - (let [{:keys [source target resolve-urls?]} transformation] - (doseq [account (:accounts source)] - (twa/user-timeline - twitter-auth - source - account - (post-tweets-to-mastodon - mastodon-auth - transformation - last-post-time))))) + (let [{:keys [source target resolve-urls?]} transformation + accounts (:accounts source)] + (infra/log (str "processing tweets for " accounts)) + (doseq [account accounts] + (-> (twa/user-timeline twitter-auth source account) + (post-tweets-to-mastodon mastodon-auth transformation last-post-time))) + (infra/log "done."))) (defn-spec post-tumblr-to-mastodon any? [mastodon-auth md/mastodon-auth? @@ -98,7 +93,9 @@ tumblr-auth td/tumblr-auth? transformation ::trd/transformation last-post-time any?] - (let [{:keys [accounts limit]} transformation] + (let [{:keys [source target]} transformation + {:keys [accounts limit]} source] + (infra/log (str "processing tumblr for " accounts)) (doseq [account accounts] (let [client (ta/tumblr-client tumblr-auth account)] (.posts client @@ -110,31 +107,30 @@ ))))) (defn-spec post-rss-to-mastodon any? - [mastodon-auth md/mastodon-auth? + [payload any? + mastodon-auth md/mastodon-auth? transformation ::trd/transformation last-post-time any?] (let [{:keys [source target resolve-urls?]} transformation] - (fn [payload] - (->> (infra/js->edn payload) - (:items) - (map ra/parse-feed) - (filter #(> (:created-at %) last-post-time)) - (remove #(blocked-content? transformation (:text %))) - (map #(intermediate-resolve-urls resolve-urls? %)) - (map #(perform-replacements transformation %)) - (map #(ma/intermediate-to-mastodon target %)) - (ma/post-items mastodon-auth target))))) + (->> (infra/js->edn payload) + (:items) + (map ra/parse-feed) + (filter #(> (:created-at %) last-post-time)) + (remove #(blocked-content? transformation (:text %))) + (map #(intermediate-resolve-urls resolve-urls? %)) + (map #(perform-replacements transformation %)) + (map #(ma/intermediate-to-mastodon target %)) + (ma/post-items mastodon-auth target)))) (defn-spec rss-to-mastodon any? [mastodon-auth md/mastodon-auth? transformation ::trd/transformation last-post-time any?] - (let [{:keys [source target]} transformation] - (doseq [[name url] (:feeds source)] - (ra/get-feed - url - (post-rss-to-mastodon - mastodon-auth - transformation - last-post-time))))) \ No newline at end of file + (let [{:keys [source target]} transformation + {:keys [feeds]} source] + (infra/log (str "processing rss for " feeds)) + (doseq [[name url] feeds] + (-> (ra/get-feed url) + (post-rss-to-mastodon mastodon-auth transformation last-post-time))) + (infra/log "done."))) \ No newline at end of file diff --git a/src/main/cljs/mastodon_bot/twitter_api.cljs b/src/main/cljs/mastodon_bot/twitter_api.cljs index 2d66dd2..ace8ee9 100755 --- a/src/main/cljs/mastodon_bot/twitter_api.cljs +++ b/src/main/cljs/mastodon_bot/twitter_api.cljs @@ -46,14 +46,14 @@ (defn-spec user-timeline any? [twitter-auth td/twitter-auth? source td/twitter-source? - account ::td/account - callback fn?] + account ::td/account] (let [{:keys [include-rts? include-replies?]} source] - (.get (twitter-client twitter-auth) - "statuses/user_timeline" - #js {:screen_name account - :tweet_mode "extended" - :include_rts (boolean include-rts?) - :exclude_replies (not (boolean include-replies?))} - callback))) + (infra/resolve-promise + (.get (twitter-client twitter-auth) + "statuses/user_timeline" + #js {:screen_name account + :tweet_mode "extended" + :include_rts (boolean include-rts?) + :exclude_replies (not (boolean include-replies?))}) + []))) \ No newline at end of file