Prechádzať zdrojové kódy

Use working days for nalunch

Innocenty Enikeew 9 rokov pred
rodič
commit
4c7380e3b3
1 zmenil súbory, kde vykonal 29 pridanie a 4 odobranie
  1. 29 4
      nalunch.lisp

+ 29 - 4
nalunch.lisp

@@ -3,12 +3,13 @@
 (defvar *nalunch-username* nil "Username")
 (defvar *nalunch-password* nil "Password")
 (defvar *nalunch-cookie-jar* (make-instance 'drakma:cookie-jar) "Cookie storage")
-
+(defvar *nalunch-calend* nil "Working calendar exceptions")
 
 (defparameter +mobile-ua+ "Mozilla/5.0 (Linux; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.114 Mobile Safari/537.36"
   "Mobile UA")
 (defparameter +nalunch-mobile+ (puri:uri "https://www.nalunch.ru/Mobile/"))
 (defparameter +nalunch-login+ (puri:uri "https://www.nalunch.ru/Mobile/Account/Login"))
+(defparameter +basicdata-calend+ (puri:uri "http://basicdata.ru/api/json/calend/"))
 
 (defun nalunch-auth (&optional body)
   (let* ((body (or body
@@ -53,12 +54,36 @@
       (list (cons :balance balance)
             (cons :recent recent)))))
 
+(defun get-calend (year)
+  (setf year (princ-to-string year))
+  (unless (aget year *nalunch-calend*)
+    (setf *nalunch-calend* (aget "data" (json-request +basicdata-calend+))))
+  (aget year *nalunch-calend*))
+
+(defun get-working-days (year month)
+  (let* ((exceptions (aget (princ-to-string month) (get-calend year)))
+         (days-in-month (local-time:days-in-month month year)))
+    (loop for day from 1 upto days-in-month
+       for ts = (local-time:encode-timestamp 0 0 0 0 day month year)
+       for dof = (local-time:timestamp-day-of-week ts)
+       for exc = (aget "isWorking" (aget (princ-to-string day) exceptions))
+       when (or (and (<= 1 dof 5)
+                     (not (equal 2 exc)))
+                (and (or (= dof 0) (= dof 6))
+                     (or (equal 0 exc)
+                         (equal 3 exc))))
+       collect day)))
+
 (defun nalunch-format (result &optional last)
   (let* ((balance (aget :balance result))
          (all (aget :recent result))
-         (recent (cons (car all) (unless last (cdr all)))))
-    (format nil "🍴 Баланс ~A руб.~{~&~A~}"
-            balance
+         (recent (cons (car all) (unless last (cdr all))))
+         (now (local-time:now))
+         (left-working-days (length (remove-if #'(lambda (d) (<= d (local-time:timestamp-day now)))
+                                               (get-working-days (local-time:timestamp-year now)
+                                                                 (local-time:timestamp-month now))))))
+    (format nil "🍴 Баланс ~A руб~@[ на ~A дней, по ~$ руб~].~{~&~A~}"
+            balance left-working-days (/ balance (max left-working-days 1))
             (mapcar (lambda (meal) (format nil "~A @ ~A — ~A руб."
                                            (aget :time meal) (aget :place meal) (aget :price meal)))
                     recent))))