Skip to content

Commit

Permalink
Merge pull request #2 from ipl31/bugfix
Browse files Browse the repository at this point in the history
Yahoo data script improvements
  • Loading branch information
Sourabh Bajaj committed Dec 21, 2012
2 parents 3dd1639 + c5c1d47 commit e1840a9
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions Tools/YahooDataPull.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@
import urllib2
import urllib
import datetime
import sys
import os


def get_data(data_path, ls_symbols):

#Create path if it doesn't exist
# Create path if it doesn't exist
if not (os.access(data_path, os.F_OK)):
os.makedirs(data_path)

#utils.clean_paths(data_path)
# utils.clean_paths(data_path)

_now =datetime.datetime.now();
miss_ctr=0; #Counts how many symbols we could get
miss_ctr=0; #Counts how many symbols we could not get
for symbol in ls_symbols:
# Preserve original symbol since it might
# get manipulated if it starts with a "$"
symbol_name = symbol
if symbol[0] == '$':
symbol = '^' + symbol[1:]

symbol_data=list()
#print "Getting " + str (symbol_name)
# print "Getting {0}".format(symbol)

try:
params= urllib.urlencode ({'a':1, 'b':1, 'c':2000, 'd':_now.month, 'e':_now.day, 'f':_now.year, 's': str(symbol)})
url_get= urllib2.urlopen("http://ichart.finance.yahoo.com/table.csv?%s" % params)
params= urllib.urlencode ({'a':1, 'b':1, 'c':2000, 'd':_now.month, 'e':_now.day, 'f':_now.year, 's': symbol})
url = "http://ichart.finance.yahoo.com/table.csv?%s" % params
url_get= urllib2.urlopen(url)

header= url_get.readline()
symbol_data.append (url_get.readline())
Expand All @@ -49,20 +51,22 @@ def get_data(data_path, ls_symbols):
f.close();

except urllib2.HTTPError:
miss_ctr= miss_ctr+1
print "Unable to fetch data for stock: " + str (symbol_name)
miss_ctr += 1
print "Unable to fetch data for stock: {0} at {1}".format(symbol_name, url)
except urllib2.URLError:
print "URL Error for stock: " + str (symbol_name)
miss_ctr += 1
print "URL Error for stock: {0} at {1}".format(symbol_name, url)

print "All done. Got " + str (len(ls_symbols) - miss_ctr) + " stocks. Could not get " + str (miss_ctr) + " stocks."
print "All done. Got {0} stocks. Could not get {1}".format(len(ls_symbols) - miss_ctr, miss_ctr)

def read_symbols(s_symbols_file):

ls_symbols=[]
file = open(s_symbols_file, 'r')
for f in file.readlines():
j = f[:-1]
ls_symbols.append(j)
for line in file.readlines():
str_line = str(line)
if str_line.strip():
ls_symbols.append(str_line.strip())
file.close()

return ls_symbols
Expand Down

0 comments on commit e1840a9

Please sign in to comment.