add mastodon
This commit is contained in:
parent
e1d0de50c6
commit
6249124c17
3 changed files with 54 additions and 19 deletions
49
src/micro_blog/mastodon.clj
Normal file
49
src/micro_blog/mastodon.clj
Normal file
|
|
@ -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))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue