From 6249124c170173793b2b30c4c88172b507972263 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Mon, 4 Aug 2025 09:07:58 +0200 Subject: [PATCH] add mastodon --- src/micro_blog/blue_sky.clj | 16 ------------ src/micro_blog/config.clj | 8 +++--- src/micro_blog/mastodon.clj | 49 +++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 src/micro_blog/mastodon.clj diff --git a/src/micro_blog/blue_sky.clj b/src/micro_blog/blue_sky.clj index bb1c5ce..b09ebb9 100644 --- a/src/micro_blog/blue_sky.clj +++ b/src/micro_blog/blue_sky.clj @@ -68,22 +68,6 @@ ;; recur :else (recur session id new-cursor new-and-prev-posts))))) -;; TODO: create post -;; const data = { -;; "remoteId": "test", -;; "authorId": "test", -;; "posted": "2022-01-01 10:00:00.123Z", -;; "source": "pleroma", -;; "tags": [ -;; "RELATION_RECORD_ID" -;; ], -;; "fullPost": "JSON", -;; "images": [ -;; "RELATION_RECORD_ID" -;; ] -;; }; -;; - (defn extract-tags [post] (let [facets (get (post :record) :facets []) features (flatten (map :features facets)) diff --git a/src/micro_blog/config.clj b/src/micro_blog/config.clj index d51c50b..b049413 100644 --- a/src/micro_blog/config.clj +++ b/src/micro_blog/config.clj @@ -6,7 +6,9 @@ {:pocket-base-pw "POCKET_BASE_PW" :pocket-base-host "POCKET_BASE_HOST" :blue-sky-api-key "BLUE_SKY_API_KEY" - :minstral-api-key "MISTRAL_API_KEY"}) + :minstral-api-key "MISTRAL_API_KEY" + :mastodon-host "MASTODON_BASE_URL" + :mastodon-account-id "MASTODON_ACCOUNT_ID"}) (defn- load-config [] (tel/log! :info "Config is being loaded") @@ -18,7 +20,7 @@ (def config (atom (load-config))) -(defn reload-config [] +(defn reload [] (reset! config (load-config))) (comment @@ -27,4 +29,4 @@ @config ;; Reload the config at runtime - (reload-config)) + (reload)) diff --git a/src/micro_blog/mastodon.clj b/src/micro_blog/mastodon.clj new file mode 100644 index 0000000..d350c95 --- /dev/null +++ b/src/micro_blog/mastodon.clj @@ -0,0 +1,49 @@ +(ns micro-blog.mastodon + (:require + [clj-http.client :as http-client] + [micro-blog.config :refer [config]] + [micro-blog.pocket-base :as pb] + [micro-blog.utils :as utils] + [taoensso.telemere :as tel])) + +(def post-res-schema [:sequential + [:map + [:id :string] + [:content :string] + [:account [:map [:id :string]]] + [:created_at :string] + [:tags [:vector [:map [:name :string]]]] + [:media_attachments [:vector [:map + [:url :string] + [:type [:= "image"]] + [:description :string]]]]]]) + +(defn get-posts-until-id [id] + (let [limit 10 + posts + (-> (http-client/get + (str (@config :mastodon-host) "/api/v1/accounts/" (@config :mastodon-account-id) "/statuses") + {:query-params {:limit limit} + :content-type :json + :as :json}) + :body + (utils/validate-with-throw post-res-schema))] + (take-while #(not= (:id %) id) posts))) + +(defn extract-images [post] + (let [images (get-in post [:embed :images] [])] + (map #(vector (:fullsize %) (:alt %)) images))) + +(defn run [] + (tel/log! :info "Running mastodon fetcher") + (let [last-saved-id (pb/get-latest-post-remote-id-by-source :mastodon) + new-posts (reverse (get-posts-until-id last-saved-id))] + (->> new-posts + (map #(hash-map :source :mastodon + :fullPost % + :remoteId (:id %) + :authorId (get-in % [:account :id]) + :tags (map :name (:tags %)) + :images (map (fn [img] [(:url img) (:description img)]) (:media_attachments %)) + :posted (:created_at %))) + (map pb/save-post))))