Skip to content

Instantly share code, notes, and snippets.

@edlich
Created February 11, 2014 15:33
Show Gist options
  • Save edlich/8937164 to your computer and use it in GitHub Desktop.
Save edlich/8937164 to your computer and use it in GitHub Desktop.
Fitness Tracker to JSON in Clojure
;; (C) Stefan Edlich Eclipse Public License
;; ToDo: Zone should be an int! ;; Null values should be inserted as null !
(import '(java.io File))
(def filepath "complete.json")
(def oldpath "complete-old.json")
(defn sport2json []
"Convert User Input to JSON text"
(print "Types: MULTI, RUN, BIKE, SPIN, POWER, SWIM, TT, WALK, YOGA, SEE, KITE, BODYSURF, FOOTBALL, etc.\n\n")
(let [date (do (print "Date (e.g. 19.10.2014): ") (flush)(read-line)) ;; get today if empty
duration (do (print "Duration (int in min): ") (flush)(read-line))
dist (do (print "Distance (double in km): ") (flush)(read-line))
type (do (print "Type (BIG LETTERS): ") (flush)(read-line))
zone (do (print "Zone (1..10): ") (flush)(read-line))
feeling (do (print "Feeling (1..10): ") (flush)(read-line))
calories (do (print "Calories (int): ") (flush)(read-line))
weight (do (print "weight (83.4): ") (flush)(read-line))
loccomm (do (print "Locaion+Comment (String): ") (flush)(read-line))
dquote "\""]
(str "{\n " dquote "DATE" dquote ": " dquote date dquote ","
"\n " dquote "DURATION" dquote ": " duration ","
"\n " dquote "DISTANCE" dquote ": " dist ","
"\n " dquote "TYPE" dquote ": " dquote type dquote ","
"\n " dquote "ZONE" dquote ": " zone ","
"\n " dquote "FEELING" dquote ": " feeling ","
"\n " dquote "CALORIES" dquote ": " calories ","
"\n " dquote "WEIGHT" dquote ": " weight ","
"\n " dquote "LOC-COMM" dquote ": " dquote loccomm dquote "\n},\n"))) ;; ERROR numbers no ""
(println "Reading the file " filepath " ...")
(def rfile (slurp filepath))
(when (.exists (clojure.java.io/as-file oldpath)) (.delete (File. oldpath)))
(println "Renaming the file " filepath " to " oldpath)
(.renameTo (File. filepath) (File. oldpath))
(println "Trying to get user input...")
(def input-result (sport2json))
(println "Writing the fresh " filepath " to disk.")
(spit filepath (str input-result rfile))
(println "Finished.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment