extract tumblr

This commit is contained in:
jem 2020-05-22 10:14:19 +02:00
parent 0a0e9fb2ae
commit 17dccf4ec8
2 changed files with 37 additions and 29 deletions

View file

@ -5,14 +5,13 @@
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st] [clojure.spec.test.alpha :as st]
[orchestra.core :refer-macros [defn-spec]] [orchestra.core :refer-macros [defn-spec]]
[cljs.core :refer [*command-line-args*]]
[clojure.string :as string]
["rss-parser" :as rss] ["rss-parser" :as rss]
["tumblr" :as tumblr]
[mastodon-bot.infra :as infra] [mastodon-bot.infra :as infra]
[mastodon-bot.transform :as transform] [mastodon-bot.transform :as transform]
[mastodon-bot.mastodon-api :as masto] [mastodon-bot.mastodon-api :as masto]
[mastodon-bot.twitter-api :as twitter])) [mastodon-bot.twitter-api :as twitter]
[mastodon-bot.tumblr-api :as tumblr]
[cljs.core :refer [*command-line-args*]]))
(s/def ::mastodon-config masto/mastodon-config?) (s/def ::mastodon-config masto/mastodon-config?)
(s/def ::twitter twitter/twitter-config?) (s/def ::twitter twitter/twitter-config?)
@ -32,28 +31,14 @@
(def config (infra/load-config)) (def config (infra/load-config))
(defmulti parse-tumblr-post :type)
(defmethod parse-tumblr-post "text" [{:keys [body date short_url]}]
{:created-at (js/Date. date)
:text (str (transform/trim-text
body
(masto/max-post-length (mastodon-config config)))
"\n\n" short_url)})
(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] (defn post-tumblrs [last-post-time]
(fn [err response] (fn [err response]
(->> response (->> response
infra/js->edn infra/js->edn
:posts :posts
(mapv parse-tumblr-post) (mapv tumblr/parse-tumblr-post)
(map #(transform/to-mastodon
(mastodon-config config) %))
(masto/post-items (masto/post-items
(mastodon-config config) (mastodon-config config)
last-post-time)))) last-post-time))))
@ -82,13 +67,6 @@
(masto/max-post-length (mastodon-config config))) (masto/max-post-length (mastodon-config config)))
"\n\n" (twitter/strip-utm link))}))))) "\n\n" (twitter/strip-utm link))})))))
(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 [] (defn -main []
(masto/get-mastodon-timeline (masto/get-mastodon-timeline
(mastodon-config config) (mastodon-config config)
@ -105,7 +83,7 @@
;;post from Tumblr ;;post from Tumblr
(when-let [{:keys [access-keys accounts limit]} (:tumblr config)] (when-let [{:keys [access-keys accounts limit]} (:tumblr config)]
(doseq [account accounts] (doseq [account accounts]
(let [client (tumblr-client access-keys account)] (let [client (tumblr/tumblr-client access-keys account)]
(.posts client #js {:limit (or limit 5)} (post-tumblrs last-post-time))))) (.posts client #js {:limit (or limit 5)} (post-tumblrs last-post-time)))))
;;post from RSS ;;post from RSS
(when-let [feeds (some-> config :rss)] (when-let [feeds (some-> config :rss)]

View file

@ -0,0 +1,30 @@
(ns mastodon-bot.tumblr-api
(:require
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st]
[orchestra.core :refer-macros [defn-spec]]
[clojure.string :as string]
[mastodon-bot.infra :as infra]
["tumblr" :as tumblr]
))
(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))))))
(defmulti parse-tumblr-post :type)
(defmethod parse-tumblr-post "text" [{:keys [body date short_url]}]
{:created-at (js/Date. date)
:text body
:untrimmed-text (str "\n\n" short_url)})
(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])