1
0

forecast.lisp 4.1 KB

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