janet-mastodon-bookmarks/main.janet

117 lines
3.2 KiB
Plaintext

(import httprequest :as r)
(import spork/json :as json)
(import ./utils :as utils)
(var server "")
(var apptoken "")
(var dbg false)
(def urls @[])
(def save-path "./bookmarks/")
(def done @[])
(defn download-media [item]
"Downloads the media from the item."
(def files @[])
(def media (item "media_attachments"))
(if dbg
(pp (item "media_attachments")))
(each m media
(def url (m "url"))
(def folder (string save-path "attachments/"))
(array/push files {:path (string "./attachments/" (utils/get-image url folder))
:description (m "description")}))
(if dbg (pp files))
files)
(defn write-markdown [item]
(def files (download-media item))
(def id (item "id"))
(def path (string save-path id ".md"))
(if (utils/exists? path)
(do
(print (string "Skipping " id))
(break)))
(def fout (file/open path :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 :path) ")\n "
(arg :description)))
files) " "))
(file/write fout
(string
# inline metadata
inline "\n\n"
# post content
(string (item "content") "\n\n")
# embedded media
media))
(file/close fout))
(defn download-bookmarks []
"Downloads the bookmarks and saves them to a file."
(while (>= (length urls) 0)
(def url (utils/array-shift urls))
# Empty array means we're done
(if (not url)
(do
(print "Done")
(os/exit 0)))
(print (string "Downloading " url))
(def response
(r/get url {"Authorization" (string "Bearer " apptoken)} {}))
(def bookmarks (json/decode (response :body) false true))
(if (and (struct? bookmarks) (bookmarks "error"))
(do
(print (string "Error: " (bookmarks "error")))
(os/exit 1)))
# Pagination
(def next-link (utils/get-link response "next"))
(if (and next-link (not (has-value? done next-link)))
(array/insert urls 0 next-link))
(def prev-link (utils/get-link response "prev"))
(if (and prev-link (not (has-value? done prev-link)))
(array/insert urls 0 prev-link))
# Debugging - write response to file
(if dbg (do (def fout (file/open "./response.json" :w))
(file/write fout (response :body))))
# Write the bookmarks to md files
(each item bookmarks
(write-markdown item))
(array/push done url)))
(defn main [& args]
(set server (args 1))
(set apptoken (args 2))
(set dbg (get args 3))
(array/push urls (string server "/api/v1/bookmarks"))
(os/mkdir "./bookmarks")
(os/mkdir "./bookmarks/attachments")
(download-bookmarks))