Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse DayOne and OhLife journal exports and save them to files that can be piped to jrnl.
;; Usage: execute each statement in a boot (http://boot-clj.com/) REPL, adjust as needed
(set-env! :dependencies '[[org.clojure/data.xml "0.2.0-alpha2"]
[clj-time "0.13.0"]])
(require '[clojure.java.io :as io]
'[clojure.data.xml :as xml]
'[clojure.string :as string]
'[clj-time.format :as f])
;; Some basic utilities to parse dayone and ohlife exports and import them into jrnl
;; http://jrnl.sh/
(def do-entries
(->> (io/file "/Users/martin/Documents/dayone-entries/")
file-seq
(filter #(.endsWith (.getName %) "doentry"))))
(def mapping
{"Creation Date" :created-at
"Entry Text" :text
"Starred" :starred
"Tags" :tags
"Time Zone" :time-zone
"UUID" :uuid})
(defn content [x]
(case (:tag x)
(:string :key) (do (assert (= 1 (-> x :content count)))
(-> x :content first))
:date (do (assert (= 1 (-> x :content count)))
(f/parse (f/formatters :date-time-no-ms)
(-> x :content first)))
:array (do (assert (= 0 (-> x :content count)) (pr-str (:content x)))
[])
:false false
:true true))
(defn parse-dayone [file]
(let [pairs (->> (xml/parse (java.io.StringReader. (slurp file)))
:content first :content (partition 2))]
(reduce
(fn [m [k v]]
(assoc m (->> k content (get mapping)) (-> v content)))
{}
pairs)))
(def jrnl-dir
(io/file "/Users/martin/Documents/do-jrnl-entries/"))
(def jrnl-date-formatter
(f/formatter "MM/dd/yyyy"))
(def jrnl-time-formatter
(f/formatter "hh:mm"))
(defn write-jrnl [day-one-files]
(doseq [f day-one-files]
(let [parsed (parse-dayone f)
jrnl-f (str (.getName f) ".jrnl")
jrnl-str (str (f/unparse jrnl-date-formatter (:created-at parsed))
" at "
(f/unparse jrnl-time-formatter (:created-at parsed))
(when (:starred parsed) "*")
": "
(:text parsed))]
(println "Writing" jrnl-f)
(spit (io/file jrnl-dir jrnl-f)
jrnl-str))))
;; oh-life
(def ohlife-file
(io/file "/Users/martin/Documents/ohlife_20130514.txt"))
(defn lines-by-date [ohlife-file]
(-> (fn [m l]
(if-let [date (some->> (re-find #"^\d\d\d\d-\d\d-\d\d$" l)
(f/parse (f/formatters :date)))]
(assoc m date [] :current date)
(update m (:current m) conj l)))
(reduce {} (line-seq (io/reader ohlife-file)))
(dissoc :current)))
(defn write-jrnl-from-oh-life [ohlife-file]
(doseq [[date lines] (lines-by-date ohlife-file)]
(spit (io/file "ohlife-jrnl-entries" (str (f/unparse (f/formatters :date) date) ".jrnl"))
(str (f/unparse jrnl-date-formatter date) ": "
(string/join "\n" (drop-while string/blank? lines))))))
(comment
(parse-dayone (first do-entries))
(write-jrnl (take 1 do-entries))
(def test-date
(f/parse "2013-05-22T10:16:46Z"))
(f/unparse jrnl-date-formatter test-date)
(f/show-formatters))
;; (:content (xml/parse (java.io.StringReader. sample)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.