2018-03-11 21:05:25 +00:00
|
|
|
#!/usr/bin/env lumo
|
|
|
|
(ns mastodon-bot.core
|
|
|
|
(:require
|
|
|
|
[cljs.core :refer [*command-line-args*]]
|
|
|
|
[cljs.reader :as edn]
|
2018-03-15 05:01:53 +00:00
|
|
|
[clojure.set :refer [rename-keys]]
|
|
|
|
[clojure.string :as string]
|
2018-03-11 21:05:25 +00:00
|
|
|
["fs" :as fs]
|
2018-03-15 05:01:53 +00:00
|
|
|
["http" :as http]
|
2018-03-11 21:05:25 +00:00
|
|
|
["https" :as https]
|
|
|
|
["mastodon-api" :as mastodon]
|
2018-03-20 12:57:25 +00:00
|
|
|
["rss-parser" :as rss]
|
2018-03-15 05:01:53 +00:00
|
|
|
["tumblr" :as tumblr]
|
2018-03-11 21:05:25 +00:00
|
|
|
["twitter" :as twitter]))
|
|
|
|
|
2018-08-18 17:25:48 +00:00
|
|
|
(defn exit-with-error [error]
|
|
|
|
(js/console.error error)
|
|
|
|
(js/process.exit 1))
|
|
|
|
|
2018-03-15 05:01:53 +00:00
|
|
|
(defn find-config []
|
|
|
|
(or (first *command-line-args*)
|
|
|
|
(-> js/process .-env .-MASTODON_BOT_CONFIG)
|
|
|
|
"config.edn"))
|
|
|
|
|
2018-05-18 20:08:41 +00:00
|
|
|
(def config (-> (find-config) (fs/readFileSync #js {:encoding "UTF-8"}) edn/read-string))
|
2018-03-15 05:01:53 +00:00
|
|
|
|
|
|
|
(def mastodon-client (or (some-> config :mastodon clj->js mastodon.)
|
2018-08-18 17:25:48 +00:00
|
|
|
(exit-with-error "missing Mastodon client configuration!")))
|
2018-03-11 21:05:25 +00:00
|
|
|
|
2018-08-18 00:02:35 +00:00
|
|
|
(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]
|
2018-08-18 17:25:48 +00:00
|
|
|
(boolean (some #(re-find % text) content-filter-regexes)))
|
2018-08-18 00:02:35 +00:00
|
|
|
|
2018-03-11 21:05:25 +00:00
|
|
|
(defn js->edn [data]
|
|
|
|
(js->clj data :keywordize-keys true))
|
|
|
|
|
2018-03-23 13:14:26 +00:00
|
|
|
(defn trim-text [text]
|
2018-08-18 00:02:35 +00:00
|
|
|
(cond
|
|
|
|
|
|
|
|
(nil? max-post-length)
|
|
|
|
text
|
|
|
|
|
|
|
|
(> (count text) max-post-length)
|
2018-03-23 13:14:26 +00:00
|
|
|
(reduce
|
|
|
|
(fn [text word]
|
|
|
|
(if (> (+ (count text) (count word)) (- max-post-length 3))
|
|
|
|
(reduced (str text "..."))
|
|
|
|
(str text " " word)))
|
|
|
|
""
|
|
|
|
(clojure.string/split text #" "))
|
2018-08-18 00:02:35 +00:00
|
|
|
|
|
|
|
:else text))
|
2018-03-23 13:14:26 +00:00
|
|
|
|
2018-03-11 21:05:25 +00:00
|
|
|
(defn delete-status [status]
|
|
|
|
(.delete mastodon-client (str "statuses/" status) #js {}))
|
|
|
|
|
|
|
|
(defn post-status
|
|
|
|
([status-text]
|
|
|
|
(post-status status-text nil))
|
|
|
|
([status-text media-ids]
|
2018-08-26 04:59:43 +00:00
|
|
|
(let [{:keys [sensitive signature visibility]} (:mastodon config)]
|
|
|
|
(.post mastodon-client "statuses"
|
|
|
|
(clj->js (merge {:status (if signature (str status-text "\n" signature) status-text)}
|
|
|
|
(when media-ids {:media_ids media-ids})
|
|
|
|
(when sensitive {:sensitive sensitive})
|
|
|
|
(when visibility {:visibility visibility})))))))
|
2018-03-11 21:05:25 +00:00
|
|
|
|
|
|
|
(defn post-image [image-stream description callback]
|
|
|
|
(-> (.post mastodon-client "media" #js {:file image-stream :description description})
|
|
|
|
(.then #(-> % .-data .-id callback))))
|
|
|
|
|
|
|
|
(defn post-status-with-images
|
|
|
|
([status-text urls]
|
|
|
|
(post-status-with-images status-text urls []))
|
|
|
|
([status-text [url & urls] ids]
|
|
|
|
(if url
|
2018-03-15 05:01:53 +00:00
|
|
|
(.get (if (string/starts-with? url "https://") https http) url
|
2018-03-11 21:05:25 +00:00
|
|
|
(fn [image-stream]
|
|
|
|
(post-image image-stream status-text #(post-status-with-images status-text urls (conj ids %)))))
|
|
|
|
(post-status status-text (not-empty ids)))))
|
|
|
|
|
|
|
|
(defn get-mastodon-timeline [callback]
|
|
|
|
(.then (.get mastodon-client "timelines/home" #js {}) #(-> % .-data js->edn callback)))
|
|
|
|
|
2018-03-15 05:01:53 +00:00
|
|
|
(defn post-items [last-post-time items]
|
2018-04-23 21:47:41 +00:00
|
|
|
(doseq [{:keys [text media-links]} (->> items
|
|
|
|
(remove #(blocked-content? (:text %)))
|
|
|
|
(filter #(> (:created-at %) last-post-time)))]
|
2018-03-15 05:01:53 +00:00
|
|
|
(if media-links
|
|
|
|
(post-status-with-images text media-links)
|
|
|
|
(post-status text))))
|
|
|
|
|
2018-03-11 21:05:25 +00:00
|
|
|
(defn parse-tweet [{created-at :created_at
|
|
|
|
text :text
|
|
|
|
{:keys [media]} :extended_entities
|
|
|
|
{:keys [screen_name]} :user :as tweet}]
|
|
|
|
{:created-at (js/Date. created-at)
|
2018-08-18 00:02:35 +00:00
|
|
|
:text (trim-text (if append-screen-name? (str text "\n - " screen_name) text))
|
2018-03-11 21:05:25 +00:00
|
|
|
:media-links (keep #(when (= (:type %) "photo") (:media_url_https %)) media)})
|
|
|
|
|
2018-03-15 05:01:53 +00:00
|
|
|
(defmulti parse-tumblr-post :type)
|
|
|
|
|
|
|
|
(defmethod parse-tumblr-post "text" [{:keys [body date short_url]}]
|
|
|
|
{:created-at (js/Date. date)
|
2018-03-23 13:14:26 +00:00
|
|
|
:text (str (trim-text body) "\n\n" short_url)})
|
2018-03-15 05:01:53 +00:00
|
|
|
|
|
|
|
(defmethod parse-tumblr-post "photo" [{:keys [caption date photos short_url] :as post}]
|
|
|
|
{:created-at (js/Date. date)
|
|
|
|
:text (string/join "\n" [(string/replace caption #"<[^>]*>" "") short_url])
|
|
|
|
:media-links (mapv #(-> % :original_size :url) photos)})
|
|
|
|
|
2018-03-15 13:24:44 +00:00
|
|
|
(defmethod parse-tumblr-post :default [post])
|
2018-03-15 05:01:53 +00:00
|
|
|
|
|
|
|
(defn post-tumblrs [last-post-time]
|
|
|
|
(fn [err response]
|
|
|
|
(->> response
|
|
|
|
js->edn
|
|
|
|
:posts
|
|
|
|
(mapv parse-tumblr-post)
|
|
|
|
(post-items last-post-time))))
|
|
|
|
|
2018-03-11 21:05:25 +00:00
|
|
|
(defn post-tweets [last-post-time]
|
|
|
|
(fn [error tweets response]
|
2018-03-15 05:01:53 +00:00
|
|
|
(->> (js->edn tweets)
|
|
|
|
(map parse-tweet)
|
|
|
|
(post-items last-post-time))))
|
2018-03-11 21:05:25 +00:00
|
|
|
|
2018-06-10 11:56:41 +00:00
|
|
|
(defn strip-utm [news-link]
|
2018-08-18 00:02:35 +00:00
|
|
|
(first (string/split news-link #"\?utm")))
|
2018-06-10 11:56:41 +00:00
|
|
|
|
2018-03-20 12:57:25 +00:00
|
|
|
(defn parse-feed [last-post-time parser [title url]]
|
|
|
|
(-> (.parseURL parser url)
|
|
|
|
(.then #(post-items
|
|
|
|
last-post-time
|
2018-03-23 13:14:26 +00:00
|
|
|
(for [{:keys [title isoDate pubDate content link]} (-> % js->edn :items)]
|
|
|
|
{:created-at (js/Date. (or isoDate pubDate))
|
2018-06-10 11:56:41 +00:00
|
|
|
:text (str (trim-text title) "\n\n" (strip-utm link))})))))
|
2018-03-20 12:57:25 +00:00
|
|
|
|
2018-08-18 17:25:48 +00:00
|
|
|
(defn twitter-client [access-keys]
|
|
|
|
(try
|
|
|
|
(twitter. (clj->js access-keys))
|
|
|
|
(catch js/Error e
|
|
|
|
(exit-with-error
|
|
|
|
(str "failed to connect to Twitter: " (.-message e))))))
|
|
|
|
|
|
|
|
(defn tumblr-client [access-keys account]
|
|
|
|
(try
|
|
|
|
(tumblr/Blog. account (clj->js access-keys))
|
|
|
|
(catch js/Error e
|
|
|
|
(exit-with-error
|
|
|
|
(str "failed to connect to Tumblr account " account ": " (.-message e))))))
|
|
|
|
|
2018-03-11 21:05:25 +00:00
|
|
|
(get-mastodon-timeline
|
|
|
|
(fn [timeline]
|
2018-03-15 05:01:53 +00:00
|
|
|
(let [last-post-time (-> timeline first :created_at (js/Date.))]
|
2018-03-20 12:57:25 +00:00
|
|
|
;;post from Twitter
|
2018-08-18 17:25:48 +00:00
|
|
|
(when-let [twitter-config (:twitter config)]
|
|
|
|
(let [{:keys [access-keys accounts include-replies? include-rts?]} twitter-config
|
|
|
|
client (twitter-client access-keys)]
|
|
|
|
(doseq [account accounts]
|
|
|
|
(.get client
|
2018-03-15 05:01:53 +00:00
|
|
|
"statuses/user_timeline"
|
2018-08-18 17:25:48 +00:00
|
|
|
#js {:screen_name account
|
|
|
|
:include_rts (boolean include-replies?)
|
|
|
|
:exclude_replies (boolean include-rts?)}
|
|
|
|
(post-tweets last-post-time)))))
|
2018-03-20 12:57:25 +00:00
|
|
|
;;post from Tumblr
|
2018-08-18 17:25:48 +00:00
|
|
|
(when-let [{:keys [accounts limit tumblr-oauth]} (:tumblr config)]
|
|
|
|
(doseq [account accounts]
|
|
|
|
(let [client (tumblr-client access-keys account)]
|
|
|
|
(.posts client #js {:limit (or limit 5)} (post-tumblrs last-post-time)))))
|
2018-03-20 12:57:25 +00:00
|
|
|
;;post from RSS
|
|
|
|
(when-let [feeds (some-> config :rss)]
|
|
|
|
(let [parser (rss.)]
|
|
|
|
(doseq [feed feeds]
|
|
|
|
(parse-feed last-post-time parser feed)))))))
|