nalunch.lisp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. (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"
  6. "Mobile UA")
  7. (defparameter +nalunch-mobile+ (puri:uri "https://www.nalunch.ru/Mobile/"))
  8. (defparameter +nalunch-login+ (puri:uri "https://www.nalunch.ru/Mobile/Account/Login"))
  9. (defun nalunch-auth (&optional body)
  10. (let* ((body (or body
  11. (drakma:http-request +nalunch-login+ :cookie-jar *nalunch-cookie-jar* :user-agent +mobile-ua+)))
  12. (dom (plump:parse body))
  13. (form (plump:get-element-by-id dom "LoginForm"))
  14. (parameters
  15. (loop for input in (get-by-tag form "input")
  16. for name = (plump:get-attribute input "name")
  17. for value = (plump:get-attribute input "value")
  18. when (and name value) collect (cons name value)
  19. when (string= name "UserName") collect (cons name *nalunch-username*)
  20. when (string= name "Password") collect (cons name *nalunch-password*)))
  21. (response (drakma:http-request +nalunch-login+
  22. :method :post
  23. :parameters parameters
  24. :cookie-jar *nalunch-cookie-jar*
  25. :user-agent +mobile-ua+)))
  26. (when (search "id=\"LoginForm\"" response)
  27. (error "Bad username or password"))
  28. response))
  29. (defun nalunch-recent ()
  30. (multiple-value-bind (body status headers uri)
  31. (drakma:http-request +nalunch-mobile+ :cookie-jar *nalunch-cookie-jar* :user-agent +mobile-ua+)
  32. (declare (ignore status headers))
  33. (let* ((body (if (puri:uri= uri +nalunch-mobile+)
  34. body
  35. (nalunch-auth body)))
  36. (dom (plump:parse body))
  37. (balance (parse-integer (plump:text (elt (clss:select ".newswire-header_balance" dom) 0))))
  38. (recent (loop for day across (clss:select ".day-feed" dom)
  39. append (loop for el across (clss:select ".media" day)
  40. for date = (select-text ".day-feed_date" day)
  41. for time = (select-text ".transaction_time" el)
  42. for price = (parse-integer (select-text ".transaction_price" el))
  43. for place = (select-text ".transaction-title" el)
  44. collect (list (cons :time (format nil "~A ~A" date time))
  45. (cons :price price)
  46. (cons :place place))))))
  47. (list (cons :balance balance)
  48. (cons :recent recent)))))
  49. (defun nalunch-format (result &optional last)
  50. (let* ((balance (aget :balance result))
  51. (all (aget :recent result))
  52. (recent (cons (car all) (unless last (cdr all)))))
  53. (format nil "🍴 Баланс ~A руб.~{~&~A~}"
  54. balance
  55. (mapcar (lambda (meal) (format nil "~A @ ~A — ~A руб."
  56. (aget :time meal) (aget :place meal) (aget :price meal)))
  57. recent))))