Yahoo stock quotes and company news interface





8
Date Submitted Sat. Aug. 5th, 2006 6:44 PM
Revision 1 of 1
Scripter ASmith
Tags "Yahoo Finance" | Finance | news | python | RSS
Comments 0 comments
This class allows you to retrieve a companies current stock quote from Yahoo Finance, as well as current news feed entries for that company.


import re
import feedparser
import csv
from urlgrabber.grabber import URLGrabber

class YFinEngine:
  def __init__(self):
    self.symbol_pattern=re.compile("^[A-Z]{4}$")
    self.grabber=URLGrabber()
 
  """
  Determines whether the symbol is a valid NYSE symbol
  "
""
  def validate_symbol(self,symbol):
    retval=False
    match=self.symbol_pattern.match(symbol)
    if match:
      retval=True
    return retval
   
  """
  Builds a CSV file url for a specific company
  "
""   
  def getstockurl(self,symbol):
    return "%ss=%s&f=sl1d1t1c1ohgv&e=.csv" %\
    ("http://finance.yahoo.com/d/quotes.csv?",symbol)
   
  """
  Returns a companies current stock quote
  "
""   
  def getCurrentQuote(self,symbol):
    url=self.getstockurl(symbol)
    data=self.grabber.urlread(url)
    return list(csv.reader(data)[0])

  """
  Returns feedparser RSS feed entries for Yahoos news feed for a specified symbol/company.
  "
""
  def getnews(self,symbol):
    url=self.getnewsurl(symbol)
    for entry in feedparser.parse(url,handlers=[self.grabber]).entries:
      yield entry
 
  """
  Builds an RSS feed url given a specific company
  "
""   
  def getnewsurl(self,symbol):
    return "%ss=%s" %("http://finance.yahoo.com/rss/headline?",symbol)

 

Comments

There are currently no comments for this snippet.

Voting