2024-06-21 22:59:04 +02:00
|
|
|
(import httprequest :as r)
|
|
|
|
(import spork/json :as json)
|
2024-06-23 10:23:18 +02:00
|
|
|
(import ./media :as media)
|
2024-06-21 22:59:04 +02:00
|
|
|
|
2024-06-22 23:42:37 +02:00
|
|
|
(def apptoken "REDACTED")
|
|
|
|
(def urls @["https://hachyderm.io/api/v1/bookmarks"])
|
2024-06-23 10:02:44 +02:00
|
|
|
(def save-path "./bookmarks/")
|
|
|
|
|
2024-06-22 23:42:37 +02:00
|
|
|
(def done @[])
|
|
|
|
|
|
|
|
(defn array/shift [arr]
|
|
|
|
"Removes the first element from the array."
|
|
|
|
(if (empty? arr) (break nil))
|
|
|
|
(def head (in arr 0))
|
|
|
|
(array/remove arr 0)
|
|
|
|
head)
|
|
|
|
|
2024-06-22 11:18:57 +02:00
|
|
|
|
|
|
|
(defn get-link [response rel]
|
|
|
|
"Gets the link with the specified relation from the response.
|
|
|
|
e.g. (get-link response \"next\")"
|
2024-06-22 23:42:37 +02:00
|
|
|
|
2024-06-22 11:18:57 +02:00
|
|
|
# Get the links from the headers
|
2024-06-22 23:42:37 +02:00
|
|
|
(def link-header ((response :headers) "Link"))
|
|
|
|
(if (= link-header nil) (break nil))
|
|
|
|
|
|
|
|
(def links (string/split "," link-header))
|
2024-06-22 11:18:57 +02:00
|
|
|
# Find the link with the specified relation
|
|
|
|
(def link (find |(not= (string/find rel $) nil) links))
|
|
|
|
# Extract the url from the link string (as an array of 1 element)
|
|
|
|
(def url (peg/match ~(sequence (any " ") "<" (capture (some (if-not ">" 1))) ">") link))
|
|
|
|
(if url (in url 0) nil))
|
2024-06-21 22:59:04 +02:00
|
|
|
|
2024-06-22 23:42:37 +02:00
|
|
|
|
|
|
|
(defn download-media [item]
|
|
|
|
"Downloads the media from the item."
|
|
|
|
(def files @[])
|
|
|
|
(def media (item "media_attachments"))
|
|
|
|
(each m media
|
|
|
|
(def url (m "url"))
|
2024-06-23 10:23:18 +02:00
|
|
|
(def folder (string save-path "attachments/"))
|
|
|
|
(array/push files (string "attachments/" (media/get-image url folder))))
|
2024-06-22 23:42:37 +02:00
|
|
|
files)
|
|
|
|
|
2024-06-21 22:59:04 +02:00
|
|
|
|
|
|
|
(defn write-markdown [item]
|
2024-06-22 23:42:37 +02:00
|
|
|
(def files (download-media item))
|
2024-06-21 22:59:04 +02:00
|
|
|
(def id (item "id"))
|
2024-06-23 10:02:44 +02:00
|
|
|
(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"))
|
|
|
|
|
2024-06-22 23:42:37 +02:00
|
|
|
(def media (string/join
|
|
|
|
(map (fn [arg] (string "![[" arg "]]")) files) " "))
|
2024-06-23 10:02:44 +02:00
|
|
|
|
2024-06-22 23:42:37 +02:00
|
|
|
(file/write fout
|
|
|
|
(string
|
2024-06-23 10:02:44 +02:00
|
|
|
# inline metadata
|
|
|
|
inline "\n\n"
|
|
|
|
# post content
|
|
|
|
(string (item "content") "\n\n")
|
2024-06-22 23:42:37 +02:00
|
|
|
# embedded media
|
|
|
|
media))
|
2024-06-21 22:59:04 +02:00
|
|
|
(file/close fout))
|
|
|
|
|
2024-06-22 23:42:37 +02:00
|
|
|
|
|
|
|
(defn download-bookmarks []
|
|
|
|
"Downloads the bookmarks and saves them to a file."
|
|
|
|
(while (>= (length urls) 0)
|
|
|
|
(def url (array/shift urls))
|
|
|
|
(print (string "Downloading " url))
|
|
|
|
|
|
|
|
(def response
|
|
|
|
(r/get url {"Authorization" (string "Bearer " apptoken)} {}))
|
|
|
|
|
|
|
|
# Pagination
|
|
|
|
(def next-link (get-link response "next"))
|
|
|
|
(if (and next-link (not (has-value? done next-link)))
|
|
|
|
(array/insert urls 0 next-link))
|
|
|
|
(def prev-link (get-link response "prev"))
|
|
|
|
(if (and prev-link (not (has-value? done prev-link)))
|
|
|
|
(array/insert urls 0 prev-link))
|
|
|
|
|
|
|
|
(def bookmarks (json/decode (response :body)))
|
|
|
|
# Debugging - write response to file
|
2024-06-23 10:02:44 +02:00
|
|
|
(def fout (file/open "./response.json" :w))
|
|
|
|
(file/write fout (response :body))
|
2024-06-22 23:42:37 +02:00
|
|
|
|
|
|
|
# Write the bookmarks to md files
|
|
|
|
(each item bookmarks
|
|
|
|
(write-markdown item))
|
|
|
|
|
|
|
|
(array/push done url)))
|
|
|
|
|
2024-06-21 22:59:04 +02:00
|
|
|
|
|
|
|
(defn main [& args]
|
|
|
|
(download-bookmarks)
|
2024-06-22 23:42:37 +02:00
|
|
|
(print "Done."))
|