2024-06-23 10:02:44 +02:00
|
|
|
(import httprequest :as r)
|
|
|
|
|
2024-06-23 10:23:18 +02:00
|
|
|
(defn get-image [url folder &named headers opts]
|
2024-06-23 10:02:44 +02:00
|
|
|
"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)))
|
2024-06-23 10:23:18 +02:00
|
|
|
(def fullpath (string folder filename))
|
2024-06-23 10:02:44 +02:00
|
|
|
|
|
|
|
# Check if the file already exists
|
|
|
|
(def existing (file/open fullpath :r))
|
|
|
|
(if existing (file/close existing))
|
|
|
|
(if (not existing)
|
|
|
|
(do
|
|
|
|
(print (string "Downloading " url))
|
|
|
|
(def response (r/get url headers opts))
|
|
|
|
(def fout (file/open fullpath :w))
|
|
|
|
(file/write fout (response :body))
|
|
|
|
(file/close fout)))
|
2024-06-23 10:23:18 +02:00
|
|
|
filename)
|