Skip to content

Commit

Permalink
Adding support for multiple symbol requesting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Hanson committed Mar 9, 2014
1 parent dfe6e04 commit 82251df
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
30 changes: 29 additions & 1 deletion test_ystockquote.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,42 @@ def test_pep8_conformance(self):

class YStockQuoteTestCase(TestWithScenarios):

def test_get_all(self):
def test_get_all_single(self):
symbol = 'GOOG'
all_info = ystockquote.get_all(symbol)
self.assertIsInstance(all_info, dict)
pc = all_info['previous_close']
self.assertNotEqual(pc, 'N/A')
self.assertGreater(float(pc), 0)

def test_get_all_multiple(self):
symbols = ['GOOG', 'TSLA']
all_info = ystockquote.get_all(symbols)
self.assertIsInstance(all_info, list)
for row in all_info:
self.assertIsInstance(row, dict)
pc = row['previous_close']
self.assertNotEqual(pc, 'N/A')
self.assertGreater(float(pc), 0)

def test_excessive_symbol_request(self):
symbols = ['GOOG'] * 201
self.assertRaises(Exception, ystockquote.get_all, (symbols))

def test_get_previous_close_single(self):
symbol = 'GOOG'
pc = ystockquote.get_previous_close(symbol)
self.assertNotEqual(pc, 'N/A')
self.assertGreater(float(pc), 0)

def test_get_previous_close_multiple(self):
symbols = ['GOOG', 'TSLA']
pcs = ystockquote.get_previous_close(symbols)
self.assertIsInstance(pcs, list)
for pc in pcs:
self.assertNotEqual(pc, 'N/A')
self.assertGreater(float(pc), 0)

def test_get_historical_prices(self):
symbol = 'GOOG'
start_date = '2013-01-02'
Expand Down
22 changes: 20 additions & 2 deletions ystockquote.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@


def _request(symbol, stat):
symbol = symbol if not isinstance(symbol, list) else "+".join(symbol)
url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
req = Request(url)
resp = urlopen(req)
content = resp.read().decode().strip()
return content
return content if "+" not in symbol else content.splitlines()


def get_all(symbol):
Expand All @@ -39,11 +40,28 @@ def get_all(symbol):
Returns a dictionary.
"""

if isinstance(symbol, list) and len(symbol) > 200:
raise Exception("Yahoo Finance only supports 200 symbols symbols in "
"any single request.")

ids = \
'ydb2r1b3qpoc1d1cd2c6t1k2p2c8m5c3m6gm7hm8k1m3lm4l1t8w1g1w4g3p' \
'1g4mg5m2g6kvjj1j5j3k4f6j6nk5n4ws1xj2va5b6k3t7a2t615l2el3e7v1' \
'e8v7e9s6b4j4p5p6rr2r5r6r7s7'
values = _request(symbol, ids).split(',')

result = _request(symbol, ids)

if isinstance(symbol, list):
for (i, row) in enumerate(result):
result[i] = _parse_array(row.split(','))

return result
else:
return _parse_array(result.split(','))


def _parse_array(values):
return dict(
dividend_yield=values[0],
dividend_per_share=values[1],
Expand Down

0 comments on commit 82251df

Please sign in to comment.