Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
get the weather data for my city
(require 'json)
(defvar owm/base-api-url "api.openweathermap.org")
(defvar owm/base-api-ver 2.5)
(defvar owm/api-key "PUT_API_KEY_HERE")
(defvar owm/default-scheme "https")
(defun owm/build-url (appid lat lon lang units)
(let* ((local (format "data/%s/weather" owm/base-api-ver))
(query
(format "lat=%s&lon=%s&appid=%s&lang=%s&units=%s"
lat lon appid lang units)))
(format "%s://%s/%s?%s" owm/default-scheme owm/base-api-url local query)))
(defun owm/get-weather (lat lon lang units)
(let ((api-url (owm/build-url owm/api-key lat lon lang units)))
(with-temp-buffer
(url-insert-file-contents api-url)
(let* ((json-false :false)
(json-encoding-pretty-print t)
(response (json-read))
(desc (cdr (assoc 'main (aref (cdr (assoc 'weather response)) 0))))
(temp (cdr (assoc 'temp (assoc 'main response))))
(city (cdr (assoc 'name response))))
(format "%s. %s, %.1f °C" city desc temp)))))
(owm/get-weather 50.43 30.52 "en" "metric") ;; ~> Misto Kyyiv. Clear, 5.6 °C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.