Last active
January 24, 2021 21:03
-
-
Save yogthos/a8054b849b4889d129d2e150e1e3ab61 to your computer and use it in GitHub Desktop.
a Lumo script for downloading Reddit videos using youtube-dl
This file contains hidden or 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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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