janet-mastodon-bookmarks/main.janet

48 lines
1.5 KiB
Plaintext
Raw Normal View History

2024-06-21 22:59:04 +02:00
(import httprequest :as r)
(import spork/json :as json)
(def url "https://hachyderm.io/api/v1/bookmarks")
2024-06-22 11:18:57 +02:00
(def auth "")
(defn get-link [response rel]
"Gets the link with the specified relation from the response.
e.g. (get-link response \"next\")"
# Get the links from the headers
(def links (string/split "," ((response :headers) "Link")))
# 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
(defn download-bookmarks []
"Downloads the bookmarks and saves them to a file."
(def response
(r/get url {"Authorization" (string "Bearer " auth)} {}))
2024-06-22 11:18:57 +02:00
(def next-link (get-link response "next"))
(def prev-link (get-link response "prev"))
(pp next-link)
(pp prev-link)
2024-06-21 22:59:04 +02:00
(def bookmarks (json/decode (response :body)))
(def fout (file/open "./response.json" :w))
(file/write fout (json/encode bookmarks)))
(defn write-markdown [item]
(def id (item "id"))
(def fout (file/open (string "./" id ".md") :w))
(file/write fout (item "content"))
(file/close fout))
(defn write-files []
"Writes the bookmarks to files."
(def fin (file/open "./response.json" :r))
(print (file/read fin :all))
(def bookmarks (json/decode (file/read fin :all)))
(each item bookmarks
(write-markdown item)))
(defn main [& args]
(download-bookmarks)
# (write-files)
(print "ok"))