def test_yahoo_symbols_url(self): """ Test to create url from a given list of stock symbols """ yahoo_test_url = ( r"https://query.yahooapis.com/v1/public/yql?q=" r"select%20*%20from%20yahoo.finance.quote%20where" r"%20symbol%20in" r"%20(%22A%22%2C%22B%22%2C%22C%22)&format=json&di" r"agnostics=true" r"&env=store%3A%2F%2Fdatatables.org%2Falltableswit" r"hkeys&callback=" ) produced = yahoo_url(["A", "B", "C"]) self.assertEqual(yahoo_test_url, produced)
def get_info(symbols, time=30, max_iterations=5): ''' Takes a list of symbols as input and outputs an array of stock objects. Stock information goes back time days. Since Yahoo API is not always reliable, allow up to max_iterations calls to the API ''' if len(symbols) == 0: return [] symbols = symbols[:30] url = yahoo_url(symbols) data = request_json(url) if 'error' in data: raise Exception('Error in call to yahoo', data['error']['description']) quotes = data['query']['results']['quote'] if (type(quotes) != list): quotes = [quotes] stocks = parallel_stocks(quotes, time, max_iterations) return stocks