Improve console log

This commit is contained in:
Michael Jerger 2021-06-22 20:19:07 +00:00
parent 9cb7ba4621
commit 2f46c6520a
6 changed files with 66 additions and 66 deletions

View file

@ -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",

View file

@ -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"

View file

@ -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))

View file

@ -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) []))

View file

@ -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)))))
(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.")))

View file

@ -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?))})
[])))