Better download function
This commit is contained in:
parent
fbea5ccde0
commit
495c113792
21
main.janet
21
main.janet
|
@ -1,5 +1,6 @@
|
|||
(import httprequest :as r)
|
||||
(import spork/json :as json)
|
||||
(import ./media :as media)
|
||||
|
||||
(def apptoken "REDACTED")
|
||||
(def urls @["https://hachyderm.io/api/v1/bookmarks"])
|
||||
|
@ -37,24 +38,8 @@
|
|||
(def media (item "media_attachments"))
|
||||
(each m media
|
||||
(def url (m "url"))
|
||||
(def filename (array/pop (string/split "/" url)))
|
||||
(def fullpath (string save-path "attachments/" filename))
|
||||
|
||||
(array/push files filename)
|
||||
|
||||
# Check if the file already exists
|
||||
(def existing (file/open fullpath :r))
|
||||
(if existing (file/close existing))
|
||||
|
||||
# If it doesn't exist, download it
|
||||
(if (not existing)
|
||||
(do
|
||||
(print (string "Downloading " url))
|
||||
(def response
|
||||
(r/get url {"Authorization" (string "Bearer " apptoken)} {}))
|
||||
(def fout (file/open fullpath :w))
|
||||
(file/write fout (response :body))
|
||||
(file/close fout))))
|
||||
(def folder (string save-path "attachments/"))
|
||||
(array/push files (string "attachments/" (media/get-image url folder))))
|
||||
files)
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
(import httprequest :as r)
|
||||
|
||||
(defn get-image [url path &named headers opts]
|
||||
(defn get-image [url folder &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))
|
||||
(def fullpath (string folder filename))
|
||||
|
||||
# Check if the file already exists
|
||||
(def existing (file/open fullpath :r))
|
||||
|
@ -18,4 +18,4 @@
|
|||
(def fout (file/open fullpath :w))
|
||||
(file/write fout (response :body))
|
||||
(file/close fout)))
|
||||
fullpath)
|
||||
filename)
|
||||
|
|
Loading…
Reference in New Issue
Block a user