Better download function

This commit is contained in:
Simon Cambier 2024-06-23 10:23:18 +02:00
parent fbea5ccde0
commit 495c113792
2 changed files with 6 additions and 21 deletions

View File

@ -1,5 +1,6 @@
(import httprequest :as r) (import httprequest :as r)
(import spork/json :as json) (import spork/json :as json)
(import ./media :as media)
(def apptoken "REDACTED") (def apptoken "REDACTED")
(def urls @["https://hachyderm.io/api/v1/bookmarks"]) (def urls @["https://hachyderm.io/api/v1/bookmarks"])
@ -37,24 +38,8 @@
(def media (item "media_attachments")) (def media (item "media_attachments"))
(each m media (each m media
(def url (m "url")) (def url (m "url"))
(def filename (array/pop (string/split "/" url))) (def folder (string save-path "attachments/"))
(def fullpath (string save-path "attachments/" filename)) (array/push files (string "attachments/" (media/get-image url folder))))
(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))))
files) files)

View File

@ -1,12 +1,12 @@
(import httprequest :as r) (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." "Download an image from a URL and save it to a file. Returns the path to the saved file."
(default headers {}) (default headers {})
(default opts {}) (default opts {})
(def filename (array/pop (string/split "/" url))) (def filename (array/pop (string/split "/" url)))
(def fullpath (string path filename)) (def fullpath (string folder filename))
# Check if the file already exists # Check if the file already exists
(def existing (file/open fullpath :r)) (def existing (file/open fullpath :r))
@ -18,4 +18,4 @@
(def fout (file/open fullpath :w)) (def fout (file/open fullpath :w))
(file/write fout (response :body)) (file/write fout (response :body))
(file/close fout))) (file/close fout)))
fullpath) filename)