forked from kennethreitz/coinbin.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
140 lines (102 loc) · 3.48 KB
/
server.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
from scraper import get_coins, get_coin, Coin, convert_to_decimal
from predictions import get_predictions
from graph import schema
from flask import Flask, jsonify, render_template, request, send_file
from flask_graphql import GraphQLView
from flask_cache import Cache
from flask_common import Common
from flask_sslify import SSLify
import crayons
import maya
import requests
import records
API_KEYS = os.environ.get('API_KEYS', '').split(':')
db = records.Database()
pro_db = records.Database(os.environ['HEROKU_POSTGRESQL_TEAL_URL'])
app = Flask(__name__)
app.debug = 'DEBUG' in os.environ
common = Common(app)
sslify = SSLify(app)
@app.route('/')
@common.cache.cached(timeout=60)
def hello():
lbc = get_coin('lbc')
lbc_42 = get_value_int('lbc', 42.01)
lbc_sc = get_exchange('lbc', 'sc')
lbc_42_sc = get_exchange_value('lbc', 'sc', 42.01)
lbc_forecast = get_forecast('lbc')
return render_template('index.html', lbc=lbc, lbc_42=lbc_42, lbc_sc=lbc_sc, lbc_42_sc=lbc_42_sc, coins=get_coins().values(), lbc_forecast=lbc_forecast)
@app.route('/coins')
def all_coins():
return jsonify(coins=get_coins())
@app.route('/<coin>')
def get_coin(coin):
c = Coin(coin.lower())
return jsonify(coin={
'name': c.name,
'ticker': c.ticker,
'rank': c.rank,
'usd': c.usd,
'btc': c.btc
})
@app.route('/<coin>/forecast')
def get_forecast(coin):
return jsonify(forecast=get_predictions(coin.lower()))
@app.route('/<coin>/forecast/graph')
def get_forecast_graph(coin):
return get_predictions(coin.lower(), render=True)
# return send_file(f_name, mimetype='image/png')
@app.route('/<coin>/<float:n>')
def get_value(coin, n):
c = Coin(coin.lower())
return jsonify(coin={
'usd': convert_to_decimal(c.usd * n),
'exchange_rate': c.usd
})
@app.route('/<coin>/<int:n>')
def get_value_int(coin, n):
return get_value(coin, n)
@app.route('/<coin>/history')
def get_history(coin):
c = Coin(coin.lower())
q = "SELECT * from api_coin WHERE name=:coin ORDER BY date desc"
if request.args.get('key') in API_KEYS:
print(crayons.red('Pro request!'))
rows = pro_db.query(q, coin=c.name)
else:
rows = db.query(q, coin=c.name)
return jsonify(history=[
{
'value': r.value,
'value.currency': 'USD',
'timestamp': maya.MayaDT.from_datetime(r.date).subtract(hours=4).iso8601(),
'when': maya.MayaDT.from_datetime(r.date).subtract(hours=4).slang_time()
} for r in rows]
)
@app.route('/<coin1>/to/<coin2>')
def get_exchange(coin1, coin2):
c = Coin(coin1.lower())
return jsonify(coin={
# 'name': c.name,
# 'ticker': c.ticker,
'exchange_rate': c.value(coin2.lower()),
})
@app.route('/<coin1>/<float:n>/to/<coin2>/')
def get_exchange_value(coin1, coin2, n):
c = Coin(coin1.lower())
v = c.value(coin2.lower())
n = convert_to_decimal(n)
return jsonify(coin={
'value': convert_to_decimal(v * n),
'value.coin': coin2,
'exchange_rate': v
})
@app.route('/<coin1>/<int:n>/to/<coin2>/')
def get_exchange_value_int(coin1, coin2, n):
return get_exchange_value(coin1.lower(), coin2, n)
# GraphQL stuff.
app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=True))
# app.add_url_rule('/graphql/batch', view_func=GraphQLView.as_view('graphql', schema=schema, batch=True))
if __name__ == '__main__':
common.serve()