start refactoring for twitter
This commit is contained in:
parent
9e701382d8
commit
bcf1e8a30a
2 changed files with 61 additions and 23 deletions
|
@ -9,12 +9,12 @@
|
|||
[clojure.string :as string]
|
||||
["rss-parser" :as rss]
|
||||
["tumblr" :as tumblr]
|
||||
["twitter" :as twitter]
|
||||
[mastodon-bot.infra :as infra]
|
||||
[mastodon-bot.mastodon-api :as masto]))
|
||||
[mastodon-bot.mastodon-api :as masto]
|
||||
[mastodon-bot.twitter-api :as tw]))
|
||||
|
||||
(s/def ::mastodon-config masto/mastodon-config?)
|
||||
(s/def ::twitter map?)
|
||||
(s/def ::twitter tw/twitter-config?)
|
||||
(s/def ::tumblr map?)
|
||||
(s/def ::rss map?)
|
||||
|
||||
|
@ -25,6 +25,10 @@
|
|||
[config config?]
|
||||
(:mastodon-config config))
|
||||
|
||||
(defn-spec twitter-config ::twitter
|
||||
[config config?]
|
||||
(:twitter config))
|
||||
|
||||
(def config (infra/load-config))
|
||||
|
||||
(defn trim-text [text]
|
||||
|
@ -96,9 +100,6 @@
|
|||
(mastodon-config config)
|
||||
last-post-time)))))
|
||||
|
||||
(defn strip-utm [news-link]
|
||||
(first (string/split news-link #"\?utm")))
|
||||
|
||||
(defn parse-feed [last-post-time parser [title url]]
|
||||
(-> (.parseURL parser url)
|
||||
(.then #(masto/post-items
|
||||
|
@ -106,14 +107,7 @@
|
|||
last-post-time
|
||||
(for [{:keys [title isoDate pubDate content link]} (-> % infra/js->edn :items)]
|
||||
{:created-at (js/Date. (or isoDate pubDate))
|
||||
:text (str (trim-text title) "\n\n" (strip-utm link))})))))
|
||||
|
||||
(defn twitter-client [access-keys]
|
||||
(try
|
||||
(twitter. (clj->js access-keys))
|
||||
(catch js/Error e
|
||||
(infra/exit-with-error
|
||||
(str "failed to connect to Twitter: " (.-message e))))))
|
||||
:text (str (trim-text title) "\n\n" (tw/strip-utm link))})))))
|
||||
|
||||
(defn tumblr-client [access-keys account]
|
||||
(try
|
||||
|
@ -129,16 +123,12 @@
|
|||
(let [last-post-time (-> timeline first :created_at (js/Date.))]
|
||||
;;post from Twitter
|
||||
(when-let [twitter-config (:twitter config)]
|
||||
(let [{:keys [access-keys accounts include-replies? include-rts?]} twitter-config
|
||||
client (twitter-client access-keys)]
|
||||
(let [{:keys [accounts]} twitter-config]
|
||||
(doseq [account accounts]
|
||||
(.get client
|
||||
"statuses/user_timeline"
|
||||
#js {:screen_name account
|
||||
:tweet_mode "extended"
|
||||
:include_rts (boolean include-rts?)
|
||||
:exclude_replies (not (boolean include-replies?))}
|
||||
(post-tweets last-post-time)))))
|
||||
(tw/user-timeline
|
||||
twitter-config
|
||||
account
|
||||
(post-tweets last-post-time)))))
|
||||
;;post from Tumblr
|
||||
(when-let [{:keys [access-keys accounts limit]} (:tumblr config)]
|
||||
(doseq [account accounts]
|
||||
|
|
48
src/main/mastodon_bot/twitter_api.cljs
Executable file
48
src/main/mastodon_bot/twitter_api.cljs
Executable file
|
@ -0,0 +1,48 @@
|
|||
(ns mastodon-bot.twitter-api
|
||||
(:require
|
||||
[clojure.spec.alpha :as s]
|
||||
[clojure.spec.test.alpha :as st]
|
||||
[orchestra.core :refer-macros [defn-spec]]
|
||||
[clojure.string :as string]
|
||||
["twitter" :as twitter]
|
||||
[mastodon-bot.infra :as infra]
|
||||
))
|
||||
|
||||
(s/def ::consumer_key string?)
|
||||
(s/def ::consumer_secret string?)
|
||||
(s/def ::access_token_key string?)
|
||||
(s/def ::access_token_secret string?)
|
||||
(s/def ::access-keys (s/keys :req-un [::consumer_key ::consumer_secret ::access_token_key
|
||||
::access_token_secret]))
|
||||
|
||||
(s/def ::include-rts? boolean?)
|
||||
(s/def ::include-replies? boolean?)
|
||||
(s/def ::account string?)
|
||||
(s/def ::accounts (s/* ::account))
|
||||
(def twitter-config? (s/keys :req-un [::access-keys ::include-rts? ::include-replies?]))
|
||||
|
||||
(defn strip-utm [news-link]
|
||||
(first (string/split news-link #"\?utm")))
|
||||
|
||||
(defn-spec twitter-client any?
|
||||
[twitter-config twitter-config?]
|
||||
(let [{:keys [access-keys]} twitter-config]
|
||||
(try
|
||||
(twitter. (clj->js access-keys))
|
||||
(catch js/Error e
|
||||
(infra/exit-with-error
|
||||
(str "failed to connect to Twitter: " (.-message e)))))))
|
||||
|
||||
(defn-spec user-timeline any?
|
||||
[twitter-config twitter-config?
|
||||
account ::account
|
||||
callback fn?]
|
||||
(let [{:keys [include-rts? include-replies?]} twitter-config]
|
||||
(.get (twitter-client twitter-config)
|
||||
"statuses/user_timeline"
|
||||
#js {:screen_name account
|
||||
:tweet_mode "extended"
|
||||
:include_rts (boolean include-rts?)
|
||||
:exclude_replies (not (boolean include-replies?))}
|
||||
callback)))
|
||||
|
Reference in a new issue