-
Notifications
You must be signed in to change notification settings - Fork 0
/
StockGrabber.py
157 lines (112 loc) · 5.67 KB
/
StockGrabber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import urlparse
from StringIO import StringIO
from urllib import urlencode
import requests
import csv
from flask import current_app
# Functions for interacting with Yahoo! Stock quote API.
# Info from http://www.jarloo.com/yahoo_finance/
FINANCE_API_URL = "http://finance.yahoo.com/d/quotes.csv?"
FINANCE_PARAMS_BASIC = { 'a': 'Ask', 'b' : 'Bid', 'c' : 'Change', 'n' : 'Name' }
FINANCE_PARAMS_EXTRA = { 'g' : 'Day Low', 'h': 'Day High', 'v': 'Volume', 'n' : 'Name' }
FINANCE_PARAMS_ALL = FINANCE_PARAMS_BASIC.copy()
FINANCE_PARAMS_ALL.update(FINANCE_PARAMS_EXTRA)
MAX_TEXT_FLASK_LENGTH = 160
MAX_TEXT_LENGTH = 1600
# Accepts [Tickers], and a dictionary { 'urlParam': 'Name of Parameter' } and returns { 'ticker': {'Name of Parameter' : 'Value'}}
def stockInfoFromTickers(tickers, paramDict = FINANCE_PARAMS_BASIC):
# Zip to tickers : { parameters : values }
returnDict = dict(zip( tickers, [ dict(zip(paramDict.values(), [None] * len(paramDict))) for _ in xrange(len(tickers)) ]))
parameters = paramDict.keys()
urlParams = { 's' : "+".join(tickers), 'f' : "".join(parameters) }
request = requests.get(FINANCE_API_URL, params = urlParams)
fileHandle = StringIO(request.text)
csvReader = csv.reader(fileHandle, delimiter = ',', dialect=None)
for index, row in enumerate(csvReader):
# Go from b2, b3 etc to Ask, Bid...
paramNames = map(lambda param: paramDict[param], parameters)
# Generate a mapping of Ask:AskVal, Bid:BidVal
stockValues = zip(paramNames, row)
returnDict[tickers[index]].update(stockValues)
return returnDict
# Given tickers, parameters as { 'yahooParam' : 'Parameter Name' }, and optionally a header returns a well-formatted string with information on each ticker.
def stockInfoAsString(stockList, params=FINANCE_PARAMS_BASIC, header = ""):
parameters = params.copy()
parameters.update({'n' : 'Name'})
stockInfo = stockInfoFromTickers(stockList, parameters)
#Return a response
returnLines = header
for stock in stockList:
if stockInfo[stock]['Name'] != 'N/A':
returnLines += "%s - %s \n" % (stockInfo[stock]['Name'], stock.upper())
paramsNoName = filter(lambda key: key != 'Name', parameters.values())
for param in paramsNoName:
returnLines += "%s : %s\n" % (param, stockInfo[stock][param])
else:
returnLines += "%s - Ticker could not be found" % stock
returnLines += "\n"
return returnLines
def stockInfoBasicPretty(stockList, maxLength = MAX_TEXT_FLASK_LENGTH):
parameters = { 'a': 'Ask', 'c1' : 'Change', 'n' : 'Name', 'p2': 'Percent Change'}
stockInfo = stockInfoFromTickers(stockList, parameters)
validStocks = set(getValidStocks(stockList))
returnLines = [""]
currentIndex = 0
for stock in validStocks:
newText = "\n%s - %s \n" % (stockInfo[stock]['Name'], stock.upper())
# Ask / ^ price/percent
arrow = ('+' if ('+' in stockInfo[stock]['Change']) else '-')
newText += "$%.2f | %s%.2f/%.2f%%\n" % (float(stockInfo[stock]['Ask'])
, arrow \
, float(stockInfo[stock]['Change'].translate(None, "+-")) \
, float(stockInfo[stock]['Percent Change'].translate(None, "+%-")))
# Determine if we want this stock in the current message, or the message afterwards
if len(newText + returnLines[currentIndex]) >= maxLength:
returnLines.append(newText)
currentIndex += 1
else:
returnLines[currentIndex] += newText
invalidStocks = list(set(stockList) - validStocks)
print invalidStocks
if invalidStocks:
returnLines.append("The following tickers could not be found: %s" % (" ".join(invalidStocks)) )
print returnLines
return returnLines
def stockInfoAllPretty(stockList, maxLength = MAX_TEXT_FLASK_LENGTH):
parameters= { 'a': 'Ask', 'c1' : 'Change', 'n' : 'Name', 'p2' : 'Percent Change', 'o':'Open', 'h' : 'High', 'g': 'Low', 'p' : 'Previous Close'}
stockInfo = stockInfoFromTickers(stockList, parameters)
validStocks = set(getValidStocks(stockList))
returnLines = [""]
currentIndex = 0
for stock in validStocks:
newText = "\n%s - %s \n" % (stockInfo[stock]['Name'], stock.upper())
# Ask / ^ price/percent
arrow = ('+' if ('+' in stockInfo[stock]['Change']) else '-' )
newText += "$%.2f | %s%.2f/%.2f%% \n" % (float(stockInfo[stock]['Ask'])
, arrow \
, float(stockInfo[stock]['Change'].translate(None, "+-")) \
, float(stockInfo[stock]['Percent Change'].translate(None, "+%-")))
newText += """\
Open: %(Open)s
High: %(High)s
Low: %(Low)s
Previous Close: %(Previous Close)s""" % stockInfo[stock]
# Determine if we want this stock in the current message, or the message afterwards
if len(newText + returnLines[currentIndex]) >= maxLength:
returnLines.append(newText)
currentIndex += 1
else:
returnLines[currentIndex] += newText
invalidStocks = list(set(stockList) - validStocks)
if invalidStocks:
returnLines.append("The following tickers could not be found: %s" % (" ".join(invalidStocks)) )
return returnLines
# Given a list of tickers, returns which are valid
def getValidStocks(stockList):
returnList = []
parameters = { 'n' : "Name" }
stockInfo = stockInfoFromTickers(stockList, parameters)
for stock in stockList:
if stockInfo[stock]['Name'] != 'N/A':
returnList.append(stock)
return returnList