Last active
April 14, 2016 16:22
-
-
Save osullivj/deda7408eecdd8e1cc5be7c3c5fd6e2a to your computer and use it in GitHub Desktop.
Tornado based Tiingo proxy for Excel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A 'bridge' HTTP server that translates Excel web invocations to tiingo API calls | |
# Requirements: Python 3.4, Tornado 4.3 | |
# JOS 2016-02-16 | |
import socket | |
import logging | |
import os | |
import tornado.ioloop | |
import tornado.web | |
from tornado.httpclient import AsyncHTTPClient, HTTPRequest | |
# Some constants - hack these to change the port, or the home page | |
PORT=80 | |
HOST=socket.gethostname( ) | |
HomePage="""<html><body><p>Excel Tiingo</p><p>http://%s:%d</p></body></html>""" % ( HOST, PORT) | |
TiingoHTTPHeaders = { | |
"Content-Type" : "application/json", | |
"Authorization" : "Token 48777192920ae042c07a1ba8cbf5b8bfd0c17c64" | |
} | |
TiingoHTMLHeader = '<html><body><table>' | |
TiingoHTMLFooter = '</table></body></html>' | |
TiingoKeys = ['date','open','high','low','close','volume','adjOpen','adjHigh','adjLow','adjClose','adjVolume'] | |
TiingoTableHeaderRow = '<tr>%s</tr>' % ''.join( ['<td>%s</td>' % k for k in TiingoKeys]) | |
class RootHandler( tornado.web.RequestHandler): | |
def get( self): | |
logging.info( "RootHandler.get: uri(%s) body(%s)" % ( self.request.uri, self.request.body)) | |
self.write( HomePage) | |
class TiingoHandler( tornado.web.RequestHandler): | |
# flag this method as async to stop tornado auto invoking self.finish( ) | |
@tornado.web.asynchronous | |
def get( self): | |
logging.info( "TiingoHandler.get: uri(%s) body(%s)" % ( self.request.uri, self.request.body)) | |
client = AsyncHTTPClient( ) | |
url = 'https://api.tiingo.com%s' % self.request.uri | |
request = HTTPRequest( url, method='GET', headers=TiingoHTTPHeaders) | |
logging.info( "TiingoHandler.get: uri(%s) url(%s)" % ( self.request.uri, url)) | |
def handle_response( response): | |
logging.info( "TiingoHandler.get.handle_response: response.body(%s)" % response.body) | |
rlist = eval( response.body) | |
self.write( TiingoHTMLHeader) | |
self.write( TiingoTableHeaderRow) | |
for r in rlist: | |
self.write( '<tr>') | |
for k in TiingoKeys: | |
self.write( '<td>%s</td>' % r.get( k, '')) | |
self.write( '</tr>') | |
self.write( TiingoHTMLFooter) | |
self.finish( ) | |
client.fetch( request, callback=handle_response) | |
def make_app( ): | |
return tornado.web.Application([ | |
(r'/tiingo/.*', TiingoHandler), | |
(r'/.*', RootHandler), | |
]) | |
if __name__ == "__main__": | |
# tornado logs to stdout by default - we want it in a file in the %TEMP% dir | |
logf = '%s\\tiingo_%d.log' % ( os.environ.get('TEMP'), os.getpid( )) | |
logfmt = '%(asctime)s %(levelname)s %(thread)s %(message)s' | |
logging.basicConfig( filename=logf, format=logfmt, level=logging.INFO) | |
app = make_app( ) | |
app.listen( PORT) | |
tornado.ioloop.IOLoop.current( ).start( ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment