From 3a0588dd65fdcf6702202c76484a7dec5c4fc782 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Mon, 18 Aug 2025 10:05:16 +0200 Subject: [PATCH] Fix mastodon scrape when no img discription is set --- src/micro_blog/mastodon.clj | 4 ++-- src/micro_blog/pocket_base.clj | 25 +++++++++++++------------ src/micro_blog/utils.clj | 1 + 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/micro_blog/mastodon.clj b/src/micro_blog/mastodon.clj index 8b7f027..24b7d0d 100644 --- a/src/micro_blog/mastodon.clj +++ b/src/micro_blog/mastodon.clj @@ -17,7 +17,7 @@ [:media_attachments [:vector [:map [:url :string] [:type [:= "image"]] - [:description :string]]]]]]) + [:description [:maybe :string]]]]]]]) (defn get-posts-until-id [id] (let [limit 10 @@ -50,7 +50,7 @@ :remoteId (:id raw-post) :authorId (get-in raw-post [:account :id]) :tags (map :name (:tags raw-post)) - :images (map (fn [img] [(:url img) (:description img)]) (:media_attachments raw-post)) + :images (map (fn [img] [(:url img) (or (:description img) "")]) (:media_attachments raw-post)) :posted (:created_at raw-post))) (defn save-post [post] diff --git a/src/micro_blog/pocket_base.clj b/src/micro_blog/pocket_base.clj index a4dc520..f74e8c4 100644 --- a/src/micro_blog/pocket_base.clj +++ b/src/micro_blog/pocket_base.clj @@ -44,6 +44,10 @@ (defn valid-source? [source] (m/validate source-enum source)) +(def post-schema [:map + [:id :string] + [:remoteId :string]]) + (defn get-all-posts-by-source ([source] (get-all-posts-by-source source [] 1)) ([source carry page] @@ -68,14 +72,11 @@ (concat carry rows) (get-all-posts-by-source source (concat carry rows) (inc page)))))) -(defn get-latest-post-remote-id-by-source [source] +(defn get-latest-post-by-source [source] (let [res-schema [:map [:items - [:vector - [:map - [:id string?] - [:remoteId string?]]]]]] + [:vector post-schema]]]] (when (not (valid-source? source)) (throw (ex-info "Invalid source" {:source source}))) (as-> @@ -85,17 +86,17 @@ "perPage" 1 :sort "-posted" :filter (str "source = '" (name source) "'") - :fields (str/join "," ["remoteId" "id"]) + ;; :fields (str/join "," ["remoteId" "id"]) "skipTotal" true} :content-type :json :as :json}) x (:body x) - (if (m/validate res-schema x) - x - (do - (m/explain res-schema x) - (throw (ex-info "Res does not follow schema" {:res x})))) - (-> x :items first :remoteId)))) + (utils/validate-with-throw x res-schema) + (-> x :items first)))) + +(defn get-latest-post-remote-id-by-source [source] + (tel/log! {:level :info :data {:source source}} "Fetching latest post remote ID for source") + (:remoteId (get-latest-post-by-source source))) (defn post-with-remote-id-already-saved? [remote-id] (-> diff --git a/src/micro_blog/utils.clj b/src/micro_blog/utils.clj index be84ba5..a675c4c 100644 --- a/src/micro_blog/utils.clj +++ b/src/micro_blog/utils.clj @@ -4,6 +4,7 @@ [malli.core :as m])) (defn validate-with-throw [value schema] + (tel/log! {:level :info :data {:value value :schema schema}} "Validating value") (if (m/validate schema value) value (do