Skip to content

Instantly share code, notes, and snippets.

@sammachin
Created August 9, 2016 17:54
Show Gist options
  • Save sammachin/728e1fe3057fc4e86bfc41218d5b6301 to your computer and use it in GitHub Desktop.
Save sammachin/728e1fe3057fc4e86bfc41218d5b6301 to your computer and use it in GitHub Desktop.
Stock price to NCCO
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
import json
import requests
def getprice(sym):
payload = {'symbol': sym}
r = requests.get('http://dev.markitondemand.com/MODApis/Api/v2/Quote/json', params=payload)
data = r.json()
return data
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.write('VAPI Stock Price Demo')
self.set_header('Content-Type', 'text/plain')
self.finish()
def post(self):
self.get()
class CallHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self, slug):
data = getprice(slug)
text = "The last price for {} was {}".format(data['Name'], data['LastPrice'])
ncco=[]
a= {}
a['action'] = 'talk'
a['text'] = text
ncco.append(a)
self.content_type = 'application/json'
self.write(json.dumps(ncco))
self.set_header('Content-Type', 'application/json')
self.finish()
def post(self):
self.get()
def main():
static_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
print static_path
application = tornado.web.Application([(r"/", MainHandler),
(r"/call/(.*)", CallHandler),
(r'/s/(.*)', tornado.web.StaticFileHandler, {'path': static_path}),
])
application.debug = True
http_server = tornado.httpserver.HTTPServer(application)
port = int(os.environ.get("PORT", 5000))
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
tornado==3.1.1
wsgiref==0.1.2
requests==2.9.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment