-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
50 lines (45 loc) · 1.59 KB
/
app.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
from chalice import Chalice
import requests
app = Chalice(app_name='stock')
@app.route('/stock/{ticker}')
def stock(ticker):
try:
stock_data = get_stock_data(ticker)
result_text = stock_data['ticker'] + ': ' + stock_data['price']
except Exception as e:
print e
result_text = "Please enter in a valid ticker."
return result_text
# Request from slack app
@app.route('/stock')
def slack_stock():
try:
stock_data = get_stock_data(app.current_request.query_params['text'])
result_text = '''*{ticker}*
Price: {price}
Amt Change: {amt_change}
% Change: {perc_change}
Day High: {day_high}
Day Low: {day_low}'''.format(ticker=stock_data['ticker'],
price=stock_data['price'],
amt_change=stock_data['amt_change'],
perc_change=stock_data['perc_change'],
day_high=stock_data['day_high'],
day_low=stock_data['day_low']
)
except Exception as e:
print e
result_text = "Please enter in a valid ticker. ie /stock xyz"
return result_text
# Get price, amount change, percent change, day high, day low
def get_stock_data(ticker):
url = "http://download.finance.yahoo.com/d/quotes.csv?s=" + ticker + "&f=l1c1p2hg"
values = requests.get(url).text.split(',')
return {
'ticker': ticker.upper(),
'price': values[0],
'amt_change': values[1],
'perc_change': values[2].replace('"',''),
'day_high': values[3],
'day_low': values[4].replace('\n','')
}