(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))))