1
0

nalunch.lisp 8.3 KB

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