1
0

forecast.lisp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. (handler-case
  6. (bordeaux-threads:with-timeout (5)
  7. (yason:parse
  8. (flexi-streams:octets-to-string
  9. (drakma:http-request (format
  10. nil
  11. "~A/~A/~A,~A~@[,~A~]?units=si&exclude=~:[currently,~;~]~:[minutely,~;~]~:[hourly,~;~]~:[daily,~;~]~:[alerts,~;~]flags&lang=ru"
  12. +forecast-api-url+ *forecast-api-key* lat lon time
  13. currently minutely hourly daily alerts))
  14. :external-format :utf8)
  15. :object-as :alist))
  16. (bordeaux-threads:timeout (e)
  17. (declare (ignore e))
  18. (error "Timeout"))))
  19. (local-time:reread-timezone-repository :timezone-repository "/usr/share/zoneinfo/")
  20. (defvar *forecast-point-formats*
  21. '((:current . (:year "-" (:month 2) "-" (:day 2) " " (:hour 2) ":" (:min 2)))
  22. (:hour . ((:hour 2) ":" (:min 2)))
  23. (:day . ((:day 2) " " :short-month " " :short-weekday)))
  24. "local-time:format-timestring formats for different points")
  25. (defvar *forecast-emojis*
  26. '(("clear-day" . "☀")
  27. ("clear-night" . "⭐")
  28. ("rain" . "☔")
  29. ("snow" . "❄")
  30. ("sleet" . "❄☔")
  31. ("wind" . "💨")
  32. ("fog" . "🌁")
  33. ("cloudy" . "☁")
  34. ("partly-cloudy-day" . "⛅")
  35. ("partly-cloudy-night" . "⛅"))
  36. "Weather-to-emoji mapping")
  37. (defun forecast-format-unixtime (unix format tz)
  38. (local-time:format-timestring nil (local-time:unix-to-timestamp unix)
  39. :format format :timezone tz))
  40. (defun forecast-format-point (type point tz)
  41. (format nil "~A -~@[ ~A~]~@[ ~A~]~:[ ~A-~A~;~:* ~A~2*~]°C"
  42. (forecast-format-unixtime (aget "time" point)
  43. (aget type *forecast-point-formats*)
  44. tz)
  45. (aget (aget "icon" point) *forecast-emojis*)
  46. (aget "summary" point)
  47. (aget "temperature" point) (aget "temperatureMin" point) (aget "temperatureMax" point)))
  48. (defun forecast-format-currently (currently &optional (tz local-time:*default-timezone*))
  49. (when currently
  50. (forecast-format-point :current currently tz)))
  51. (defun forecast-format-hourly (hourly &optional (tz local-time:*default-timezone*))
  52. (when hourly
  53. (format nil "~@[~A~]~{~&~A~}"
  54. (aget "summary" hourly)
  55. (mapcar #'(lambda (point) (forecast-format-point :hour point tz))
  56. (subseq (aget "data" hourly) 0 24)))))
  57. (defun forecast-format-daily (daily &optional (tz local-time:*default-timezone*))
  58. (when daily
  59. (format nil "~@[~A~]~{~&~A~}"
  60. (aget "summary" daily)
  61. (mapcar #'(lambda (point) (forecast-format-point :day point tz))
  62. (subseq (aget "data" daily) 0 7)))))
  63. (defun forecast-format (forecast)
  64. (let* ((timezone (local-time:find-timezone-by-location-name
  65. (aget "timezone" forecast))))
  66. (format nil "~@[Сейчас: ~A~]~@[~&Сегодня: ~A~]~@[~&Неделя: ~A~]"
  67. (forecast-format-currently (aget "currently" forecast) timezone)
  68. (forecast-format-hourly (aget "hourly" forecast) timezone)
  69. (forecast-format-daily (aget "daily" forecast) timezone))))