1
0

nalunch.lisp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. (in-package #:chatikbot)
  2. (defvar *nalunch/calend* nil "Working calendar exceptions")
  3. (defparameter +nalunch/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"
  4. "Mobile UA")
  5. (defparameter +nalunch/mobile-uri+ "https://www.nalunch.ru/Mobile")
  6. (defparameter +nalunch/login-uri+ "https://www.nalunch.ru/Mobile/Account/Login")
  7. (defparameter +nalunch/basicdata-calend+ "http://basicdata.ru/api/json/calend/")
  8. (defun nalunch/auth (login pass cookies &optional dom)
  9. (let* ((dom (or dom
  10. (xml-request +nalunch/login-uri+ :cookie-jar cookies :user-agent +nalunch/mobile-ua+)))
  11. (form (plump:get-element-by-id dom "LoginForm"))
  12. (parameters
  13. (loop for input in (get-by-tag form "input")
  14. for name = (plump:get-attribute input "name")
  15. for value = (plump:get-attribute input "value")
  16. when (and name value) collect (cons name value)
  17. when (string= name "UserName") collect (cons name login)
  18. when (string= name "Password") collect (cons name pass))))
  19. (multiple-value-bind (response status response-headers)
  20. (http-request +nalunch/login-uri+
  21. :method :post
  22. :content parameters
  23. :cookie-jar cookies
  24. :user-agent +nalunch/mobile-ua+)
  25. (when (and (member status '(301 302 303 307) :test #'=)
  26. (gethash "location" response-headers))
  27. (setf response (http-request (quri:merge-uris
  28. (quri:uri (gethash "location" response-headers))
  29. (quri:uri +nalunch/login-uri+))
  30. :cookie-jar cookies
  31. :user-agent +nalunch/mobile-ua+)))
  32. (when (search "id=\"LoginForm\"" response)
  33. (error "Bad username or password"))
  34. (if (search "<title>Чек</title>" response) ;; Reload feed page on 'Cheque'
  35. (xml-request +nalunch/mobile-uri+ :cookie-jar cookies :user-agent +nalunch/mobile-ua+)
  36. (plump:parse response)))))
  37. (defun nalunch/recent (login pass &optional cookies)
  38. (let ((cookies (or cookies (cl-cookie:make-cookie-jar))))
  39. (multiple-value-bind (dom status headers uri)
  40. (xml-request +nalunch/mobile-uri+ :cookie-jar cookies :user-agent +nalunch/mobile-ua+)
  41. (declare (ignore status headers))
  42. (let* ((dom (if (quri:uri= uri (quri:uri +nalunch/mobile-uri+))
  43. dom
  44. (nalunch/auth login pass cookies dom)))
  45. (balance (parse-integer (plump:text (elt (clss:select ".newswire-header_balance" dom) 0))))
  46. (recent (loop for day across (clss:select ".day-feed" dom)
  47. append (loop for el across (clss:select ".media" day)
  48. for date = (select-text ".day-feed_date" day)
  49. for time = (select-text ".transaction_time" el)
  50. for price = (parse-integer (select-text ".transaction_price" el))
  51. for place = (select-text ".transaction-title" el)
  52. collect (list (cons :time (format nil "~A ~A" date time))
  53. (cons :price price)
  54. (cons :place place))))))
  55. (list (cons :balance balance)
  56. (cons :recent recent))))))
  57. (defun %nalunch/get-calend (year)
  58. (setf year (princ-to-string year))
  59. (unless (aget year *nalunch/calend*)
  60. (setf *nalunch/calend* (aget "data" (json-request +nalunch/basicdata-calend+))))
  61. (aget year *nalunch/calend*))
  62. (defun %nalunch/get-working-days (year month)
  63. (let* ((exceptions (aget (princ-to-string month) (%nalunch/get-calend year)))
  64. (days-in-month (local-time:days-in-month month year)))
  65. (loop for day from 1 upto days-in-month
  66. for ts = (local-time:encode-timestamp 0 0 0 0 day month year)
  67. for dof = (local-time:timestamp-day-of-week ts)
  68. for exc = (aget "isWorking" (aget (princ-to-string day) exceptions))
  69. when (or (and (<= 1 dof 5)
  70. (not (equal 2 exc)))
  71. (and (or (= dof 0) (= dof 6))
  72. (or (equal 0 exc)
  73. (equal 3 exc))))
  74. collect day)))
  75. (defun %nalunch/format (result &optional last)
  76. (let* ((balance (aget :balance result))
  77. (all (aget :recent result))
  78. (recent (cons (car all) (unless last (cdr all))))
  79. (now (local-time:now))
  80. (left-working-days (length (remove-if #'(lambda (d) (<= d (local-time:timestamp-day now)))
  81. (%nalunch/get-working-days (local-time:timestamp-year now)
  82. (local-time:timestamp-month now))))))
  83. (format nil "🍴 Баланс ~A руб~@[ на ~A дней, по ~$ руб~].~{~&~A~}"
  84. balance left-working-days (/ balance (max left-working-days 1))
  85. (mapcar (lambda (meal) (format nil "~A @ ~A — ~A руб."
  86. (aget :time meal) (aget :place meal) (aget :price meal)))
  87. recent))))
  88. ;; Cron
  89. (defvar *nalunch/last-results* (make-hash-table) "Last check results")
  90. (defvar *nalunch/jars* (make-hash-table) "Cookie jars")
  91. (defcron process-nalunch (:minute '(member 0 10 20 30 40 50))
  92. (dolist (chat-id (lists-get :nalunch))
  93. (secret/with (login-pass (list :nalunch chat-id))
  94. (if login-pass
  95. (let* ((cookie-jar (or (gethash chat-id *nalunch/jars*)
  96. (cl-cookie:make-cookie-jar)))
  97. (old (gethash chat-id *nalunch/last-results*))
  98. (new (nalunch/recent (car login-pass) (cdr login-pass) cookie-jar)))
  99. (when new
  100. (when (and old (not (equal (aget :balance old)
  101. (aget :balance new))))
  102. (send-response chat-id (%nalunch/format new t)))
  103. (setf (gethash chat-id *nalunch/last-results*) new
  104. (gethash chat-id *nalunch/jars*) cookie-jar)))
  105. (progn
  106. (log:warn "nalunch no login/pass for" chat-id)
  107. ;; (lists-set-entry :nalunch chat-id nil) ;; Comment out for now
  108. )))))
  109. ;; Hooks
  110. (def-message-cmd-handler handle-cmd-nalunch (:nalunch)
  111. (if (member chat-id *admins*)
  112. (send-response chat-id (nalunch-format
  113. (or *nalunch-last-result*
  114. (setf *nalunch-last-result*
  115. (nalunch-recent)))))
  116. (send-dont-understand chat-id)))
  117. (defun nalunch/handle-set-cron (chat-id enable)
  118. (lists-set-entry :nalunch chat-id enable)
  119. (bot-send-message chat-id
  120. (if enable
  121. "Включил рассылку. '/nalunch off' чтобы выключить, /nalunch - показать последние."
  122. "Без рассылки. '/nalunch on' - включить, /nalunch - последние.")))
  123. (defun nalunch/handle-auth (chat-id login pass)
  124. (let ((cookies (cl-cookie:make-cookie-jar)))
  125. (handler-case
  126. (progn
  127. (nalunch/auth login pass cookies)
  128. (secret/set `(:nalunch ,chat-id) (cons login pass))
  129. (nalunch/handle-set-cron chat-id t))
  130. (error () (bot-send-message chat-id "Чот не смог, пропробуй другие.")))))
  131. (defun nalunch/handle-recent (chat-id)
  132. (secret/with (login-pass (list :nalunch chat-id))
  133. (bot-send-message chat-id
  134. (if login-pass
  135. (let* ((cookies (or (gethash chat-id *nalunch/jars*)
  136. (cl-cookie:make-cookie-jar)))
  137. (data (nalunch/recent (car login-pass) (cdr login-pass) cookies)))
  138. (if data
  139. (progn
  140. (setf (gethash chat-id *nalunch/jars*) cookies)
  141. (%nalunch/format data))
  142. "Не смог получить данные. Попробуй перелогинься. /nalunch <login> <pass>"))
  143. "Нужен логин-пароль. /nalunch <login> <pass>")
  144. :parse-mode "markdown")))
  145. (def-message-cmd-handler handle-cmd-nalunch (:nalunch)
  146. (cond
  147. ((= 1 (length args))
  148. (nalunch/handle-set-cron chat-id (equal "on" (car args))))
  149. ((= 2 (length args)) (apply 'nalunch/handle-auth chat-id args))
  150. (:otherwise (nalunch/handle-recent chat-id))))