Skip to content

Commit ced6b7e

Browse files
Merge pull request #83 from davidastephens/url_encode_cleanup
ENH: Add _encode_url to improve readability of url construction
2 parents f7b92a2 + 646e2c9 commit ced6b7e

File tree

5 files changed

+59
-32
lines changed

5 files changed

+59
-32
lines changed

pandas_datareader/google/daily.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from pandas.io.common import urlencode
2-
from pandas_datareader.utils import _retry_read_url
2+
from pandas_datareader.utils import _retry_read_url, _encode_url
33
from pandas_datareader.utils import _sanitize_dates
44
from pandas_datareader.utils import _get_data_from
55

6-
_URL = 'http://www.google.com/finance/historical?'
6+
_URL = 'http://www.google.com/finance/historical'
77

88

99
def _get_data(symbols=None, start=None, end=None, retry_count=3,
@@ -49,9 +49,11 @@ def _get_data_one(sym, start, end, interval, retry_count, pause):
4949
start, end = _sanitize_dates(start, end)
5050

5151
# www.google.com/finance/historical?q=GOOG&startdate=Jun+9%2C+2011&enddate=Jun+8%2C+2013&output=csv
52-
url = "%s%s" % (_URL,
53-
urlencode({"q": sym,
54-
"startdate": start.strftime('%b %d, ' '%Y'),
55-
"enddate": end.strftime('%b %d, %Y'),
56-
"output": "csv"}))
52+
params = {
53+
'q': sym,
54+
'startdate': start.strftime('%b %d, %Y'),
55+
'enddate': end.strftime('%b %d, %Y'),
56+
'output': "csv"
57+
}
58+
url = _encode_url(_URL, params)
5759
return _retry_read_url(url, retry_count, pause, 'Google')

pandas_datareader/utils.py

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
from pandas.compat import StringIO, bytes_to_str
1313
from pandas.util.testing import _network_error_classes
1414

15+
if compat.PY3:
16+
from urllib.parse import urlencode
17+
else:
18+
from urllib import urlencode
1519

1620
class SymbolWarning(UserWarning):
1721
pass
@@ -85,6 +89,16 @@ def _in_chunks(seq, size):
8589
"""
8690
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
8791

92+
def _encode_url(url, params):
93+
"""
94+
Return encoded url with parameters
95+
"""
96+
s_params = urlencode(params)
97+
if s_params:
98+
return url + '?' + s_params
99+
else:
100+
return url
101+
88102
def _retry_read_url(url, retry_count, pause, name):
89103
"""
90104
Open url (and retry)

pandas_datareader/yahoo/actions.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from pandas.util.testing import _network_error_classes
66
from pandas.compat import StringIO, bytes_to_str
77

8-
from pandas_datareader.utils import _sanitize_dates
8+
from pandas_datareader.utils import _sanitize_dates, _encode_url
99

10-
_URL = 'http://ichart.finance.yahoo.com/x?'
10+
_URL = 'http://ichart.finance.yahoo.com/x'
1111

1212

1313
def _get_data(symbol, start=None, end=None, retry_count=3, pause=0.001):
@@ -31,14 +31,17 @@ def _get_data(symbol, start=None, end=None, retry_count=3, pause=0.001):
3131
"""
3232

3333
start, end = _sanitize_dates(start, end)
34-
url = (_URL + 's=%s' % symbol + \
35-
'&a=%s' % (start.month - 1) + \
36-
'&b=%s' % start.day + \
37-
'&c=%s' % start.year + \
38-
'&d=%s' % (end.month - 1) + \
39-
'&e=%s' % end.day + \
40-
'&f=%s' % end.year + \
41-
'&g=v')
34+
params = {
35+
's': symbol,
36+
'a': start.month - 1,
37+
'b': start.day,
38+
'c': start.year,
39+
'd': end.month - 1,
40+
'e': end.day,
41+
'f': end.year,
42+
'g': 'v'
43+
}
44+
url = _encode_url(_URL, params)
4245

4346
for _ in range(retry_count):
4447
time.sleep(pause)

pandas_datareader/yahoo/daily.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from pandas_datareader.utils import _retry_read_url
1+
from pandas_datareader.utils import _retry_read_url, _encode_url
22
from pandas_datareader.utils import _sanitize_dates
33
from pandas_datareader.utils import _get_data_from
44

5-
_URL = 'http://ichart.finance.yahoo.com/table.csv?'
5+
_URL = 'http://ichart.finance.yahoo.com/table.csv'
66

77
def _get_data(symbols=None, start=None, end=None, retry_count=3,
88
pause=0.001, adjust_price=False, ret_index=False,
@@ -96,13 +96,16 @@ def _get_data_one(sym, start, end, interval, retry_count, pause):
9696
Returns a DataFrame.
9797
"""
9898
start, end = _sanitize_dates(start, end)
99-
url = (_URL + 's=%s' % sym +
100-
'&a=%s' % (start.month - 1) +
101-
'&b=%s' % start.day +
102-
'&c=%s' % start.year +
103-
'&d=%s' % (end.month - 1) +
104-
'&e=%s' % end.day +
105-
'&f=%s' % end.year +
106-
'&g=%s' % interval +
107-
'&ignore=.csv')
99+
params = {
100+
's': sym,
101+
'a': start.month - 1,
102+
'b': start.day,
103+
'c': start.year,
104+
'd': end.month - 1,
105+
'e': end.day,
106+
'f': end.year,
107+
'g': interval,
108+
'ignore': '.csv'
109+
}
110+
url = _encode_url(_URL, params)
108111
return _retry_read_url(url, retry_count, pause, 'Yahoo!')

pandas_datareader/yahoo/quotes.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
import pandas.compat as compat
33
from pandas.io.common import urlopen
44
from pandas import DataFrame
5+
from pandas_datareader.utils import _encode_url
56

67
_yahoo_codes = {'symbol': 's', 'last': 'l1', 'change_pct': 'p2', 'PE': 'r',
78
'time': 't1', 'short_ratio': 's7'}
89

910

10-
_URL = 'http://finance.yahoo.com/d/quotes.csv?'
11+
_URL = 'http://finance.yahoo.com/d/quotes.csv'
1112

1213

1314
def _get_data(symbols):
@@ -27,10 +28,14 @@ def _get_data(symbols):
2728

2829
data = defaultdict(list)
2930

30-
url_str = _URL + 's=%s&f=%s' % (sym_list, request)
31+
params = {
32+
's': sym_list,
33+
'f': request
34+
}
35+
url = _encode_url(_URL, params)
3136

32-
with urlopen(url_str) as url:
33-
lines = url.readlines()
37+
with urlopen(url) as response:
38+
lines = response.readlines()
3439

3540
for line in lines:
3641
fields = line.decode('utf-8').strip().split(',')

0 commit comments

Comments
 (0)