Skip to content

Instantly share code, notes, and snippets.

@edbond
Created October 29, 2015 21:30
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 edbond/d3becd4025f42335152f to your computer and use it in GitHub Desktop.
Save edbond/d3becd4025f42335152f to your computer and use it in GitHub Desktop.
Download youtube mp3 from playlists on radiorelax.ua site
(ns relaxfm.core
(:gen-class)
(:require [hickory.core :as hc]
[hickory.select :as hs]
[clj-http.client :as http]
[clojure.string :as s]
[clojure.java.shell :as sh]
[clj-time.core :as t]
[clj-time.format :as tf]
[clj-time.coerce :as tc]))
;; http://www.radiorelax.ua/playlist/27-10-2015.html
(def date-fmt (tf/formatter "dd-MM-yyyy"))
(defn extract
"Extract links to youtube from html page"
[html]
(let [tree (-> (:body html) hc/parse hc/as-hickory)
rows (hs/select (hs/child (hs/attr "data-video")) tree)]
(map #(get-in % [:attrs :data-video]) rows)))
(defn on-date
"Load html playlist and extract youtube links"
[date]
(let [d (tf/unparse date-fmt date)
url (str "http://www.radiorelax.ua/playlist/" d ".html")
h (http/get url)]
;; (prn "Get " url)
(extract h)))
(defn schedule
"Lazy list of schedule, infinite list of youtube links from today to previous days"
[]
(let [today (t/today-at-midnight)
dates (map #(t/minus today (t/days %)) (range))]
(map on-date dates)))
;; youtube-dl -x -i --audio-format mp3 -a 20151028.txt
(defn download
"Launch youtube-dl to download mp3 file to folder"
[url]
(sh/sh "youtube-dl" "-x" "-i" "--audio-format" "mp3" url
:dir "20151028"))
(defn -main
[n-str]
(let [n (Integer/parseInt n-str)]
(as-> n n
(flatten (take n (schedule)))
(distinct (filter #(and (not (s/blank? %))
(re-find #"youtube" %)) n))
(dorun
(pmap download n)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment