Skip to content

Instantly share code, notes, and snippets.

@yogthos
Last active January 24, 2021 21:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogthos/a8054b849b4889d129d2e150e1e3ab61 to your computer and use it in GitHub Desktop.
Save yogthos/a8054b849b4889d129d2e150e1e3ab61 to your computer and use it in GitHub Desktop.
a Lumo script for downloading Reddit videos using youtube-dl
#!/usr/bin/env lumo
(ns reddit-video.core
(:require
[cljs.core :refer [*command-line-args*]]
[clojure.string :as string]))
(def https (js/require "https"))
(def process (js/require "child_process"))
(defn js->edn [data]
(js->clj data :keywordize-keys true))
(defn find-media [data]
(-> data first :data :children first :data :secure_media :reddit_video))
(defn download-fallback [url]
(process.exec (str "wget -O " (.getTime (js/Date.)) ".mp4 " url)
(fn [err stdout stderr]
(cond
err (println err)
stderr (println stderr)
stdout (println stdout "\nsuccess")
:else (println url "downloaded successuflly")))))
(defn download [{:keys [dash_url fallback_url]}]
(process.exec (str "youtube-dl -f bestvideo+bestaudio " dash_url)
(fn [err stdout stderr]
(cond
(or err (not-empty stderr)) (download-fallback fallback_url)
stdout (println stdout "\nsuccess"))
:else (println dash_url "downloaded successfully"))))
(defn download-media [url]
(https.get url
(fn [resp]
(let [data (atom "")]
(.on resp "data"
(fn [chunk]
(swap! data str chunk)))
(.on resp "end"
(fn []
(-> @data js/JSON.parse js->edn find-media download)))
(.on resp "error"
(fn [error]
(js/console.log error)))))))
(defn fix-url [url]
(cond-> url
(.endsWith url "/") (subs 0 (dec (count url)))
(not (.endsWith url ".json")) (str ".json")))
(-> (.-argv js/process) last fix-url download-media)
@yogthos
Copy link
Author

yogthos commented Feb 13, 2019

The script assumes that youtube-dl is installed on the system, and accepts a Reddit link with a video as its input, e.g: ./reddit-video.cljs https://www.reddit.com/r/funny/comments/aq5fjv/you_can_run_but_you_cant_bike

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