Commits

Miki Tebeka  committed f4477d1

Getting time

  • Participants
  • Parent commits 3ab6002

Comments (0)

Files changed (1)

File src/clj2010.clj

 (ns clj2010
   (:require [apricot-soup :as soup])
-  (:use [clojure.contrib.string :only (trim lower-case)]))
+  (:use [clj-time.core :only (hours minutes date-time plus)])
+  (:use [clojure.contrib.string :only (trim lower-case split)]))
 
 ; "chouser: " -> "chouser"
 (defn fix-user 
   (trim (apply str (butlast user))))
 
 (defn tokenize 
-  "poor man's tokenizer"
+  "Poor man's tokenizer"
   [sentence]
   (map lower-case (re-seq #"[a-zA-Z0-9'_-]+" sentence)))
 
-(defn parse-p [previous-log p]
-  (let [[match time user text] (re-find #"^(\d+:\d+)(.+:)?(.*)" (soup/text p))]
-    { :time time
+(defn str->long [s]
+  (Long/valueOf s))
+
+(defn logfile-day [logfile]
+  (let [[match year month day] (re-find #"(\d+)-(\d+)-(\d+)" logfile)]
+    (apply date-time (map str->long [year month day]))))
+
+(defn log-time [day hour minute]
+  (plus (plus day (hours hour)) (minutes minute)))
+
+(defn parse-text [text]
+  (rest (re-find #"^(\d+):(\d+)(.+:)?(.*)" text)))
+
+(defn process-p [day previous-log p]
+  (let [[hour minute user text] (parse-text (soup/text p))]
+    { :time (log-time day (str->long hour) (str->long minute))
       :tokens (tokenize text)
       :user (if user (fix-user user) (:user previous-log))}))
 
 (defn process-logfile [logfile]
-  (rest (reductions parse-p (cons nil (soup/$ (slurp logfile) p)))))
+  (let [day (logfile-day logfile)
+        pp (partial process-p day)]
+    (rest (reductions pp (cons nil (soup/$ (slurp logfile) p))))))
 
 (defn -main []
   (dorun (map println (take 10 (process-logfile "logs/2010-01-01.html")))))