-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
31 lines (27 loc) · 1.04 KB
/
app.py
File metadata and controls
31 lines (27 loc) · 1.04 KB
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
from flask import Flask, request, jsonify
from pycoingecko import CoinGeckoAPI
app = Flask(__name__)
cg = CoinGeckoAPI()
@app.route('/current-price')
def get_current_price():
crypto_id = request.args.get('crypto_id', 'bitcoin')
currency = request.args.get('currency', 'usd')
try:
price_data = cg.get_price(ids=crypto_id, vs_currencies=currency)
price = price_data[crypto_id][currency]
return jsonify({'price': price})
except Exception as e:
return jsonify({'error': str(e)})
@app.route('/historical-price')
def get_historical_price():
crypto_id = request.args.get('crypto_id', 'bitcoin')
date = request.args.get('date', '01-01-2022')
currency = request.args.get('currency', 'usd')
try:
historical_data = cg.get_coin_history_by_id(id=crypto_id, date=date)
price = historical_data['market_data']['current_price'][currency]
return jsonify({'price': price})
except Exception as e:
return jsonify({'error': str(e)})
if __name__ == '__main__':
app.run(debug=True)