Last active
November 21, 2023 11:53
-
-
Save yogthos/94da937bcae2552b9501bfc4d9045f21 to your computer and use it in GitHub Desktop.
a script to download photos from Pixelfed data export JSON file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Won't work if outbox.json cannot be generated because Pixelfed does not manage yet more than 500 statuses :(