Skip to content

Instantly share code, notes, and snippets.

@RobSpectre
Created June 24, 2012 19:44
Show Gist options
  • Save RobSpectre/2984649 to your computer and use it in GitHub Desktop.
Save RobSpectre/2984649 to your computer and use it in GitHub Desktop.
Get latest quote on the iShares Morningstar Small Value fund (JKL)
import os
import signal
from flask import Flask
from flask import request
import requests
import simplejson as json
from twilio import twiml
# Declare and configure application
app = Flask(__name__)
# Voice Request URL
@app.route('/voice', methods=['GET', 'POST'])
def voice():
response = twiml.Response()
quote = getJKLQuote()
response.say(quote)
return str(response)
# SMS Request URL
@app.route('/sms', methods=['GET', 'POST'])
def sms():
response = twiml.Response()
quote = getJKLQuote()
response.sms(quote)
return str(response)
def getJKLQuote():
response = requests.get(
"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'JKL'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback")
try:
data = json.loads(response.text)
data = data['query']['results']['quote']
except:
return "Could not retrieve stock data. Try again shortly."
return "JKL Last Price: %s Change Percent: %s Avg Daily Volume: %s" % \
(data['AskRealtime'], data['Change'], data['AverageDailyVolume'])
@app.errorhandler(500)
def server_error():
response = twiml.Response()
if request.form['CallSid']:
response.say("JKL is currently unavailable. Try again shortly.")
else:
response.sms("JKL is currently unavailable. Try again shortly.")
return str(response)
# Handles SIGTERM so that we don't get an error when Heroku wants or needs to
# restart the dyno
def graceful_shutdown(signum, frame):
exit()
signal.signal(signal.SIGTERM, graceful_shutdown)
# If PORT not specified by environment, assume development config.
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
if port == 5000:
app.debug = True
app.run(host='0.0.0.0', port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment