Skip to content

Instantly share code, notes, and snippets.

@yogthos
Last active November 21, 2023 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogthos/94da937bcae2552b9501bfc4d9045f21 to your computer and use it in GitHub Desktop.
Save yogthos/94da937bcae2552b9501bfc4d9045f21 to your computer and use it in GitHub Desktop.
a script to download photos from Pixelfed data export JSON file
#!/usr/bin/env lumo
(ns pixelfed-images.core
(:require
[clojure.walk :refer [prewalk]]
[cljs.core :refer [*command-line-args*]]
[clojure.string :as string]))
(def fs (js/require "fs"))
(def https (js/require "https"))
(def stream (.-Transform (js/require "stream")))
(defn download-media [url filename]
(https.get url
(fn [resp]
(let [data (stream.)]
(.on resp "data"
(fn [chunk]
(.push data chunk)))
(.on resp "end"
(fn []
(fs.writeFileSync filename (.read data))))
(.on resp "error"
(fn [error]
(js/console.log error)))))))
(defn parse-data [filename]
(let [data (-> (fs.readFileSync filename "utf8") js/JSON.parse)]
(doseq [item data]
(doseq [attachment (.-attachment item)]
(when attachment
(let [url (.-url attachment)
filename (last (string/split url #"/"))]
(when-not (fs.existsSync filename)
(js/console.log (str "downloading: " filename))
(download-media url filename))))))))
(-> (.-argv js/process) last parse-data)
;; Usage
;; install Lumo (https://github.com/anmonteiro/lumo): npx -p lumo-cljs lumo
;; download statuses from the Pixelfed Data Export menu, the file will be called outbox.json
;; run: lumo pixelfed-images.cljs outbox.json
@pylapp
Copy link

pylapp commented Nov 21, 2023

Won't work if outbox.json cannot be generated because Pixelfed does not manage yet more than 500 statuses :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment