From 21c908ea85f3b98c5cae61558eba1778c088111f Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 29 May 2020 15:54:32 +0200 Subject: [PATCH] first step toward transformations --- README.md | 19 +++++++++++-------- src/main/mastodon_bot/core.cljs | 13 +++++++------ src/main/mastodon_bot/transform.cljs | 21 +++++++++++++-------- src/main/mastodon_bot/twitter_api.cljs | 23 ++++++++++++----------- 4 files changed, 43 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 10dea30..2fb5939 100644 --- a/README.md +++ b/README.md @@ -32,13 +32,7 @@ with later timestamps to avoid duplicate posts. On the first run the timestamp w {:consumer_key "XXXX" :consumer_secret "XXXX" :access_token_key "XXXX" - :access_token_secret "XXXX"} - ;; optional, defaults to false - :include-replies? false - ;; optional, defaults to false - :include-rts? false - ;; accounts you wish to mirror - :accounts ["arstechnica" "WIRED"]} + :access_token_secret "XXXX"}} ;; add Tumblr config to mirror Tumblr accounts :tumblr {:access-keys {:consumer_key "XXXX" @@ -81,7 +75,16 @@ with later timestamps to avoid duplicate posts. On the first run the timestamp w ;; any posts not matching the regexes will be filtered out :keyword-filters [".*clojure.*"] ;; Replace Twitter links by Nitter - :nitter-urls? false}} + :nitter-urls? false} +:transform [{:source {:type :twitter-source + ;; optional, defaults to false + :include-replies? false + ;; optional, defaults to false + :include-rts? false + ;; accounts you wish to mirror + :accounts ["arstechnica" "WIRED"]} + :target {}}] +} ``` * the bot looks for `config.edn` at its relative path by default, an alternative location can be specified either using the `MASTODON_BOT_CONFIG` environment variable or passing the path to config as an argument diff --git a/src/main/mastodon_bot/core.cljs b/src/main/mastodon_bot/core.cljs index f50d9fa..a6bfb1a 100755 --- a/src/main/mastodon_bot/core.cljs +++ b/src/main/mastodon_bot/core.cljs @@ -68,12 +68,13 @@ (let [last-post-time (-> timeline first :created_at (js/Date.))] ;;post from Twitter (when-let [twitter-auth (twitter-auth config)] - (let [{:keys [accounts]} twitter-auth] - (transform/tweets-to-mastodon - mastodon-auth - twitter-auth - accounts - last-post-time))) + (let [{:keys [transform]} config] + (doseq [transformation transform] + (transform/tweets-to-mastodon + mastodon-auth + twitter-auth + transformation + last-post-time)))) ;;post from Tumblr (when-let [{:keys [access-keys accounts limit]} (:tumblr config)] (doseq [account accounts] diff --git a/src/main/mastodon_bot/transform.cljs b/src/main/mastodon_bot/transform.cljs index 8bf1ead..45ad0ad 100644 --- a/src/main/mastodon_bot/transform.cljs +++ b/src/main/mastodon_bot/transform.cljs @@ -73,12 +73,17 @@ (masto/post-items mastodon-config last-post-time))))) (defn-spec tweets-to-mastodon any? - [mastodon-config masto/mastodon-auth? - twitter-config twitter/twitter-auth? - accounts (s/* string?) + [mastodon-auth masto/mastodon-auth? + twitter-auth twitter/twitter-auth? + transformation ::transformation last-post-time any?] - (doseq [account accounts] - (twitter/user-timeline - twitter-config - account - (post-tweets mastodon-config last-post-time)))) + (let [{:keys [source target]} transformation + {:keys [accounts include-rts? include-replies?]} source] + (doseq [account accounts] + (println account) + (twitter/user-timeline + twitter-auth + include-rts? + include-replies? + account + (post-tweets mastodon-auth last-post-time))))) diff --git a/src/main/mastodon_bot/twitter_api.cljs b/src/main/mastodon_bot/twitter_api.cljs index f2856fb..16cb8e3 100755 --- a/src/main/mastodon_bot/twitter_api.cljs +++ b/src/main/mastodon_bot/twitter_api.cljs @@ -23,8 +23,8 @@ (def twitter-source? (s/keys :req-un [::include-rts? ::include-replies?])) (defn-spec twitter-client any? - [twitter-config twitter-auth?] - (let [{:keys [access-keys]} twitter-config] + [twitter-auth twitter-auth?] + (let [{:keys [access-keys]} twitter-auth] (try (twitter. (clj->js access-keys)) (catch js/Error e @@ -52,15 +52,16 @@ :media-links (keep #(when (= (:type %) "photo") (:media_url_https %)) media)}) (defn-spec user-timeline any? - [twitter-config twitter-auth? + [twitter-auth twitter-auth? + include-rts? ::include-rts? + include-replies? ::include-replies? account ::account callback fn?] - (let [{:keys [include-rts? include-replies?]} twitter-config] - (.get (twitter-client twitter-config) - "statuses/user_timeline" - #js {:screen_name account - :tweet_mode "extended" - :include_rts (boolean include-rts?) - :exclude_replies (not (boolean include-replies?))} - callback))) + (.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