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

118 lines
3.8 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]]
[cljs.core :refer [*command-line-args*]]
[clojure.string :as string]
["rss-parser" :as rss]
["tumblr" :as tumblr]
[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-15 17:31:36 +00:00
[mastodon-bot.twitter-api :as twitter]))
2020-05-12 16:17:37 +00:00
(s/def ::mastodon-config masto/mastodon-config?)
2020-05-15 17:31:36 +00:00
(s/def ::twitter twitter/twitter-config?)
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
(defn-spec mastodon-config ::mastodon-config
[config config?]
2020-05-14 06:56:50 +00:00
(:mastodon-config config))
2020-05-12 16:17:37 +00:00
2020-05-15 17:25:31 +00:00
(defn-spec twitter-config ::twitter
[config config?]
(:twitter config))
2020-05-14 06:56:50 +00:00
(def config (infra/load-config))
2020-05-12 16:17:37 +00:00
(defmulti parse-tumblr-post :type)
(defmethod parse-tumblr-post "text" [{:keys [body date short_url]}]
{:created-at (js/Date. date)
2020-05-21 10:20:50 +00:00
:text (str (transform/trim-text
2020-05-21 10:42:27 +00:00
body
(masto/max-post-length (mastodon-config config)))
2020-05-21 10:20:50 +00:00
"\n\n" short_url)})
2020-05-12 16:17:37 +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)})
(defmethod parse-tumblr-post :default [post])
(defn post-tumblrs [last-post-time]
(fn [err response]
(->> response
infra/js->edn
:posts
(mapv parse-tumblr-post)
(masto/post-items
(mastodon-config config)
last-post-time))))
(defn post-tweets [last-post-time]
(fn [error tweets response]
(if error
(infra/exit-with-error error)
(->> (infra/js->edn tweets)
2020-05-21 11:54:56 +00:00
(map twitter/parse-tweet)
(map #(transform/to-mastodon
(mastodon-config config) %))
2020-05-12 16:17:37 +00:00
(masto/post-items
(mastodon-config config)
last-post-time)))))
(defn parse-feed [last-post-time parser [title url]]
(-> (.parseURL parser url)
(.then #(masto/post-items
(mastodon-config config)
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
(masto/max-post-length (mastodon-config config)))
2020-05-21 10:20:50 +00:00
"\n\n" (twitter/strip-utm link))})))))
2020-05-12 16:17:37 +00:00
(defn tumblr-client [access-keys account]
(try
(tumblr/Blog. account (clj->js access-keys))
(catch js/Error e
(infra/exit-with-error
(str "failed to connect to Tumblr account " account ": " (.-message e))))))
(defn -main []
(masto/get-mastodon-timeline
(mastodon-config config)
(fn [timeline]
(let [last-post-time (-> timeline first :created_at (js/Date.))]
;;post from Twitter
2020-05-14 06:56:50 +00:00
(when-let [twitter-config (:twitter config)]
2020-05-15 17:25:31 +00:00
(let [{:keys [accounts]} twitter-config]
2020-05-12 16:17:37 +00:00
(doseq [account accounts]
2020-05-15 17:31:36 +00:00
(twitter/user-timeline
2020-05-15 17:25:31 +00:00
twitter-config
account
(post-tweets last-post-time)))))
2020-05-12 16:17:37 +00:00
;;post from Tumblr
(when-let [{:keys [access-keys accounts limit]} (:tumblr config)]
(doseq [account accounts]
(let [client (tumblr-client access-keys account)]
(.posts client #js {:limit (or limit 5)} (post-tumblrs last-post-time)))))
;;post from RSS
(when-let [feeds (some-> config :rss)]
(let [parser (rss.)]
(doseq [feed feeds]
(parse-feed last-post-time parser feed))))))))
(set! *main-cli-fn* -main)
2020-05-13 15:49:37 +00:00
(st/instrument 'mastodon-config)