forked from hareeshnagaraj/stockmusic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
80 lines (63 loc) · 2.12 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
# app.py
# created by: Ryan Beatty and Hareesh Nagaraj
#
# main flask app for stockmusic web app.
# Stockmusic queries the various stock
# aggregating API's and converts that data
# into an arrangement of musical notes which
# produce MIDI sounds.
from flask import Flask
import flask
import requests
import sys
from flask import render_template
import getprices
import json
import datetime
app = Flask(__name__)
try:
# Attempt to read quandl api token
# or exit if file not found
with open('token.txt', 'r') as f:
TOKEN = f.read().strip()
QUERY_STRING = "https://www.quandl.com/api/v1/datasets/WIKI/{ticker}.json?" \
"sort_order=asc&trim_start={start_date}&trim_end={end_date}?" \
"auth_token=%(token)s" % {'token': TOKEN}
except IOError as e:
print "error: file 'token.txt' not found"
sys.exit(1)
"""
Sublclasses JSONEncoder in order to implement
date json serialization
"""
class DateTimeEncoder(json.JSONEncoder):
def default(self, obj):
"""
returns serialized string representation of obj
"""
# if obj is a date object, return the isoformat string
if isinstance(obj, datetime.date):
return obj.isoformat()
# else call regular JSONEncoder method to serialize obj
return json.JSONEncoder.default(self, obj)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/home")
def home():
return render_template('basic.html')
# query quandl api for stock market data and return results as json
@app.route("/query")
def get_data():
data = getprices.get_prices(['--ip', '10.8.8.1',
'-s', flask.request.args.get('ticker', '')])
return flask.Response(json.dumps(data, cls=DateTimeEncoder), mimetype="application/json")
# generate string used to query quandl api
def build_query_string(args):
return QUERY_STRING.format(ticker=args.get('ticker', ''),
start_date=args.get('start_date', ''),
end_date=args.get('end_date', ''))
if __name__ == '__main__':
app.debug = True
app.run()