diff --git a/ystockquote.py b/ystockquote.py index 4bb388e..649965c 100644 --- a/ystockquote.py +++ b/ystockquote.py @@ -13,16 +13,18 @@ # Requires: Python 2.7/3.3+ -__version__ = '0.2.5dev' +__version__ = '0.2.6dev' try: # py3 + import datetime from urllib.request import Request, urlopen from urllib.parse import urlencode except ImportError: # py2 - from urllib2 import Request, urlopen + import datetime from urllib import urlencode + from urllib2 import Request, urlopen def _request(symbol, stat): @@ -464,11 +466,19 @@ def get_short_ratio(symbol): def get_historical_prices(symbol, start_date, end_date): """ Get historical prices for the given ticker symbol. - Date format is 'YYYY-MM-DD' + Date format is 'YYYY-MM-DD'. Also accepts datetime + in addition to strings. Returns a nested dictionary (dict of dicts). outer dict keys are dates ('YYYY-MM-DD') """ + try: + start_date = start_date.strftime('%Y-%m-%d') + end_date = end_date.strftime('%Y-%m-%d') + except AttributeError: + start_date = str(start_date) + end_date = str(end_date) + params = urlencode({ 's': symbol, 'a': int(start_date[5:7]) - 1,