2020-05-12 16:17:37 +00:00
|
|
|
#!/usr/bin/env lumo
|
|
|
|
|
|
|
|
(ns mastodon-bot.core
|
|
|
|
(:require
|
|
|
|
[clojure.spec.alpha :as s]
|
|
|
|
[clojure.spec.test.alpha :as st]
|
|
|
|
[orchestra.core :refer-macros [defn-spec]]
|
|
|
|
["rss-parser" :as rss]
|
|
|
|
[mastodon-bot.infra :as infra]
|
2020-05-21 10:20:50 +00:00
|
|
|
[mastodon-bot.transform :as transform]
|
2020-05-15 17:25:31 +00:00
|
|
|
[mastodon-bot.mastodon-api :as masto]
|
2020-05-22 08:14:19 +00:00
|
|
|
[mastodon-bot.twitter-api :as twitter]
|
|
|
|
[mastodon-bot.tumblr-api :as tumblr]
|
|
|
|
[cljs.core :refer [*command-line-args*]]))
|
2020-05-12 16:17:37 +00:00
|
|
|
|
2020-05-26 15:10:58 +00:00
|
|
|
(s/def ::mastodon-auth masto/mastodon-auth?)
|
|
|
|
(s/def ::transform transform/transformations?)
|
|
|
|
(s/def ::twitter twitter/twitter-auth?)
|
2020-05-13 15:41:27 +00:00
|
|
|
(s/def ::tumblr map?)
|
|
|
|
(s/def ::rss map?)
|
2020-05-13 15:49:37 +00:00
|
|
|
|
2020-05-14 06:56:50 +00:00
|
|
|
(def config? (s/keys :req-un [::mastodon-config]
|
|
|
|
:opt-un [::twitter ::tumblr ::rss]))
|
2020-05-12 16:17:37 +00:00
|
|
|
|
2020-05-26 15:10:58 +00:00
|
|
|
(defn-spec mastodon-auth ::mastodon-auth
|
2020-05-13 16:27:01 +00:00
|
|
|
[config config?]
|
2020-05-14 06:56:50 +00:00
|
|
|
(:mastodon-config config))
|
2020-05-12 16:17:37 +00:00
|
|
|
|
2020-05-26 15:10:58 +00:00
|
|
|
(defn-spec twitter-auth ::twitter
|
2020-05-15 17:25:31 +00:00
|
|
|
[config config?]
|
|
|
|
(:twitter config))
|
|
|
|
|
2020-05-26 15:10:58 +00:00
|
|
|
(defn-spec transform ::transform
|
|
|
|
[config config?]
|
|
|
|
(:transform config))
|
|
|
|
|
2020-05-14 06:56:50 +00:00
|
|
|
(def config (infra/load-config))
|
2020-05-12 16:17:37 +00:00
|
|
|
|
|
|
|
(defn post-tumblrs [last-post-time]
|
|
|
|
(fn [err response]
|
|
|
|
(->> response
|
|
|
|
infra/js->edn
|
|
|
|
:posts
|
2020-05-22 08:14:19 +00:00
|
|
|
(mapv tumblr/parse-tumblr-post)
|
|
|
|
(map #(transform/to-mastodon
|
2020-05-26 15:10:58 +00:00
|
|
|
(mastodon-auth config) %))
|
2020-05-12 16:17:37 +00:00
|
|
|
(masto/post-items
|
2020-05-26 15:10:58 +00:00
|
|
|
(mastodon-auth config)
|
2020-05-12 16:17:37 +00:00
|
|
|
last-post-time))))
|
|
|
|
|
|
|
|
(defn parse-feed [last-post-time parser [title url]]
|
|
|
|
(-> (.parseURL parser url)
|
|
|
|
(.then #(masto/post-items
|
2020-05-26 15:10:58 +00:00
|
|
|
(mastodon-auth config)
|
2020-05-12 16:17:37 +00:00
|
|
|
last-post-time
|
|
|
|
(for [{:keys [title isoDate pubDate content link]} (-> % infra/js->edn :items)]
|
|
|
|
{:created-at (js/Date. (or isoDate pubDate))
|
2020-05-21 10:20:50 +00:00
|
|
|
:text (str (transform/trim-text
|
2020-05-21 10:42:27 +00:00
|
|
|
title
|
2020-05-26 15:10:58 +00:00
|
|
|
(masto/max-post-length (mastodon-auth config)))
|
2020-05-21 10:20:50 +00:00
|
|
|
"\n\n" (twitter/strip-utm link))})))))
|
2020-05-12 16:17:37 +00:00
|
|
|
|
|
|
|
(defn -main []
|
2020-05-26 15:10:58 +00:00
|
|
|
(let [mastodon-auth (mastodon-auth config)]
|
|
|
|
(masto/get-mastodon-timeline
|
|
|
|
mastodon-auth
|
|
|
|
(fn [timeline]
|
|
|
|
(let [last-post-time (-> timeline first :created_at (js/Date.))]
|
2020-05-12 16:17:37 +00:00
|
|
|
;;post from Twitter
|
2020-05-26 15:10:58 +00:00
|
|
|
(when-let [twitter-auth (twitter-auth config)]
|
|
|
|
(let [{:keys [accounts]} twitter-auth]
|
|
|
|
(transform/tweets-to-mastodon
|
|
|
|
mastodon-auth
|
|
|
|
twitter-auth
|
|
|
|
accounts
|
|
|
|
last-post-time)))
|
2020-05-12 16:17:37 +00:00
|
|
|
;;post from Tumblr
|
2020-05-26 15:10:58 +00:00
|
|
|
(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)))))
|
2020-05-12 16:17:37 +00:00
|
|
|
;;post from RSS
|
2020-05-26 15:10:58 +00:00
|
|
|
(when-let [feeds (some-> config :rss)]
|
|
|
|
(let [parser (rss.)]
|
|
|
|
(doseq [feed feeds]
|
|
|
|
(parse-feed last-post-time parser feed)))))))))
|
2020-05-12 16:17:37 +00:00
|
|
|
|
|
|
|
(set! *main-cli-fn* -main)
|
2020-05-26 15:10:58 +00:00
|
|
|
(st/instrument 'mastodon-auth)
|
|
|
|
(st/instrument 'twitter-auth)
|
|
|
|
(st/instrument 'transform)
|