Created
January 18, 2011 16:56
-
-
Save timvisher/784744 to your computer and use it in GitHub Desktop.
wallpapers!-core
This file contains 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
(ns wallpaper-manager-core.core | |
(:import [java.io File FileInputStream] | |
javax.imageio.ImageIO)) | |
;; Properties | |
(def wallpaper-source (str (System/getProperty "user.home") "/Downloads/")) | |
(def wallpaper-dest (str (System/getProperty "user.home") "/Pictures/Wallpaper")) | |
;; Global Structures | |
(defstruct resolution :width :height) | |
(defstruct wallpaper :file :resolution :destination-file) | |
;; Worker Functions | |
(def wallpaper-resolutions | |
(for [[width height] | |
[[1280 1024] [1920 1080] [2560 1600] [1920 1200] [1680 1050] [1440 900] [1280 800]]] | |
(struct resolution width height))) | |
(defn to-resolution [image] | |
(struct resolution (.getWidth image) (.getHeight image))) | |
(defn wallpaper-resolution? [image] | |
(some #(= (to-resolution image) %) wallpaper-resolutions)) | |
(defn to-dir-name [{{:keys [width height]} :resolution}] | |
(str width "x" height)) | |
(defn name-has-resolution? [file-name expected-suffix] | |
(.matches file-name (str ".+" expected-suffix "\\..+"))) | |
(defn to-file-name-suffix [wallpaper] | |
(str "_" (to-dir-name wallpaper))) | |
(defn to-wallpaper-name [{:keys [file resolution] :as wallpaper}] | |
(let [name (.getName file) | |
extension-index (.lastIndexOf name ".")] | |
(if (not (name-has-resolution? name (to-file-name-suffix wallpaper))) | |
(str (.substring name 0 extension-index) | |
(to-file-name-suffix wallpaper) | |
(.substring name extension-index)) | |
(.getName file)))) | |
(defn to-destination-file [wallpaper] | |
(when wallpaper | |
(File. (str wallpaper-dest "/" (to-dir-name wallpaper) "/" (to-wallpaper-name wallpaper))))) | |
(defn file-to-wallpaper [file] | |
(with-open [r (FileInputStream. file)] | |
(if-let [image (ImageIO/read r)] | |
(if (wallpaper-resolution? image) | |
(let [without-destination-file (struct wallpaper file (to-resolution image))] | |
(assoc without-destination-file :destination-file (to-destination-file without-destination-file))))))) | |
(defn wallpaper-seq [] | |
(let [raw-files (file-seq (File. wallpaper-source)) | |
regular-files (filter #(.isFile %) raw-files)] | |
(map file-to-wallpaper regular-files))) | |
(defn do-file-wallpaper [{:keys [file] :as wallpaper}] | |
(do | |
(let [destination-file (to-destination-file wallpaper)] | |
(.mkdirs (File. (.getParent destination-file))) | |
(.renameTo file destination-file)) | |
)) | |
;; Use | |
(comment | |
;; Sort wallpapers in ~/Downloads | |
(doseq [wallpaper (wallpaper-seq)] (when wallpaper (do-file-wallpaper wallpaper)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment