add mastodon
This commit is contained in:
parent
e1d0de50c6
commit
6249124c17
3 changed files with 54 additions and 19 deletions
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
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