Extract links from headers
This commit is contained in:
parent
fe1755043f
commit
ff22cdc7d7
19
main.janet
19
main.janet
|
@ -2,14 +2,27 @@
|
||||||
(import spork/json :as json)
|
(import spork/json :as json)
|
||||||
|
|
||||||
(def url "https://hachyderm.io/api/v1/bookmarks")
|
(def url "https://hachyderm.io/api/v1/bookmarks")
|
||||||
(def auth "REDACTED")
|
(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))
|
||||||
|
|
||||||
(defn download-bookmarks []
|
(defn download-bookmarks []
|
||||||
"Downloads the bookmarks and saves them to a file."
|
"Downloads the bookmarks and saves them to a file."
|
||||||
(def response
|
(def response
|
||||||
(r/get url {"Authorization" (string "Bearer " auth)} {}))
|
(r/get url {"Authorization" (string "Bearer " auth)} {}))
|
||||||
(def links ((response :headers) "Link"))
|
(def next-link (get-link response "next"))
|
||||||
(pp links)
|
(def prev-link (get-link response "prev"))
|
||||||
|
(pp next-link)
|
||||||
|
(pp prev-link)
|
||||||
(def bookmarks (json/decode (response :body)))
|
(def bookmarks (json/decode (response :body)))
|
||||||
(def fout (file/open "./response.json" :w))
|
(def fout (file/open "./response.json" :w))
|
||||||
(file/write fout (json/encode bookmarks)))
|
(file/write fout (json/encode bookmarks)))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user