'''
Created on Mar 1, 2011

@author: apsd
'''

import ystockquote

def processRawData(raw_data):
    """
    The data that ystockquote gives us is a list of lists of strings, which can be thought
    of as a 2-D table (the outer list is a list of rows, and within each row, there
    is an element for each column). The first row are the headers; they're strings 
    describing what each column represents. We care about columns 0 (date), 4 (price),
    and 5 (volume traded). All the other rows are for dates, though they're sorted most
    recent to least recent (chronologically backwards).
    We want to return a list containing tuples of just the data we care about, sorted
    from least recent to most recent (chronologically forwards).
    """
    data = raw_data[1:]  # Get rid of the column titles
    data.reverse()  # Put it in chronological order
    return [(x[0], float(x[4]), int(x[5])) for x in data]

def getFileData(filename):
    '''
    The filename should specify a file with data from ystockquote. We return a list of
    just the data we care about (date, price, volume traded), sorted chronologically.
    '''
    file = open(filename)
    data = [line.split() for line in file]
    file.close()
    return processRawData(data)

def getWebData(symbol, start_date, end_date):
    '''
    symbol is a string denoting the ticker symbol of the stock.
    start_date and end_date are strings in the form YYYYMMDD.
    Use the ystockquote library to grab historical data on a stock. 
    Returns a list of (date, price) pairs, where date is a string and 
    price is a float.
    '''
    data = ystockquote.get_historical_prices(symbol, start_date, end_date)
    #print data
    return processRawData(data)

if __name__ == "__main__":
    data = getWebData("GOOG", "20120913", "20121009")
    print data
    print len(data)
    # if you want to test various functions, you can do it here.
