From f90cc7d2a0d84708d3114c26616832a39cc263c8 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Mon, 24 Jun 2024 18:38:41 +0200 Subject: [PATCH] Removed logs and don't overwrite existing markdown files --- main.janet | 15 +++++++++++---- utils.janet | 7 ++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/main.janet b/main.janet index bc4cd81..69bc5e2 100644 --- a/main.janet +++ b/main.janet @@ -15,20 +15,27 @@ "Downloads the media from the item." (def files @[]) (def media (item "media_attachments")) - (pp (item "media_attachments")) + (if dbg + (pp (item "media_attachments"))) (each m media (def url (m "url")) (def folder (string save-path "attachments/")) (array/push files {:path (string "./attachments/" (utils/get-image url folder)) :description (m "description")})) - (pp files) + (if dbg (pp files)) files) (defn write-markdown [item] (def files (download-media item)) (def id (item "id")) - (def fout (file/open (string save-path id ".md") :w)) + (def path (string save-path id ".md")) + + (if (utils/exists? path) + (do + (print (string "Skipping " id)) + (break))) + (def fout (file/open path :w)) (def inline (string "mastodon-url:: " (item "url") " \n" @@ -103,7 +110,7 @@ (set dbg (get args 3)) (array/push urls (string server "/api/v1/bookmarks")) - + (os/mkdir "./bookmarks") (os/mkdir "./bookmarks/attachments") (download-bookmarks)) diff --git a/utils.janet b/utils.janet index 19332aa..f56340f 100644 --- a/utils.janet +++ b/utils.janet @@ -1,5 +1,10 @@ (import httprequest :as r) +(def exists? (fn [path] + "Returns whether a file exists at the specified path." + (def f (file/open path :r)) + (if f (do (file/close f) true) false))) + (defn get-image [url folder &named headers opts] "Download an image from a URL and save it to a file. Returns the path to the saved file." (default headers {}) @@ -42,4 +47,4 @@ (if (empty? arr) (break nil)) (def head (in arr 0)) (array/remove arr 0) - head) \ No newline at end of file + head)