From 350b265000a9438c97de653f32dc95dc34f16360 Mon Sep 17 00:00:00 2001 From: Dmitri Sotnikov Date: Fri, 17 Aug 2018 20:02:35 -0400 Subject: [PATCH] made screenname optional, removed default post limit --- README.md | 4 ++++ mastodon-bot.cljs | 31 +++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 21d2e4b..60b045d 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,11 @@ If you get a [permission failure](https://github.com/anmonteiro/lumo/issues/206) "r/Clojure" "https://www.reddit.com/r/clojure/.rss"} :mastodon {:access_token "XXXX" :api_url "https://botsin.space/api/v1/" + ;; optional limit for the post length :max-post-length 300 + ;; 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" ;; optional content filter regexes diff --git a/mastodon-bot.cljs b/mastodon-bot.cljs index 2bb8903..4012b8c 100755 --- a/mastodon-bot.cljs +++ b/mastodon-bot.cljs @@ -20,23 +20,30 @@ (def config (-> (find-config) (fs/readFileSync #js {:encoding "UTF-8"}) edn/read-string)) -(def content-filter-regexes (mapv re-pattern (-> config :mastodon :content-filters))) - -(defn blocked-content? [text] - (boolean (some #(re-matches % text) content-filter-regexes))) - -(def max-post-length (or (-> config :mastodon :max-post-length) 300)) - (def mastodon-client (or (some-> config :mastodon clj->js mastodon.) (do (js/console.error "missing Mastodon client configuration!") (js/process.exit 1)))) +(def content-filter-regexes (mapv re-pattern (-> config :mastodon :content-filters))) + +(def append-screen-name? (boolean (-> config :mastodon :append-screen-name?))) + +(def max-post-length (-> config :mastodon :max-post-length)) + +(defn blocked-content? [text] + (boolean (some #(re-matches % text) content-filter-regexes))) + (defn js->edn [data] (js->clj data :keywordize-keys true)) (defn trim-text [text] - (if (> (count text) max-post-length) + (cond + + (nil? max-post-length) + text + + (> (count text) max-post-length) (reduce (fn [text word] (if (> (+ (count text) (count word)) (- max-post-length 3)) @@ -44,7 +51,8 @@ (str text " " word))) "" (clojure.string/split text #" ")) - text)) + + :else text)) (defn delete-status [status] (.delete mastodon-client (str "statuses/" status) #js {})) @@ -89,7 +97,7 @@ {:keys [media]} :extended_entities {:keys [screen_name]} :user :as tweet}] {:created-at (js/Date. created-at) - :text (str text "\n - " screen_name) + :text (trim-text (if append-screen-name? (str text "\n - " screen_name) text)) :media-links (keep #(when (= (:type %) "photo") (:media_url_https %)) media)}) (defmulti parse-tumblr-post :type) @@ -120,8 +128,7 @@ (post-items last-post-time)))) (defn strip-utm [news-link] - (first - (string/split news-link #"\?utm"))) + (first (string/split news-link #"\?utm"))) (defn parse-feed [last-post-time parser [title url]] (-> (.parseURL parser url)