1
0

forecast.lisp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. (in-package #:chatikbot)
  2. (defvar *forecast-api-key* nil "forecast.io APIKEY")
  3. (defparameter +forecast-api-url+ "https://api.forecast.io/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. (defvar *forecast-point-formats*
  10. '((:current . (:year "-" (:month 2) "-" (:day 2) " " (:hour 2) ":" (:min 2)))
  11. (:hour . ((:hour 2) ":" (:min 2)))
  12. (:day . ((:day 2) " " :short-month " " :short-weekday)))
  13. "local-time:format-timestring formats for different points")
  14. (defvar *forecast-emojis*
  15. '(("clear-day" . "☀")
  16. ("clear-night" . "⭐")
  17. ("rain" . "☔")
  18. ("snow" . "❄")
  19. ("sleet" . "❄☔")
  20. ("wind" . "💨")
  21. ("fog" . "🌁")
  22. ("cloudy" . "☁")
  23. ("partly-cloudy-day" . "⛅")
  24. ("partly-cloudy-night" . "⛅"))
  25. "Weather-to-emoji mapping")
  26. (defun forecast-format-unixtime (unix format tz)
  27. (local-time:format-timestring nil (local-time:unix-to-timestamp unix)
  28. :format format :timezone tz))
  29. (defun forecast-format-point (type point tz)
  30. (format nil "~A -~@[ ~A~]~@[ ~A~]~:[ ~A-~A~;~:* ~A~2*~]°C"
  31. (forecast-format-unixtime (aget "time" point)
  32. (aget type *forecast-point-formats*)
  33. tz)
  34. (aget (aget "icon" point) *forecast-emojis*)
  35. (aget "summary" point)
  36. (aget "temperature" point) (aget "temperatureMin" point) (aget "temperatureMax" point)))
  37. (defun forecast-format-currently (currently &optional (tz local-time:*default-timezone*))
  38. (when currently
  39. (forecast-format-point :current currently tz)))
  40. (defun forecast-format-hourly (hourly &optional (tz local-time:*default-timezone*))
  41. (when hourly
  42. (format nil "~@[~A~]~{~&~A~}"
  43. (aget "summary" hourly)
  44. (mapcar #'(lambda (point) (forecast-format-point :hour point tz))
  45. (subseq (aget "data" hourly) 0 24)))))
  46. (defun forecast-format-daily (daily &optional (tz local-time:*default-timezone*))
  47. (when daily
  48. (format nil "~@[~A~]~{~&~A~}"
  49. (aget "summary" daily)
  50. (mapcar #'(lambda (point) (forecast-format-point :day point tz))
  51. (subseq (aget "data" daily) 0 7)))))
  52. (defun forecast-format (forecast)
  53. (let* ((timezone (local-time:find-timezone-by-location-name
  54. (aget "timezone" forecast))))
  55. (format nil "~@[Сейчас: ~A~]~@[~&Сегодня: ~A~]~@[~&Неделя: ~A~]"
  56. (forecast-format-currently (aget "currently" forecast) timezone)
  57. (forecast-format-hourly (aget "hourly" forecast) timezone)
  58. (forecast-format-daily (aget "daily" forecast) timezone))))