Browse files

Move to nodejs.

  • Loading branch information...
1 parent 9329b11 commit 0c442a27dd73b12310733d87404000518239f1b8 @Raynes committed Sep 4, 2011
Showing with 44 additions and 39 deletions.
  1. +0 −5 project.clj
  2. +0 −34 src/mkvrename/core.clj
  3. +44 −0 src/mkvrename/core.cljs
View
5 project.clj
@@ -1,5 +0,0 @@
-(defproject mkvrename "0.0.1-SNAPSHOT"
- :description "TODO: add summary of your project"
- :dependencies [[clojure "1.2.0"]
- [useful "0.7.0-alpha5"]]
- :main mkvrename.core)
View
34 src/mkvrename/core.clj
@@ -1,34 +0,0 @@
-(ns mkvrename.core
- (:use [useful.seq :only [separate]]
- [clojure.java.io :only [file]])
- (:gen-class))
-
-(def cd (System/getProperty "user.dir"))
-
-(defn rename [template n f]
- (let [new-file (file cd (str template n ".mkv"))]
- (println (str "Renaming " (.getName f) " to " (.getName new-file)))
- (.renameTo f new-file)))
-
-(defn prompt-read [& strs]
- (map (fn [s]
- (print s)
- (flush)
- (read-line))
- strs))
-
-(defn template [f]
- (if f
- (re-find #".*- S\d+E" (.getName f))
- (apply format "%s - S%sE" (prompt-read "Show Title: " "This Season: "))))
-
-(defn -main [& args]
- (let [[new done] (separate #(.startsWith (.getName %) "title")
- (filter (comp (partial re-find #".*\.mkv") str)
- (file-seq (file cd))))
- done-count (inc (count done))
- template (template (first done))]
- (doall
- (map (partial rename template)
- (range done-count (+ (count new) done-count))
- new))))
View
44 src/mkvrename/core.cljs
@@ -0,0 +1,44 @@
+(ns mkvrename.core
+ (:require [cljs.nodejs :as nodejs]))
+
+(def fs (nodejs/require "fs"))
+(def path (nodejs/require "path"))
+
+(defn separate [pred s]
+ (reduce
+ (fn [[x y] e]
+ (if (pred e) [(conj x e) y] [x (conj y e)]))
+ [[] []]
+ s))
+
+(def cd ((.cwd nodejs/process)))
+
+(defn rename [template n f]
+ (let [new-file (.join path cd (str template n ".mkv"))]
+ (println "Renaming" (.basename path f) "to" (.basename path new-file))
+ (.renameSync fs f new-file)))
+
+;; Because something fishy is going on with the args passed to -main,
+(def args (drop 2 (seq (.argv nodejs/process))))
+
+(defn template [f]
+ (if f
+ (re-find #".*- S\d+E" (.basename path f))
+ (if-let [[name season] args]
+ (str name " - " "S" season "E"))))
+
+(defn file-seq [dir]
+ (map #(.join path dir %) (.readdirSync fs dir)))
+
+(defn -main [& _]
+ (let [[new done] (separate #(re-find #"title\d+" %)
+ (filter #(= (.extname path %) ".mkv") (file-seq cd)))
+ start-count (inc (count done))]
+ (if (or (> start-count 1) (= (count args) 2))
+ (doall
+ (map (partial rename (template (first done)))
+ (range start-count (+ (count new) start-count))
+ new))
+ (println "Please tell me the name of the show and the number of this current season."))))
+
+(set! *main-cli-fn* -main)

0 comments on commit 0c442a2

Please sign in to comment.