introduce transform

This commit is contained in:
jem 2020-05-21 12:20:50 +02:00
parent 3576ca2352
commit 0cfe940226
2 changed files with 40 additions and 23 deletions

View file

@ -10,6 +10,7 @@
["rss-parser" :as rss] ["rss-parser" :as rss]
["tumblr" :as tumblr] ["tumblr" :as tumblr]
[mastodon-bot.infra :as infra] [mastodon-bot.infra :as infra]
[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]))
@ -31,24 +32,6 @@
(def config (infra/load-config)) (def config (infra/load-config))
(defn trim-text [text]
(let [max-post-length (masto/max-post-length (mastodon-config config))]
(cond
(nil? max-post-length)
text
(> (count text) max-post-length)
(reduce
(fn [text word]
(if (> (+ (count text) (count word)) (- max-post-length 3))
(reduced (str text "..."))
(str text " " word)))
""
(string/split text #" "))
:else text)))
(defn in [needle haystack] (defn in [needle haystack]
(some (partial = needle) haystack)) (some (partial = needle) haystack))
@ -62,16 +45,21 @@
{:keys [media]} :extended_entities {:keys [media]} :extended_entities
{:keys [screen_name]} :user :as tweet}] {:keys [screen_name]} :user :as tweet}]
{:created-at (js/Date. created-at) {:created-at (js/Date. created-at)
:text (trim-text (str (chop-tail-media-url text media) :text (transform/trim-text
(if (masto/append-screen-name? (mastodon-config config)) (mastodon-config config)
(str "\n - " screen_name) ""))) (str (chop-tail-media-url text media)
(if (masto/append-screen-name? (mastodon-config config))
(str "\n - " screen_name) "")))
:media-links (keep #(when (= (:type %) "photo") (:media_url_https %)) media)}) :media-links (keep #(when (= (:type %) "photo") (:media_url_https %)) media)})
(defmulti parse-tumblr-post :type) (defmulti parse-tumblr-post :type)
(defmethod parse-tumblr-post "text" [{:keys [body date short_url]}] (defmethod parse-tumblr-post "text" [{:keys [body date short_url]}]
{:created-at (js/Date. date) {:created-at (js/Date. date)
:text (str (trim-text body) "\n\n" short_url)}) :text (str (transform/trim-text
(mastodon-config config)
body)
"\n\n" short_url)})
(defmethod parse-tumblr-post "photo" [{:keys [caption date photos short_url] :as post}] (defmethod parse-tumblr-post "photo" [{:keys [caption date photos short_url] :as post}]
{:created-at (js/Date. date) {:created-at (js/Date. date)
@ -107,7 +95,10 @@
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 (trim-text title) "\n\n" (twitter/strip-utm link))}))))) :text (str (transform/trim-text
(mastodon-config config)
title)
"\n\n" (twitter/strip-utm link))})))))
(defn tumblr-client [access-keys account] (defn tumblr-client [access-keys account]
(try (try

View file

@ -0,0 +1,26 @@
(ns mastodon-bot.transform
(:require
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st]
[orchestra.core :refer-macros [defn-spec]]
[clojure.string :as string]
;; TODO: not allowed dep - move needed config parts to this ns
[mastodon-bot.mastodon-api :as masto]))
(defn trim-text [masto-config text]
(let [max-post-length (masto/max-post-length masto-config)]
(cond
(nil? max-post-length)
text
(> (count text) max-post-length)
(reduce
(fn [text word]
(if (> (+ (count text) (count word)) (- max-post-length 3))
(reduced (str text "..."))
(str text " " word)))
""
(string/split text #" "))
:else text)))