From 1072af014505925e42ab1f7a3a187cdc759a0464 Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 29 May 2020 19:48:48 +0200 Subject: [PATCH] moved signature tor transform --- README.md | 6 +-- src/main/mastodon_bot/core.cljs | 4 +- src/main/mastodon_bot/mastodon_api.cljs | 33 ++++++++-------- src/main/mastodon_bot/transform.cljs | 47 ++++++++++++++--------- src/main/mastodon_bot/twitter_api.cljs | 20 +++++----- src/test/mastodon_bot/transform_test.cljs | 3 +- 6 files changed, 62 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 2fb5939..3a703cc 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,6 @@ with later timestamps to avoid duplicate posts. On the first run the timestamp w ;; optional flag specifying wether the name of the account ;; will be appended in the post, defaults to false :append-screen-name? false - ;; optional signature for posts - :signature "#newsbot" ;; optionally try to resolve URLs in posts to skip URL shorteners ;; defaults to false :resolve-urls? true @@ -83,7 +81,9 @@ with later timestamps to avoid duplicate posts. On the first run the timestamp w :include-rts? false ;; accounts you wish to mirror :accounts ["arstechnica" "WIRED"]} - :target {}}] + :target {:type :mastodon-target + ;; optional signature for posts + :signature "#newsbot"}}] } ``` diff --git a/src/main/mastodon_bot/core.cljs b/src/main/mastodon_bot/core.cljs index a6bfb1a..4a4bb5f 100755 --- a/src/main/mastodon_bot/core.cljs +++ b/src/main/mastodon_bot/core.cljs @@ -43,7 +43,9 @@ :posts (mapv tumblr/parse-tumblr-post) (map #(transform/to-mastodon - (mastodon-auth config) %)) + (mastodon-auth config) + ;todo: fix this + (:target (first (transform config))) %)) (masto/post-items (mastodon-auth config) last-post-time)))) diff --git a/src/main/mastodon_bot/mastodon_api.cljs b/src/main/mastodon_bot/mastodon_api.cljs index 55eaf84..78096be 100755 --- a/src/main/mastodon_bot/mastodon_api.cljs +++ b/src/main/mastodon_bot/mastodon_api.cljs @@ -28,12 +28,17 @@ (s/def ::content-filters (s/* ::content-filter)) (s/def ::keyword-filters (s/* ::keyword-filter)) -(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-auth? mastodon-transform?)) +(def mastodon-auth? (s/keys :req-un [::account-id ::access_token ::api_url])) +(def mastodon-target? (s/keys :req-un [ + ;::content-filters ::keyword-filters + ;::max-post-length + ::signature + ;::visibility + ;::append-screen-name? ::sensitive? ::resolve-urls? + ;::nitter-urls? ::replacements + ])) +(def mastodon-config? (s/merge mastodon-auth? mastodon-target?)) + (defn-spec content-filter-regexes ::content-filters [mastodon-config mastodon-config?] @@ -103,13 +108,6 @@ (:nitter-urls? mastodon-config) (string/replace #"https://twitter.com" "https://nitter.net"))) -(defn-spec set-signature string? - [mastodon-config mastodon-config? - text string?] - (if-let [signature (:signature mastodon-config )] - (str text "\n" signature) - text)) - (defn post-status ([mastodon-config status-text] (post-status mastodon-config status-text nil print)) @@ -120,8 +118,7 @@ (-> (.post (mastodon-client mastodon-config) "statuses" (clj->js (merge {:status (->> status-text (resolve-urls mastodon-config) - (perform-replacements mastodon-config) - (set-signature mastodon-config))} + (perform-replacements mastodon-config))} (when media-ids {:media_ids media-ids}) (when sensitive? {:sensitive sensitive?}) (when visibility {:visibility visibility})))) @@ -152,10 +149,10 @@ (post-status mastodon-config status-text (not-empty ids) callback)))) (defn-spec get-mastodon-timeline any? - [mastodon-config mastodon-config? + [mastodon-auth mastodon-auth? callback fn?] - (.then (.get (mastodon-client mastodon-config) - (str "accounts/" (:account-id mastodon-config)"/statuses") #js {}) + (.then (.get (mastodon-client mastodon-auth) + (str "accounts/" (:account-id mastodon-auth)"/statuses") #js {}) #(let [response (-> % .-data infra/js->edn)] (if-let [error (::error response)] (infra/exit-with-error error) diff --git a/src/main/mastodon_bot/transform.cljs b/src/main/mastodon_bot/transform.cljs index 45ad0ad..f8d09b7 100644 --- a/src/main/mastodon_bot/transform.cljs +++ b/src/main/mastodon_bot/transform.cljs @@ -23,7 +23,10 @@ (defmethod source-type :twitter-source [_] (s/merge (s/keys :req-un[::type]) twitter/twitter-source?)) (s/def ::source (s/multi-spec source-type ::type)) -(s/def ::target any?) +(defmulti target-type :type) +(defmethod target-type :mastodon-target [_] + (s/merge (s/keys :req-un [::type]) masto/mastodon-target?)) +(s/def ::target (s/multi-spec target-type ::type)) (s/def ::transformation (s/keys :req-un [::source ::target])) (def transformations? (s/* ::transformation)) @@ -45,45 +48,53 @@ :else text)) (defn-spec to-mastodon mastodon-output? - [mastodon-config masto/mastodon-auth? + [mastodon-auth masto/mastodon-auth? + target masto/mastodon-target? input input?] (let [{:keys [created-at text media-links screen_name untrimmed-text]} input + {:keys [signature]} target untrimmed (if (some? untrimmed-text) - (str " " untrimmed-text) "") - sname (if (masto/append-screen-name? mastodon-config) + (str " " untrimmed-text) "") + sname (if (masto/append-screen-name? mastodon-auth) (str "\n - " screen_name) "") - trim-length (- (masto/max-post-length mastodon-config) + signature_text (if (some? signature) + (str "\n" signature) + "") + trim-length (- (masto/max-post-length mastodon-auth) (count untrimmed) - (count sname))] + (count sname) + (count signature_text))] {:created-at created-at :text (str (trim-text text trim-length) untrimmed - sname) + sname + signature_text) :media-links media-links})) -(defn-spec post-tweets any? - [mastodon-config masto/mastodon-auth? +(defn-spec post-tweets-to-mastodon any? + [mastodon-auth masto/mastodon-auth? + target masto/mastodon-target? last-post-time any?] (fn [error tweets response] (if error (infra/exit-with-error error) (->> (infra/js->edn tweets) (map twitter/parse-tweet) - (map #(to-mastodon mastodon-config %)) - (masto/post-items mastodon-config last-post-time))))) + (map #(to-mastodon mastodon-auth target %)) + (masto/post-items mastodon-auth last-post-time))))) (defn-spec tweets-to-mastodon any? [mastodon-auth masto/mastodon-auth? twitter-auth twitter/twitter-auth? transformation ::transformation last-post-time any?] - (let [{:keys [source target]} transformation - {:keys [accounts include-rts? include-replies?]} source] - (doseq [account accounts] - (println account) + (let [{:keys [source target]} transformation] + (doseq [account (:accounts source)] (twitter/user-timeline twitter-auth - include-rts? - include-replies? + source account - (post-tweets mastodon-auth last-post-time))))) + (post-tweets-to-mastodon + mastodon-auth + target + last-post-time))))) diff --git a/src/main/mastodon_bot/twitter_api.cljs b/src/main/mastodon_bot/twitter_api.cljs index 16cb8e3..312a182 100755 --- a/src/main/mastodon_bot/twitter_api.cljs +++ b/src/main/mastodon_bot/twitter_api.cljs @@ -20,7 +20,7 @@ (s/def ::include-replies? boolean?) (s/def ::account string?) (s/def ::accounts (s/* ::account)) -(def twitter-source? (s/keys :req-un [::include-rts? ::include-replies?])) +(def twitter-source? (s/keys :req-un [::include-rts? ::include-replies? ::accounts])) (defn-spec twitter-client any? [twitter-auth twitter-auth?] @@ -53,15 +53,15 @@ (defn-spec user-timeline any? [twitter-auth twitter-auth? - include-rts? ::include-rts? - include-replies? ::include-replies? + source twitter-source? account ::account callback fn?] - (.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)) + (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))) \ No newline at end of file diff --git a/src/test/mastodon_bot/transform_test.cljs b/src/test/mastodon_bot/transform_test.cljs index b38cb8c..856b3f3 100755 --- a/src/test/mastodon_bot/transform_test.cljs +++ b/src/test/mastodon_bot/transform_test.cljs @@ -13,4 +13,5 @@ :include-replies? false :include-rts? true :accounts ["an-twitter-account"]} - :target {}}]))) + :target {:type :mastodon-target + :signature "my-bot"}}])))