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] [mastodon-bot.tumblr-api :as tumblr]
[cljs.core :refer [*command-line-args*]])) [cljs.core :refer [*command-line-args*]]))
(s/def ::mastodon-config masto/mastodon-config?) (s/def ::mastodon-auth masto/mastodon-auth?)
(s/def ::twitter twitter/twitter-config?) (s/def ::transform transform/transformations?)
(s/def ::twitter twitter/twitter-auth?)
(s/def ::tumblr map?) (s/def ::tumblr map?)
(s/def ::rss map?) (s/def ::rss map?)
(def config? (s/keys :req-un [::mastodon-config] (def config? (s/keys :req-un [::mastodon-config]
:opt-un [::twitter ::tumblr ::rss])) :opt-un [::twitter ::tumblr ::rss]))
(defn-spec mastodon-config ::mastodon-config (defn-spec mastodon-auth ::mastodon-auth
[config config?] [config config?]
(:mastodon-config config)) (:mastodon-config config))
(defn-spec twitter-config ::twitter (defn-spec twitter-auth ::twitter
[config config?] [config config?]
(:twitter config)) (:twitter config))
(defn-spec transform ::transform
[config config?]
(:transform config))
(def config (infra/load-config)) (def config (infra/load-config))
(defn post-tumblrs [last-post-time] (defn post-tumblrs [last-post-time]
@ -38,34 +43,35 @@
:posts :posts
(mapv tumblr/parse-tumblr-post) (mapv tumblr/parse-tumblr-post)
(map #(transform/to-mastodon (map #(transform/to-mastodon
(mastodon-config config) %)) (mastodon-auth config) %))
(masto/post-items (masto/post-items
(mastodon-config config) (mastodon-auth config)
last-post-time)))) last-post-time))))
(defn parse-feed [last-post-time parser [title url]] (defn parse-feed [last-post-time parser [title url]]
(-> (.parseURL parser url) (-> (.parseURL parser url)
(.then #(masto/post-items (.then #(masto/post-items
(mastodon-config config) (mastodon-auth config)
last-post-time last-post-time
(for [{:keys [title isoDate pubDate content link]} (-> % infra/js->edn :items)] (for [{:keys [title isoDate pubDate content link]} (-> % infra/js->edn :items)]
{:created-at (js/Date. (or isoDate pubDate)) {:created-at (js/Date. (or isoDate pubDate))
:text (str (transform/trim-text :text (str (transform/trim-text
title title
(masto/max-post-length (mastodon-config config))) (masto/max-post-length (mastodon-auth config)))
"\n\n" (twitter/strip-utm link))}))))) "\n\n" (twitter/strip-utm link))})))))
(defn -main [] (defn -main []
(let [mastodon-auth (mastodon-auth config)]
(masto/get-mastodon-timeline (masto/get-mastodon-timeline
(mastodon-config config) mastodon-auth
(fn [timeline] (fn [timeline]
(let [last-post-time (-> timeline first :created_at (js/Date.))] (let [last-post-time (-> timeline first :created_at (js/Date.))]
;;post from Twitter ;;post from Twitter
(when-let [twitter-config (:twitter config)] (when-let [twitter-auth (twitter-auth config)]
(let [{:keys [accounts]} twitter-config] (let [{:keys [accounts]} twitter-auth]
(transform/tweets-to-mastodon (transform/tweets-to-mastodon
(mastodon-config config) mastodon-auth
twitter-config twitter-auth
accounts accounts
last-post-time))) last-post-time)))
;;post from Tumblr ;;post from Tumblr
@ -77,7 +83,9 @@
(when-let [feeds (some-> config :rss)] (when-let [feeds (some-> config :rss)]
(let [parser (rss.)] (let [parser (rss.)]
(doseq [feed feeds] (doseq [feed feeds]
(parse-feed last-post-time parser feed)))))))) (parse-feed last-post-time parser feed)))))))))
(set! *main-cli-fn* -main) (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 ::content-filters (s/* ::content-filter))
(s/def ::keyword-filters (s/* ::keyword-filter)) (s/def ::keyword-filters (s/* ::keyword-filter))
(s/def ::mastodon-js-config (s/keys :req-un [::access_token ::api_url])) (def mastodon-auth? (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-transform? (s/keys :req-un [::account-id ::content-filters ::keyword-filters
::max-post-length ::signature ::visibility ::max-post-length ::signature ::visibility
::append-screen-name? ::sensitive? ::resolve-urls? ::append-screen-name? ::sensitive? ::resolve-urls?
::nitter-urls? ::replacements])) ::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 (defn-spec content-filter-regexes ::content-filters
[mastodon-config mastodon-config?] [mastodon-config mastodon-config?]
@ -57,9 +57,11 @@
(reduce-kv string/replace text (:replacements mastodon-config))) (reduce-kv string/replace text (:replacements mastodon-config)))
(defn-spec mastodon-client any? (defn-spec mastodon-client any?
[mastodon-config mastodon-config?] [mastodon-auth mastodon-auth?]
(or (some-> mastodon-config clj->js mastodon.) (or (some-> mastodon-auth
(infra/exit-with-error "missing Mastodon client configuration!"))) clj->js
mastodon.)
(infra/exit-with-error "missing Mastodon auth configuration!")))
(defn-spec blocked-content? boolean? (defn-spec blocked-content? boolean?
[mastodon-config mastodon-config? [mastodon-config mastodon-config?

View file

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