From fbea5ccde06392ff8cdf4b7eded6ed46fb3bceb9 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Sun, 23 Jun 2024 10:02:44 +0200 Subject: [PATCH] Inline fields --- main.janet | 35 ++++++++++++++++++++++------------- media.janet | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 media.janet diff --git a/main.janet b/main.janet index 01a2898..8ef0c9b 100644 --- a/main.janet +++ b/main.janet @@ -3,6 +3,8 @@ (def apptoken "REDACTED") (def urls @["https://hachyderm.io/api/v1/bookmarks"]) +(def save-path "./bookmarks/") + (def done @[]) (defn array/shift [arr] @@ -36,7 +38,7 @@ (each m media (def url (m "url")) (def filename (array/pop (string/split "/" url))) - (def fullpath (string "./bookmarks/attachments/" filename)) + (def fullpath (string save-path "attachments/" filename)) (array/push files filename) @@ -59,20 +61,27 @@ (defn write-markdown [item] (def files (download-media item)) (def id (item "id")) - (def fout (file/open (string "./bookmarks/" id ".md") :w)) - (def frontmatter (string "---\n" - "url: " (item "url") "\n" - "created: " (item "created_at") "\n" - "user: " ((item "account") "url") "\n" - "---\n")) + (def fout (file/open (string save-path id ".md") :w)) + + (def inline (string + "mastodon-url:: " (item "url") " \n" + "mastodon-user:: " + "[" ((item "account") "acct") "]" + "(" ((item "account") "url") ")" " \n" + "mastodon-published:: " (item "created_at") " \n" + "mastodon-tags:: " (string/join + (map (fn [tag] (string "[" (tag "name") "]" "(" (tag "url") ")")) (item "tags")) " ") + " \n")) + (def media (string/join (map (fn [arg] (string "![[" arg "]]")) files) " ")) + (file/write fout (string - # metadata - frontmatter - # content - (string "\n" (item "content") "\n\n") + # inline metadata + inline "\n\n" + # post content + (string (item "content") "\n\n") # embedded media media)) (file/close fout)) @@ -97,8 +106,8 @@ (def bookmarks (json/decode (response :body))) # Debugging - write response to file - # (def fout (file/open "./response.json" :w)) - # (file/write fout (response :body)) + (def fout (file/open "./response.json" :w)) + (file/write fout (response :body)) # Write the bookmarks to md files (each item bookmarks diff --git a/media.janet b/media.janet new file mode 100644 index 0000000..5c3f520 --- /dev/null +++ b/media.janet @@ -0,0 +1,21 @@ +(import httprequest :as r) + +(defn get-image [url path &named headers opts] + "Download an image from a URL and save it to a file. Returns the path to the saved file." + (default headers {}) + (default opts {}) + + (def filename (array/pop (string/split "/" url))) + (def fullpath (string path filename)) + + # Check if the file already exists + (def existing (file/open fullpath :r)) + (if existing (file/close existing)) + (if (not existing) + (do + (print (string "Downloading " url)) + (def response (r/get url headers opts)) + (def fout (file/open fullpath :w)) + (file/write fout (response :body)) + (file/close fout))) + fullpath)