refactor parse-tweet

This commit is contained in:
jem 2020-05-21 13:54:56 +02:00
parent a0adf99dc3
commit 8ccdbde8ec
3 changed files with 32 additions and 15 deletions

View file

@ -32,18 +32,6 @@
(def config (infra/load-config))
(defn parse-tweet [{created-at :created_at
text :full_text
{:keys [media]} :extended_entities
{:keys [screen_name]} :user :as tweet}]
{:created-at (js/Date. created-at)
:text (transform/trim-text
(str (twitter/chop-tail-media-url text media)
(if (masto/append-screen-name? (mastodon-config config))
(str "\n - " screen_name) ""))
(masto/max-post-length (mastodon-config config)))
:media-links (keep #(when (= (:type %) "photo") (:media_url_https %)) media)})
(defmulti parse-tumblr-post :type)
(defmethod parse-tumblr-post "text" [{:keys [body date short_url]}]
@ -75,7 +63,9 @@
(if error
(infra/exit-with-error error)
(->> (infra/js->edn tweets)
(map parse-tweet)
(map twitter/parse-tweet)
(map #(transform/to-mastodon
(mastodon-config config) %))
(masto/post-items
(mastodon-config config)
last-post-time)))))

View file

@ -3,7 +3,14 @@
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st]
[orchestra.core :refer-macros [defn-spec]]
[clojure.string :as string]))
[clojure.string :as string]
[mastodon-bot.mastodon-api :as masto]))
(s/def ::created-at any?)
(s/def ::text string?)
(s/def ::media-links string?)
(s/def ::screen_name string?)
(def element? (s/keys :req-un [::created-at ::text ::media-links ::screen_name]))
(defn trim-text [text max-post-length]
(cond
@ -20,4 +27,16 @@
""
(string/split text #" "))
:else text))
:else text))
(defn-spec to-mastodon any?
[mastodon-config masto/mastodon-config?
input element?]
(let [{:keys [created-at text media-links screen_name]} input]
{:created-at created-at
:text (trim-text
(str text
(if (masto/append-screen-name? mastodon-config)
(str "\n - " screen_name) ""))
(masto/max-post-length mastodon-config))
:media-links media-links}))

View file

@ -41,6 +41,14 @@
(defn chop-tail-media-url [text media]
(string/replace text #" (\S+)$" #(if (in (%1 1) (map :url media)) "" (%1 0))))
(defn parse-tweet [{created-at :created_at
text :full_text
{:keys [media]} :extended_entities
{:keys [screen_name]} :user :as tweet}]
{:created-at (js/Date. created-at)
:text (chop-tail-media-url text media)
:media-links (keep #(when (= (:type %) "photo") (:media_url_https %)) media)})
(defn-spec user-timeline any?
[twitter-config twitter-config?
account ::account