From 82251df6ed806e3342996e0341f544f789bc8cb9 Mon Sep 17 00:00:00 2001 From: Austin Hanson Date: Sat, 8 Mar 2014 22:02:56 -0800 Subject: [PATCH 1/3] Adding support for multiple symbol requesting. --- test_ystockquote.py | 30 +++++++++++++++++++++++++++++- ystockquote.py | 22 ++++++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) mode change 100644 => 100755 ystockquote.py diff --git a/test_ystockquote.py b/test_ystockquote.py index bf33a6a..b2f6d7e 100644 --- a/test_ystockquote.py +++ b/test_ystockquote.py @@ -32,7 +32,7 @@ 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) @@ -40,6 +40,34 @@ def test_get_all(self): 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' diff --git a/ystockquote.py b/ystockquote.py old mode 100644 new mode 100755 index 7cce2d2..078e58e --- a/ystockquote.py +++ b/ystockquote.py @@ -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): @@ -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], From f32b11b2ee8864816dbea60192a2cad6c9da50d2 Mon Sep 17 00:00:00 2001 From: Austin Hanson Date: Sat, 8 Mar 2014 22:07:29 -0800 Subject: [PATCH 2/3] Updating README to reflect multiple symbol requests --- README.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.rst b/README.rst index 1f10f66..5907eba 100644 --- a/README.rst +++ b/README.rst @@ -47,6 +47,10 @@ Example Usage >>> print(ystockquote.get_bid_realtime('GOOG')) '904.77' >>> + >>> print(ystockquote.get_price_book(['GOOG','TSLA'])) + [u'4.69', u'46.67'] + >>> print(ystockquote.get_bid_realtime(['GOOG','TSLA'])) + [u'1214.3199', u'245.41'] .. code:: python From 689884d0485d1937adee54960ba86d465695595e Mon Sep 17 00:00:00 2001 From: Austin Hanson Date: Sat, 8 Mar 2014 22:09:06 -0800 Subject: [PATCH 3/3] Minor README cleanup --- README.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/README.rst b/README.rst index 5907eba..a659eb1 100644 --- a/README.rst +++ b/README.rst @@ -46,7 +46,6 @@ Example Usage '51.18' >>> print(ystockquote.get_bid_realtime('GOOG')) '904.77' - >>> >>> print(ystockquote.get_price_book(['GOOG','TSLA'])) [u'4.69', u'46.67'] >>> print(ystockquote.get_bid_realtime(['GOOG','TSLA']))