prepare auth / transform separation

This commit is contained in:
jem 2020-05-26 17:10:58 +02:00
parent 27b29af40a
commit 9a4d754a1d
3 changed files with 50 additions and 38 deletions

View file

@ -13,22 +13,27 @@
[mastodon-bot.tumblr-api :as tumblr]
[cljs.core :refer [*command-line-args*]]))
(s/def ::mastodon-config masto/mastodon-config?)
(s/def ::twitter twitter/twitter-config?)
(s/def ::mastodon-auth masto/mastodon-auth?)
(s/def ::transform transform/transformations?)
(s/def ::twitter twitter/twitter-auth?)
(s/def ::tumblr map?)
(s/def ::rss map?)
(def config? (s/keys :req-un [::mastodon-config]
:opt-un [::twitter ::tumblr ::rss]))
(defn-spec mastodon-config ::mastodon-config
(defn-spec mastodon-auth ::mastodon-auth
[config config?]
(:mastodon-config config))
(defn-spec twitter-config ::twitter
(defn-spec twitter-auth ::twitter
[config config?]
(:twitter config))
(defn-spec transform ::transform
[config config?]
(:transform config))
(def config (infra/load-config))
(defn post-tumblrs [last-post-time]
@ -38,34 +43,35 @@
:posts
(mapv tumblr/parse-tumblr-post)
(map #(transform/to-mastodon
(mastodon-config config) %))
(mastodon-auth config) %))
(masto/post-items
(mastodon-config config)
(mastodon-auth config)
last-post-time))))
(defn parse-feed [last-post-time parser [title url]]
(-> (.parseURL parser url)
(.then #(masto/post-items
(mastodon-config config)
(mastodon-auth config)
last-post-time
(for [{:keys [title isoDate pubDate content link]} (-> % infra/js->edn :items)]
{:created-at (js/Date. (or isoDate pubDate))
:text (str (transform/trim-text
title
(masto/max-post-length (mastodon-config config)))
(masto/max-post-length (mastodon-auth config)))
"\n\n" (twitter/strip-utm link))})))))
(defn -main []
(let [mastodon-auth (mastodon-auth config)]
(masto/get-mastodon-timeline
(mastodon-config config)
mastodon-auth
(fn [timeline]
(let [last-post-time (-> timeline first :created_at (js/Date.))]
;;post from Twitter
(when-let [twitter-config (:twitter config)]
(let [{:keys [accounts]} twitter-config]
(when-let [twitter-auth (twitter-auth config)]
(let [{:keys [accounts]} twitter-auth]
(transform/tweets-to-mastodon
(mastodon-config config)
twitter-config
mastodon-auth
twitter-auth
accounts
last-post-time)))
;;post from Tumblr
@ -77,7 +83,9 @@
(when-let [feeds (some-> config :rss)]
(let [parser (rss.)]
(doseq [feed feeds]
(parse-feed last-post-time parser feed))))))))
(parse-feed last-post-time parser feed)))))))))
(set! *main-cli-fn* -main)
(st/instrument 'mastodon-config)
(st/instrument 'mastodon-auth)
(st/instrument 'twitter-auth)
(st/instrument 'transform)

View file

@ -28,12 +28,12 @@
(s/def ::content-filters (s/* ::content-filter))
(s/def ::keyword-filters (s/* ::keyword-filter))
(s/def ::mastodon-js-config (s/keys :req-un [::access_token ::api_url]))
(s/def ::mastodon-clj-config (s/keys :req-un [::account-id ::content-filters ::keyword-filters
(def mastodon-auth? (s/keys :req-un [::access_token ::api_url]))
(def mastodon-transform? (s/keys :req-un [::account-id ::content-filters ::keyword-filters
::max-post-length ::signature ::visibility
::append-screen-name? ::sensitive? ::resolve-urls?
::nitter-urls? ::replacements]))
(def mastodon-config? (s/merge ::mastodon-js-config ::mastodon-clj-config))
(def mastodon-config? (s/merge mastodon-auth? mastodon-transform?))
(defn-spec content-filter-regexes ::content-filters
[mastodon-config mastodon-config?]
@ -57,9 +57,11 @@
(reduce-kv string/replace text (:replacements mastodon-config)))
(defn-spec mastodon-client any?
[mastodon-config mastodon-config?]
(or (some-> mastodon-config clj->js mastodon.)
(infra/exit-with-error "missing Mastodon client configuration!")))
[mastodon-auth mastodon-auth?]
(or (some-> mastodon-auth
clj->js
mastodon.)
(infra/exit-with-error "missing Mastodon auth configuration!")))
(defn-spec blocked-content? boolean?
[mastodon-config mastodon-config?

View file

@ -19,6 +19,8 @@
(def mastodon-output? (s/keys :req-un [::created-at ::text]
:opt-un [::media-links]))
(def transformations? any?)
(defn trim-text [text max-post-length]
(cond
@ -37,7 +39,7 @@
:else text))
(defn-spec to-mastodon mastodon-output?
[mastodon-config masto/mastodon-config?
[mastodon-config masto/mastodon-auth?
input input?]
(let [{:keys [created-at text media-links screen_name untrimmed-text]} input
untrimmed (if (some? untrimmed-text)
@ -54,7 +56,7 @@
:media-links media-links}))
(defn-spec post-tweets any?
[mastodon-config masto/mastodon-config?
[mastodon-config masto/mastodon-auth?
last-post-time any?]
(fn [error tweets response]
(if error
@ -65,8 +67,8 @@
(masto/post-items mastodon-config last-post-time)))))
(defn-spec tweets-to-mastodon any?
[mastodon-config masto/mastodon-config?
twitter-config twitter/twitter-config?
[mastodon-config masto/mastodon-auth?
twitter-config twitter/twitter-auth?
accounts (s/* string?)
last-post-time any?]
(doseq [account accounts]