From 1f411891a82fd1a1a96e987389ff65d10101c703 Mon Sep 17 00:00:00 2001 From: "dmitri.sotnikov@gmail.com" Date: Tue, 20 Mar 2018 08:57:25 -0400 Subject: [PATCH] added RSS support --- README.md | 5 ++++- mastodon-bot.cljs | 18 +++++++++++++++++- package.json | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2deb3f0..23b96cd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ### description -the bot will read the timeline from the specified Twitter/Tumblr accounts, and post it to Mastodon +the bot will post the timeline from the specified Twitter/Tumblr accounts and RSS feeds to Mastodon ### installation @@ -30,6 +30,9 @@ the bot will read the timeline from the specified Twitter/Tumblr accounts, and p :token "XXXX" :token_secret "XXXX"} :accounts ["cyberpunky.tumblr.com" "scipunk.tumblr.com"]} + ;; add RSS config to follow feeds + :rss {"Hacker News" "https://hnrss.org/newest" + "r/Clojure" "https://www.reddit.com/r/clojure/.rss"} :mastodon {:access_token "XXXX" :api_url "https://botsin.space/api/v1/"}} ``` diff --git a/mastodon-bot.cljs b/mastodon-bot.cljs index 089ddba..76081a4 100755 --- a/mastodon-bot.cljs +++ b/mastodon-bot.cljs @@ -9,6 +9,7 @@ ["http" :as http] ["https" :as https] ["mastodon-api" :as mastodon] + ["rss-parser" :as rss] ["tumblr" :as tumblr] ["twitter" :as twitter])) @@ -97,15 +98,30 @@ (map parse-tweet) (post-items last-post-time)))) +(defn parse-feed [last-post-time parser [title url]] + (-> (.parseURL parser url) + (.then #(post-items + last-post-time + (for [{:keys [title isoDate content link]} (-> % js->edn :items)] + {:created-at (js/Date. isoDate) + :text (str title "\n\n" link)}))))) + (get-mastodon-timeline (fn [timeline] (let [last-post-time (-> timeline first :created_at (js/Date.))] + ;;post from Twitter (when-let [twitter-client (some-> config :twitter :access-keys clj->js twitter.)] (doseq [account (-> config :twitter :accounts)] (.get twitter-client "statuses/user_timeline" #js {:screen_name account :include_rts false} (post-tweets last-post-time)))) + ;;post from Tumblr (when-let [tumblr-oauth (some-> config :tumblr :access-keys clj->js)] (when-let [tumblr-client (some-> config :tumblr :accounts first (tumblr/Blog. tumblr-oauth))] - (.posts tumblr-client #js {:limit 5} (post-tumblrs last-post-time))))))) + (.posts tumblr-client #js {:limit 5} (post-tumblrs last-post-time)))) + ;;post from RSS + (when-let [feeds (some-> config :rss)] + (let [parser (rss.)] + (doseq [feed feeds] + (parse-feed last-post-time parser feed))))))) diff --git a/package.json b/package.json index 9c94560..2b757a6 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "dependencies": { "lumo-cljs": "^1.8.0", "mastodon-api": "1.3.0", + "rss-parser": "3.1.2", "tumblr": "0.4.1", "twitter": "1.7.1" },