Inline fields

This commit is contained in:
Simon Cambier 2024-06-23 10:02:44 +02:00
parent 89cad9b78c
commit fbea5ccde0
2 changed files with 43 additions and 13 deletions

View File

@ -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

21
media.janet Normal file
View File

@ -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)