forecast.lisp 4.2 KB

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