You are not logged in.

#1 2017-07-23 22:04:42

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,784

Emacs Lisp Weather Underground

I am finally trying to learn a little lisp -- and it is kicking my ass.

Never-the-less, below is an elisp file that gets some data from weather underground and puts some interesting data into another new buffer.  To use it, one needs to set the api key through the emacs customization groups.  One should also set their own location rather than the default (near my home)

If anyone is good with elisp, I would appreciate some feedback.

(require 'request)
(require 'json)

(defgroup wunderground nil
  "Obtain weather report from wunderground."
  :prefix "wunder-"
  :group 'applications)

(defcustom wunderground-addresss "http://api.wunderground.com/"
  "Base address for the Wunderground URL" 
  :group 'wunderground
  :type 'string)

(defcustom wunderground-apikey ""
  "API key for Wunderground" 
  :group 'wunderground
  :type 'string)

(defcustom wunderground-location "91104"
  "Location for Wunderground" 
  :group 'wunderground
  :type 'string)

(makunbound 'interestingItems)

(defconst  interestingItems '(("Obsertvation Time: %s\n"     (current_observation observation_time))
			      ("Location: %s\n"              (current_observation observation_location full))
			      ("Latitude / Longitutude: %s " (current_observation display_location latitude))
			      ("%s \n"                       (current_observation display_location longitude))
			      ("Temperature: %s\n"           (current_observation temperature_string))
			      ("Dewpoint: %s\n"              (current_observation dewpoint_string))
			      ("Humidity %s\n"               (current_observation relative_humidity))
			      ("Wind: %s\n    "              (current_observation wind_string))
			      ("Pressure: %s in, "           (current_observation pressure_in))
			      ("Trend: %s \n"                (current_observation pressure_trend))
			      ("Lunar phase:  %s\n"          (moon_phase phaseofMoon))
			      ("Sunrise %s"                  (sun_phase sunrise hour))
			      (":%s"                         (sun_phase sunrise minute))
			      ("; Sunset %s"                 (sun_phase sunset hour))
			      (":%s\n"                       (sun_phase sunset minute)))
  
  "  Define the fields of interest from the Wunderground json data.
     Interesting items are defined as a list of entries.  Each entry consists of:
       FORMAT_STRING LIST
     where LIST contains the nodes to decend to locate the data of interest under the indentured structure"   
  )
  

  
(defun wunderground ()
  "Obtain weather forcast from Weather Underground"
  (interactive)
  (request
   (concat wunderground-addresss
	   "/api/"
	   wunderground-apikey
	   "/conditions/astronomy/q/"
	   wunderground-location
	   ".json")
   :parser 'json-read
   :success (function*
	     (lambda (&key data &allow-other-keys)
	       (progn
		 (with-current-buffer (get-buffer-create "*Current Weather*")
		   (erase-buffer)
		   (dolist (item interestingItems)
		     (let ((thePath data))
		       (dolist (theLevel (nth 1 item))
			 (if (listp (cdr (assoc theLevel thePath)))
			     (setq thePath (assoc theLevel thePath))
			   (insert-string 
			    (format (car item) (cdr (assoc theLevel thePath))))))))
		   (pop-to-buffer (current-buffer))))))
  
;   :error
;   (function* (lambda (&key error-thrown &allow-other-keys&rest )
;		(message "Got error: %S" error-thrown)))
   :complete (lambda (&rest _) (message "Finished!"))
   :status-code '((400 . (lambda (&rest _) (message "Got 400.")))
		  (418 . (lambda (&rest _) (message "Got 418."))))
   )
  )


(global-set-key (kbd "<pause> w") 'wunderground)

Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

Board footer

Powered by FluxBB