Skip to content

Commit

Permalink
fail elegantly when stock symbol not found
Browse files Browse the repository at this point in the history
  • Loading branch information
nmelo committed Apr 17, 2015
1 parent e98de78 commit fe3f438
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 19 deletions.
60 changes: 60 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Created by .ignore support plugin (hsz.mobi)
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/


2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 59 additions & 13 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 49 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,79 @@
app = Flask(__name__)


CONTENT_TYPE = {'Content-Type': 'application/json;charset=UTF-8'}

def generate_response(output_speech, card_title="", card_subtitle="", card_content="", session_attributes={}, should_end_session=True):
response = {
"version": "1.0",
"sessionAttributes": {
"user": {
"name": "nelson"
}
},
"response": {
"outputSpeech": {
"type": "PlainText",
"text": output_speech
},
"card": {
"type": "Simple",
"title": card_title,
"subtitle": card_subtitle,
"content": card_content
},
"shouldEndSession": should_end_session
}
}
return json.dumps(response)


@app.route('/', methods=['GET', 'POST'])
def post():
logging.info(json.dumps(request.json, indent=4, sort_keys=False))

response = ""

stock_name = request.json["request"]["intent"]["slots"]["Stock"]["value"]

logging.info("Stock name: %s" % stock_name)

# Query API to convert name to Symbol
name_dict = requests.get('http://dev.markitondemand.com/Api/v2/Lookup/json?input={}'.format(stock_name)).json()
symbol = name_dict[0]["Symbol"]
company_name = name_dict[0]["Name"]

try:
symbol = name_dict[0]["Symbol"]
company_name = name_dict[0]["Name"]
except IndexError:
response = generate_response("Symbol not found for %s." % stock_name)
return response, 200, CONTENT_TYPE

logging.info("Symbol: %s" % symbol)
logging.info("Name: %s" % company_name)

# Query API to get quote for symbol
quote_dict = requests.get('http://dev.markitondemand.com/Api/v2/Quote/json?symbol={}'.format(symbol)).json()
last_price = quote_dict["LastPrice"]
try:
last_price = quote_dict["LastPrice"]
except IndexError:
response = generate_response("Price not found for %s." % stock_name)
return response, 200, CONTENT_TYPE

logging.info("Last Price: %s" % last_price)

# If it's after 4:00pm say closed, otherwise say current price
# Subscribe to a notification when the price of the stock reaches something

response = '{"version":"1.0","sessionAttributes":{"user":{"name":"nelson"}},"response":{"outputSpeech":{"type":"PlainText","text":"Today, %s closed at %s dollars."},"card":{"type":"Simple","title":"%s","subtitle":"%s closed at %s dollars.","content":""},"shouldEndSession":true}}' % (company_name, last_price, symbol, company_name, last_price)
closing_text = "%s closed at %s dollars." % (company_name, last_price)

response = generate_response(
output_speech=closing_text,
card_title=symbol,
card_subtitle=closing_text,
card_content="")

logging.info(json.dumps(json.loads(response), indent=4, sort_keys=False))
return response, 200, {'Content-Type': 'application/json;charset=UTF-8'}
return response, 200, CONTENT_TYPE


if __name__ == '__main__':
Expand Down
Binary file modified main.pyc
Binary file not shown.

0 comments on commit fe3f438

Please sign in to comment.