forecast.lisp 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. (in-package #:chatikbot)
  2. (defsetting *forecast-api-key* nil "forecast.io APIKEY")
  3. (defparameter +forecast-api-url+ "https://api.darksky.net/forecast" "forecast.io API endpoint")
  4. (defun forecast (lat lon &key time (currently t) minutely hourly daily alerts)
  5. (handler-case
  6. (bordeaux-threads:with-timeout (5)
  7. (json-request (format nil
  8. "~A/~A/~A,~A~@[,~A~]?units=si&exclude=~:[currently,~;~]~:[minutely,~;~]~:[hourly,~;~]~:[daily,~;~]~:[alerts,~;~]flags&lang=ru"
  9. +forecast-api-url+ *forecast-api-key* lat lon time
  10. currently minutely hourly daily alerts)))
  11. (bordeaux-threads:timeout () (error "Timeout"))))
  12. (defvar *forecast-point-formats*
  13. '((:current . (:year "-" (:month 2) "-" (:day 2) " " (:hour 2) ":" (:min 2)))
  14. (:hour . ((:hour 2) ":" (:min 2)))
  15. (:day . ((:day 2) " " :short-month " " :short-weekday)))
  16. "local-time:format-timestring formats for different points")
  17. (defvar *forecast-emojis*
  18. '(("clear-day" . "☀")
  19. ("clear-night" . "⭐")
  20. ("rain" . "☔")
  21. ("snow" . "❄")
  22. ("sleet" . "❄☔")
  23. ("wind" . "💨")
  24. ("fog" . "🌁")
  25. ("cloudy" . "☁")
  26. ("partly-cloudy-day" . "⛅")
  27. ("partly-cloudy-night" . "⛅"))
  28. "Weather-to-emoji mapping")
  29. (defun forecast-format-unixtime (unix format tz)
  30. (local-time:format-timestring nil (local-time:unix-to-timestamp unix)
  31. :format format :timezone tz))
  32. (defun forecast-format-point (type point tz)
  33. (format nil "~A -~@[ ~A~]~@[ ~A~]~:[ ~A-~A~;~:* ~A~2*~]°C"
  34. (forecast-format-unixtime (aget "time" point)
  35. (aget type *forecast-point-formats*)
  36. tz)
  37. (aget (aget "icon" point) *forecast-emojis*)
  38. (aget "summary" point)
  39. (aget "temperature" point) (aget "temperatureMin" point) (aget "temperatureMax" point)))
  40. (defun forecast-format-currently (currently &optional (tz local-time:*default-timezone*))
  41. (when currently
  42. (forecast-format-point :current currently tz)))
  43. (defun forecast-format-hourly (hourly &optional (tz local-time:*default-timezone*))
  44. (when hourly
  45. (format nil "~@[~A~]~{~&~A~}"
  46. (aget "summary" hourly)
  47. (mapcar #'(lambda (point) (forecast-format-point :hour point tz))
  48. (subseq (aget "data" hourly) 0 24)))))
  49. (defun forecast-format-daily (daily &optional (tz local-time:*default-timezone*))
  50. (when daily
  51. (format nil "~@[~A~]~{~&~A~}"
  52. (aget "summary" daily)
  53. (mapcar #'(lambda (point) (forecast-format-point :day point tz))
  54. (subseq (aget "data" daily) 0 7)))))
  55. (defun forecast-format (forecast)
  56. (let* ((timezone (local-time:find-timezone-by-location-name
  57. (aget "timezone" forecast))))
  58. (format nil "~@[Сейчас: ~A~]~@[~&Сегодня: ~A~]~@[~&Неделя: ~A~]"
  59. (forecast-format-currently (aget "currently" forecast) timezone)
  60. (forecast-format-hourly (aget "hourly" forecast) timezone)
  61. (forecast-format-daily (aget "daily" forecast) timezone))))
  62. ;;; Hooks
  63. (defvar *chat-locations* nil "ALIST of chat->location")
  64. (def-message-handler handle-location (message)
  65. (let ((chat-id (aget "id" (aget "chat" message)))
  66. (location (aget "location" message)))
  67. (when location
  68. (log:info "handler-location" chat-id location)
  69. (set-setting '*chat-locations* (cons (cons chat-id location) *chat-locations*))
  70. (bot-send-message chat-id "Взял на карандаш")
  71. t)))
  72. (def-message-cmd-handler handle-cmd-weather (:weather :hourly :daily)
  73. (let* ((location (cdr (assoc chat-id *chat-locations*)))
  74. (response (if location
  75. (forecast-format
  76. (forecast
  77. (aget "latitude" location)
  78. (aget "longitude" location)
  79. :hourly (find "hourly" (cons (string cmd) args) :key #'string-downcase :test #'equal)
  80. :daily (find "daily" (cons (string cmd) args) :key #'string-downcase :test #'equal)))
  81. "Так а ты чьих будешь?")))
  82. (bot-send-message chat-id response)))