From 9a4d754a1d8972dc5f909161941d77778fbeaf43 Mon Sep 17 00:00:00 2001 From: jem Date: Tue, 26 May 2020 17:10:58 +0200 Subject: [PATCH] prepare auth / transform separation --- src/main/mastodon_bot/core.cljs | 64 ++++++++++++++----------- src/main/mastodon_bot/mastodon_api.cljs | 14 +++--- src/main/mastodon_bot/transform.cljs | 10 ++-- 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/main/mastodon_bot/core.cljs b/src/main/mastodon_bot/core.cljs index 805a151..f50d9fa 100755 --- a/src/main/mastodon_bot/core.cljs +++ b/src/main/mastodon_bot/core.cljs @@ -13,22 +13,27 @@ [mastodon-bot.tumblr-api :as tumblr] [cljs.core :refer [*command-line-args*]])) -(s/def ::mastodon-config masto/mastodon-config?) -(s/def ::twitter twitter/twitter-config?) +(s/def ::mastodon-auth masto/mastodon-auth?) +(s/def ::transform transform/transformations?) +(s/def ::twitter twitter/twitter-auth?) (s/def ::tumblr map?) (s/def ::rss map?) (def config? (s/keys :req-un [::mastodon-config] :opt-un [::twitter ::tumblr ::rss])) -(defn-spec mastodon-config ::mastodon-config +(defn-spec mastodon-auth ::mastodon-auth [config config?] (:mastodon-config config)) -(defn-spec twitter-config ::twitter +(defn-spec twitter-auth ::twitter [config config?] (:twitter config)) +(defn-spec transform ::transform + [config config?] + (:transform config)) + (def config (infra/load-config)) (defn post-tumblrs [last-post-time] @@ -38,46 +43,49 @@ :posts (mapv tumblr/parse-tumblr-post) (map #(transform/to-mastodon - (mastodon-config config) %)) + (mastodon-auth config) %)) (masto/post-items - (mastodon-config config) + (mastodon-auth config) last-post-time)))) (defn parse-feed [last-post-time parser [title url]] (-> (.parseURL parser url) (.then #(masto/post-items - (mastodon-config config) + (mastodon-auth config) last-post-time (for [{:keys [title isoDate pubDate content link]} (-> % infra/js->edn :items)] {:created-at (js/Date. (or isoDate pubDate)) :text (str (transform/trim-text title - (masto/max-post-length (mastodon-config config))) + (masto/max-post-length (mastodon-auth config))) "\n\n" (twitter/strip-utm link))}))))) (defn -main [] - (masto/get-mastodon-timeline - (mastodon-config config) - (fn [timeline] - (let [last-post-time (-> timeline first :created_at (js/Date.))] + (let [mastodon-auth (mastodon-auth config)] + (masto/get-mastodon-timeline + mastodon-auth + (fn [timeline] + (let [last-post-time (-> timeline first :created_at (js/Date.))] ;;post from Twitter - (when-let [twitter-config (:twitter config)] - (let [{:keys [accounts]} twitter-config] - (transform/tweets-to-mastodon - (mastodon-config config) - twitter-config - accounts - last-post-time))) + (when-let [twitter-auth (twitter-auth config)] + (let [{:keys [accounts]} twitter-auth] + (transform/tweets-to-mastodon + mastodon-auth + twitter-auth + accounts + last-post-time))) ;;post from Tumblr - (when-let [{:keys [access-keys accounts limit]} (:tumblr config)] - (doseq [account accounts] - (let [client (tumblr/tumblr-client access-keys account)] - (.posts client #js {:limit (or limit 5)} (post-tumblrs last-post-time))))) + (when-let [{:keys [access-keys accounts limit]} (:tumblr config)] + (doseq [account accounts] + (let [client (tumblr/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.)] - (doseq [feed feeds] - (parse-feed last-post-time parser feed)))))))) + (when-let [feeds (some-> config :rss)] + (let [parser (rss.)] + (doseq [feed feeds] + (parse-feed last-post-time parser feed))))))))) (set! *main-cli-fn* -main) -(st/instrument 'mastodon-config) +(st/instrument 'mastodon-auth) +(st/instrument 'twitter-auth) +(st/instrument 'transform) diff --git a/src/main/mastodon_bot/mastodon_api.cljs b/src/main/mastodon_bot/mastodon_api.cljs index 71f996a..55eaf84 100755 --- a/src/main/mastodon_bot/mastodon_api.cljs +++ b/src/main/mastodon_bot/mastodon_api.cljs @@ -28,12 +28,12 @@ (s/def ::content-filters (s/* ::content-filter)) (s/def ::keyword-filters (s/* ::keyword-filter)) -(s/def ::mastodon-js-config (s/keys :req-un [::access_token ::api_url])) -(s/def ::mastodon-clj-config (s/keys :req-un [::account-id ::content-filters ::keyword-filters +(def mastodon-auth? (s/keys :req-un [::access_token ::api_url])) +(def mastodon-transform? (s/keys :req-un [::account-id ::content-filters ::keyword-filters ::max-post-length ::signature ::visibility ::append-screen-name? ::sensitive? ::resolve-urls? ::nitter-urls? ::replacements])) -(def mastodon-config? (s/merge ::mastodon-js-config ::mastodon-clj-config)) +(def mastodon-config? (s/merge mastodon-auth? mastodon-transform?)) (defn-spec content-filter-regexes ::content-filters [mastodon-config mastodon-config?] @@ -57,9 +57,11 @@ (reduce-kv string/replace text (:replacements mastodon-config))) (defn-spec mastodon-client any? - [mastodon-config mastodon-config?] - (or (some-> mastodon-config clj->js mastodon.) - (infra/exit-with-error "missing Mastodon client configuration!"))) + [mastodon-auth mastodon-auth?] + (or (some-> mastodon-auth + clj->js + mastodon.) + (infra/exit-with-error "missing Mastodon auth configuration!"))) (defn-spec blocked-content? boolean? [mastodon-config mastodon-config? diff --git a/src/main/mastodon_bot/transform.cljs b/src/main/mastodon_bot/transform.cljs index 715083e..b5c7a47 100644 --- a/src/main/mastodon_bot/transform.cljs +++ b/src/main/mastodon_bot/transform.cljs @@ -19,6 +19,8 @@ (def mastodon-output? (s/keys :req-un [::created-at ::text] :opt-un [::media-links])) +(def transformations? any?) + (defn trim-text [text max-post-length] (cond @@ -37,7 +39,7 @@ :else text)) (defn-spec to-mastodon mastodon-output? - [mastodon-config masto/mastodon-config? + [mastodon-config masto/mastodon-auth? input input?] (let [{:keys [created-at text media-links screen_name untrimmed-text]} input untrimmed (if (some? untrimmed-text) @@ -54,7 +56,7 @@ :media-links media-links})) (defn-spec post-tweets any? - [mastodon-config masto/mastodon-config? + [mastodon-config masto/mastodon-auth? last-post-time any?] (fn [error tweets response] (if error @@ -65,8 +67,8 @@ (masto/post-items mastodon-config last-post-time))))) (defn-spec tweets-to-mastodon any? - [mastodon-config masto/mastodon-config? - twitter-config twitter/twitter-config? + [mastodon-config masto/mastodon-auth? + twitter-config twitter/twitter-auth? accounts (s/* string?) last-post-time any?] (doseq [account accounts]