Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for multiple symbol requesting #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ 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']))
[u'1214.3199', u'245.41']

.. code:: python

Expand Down
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