This repository has been archived on 2023-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-bot/src/main/mastodon_bot/core.cljs

84 lines
2.7 KiB
Text
Raw Normal View History

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]]
[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-30 15:14:49 +00:00
(s/def ::mastodon masto/mastodon-auth?)
2020-05-26 15:10:58 +00:00
(s/def ::twitter twitter/twitter-auth?)
2020-06-08 17:47:04 +00:00
(s/def ::tumblr tumblr/tumblr-auth?)
2020-05-30 15:14:49 +00:00
(s/def ::transform transform/transformations?)
2020-06-08 17:47:04 +00:00
(s/def ::auth (s/keys :opt-un [::mastodon ::twitter ::tumblr]))
2020-05-30 15:14:49 +00:00
(def config?
(s/keys :req-un [::auth ::transform]))
2020-05-13 15:49:37 +00:00
2020-05-30 15:14:49 +00:00
(defn-spec mastodon-auth ::mastodon
[config config?]
2020-05-30 15:14:49 +00:00
(get-in config [:auth :mastodon]))
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?]
2020-05-30 15:14:49 +00:00
(get-in config [:auth :twitter]))
2020-05-15 17:25:31 +00:00
2020-06-08 17:47:04 +00:00
(defn-spec tumblr-auth ::tumblr
[config config?]
(get-in config [:auth :tumblr]))
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 -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-31 17:59:20 +00:00
(let [{:keys [transform]} config]
(doseq [transformation transform]
2020-06-08 17:47:04 +00:00
(let [source-type (get-in transformation [:source :source-type])
target-type (get-in transformation [:target :target-type])]
2020-05-31 17:59:20 +00:00
(cond
;;post from Twitter
2020-06-08 17:47:04 +00:00
(and (= :twitter source-type)
(= :mastodon target-type))
2020-05-31 17:59:20 +00:00
(when-let [twitter-auth (twitter-auth config)]
(transform/tweets-to-mastodon
mastodon-auth
twitter-auth
transformation
last-post-time))
;;post from RSS
2020-06-08 17:47:04 +00:00
(and (= :rss source-type)
(= :mastodon target-type))
2020-05-31 17:59:20 +00:00
(transform/rss-to-mastodon
mastodon-auth
transformation
last-post-time)
;;post from Tumblr
2020-06-08 17:47:04 +00:00
(and (= :tumblr source-type)
(= :mastodon target-type))
(when-let [tumblr-auth (tumblr-auth config)]
(transform/tumblr-to-mastodon
mastodon-auth
tumblr-auth
transformation
last-post-time))
))))
2020-05-31 17:59:20 +00:00
)))))
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)