Innocenty Enikeew 11 anni fa
parent
commit
90157c8854
4 ha cambiato i file con 50 aggiunte e 4 eliminazioni
  1. 1 0
      src/locations.lisp
  2. 11 1
      src/utils.lisp
  3. 31 1
      src/web.lisp
  4. 7 2
      timeliner.asd

+ 1 - 0
src/locations.lisp

@@ -3,6 +3,7 @@
   (:use :cl #:timeliner.utils :cl-mongo)
   (:export
    #:import-location-events
+   #:takedown-import
    #:find-location-at
    #:point->doc))
 (in-package #:timeliner.locations)

+ 11 - 1
src/utils.lisp

@@ -10,7 +10,8 @@
    #:starts-with
    #:aget
    #:doc->plist
-   #:load-chrome-cookie-jar))
+   #:load-chrome-cookie-jar
+   #:today))
 (in-package :timeliner.utils)
 
 (defun ts->ms (ts)
@@ -21,6 +22,13 @@
   (multiple-value-bind (unix msec) (floor ms 1000)
     (local-time:unix-to-timestamp unix :nsec (* msec 1000000))))
 
+(defun today ()
+  (local-time:adjust-timestamp (local-time:now)
+    (set :hour 0)
+    (set :minute 0)
+    (set :sec 0)
+    (set :nsec 0)))
+
 (defgeneric $between (a from to)
   (:documentation "cl-mongo between query"))
 
@@ -95,6 +103,8 @@
   (merge-pathnames #P".config/google-chrome/Default/Cookies"
                    (user-homedir-pathname)))
 
+(define-condition bad-encoding-error (simple-error) ())
+
 (defun chrome-cookie-decode (value)
   (when (mismatch (subseq value 0 3)
                   (mapcar #'char-code (coerce "v10" 'list)))

+ 31 - 1
src/web.lisp

@@ -10,15 +10,45 @@
 
 (restas::register-pkgmodule-traits  'timeliner.web :render-method (lambda () (make-instance 'timeliner.web::renderer)))
 
+;; Cron
+(defun import-events ()
+  (timeliner.locations:import-location-events (local-time:timestamp- (today) 1 :day)))
+(defun import-finance ()
+  (timeliner.financisto:import-financisto-events))
+
 (defmethod restas:initialize-module-instance :before ((module (eql #.*package*)) context)
   (restas:with-context context
     (cl-mongo:mongo :host "10.8.0.6")
-    (cl-mongo:db.use "timeline")))
+    (cl-mongo:db.use "timeline")
+    (restas:context-add-variable context
+     '*import-events-timer*
+     (clon:schedule-function #'import-events
+                             (clon:make-scheduler (clon:make-typed-cron-schedule
+                                                   :minute 10 :hour 0))
+                             :name "import-events"
+                             :thread t))
+    (restas:context-add-variable context
+     '*import-finance-timer*
+     (clon:schedule-function #'import-finance
+                             (clon:make-scheduler (clon:make-typed-cron-schedule
+                                                   :minute 0 :hour 7))
+                             :name "import-finance"
+                             :thread t))))
+
+(defmethod restas:finalize-module-instance :after ((module (eql #.*package*)) context)
+  (let ((events-timer (restas:context-symbol-value context '*import-events-timer*))
+        (finance-timer (restas:context-symbol-value context '*import-finance-timer*)))
+    (when events-timer
+      (trivial-timers::unschedule-timer events-timer))
+    (when finance-timer
+      (trivial-timers::unschedule-timer finance-timer))))
 
+;; Static file path
 (defparameter *resources*
   (merge-pathnames "resources/"
                    (asdf:component-pathname (asdf:find-system '#:timeliner))))
 
+;; Rendering
 (defgeneric finalize-page (data)
   (:documentation "Final rendering step"))
 (defgeneric render-route-data (route data)

+ 7 - 2
timeliner.asd

@@ -47,11 +47,16 @@ THE SOFTWARE.
                :parenscript
                :css-lite
                :sqlite
-               :gzip-stream)
+               :gzip-stream
+               :clon)
   :components ((:module "src"
                 :components
                 ((:file "utils")
-                 (:file "web" :depends-on ("utils")))))
+                 (:file "locations" :depends-on ("utils"))
+                 (:file "financisto" :depends-on ("utils"))
+                 (:file "web" :depends-on ("utils"
+                                           "locations"
+                                           "financisto")))))
   :description "Personal Timeline"
   :long-description
   #.(with-open-file (stream (merge-pathnames