forecast.lisp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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:make-flexi-stream
  9. (drakma:http-request
  10. (format 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. :force-binary t :want-stream t :decode-content t)
  15. :external-format :utf8)
  16. :object-as :alist))
  17. (bordeaux-threads:timeout () (error "Timeout"))))
  18. (defvar *forecast-point-formats*
  19. '((:current . (:year "-" (:month 2) "-" (:day 2) " " (:hour 2) ":" (:min 2)))
  20. (:hour . ((:hour 2) ":" (:min 2)))
  21. (:day . ((:day 2) " " :short-month " " :short-weekday)))
  22. "local-time:format-timestring formats for different points")
  23. (defvar *forecast-emojis*
  24. '(("clear-day" . "☀")
  25. ("clear-night" . "⭐")
  26. ("rain" . "☔")
  27. ("snow" . "❄")
  28. ("sleet" . "❄☔")
  29. ("wind" . "💨")
  30. ("fog" . "🌁")
  31. ("cloudy" . "☁")
  32. ("partly-cloudy-day" . "⛅")
  33. ("partly-cloudy-night" . "⛅"))
  34. "Weather-to-emoji mapping")
  35. (defun forecast-format-unixtime (unix format tz)
  36. (local-time:format-timestring nil (local-time:unix-to-timestamp unix)
  37. :format format :timezone tz))
  38. (defun forecast-format-point (type point tz)
  39. (format nil "~A -~@[ ~A~]~@[ ~A~]~:[ ~A-~A~;~:* ~A~2*~]°C"
  40. (forecast-format-unixtime (aget "time" point)
  41. (aget type *forecast-point-formats*)
  42. tz)
  43. (aget (aget "icon" point) *forecast-emojis*)
  44. (aget "summary" point)
  45. (aget "temperature" point) (aget "temperatureMin" point) (aget "temperatureMax" point)))
  46. (defun forecast-format-currently (currently &optional (tz local-time:*default-timezone*))
  47. (when currently
  48. (forecast-format-point :current currently tz)))
  49. (defun forecast-format-hourly (hourly &optional (tz local-time:*default-timezone*))
  50. (when hourly
  51. (format nil "~@[~A~]~{~&~A~}"
  52. (aget "summary" hourly)
  53. (mapcar #'(lambda (point) (forecast-format-point :hour point tz))
  54. (subseq (aget "data" hourly) 0 24)))))
  55. (defun forecast-format-daily (daily &optional (tz local-time:*default-timezone*))
  56. (when daily
  57. (format nil "~@[~A~]~{~&~A~}"
  58. (aget "summary" daily)
  59. (mapcar #'(lambda (point) (forecast-format-point :day point tz))
  60. (subseq (aget "data" daily) 0 7)))))
  61. (defun forecast-format (forecast)
  62. (let* ((timezone (local-time:find-timezone-by-location-name
  63. (aget "timezone" forecast))))
  64. (format nil "~@[Сейчас: ~A~]~@[~&Сегодня: ~A~]~@[~&Неделя: ~A~]"
  65. (forecast-format-currently (aget "currently" forecast) timezone)
  66. (forecast-format-hourly (aget "hourly" forecast) timezone)
  67. (forecast-format-daily (aget "daily" forecast) timezone))))