1
0

nalunch.lisp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. (in-package #:chatikbot)
  2. (defvar *nalunch-username* nil "Username")
  3. (defvar *nalunch-password* nil "Password")
  4. (defvar *nalunch-cookie-jar* (make-instance 'drakma:cookie-jar) "Cookie storage")
  5. (defvar *nalunch-calend* nil "Working calendar exceptions")
  6. (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"
  7. "Mobile UA")
  8. (defparameter +nalunch-mobile+ (puri:uri "https://www.nalunch.ru/Mobile/"))
  9. (defparameter +nalunch-login+ (puri:uri "https://www.nalunch.ru/Mobile/Account/Login"))
  10. (defparameter +basicdata-calend+ (puri:uri "http://basicdata.ru/api/json/calend/"))
  11. (defun nalunch-auth (&optional body)
  12. (let* ((body (or body
  13. (drakma:http-request +nalunch-login+ :cookie-jar *nalunch-cookie-jar* :user-agent +mobile-ua+)))
  14. (dom (plump:parse body))
  15. (form (plump:get-element-by-id dom "LoginForm"))
  16. (parameters
  17. (loop for input in (get-by-tag form "input")
  18. for name = (plump:get-attribute input "name")
  19. for value = (plump:get-attribute input "value")
  20. when (and name value) collect (cons name value)
  21. when (string= name "UserName") collect (cons name *nalunch-username*)
  22. when (string= name "Password") collect (cons name *nalunch-password*)))
  23. (response (drakma:http-request +nalunch-login+
  24. :method :post
  25. :parameters parameters
  26. :cookie-jar *nalunch-cookie-jar*
  27. :user-agent +mobile-ua+)))
  28. (when (search "id=\"LoginForm\"" response)
  29. (error "Bad username or password"))
  30. response))
  31. (defun nalunch-recent ()
  32. (multiple-value-bind (body status headers uri)
  33. (drakma:http-request +nalunch-mobile+ :cookie-jar *nalunch-cookie-jar* :user-agent +mobile-ua+)
  34. (declare (ignore status headers))
  35. (let* ((body (if (puri:uri= uri +nalunch-mobile+)
  36. body
  37. (nalunch-auth body)))
  38. (dom (plump:parse body))
  39. (balance (parse-integer (plump:text (elt (clss:select ".newswire-header_balance" dom) 0))))
  40. (recent (loop for day across (clss:select ".day-feed" dom)
  41. append (loop for el across (clss:select ".media" day)
  42. for date = (select-text ".day-feed_date" day)
  43. for time = (select-text ".transaction_time" el)
  44. for price = (parse-integer (select-text ".transaction_price" el))
  45. for place = (select-text ".transaction-title" el)
  46. collect (list (cons :time (format nil "~A ~A" date time))
  47. (cons :price price)
  48. (cons :place place))))))
  49. (list (cons :balance balance)
  50. (cons :recent recent)))))
  51. (defun get-calend (year)
  52. (setf year (princ-to-string year))
  53. (unless (aget year *nalunch-calend*)
  54. (setf *nalunch-calend* (aget "data" (json-request +basicdata-calend+))))
  55. (aget year *nalunch-calend*))
  56. (defun get-working-days (year month)
  57. (let* ((exceptions (aget (princ-to-string month) (get-calend year)))
  58. (days-in-month (local-time:days-in-month month year)))
  59. (loop for day from 1 upto days-in-month
  60. for ts = (local-time:encode-timestamp 0 0 0 0 day month year)
  61. for dof = (local-time:timestamp-day-of-week ts)
  62. for exc = (aget "isWorking" (aget (princ-to-string day) exceptions))
  63. when (or (and (<= 1 dof 5)
  64. (not (equal 2 exc)))
  65. (and (or (= dof 0) (= dof 6))
  66. (or (equal 0 exc)
  67. (equal 3 exc))))
  68. collect day)))
  69. (defun nalunch-format (result &optional last)
  70. (let* ((balance (aget :balance result))
  71. (all (aget :recent result))
  72. (recent (cons (car all) (unless last (cdr all))))
  73. (now (local-time:now))
  74. (left-working-days (length (remove-if #'(lambda (d) (<= d (local-time:timestamp-day now)))
  75. (get-working-days (local-time:timestamp-year now)
  76. (local-time:timestamp-month now))))))
  77. (format nil "🍴 Баланс ~A руб~@[ на ~A дней, по ~$ руб~].~{~&~A~}"
  78. balance left-working-days (/ balance (max left-working-days 1))
  79. (mapcar (lambda (meal) (format nil "~A @ ~A — ~A руб."
  80. (aget :time meal) (aget :place meal) (aget :price meal)))
  81. recent))))
  82. ;; Cron
  83. (defvar *nalunch-last-result* nil "Last check result")
  84. (defun process-nalunch ()
  85. (handler-case
  86. (let ((result (nalunch-recent)))
  87. (unless (equal (aget :balance *nalunch-last-result*)
  88. (aget :balance result))
  89. (send-response (car *admins*) (nalunch-format result t))
  90. (setf *nalunch-last-result* result)))
  91. (error (e) (log:error "~A" e))))
  92. ;; Hooks
  93. (def-message-cmd-handler handler-cmd-nalunch (:nalunch)
  94. (if (member chat-id *admins*)
  95. (send-response chat-id (nalunch-format
  96. (or *nalunch-last-result*
  97. (setf *nalunch-last-result*
  98. (nalunch-recent)))))
  99. (send-dont-understand chat-id)))