tags:

views:

257

answers:

2
+2  A: 

Check out http://edward.oconnor.cx/elisp/. Edward has some examples of interacting with various services using HTTP, and if you can't find a Yahoo client library you could write one using these techniques.

Jim Ferrans
+2  A: 

Here's some code to get you started; I show how to grab the url to a buffer, parse each line and then display the ticker and price of each item. You can modify it from there to do what you need.

This parses each line of stock data into a list, and it's straight forward to grab the values using the first, second, third functions, or using nth. You can write functions to grab each element you want, such as get-ticker(quote) which would just return (first ticker)

I wouldn't over think what kind of data structure to use; whatever is easiest is fine. If you need high performance then you shouldn't be using emacs lisp for this anyway.

(defun test()
  (interactive)
  (let ((quotes (get-quotes '("AAPL" "GOOG" "MSFT" "ORCL" "ERTS" "THQI") "sb")))
    (show-quotes quotes)))

(defun show-quotes(quotes)
  (dolist (quote quotes)
    (message (format "%s $%.2f" (first quote) (string-to-number (second quote))))))

(defun get-quotes(tickers field-string)
  "Given a list of ticker names and a string of fields to return as above, this grabs them
from Yahoo, and parses them"
  (let ((results-buffer (get-yahoo-quotes-to-buffer (get-price-url tickers field-string))))
    (switch-to-buffer results-buffer)
    (parse-quote-buffer results-buffer)))

(defun get-price-url (tickers field-string)
  "Set up the get url"
  (concat "http://download.finance.yahoo.com/d/quotes.csv?s=" 
      (mapconcat 'identity tickers "+") 
      "&f=" field-string))

(defun get-yahoo-quotes-to-buffer(url)
  "Retrieve the quotes to a buffer and return it"
  (url-retrieve-synchronously url))

(defun parse-quote-buffer(b)
  "Parse the buffer for quotes"
  (goto-line 1)
  (re-search-forward "^\n")
  (beginning-of-line)
  (let ((res nil))
    (while (> (point-max) (point))
      (setf res (cons  (split-string (thing-at-point 'line) ",") res))
      (forward-line 1))
    (reverse res)))
justinhj