/
generate_podcast.clj
executable file
·63 lines (56 loc) · 1.83 KB
/
generate_podcast.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env bb
(ns generate-podcast
(:require [clojure.java.io :as io]
[clojure.data.xml :as xml]
[clojure.string :as str]
[clojure.tools.cli :as cli])
(:import [java.io File]
[java.nio.file Files LinkOption]))
(defn created [^File f]
(-> (.toPath f)
(Files/getAttribute "creationTime" (into-array LinkOption []))
str))
(defn item [^File f base-url]
(xml/element
:item
{}
(xml/element :title {} (.getName f))
(xml/element :pubDate {} (created f))
(xml/element :enclosure {:url (str base-url "/"
(-> (.getName f)
(java.net.URLEncoder/encode)
(str/replace "+" "%20")))
:type "audio/mpeg"})))
(defn rss [title base-url podcast-file]
(xml/element
:rss {}
(xml/element
:channel {}
(xml/element :title {} title)
(xml/element :link {} (str base-url "/" podcast-file))
(for [^File f (file-seq (io/file "."))
:when (str/ends-with? (.getName f) ".mp3")]
(item f base-url)))))
(def cli-options
[["-f" "--file FILE" "Podcast file to generate"]
["-t" "--title TITLE" "Title for the podcast"]
["-b" "--base-url URL" "Base URL of server"]
["-h" "--help"]])
(defn print-help
[]
(println "generate_podcast.clj:")
(println)
(doseq [o cli-options]
(apply println o)))
(let [args (cli/parse-opts *command-line-args* cli-options)
{:keys [:file :base-url :title :help]} (:options args)]
(if (or (not file)
(not base-url)
(not title)
help)
(print-help)
(let [base-url (str/replace base-url #"/*$" "")]
(spit file
(xml/emit-str
(rss title base-url file)))
(println "Written podcast for" title "at" file))))