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 apptoken "REDACTED")
(def urls @["https://hachyderm.io/api/v1/bookmarks"]) (def urls @["https://hachyderm.io/api/v1/bookmarks"])
(def save-path "./bookmarks/")
(def done @[]) (def done @[])
(defn array/shift [arr] (defn array/shift [arr]
@ -36,7 +38,7 @@
(each m media (each m media
(def url (m "url")) (def url (m "url"))
(def filename (array/pop (string/split "/" 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) (array/push files filename)
@ -59,20 +61,27 @@
(defn write-markdown [item] (defn write-markdown [item]
(def files (download-media item)) (def files (download-media item))
(def id (item "id")) (def id (item "id"))
(def fout (file/open (string "./bookmarks/" id ".md") :w)) (def fout (file/open (string save-path id ".md") :w))
(def frontmatter (string "---\n"
"url: " (item "url") "\n" (def inline (string
"created: " (item "created_at") "\n" "mastodon-url:: " (item "url") " \n"
"user: " ((item "account") "url") "\n" "mastodon-user:: "
"---\n")) "[" ((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 (def media (string/join
(map (fn [arg] (string "![[" arg "]]")) files) " ")) (map (fn [arg] (string "![[" arg "]]")) files) " "))
(file/write fout (file/write fout
(string (string
# metadata # inline metadata
frontmatter inline "\n\n"
# content # post content
(string "\n" (item "content") "\n\n") (string (item "content") "\n\n")
# embedded media # embedded media
media)) media))
(file/close fout)) (file/close fout))
@ -97,8 +106,8 @@
(def bookmarks (json/decode (response :body))) (def bookmarks (json/decode (response :body)))
# Debugging - write response to file # Debugging - write response to file
# (def fout (file/open "./response.json" :w)) (def fout (file/open "./response.json" :w))
# (file/write fout (response :body)) (file/write fout (response :body))
# Write the bookmarks to md files # Write the bookmarks to md files
(each item bookmarks (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)