From 1925e57db44a1156b1c5917b8e39f1b6be54ac70 Mon Sep 17 00:00:00 2001 From: Alex Malek Date: Fri, 26 May 2017 23:21:36 -0400 Subject: [PATCH 1/4] new historical quote url/parsing/api code --- ystockquote.py | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/ystockquote.py b/ystockquote.py index 905b362..98e9853 100644 --- a/ystockquote.py +++ b/ystockquote.py @@ -13,7 +13,7 @@ # Requires: Python 2.7/3.3+ -__version__ = '0.2.6dev' # NOQA +__version__ = '0.2.7dev' # NOQA try: @@ -24,6 +24,9 @@ # py2 from urllib2 import Request, urlopen from urllib import urlencode +from datetime import datetime, timedelta +import re +import json def _request(symbol, stat): @@ -470,32 +473,30 @@ def get_historical_prices(symbol, start_date, end_date): Returns a nested dictionary (dict of dicts). outer dict keys are dates ('YYYY-MM-DD') """ + epoch = datetime(1970, 1, 1) + p1 = datetime.strptime(start_date, '%Y-%m-%d') + p2 = datetime.strptime(end_date, '%Y-%m-%d') params = urlencode({ - 's': symbol, - 'a': int(start_date[5:7]) - 1, - 'b': int(start_date[8:10]), - 'c': int(start_date[0:4]), - 'd': int(end_date[5:7]) - 1, - 'e': int(end_date[8:10]), - 'f': int(end_date[0:4]), - 'g': 'd', - 'ignore': '.csv', + 'period1': int((p1 - epoch).total_seconds()), + 'period2': int((p2 - epoch + timedelta(days=1)).total_seconds()), + 'frequency': '1d', + 'filter': 'history', }) - url = 'http://real-chart.finance.yahoo.com/table.csv?%s' % params + url = 'https://finance.yahoo.com/quote/%s/history?%s' % (symbol, params) req = Request(url) resp = urlopen(req) - content = str(resp.read().decode('utf-8').strip()) - daily_data = content.splitlines() + content = resp.read() + quotes = re.findall('{"date":\d+[^}]+}', content) hist_dict = dict() - keys = daily_data[0].split(',') - for day in daily_data[1:]: - day_data = day.split(',') - date = day_data[0] - hist_dict[date] = \ - {keys[1]: day_data[1], - keys[2]: day_data[2], - keys[3]: day_data[3], - keys[4]: day_data[4], - keys[5]: day_data[5], - keys[6]: day_data[6]} + for quote in quotes: + j = json.loads(quote) + for k in ('open', 'close', 'high', 'low', 'unadjclose'): + j[k] = "%.2f" % j[k] + d = timedelta(seconds=j["date"]) + # these keys are for backwards compatibility + for key in j.keys(): + j[key.capitalize()] = j[key] + j['Adj Close'] = j['unadjclose'] + date = epoch + d + hist_dict[date.date().isoformat()] = j return hist_dict From f3a20b5305ec633068373a1d6060c63e4207afe1 Mon Sep 17 00:00:00 2001 From: Alex Malek Date: Thu, 6 Jul 2017 09:42:53 -0400 Subject: [PATCH 2/4] fix version from cgoldberg: "this should still be 0.2.6dev.. next release will be 0.2.6" --- ystockquote.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ystockquote.py b/ystockquote.py index 98e9853..0e72e6c 100644 --- a/ystockquote.py +++ b/ystockquote.py @@ -13,7 +13,7 @@ # Requires: Python 2.7/3.3+ -__version__ = '0.2.7dev' # NOQA +__version__ = '0.2.6dev' # NOQA try: From 20261fb51b5f9ac03e17be8103efd6698dd5cc88 Mon Sep 17 00:00:00 2001 From: Alex Malek Date: Thu, 6 Jul 2017 09:45:41 -0400 Subject: [PATCH 3/4] Update ystockquote.py cgoldberg: "alphabetize imports says PEP8" --- ystockquote.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ystockquote.py b/ystockquote.py index 0e72e6c..d9670a9 100644 --- a/ystockquote.py +++ b/ystockquote.py @@ -25,8 +25,8 @@ from urllib2 import Request, urlopen from urllib import urlencode from datetime import datetime, timedelta -import re import json +import re def _request(symbol, stat): From 4c00608508632609d9e9e7427021a0ebe210053c Mon Sep 17 00:00:00 2001 From: Alex Malek Date: Thu, 6 Jul 2017 10:34:15 -0400 Subject: [PATCH 4/4] "Close" and "Adj Close" will yield same result as old api use the lower case keys to get the "new" values Not sure if under the old api "High" / "Low" was adjusted or not If so more work will need to be done to be backwards compatible --- ystockquote.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ystockquote.py b/ystockquote.py index d9670a9..dc9aff6 100644 --- a/ystockquote.py +++ b/ystockquote.py @@ -496,7 +496,9 @@ def get_historical_prices(symbol, start_date, end_date): # these keys are for backwards compatibility for key in j.keys(): j[key.capitalize()] = j[key] - j['Adj Close'] = j['unadjclose'] + # in the old api "Close" was the unadj close + j['Close'] = j['unadjclose'] + j['Adj Close'] = j['close'] date = epoch + d hist_dict[date.date().isoformat()] = j return hist_dict