]> njoseph.me Git - babashka-scripts.git/commitdiff
Get rid of subprocess call to unzip
authorJoseph Nuthalapati <njoseph@riseup.net>
Thu, 11 Feb 2021 18:47:59 +0000 (00:17 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Thu, 11 Feb 2021 18:47:59 +0000 (00:17 +0530)
ebook-to-audio-book.clj

index e6fa40ac90c8d2ce11a7d8e337bacbe0de05d0b4..8305c2e92b45429be875c6f40df61ba7d228a17c 100755 (executable)
@@ -2,6 +2,8 @@
 
 ; A utility to listen to your ebooks using TTS programs
 
 
 ; A utility to listen to your ebooks using TTS programs
 
+(import '[java.nio.file Files FileSystems CopyOption])
+
 (require '[babashka.process :as p]
          '[clojure.java.io :as io]
          '[clojure.string :refer [split]])
 (require '[babashka.process :as p]
          '[clojure.java.io :as io]
          '[clojure.string :refer [split]])
 
 ;; TODO Use festival-tts or say depending on OS
 
 
 ;; TODO Use festival-tts or say depending on OS
 
+
 (defn run-cmd
   [command]
 (defn run-cmd
   [command]
-  (->> command 
+  (->> command
        p/process
        :out
        slurp))
        p/process
        :out
        slurp))
-       
+
+(defn extract-file-from-zip
+  [zip-file-name source destination]
+  (let [zip-file (io/file zip-file-name)
+        src (io/file source)
+        dest (io/file destination)
+        fs (FileSystems/newFileSystem (.toPath zip-file) nil)
+        file-in-zip (.getPath fs source (into-array String []))]
+    (Files/copy file-in-zip (.toPath dest)
+                (into-array CopyOption []))))
+
 (let [book-file (first *command-line-args*)
       title (first (split book-file #"\."))
       txtz-file (str title ".txtz")
 (let [book-file (first *command-line-args*)
       title (first (split book-file #"\."))
       txtz-file (str title ".txtz")
       mp3-file (str title ".mp3")]
   (println "Converting to text...")
   (run-cmd ["ebook-convert" book-file txtz-file])
       mp3-file (str title ".mp3")]
   (println "Converting to text...")
   (run-cmd ["ebook-convert" book-file txtz-file])
-  (spit txt-file (run-cmd ["unzip" "-p" txtz-file "index.txt"]))
+  (extract-file-from-zip txtz-file "index.txt" txt-file)
   (println "Converting text to audio. Don't hold your breath!")
   (run-cmd ["say" "-f" txt-file "-o" aiff-file])
   (println "Creating mp3 file...")
   (run-cmd ["ffmpeg" "-i" aiff-file mp3-file])
   (println "Cleaning up...")
   (println "Converting text to audio. Don't hold your breath!")
   (run-cmd ["say" "-f" txt-file "-o" aiff-file])
   (println "Creating mp3 file...")
   (run-cmd ["ffmpeg" "-i" aiff-file mp3-file])
   (println "Cleaning up...")
-  (doseq [temp-file [txtz-file txt-file aiff-file]]
-    (io/delete-file temp-file))
+  (run! #(io/delete-file %) [txtz-file txt-file aiff-file])
   (println (str "Done! Check out " mp3-file)))
   (println (str "Done! Check out " mp3-file)))