Skip to content

Commit 8c1c8cd

Browse files
author
ravi
committed
added bit coin symbol
1 parent 6a09907 commit 8c1c8cd

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

forex_python/bitcoin.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import requests
2+
3+
4+
class BtcConverter(object):
5+
"""
6+
Get bit coin rates convert
7+
"""
8+
9+
def get_latest_price(self, currency):
10+
url = 'https://api.coindesk.com/v1/bpi/currentprice/{}.json'.format(currency)
11+
response = requests.get(url)
12+
if response.status_code == 200:
13+
data = response.json()
14+
price = data.get('bpi').get(currency, {}).get('rate_float', None)
15+
return price
16+
return None
17+
18+
def get_previous_price(self, currency, date_obj):
19+
start = date_obj.strftime('%Y-%m-%d')
20+
end = date_obj.strftime('%Y-%m-%d')
21+
url = (
22+
'https://api.coindesk.com/v1/bpi/historical/close.json'
23+
'?start={}&end={}&currency={}'.format(
24+
start, end, currency
25+
)
26+
)
27+
response = requests.get(url)
28+
if response.status_code == 200:
29+
data = response.json()
30+
price = data.get('bpi', {}).get(start, None)
31+
return price
32+
return None
33+
34+
def get_previous_price_list(self, currency, start_date, end_date):
35+
start = start_date.strftime('%Y-%m-%d')
36+
end = end_date.strftime('%Y-%m-%d')
37+
url = (
38+
'https://api.coindesk.com/v1/bpi/historical/close.json'
39+
'?start={}&end={}&currency={}'.format(
40+
start, end, currency
41+
)
42+
)
43+
response = requests.get(url)
44+
if response.status_code == 200:
45+
data = response.json()
46+
price_dict = data.get('bpi', {})
47+
return price_dict
48+
return {}
49+
50+
def convert_to_btc(self, amount, currency):
51+
url = 'https://api.coindesk.com/v1/bpi/currentprice/{}.json'.format(currency)
52+
response = requests.get(url)
53+
if response.status_code == 200:
54+
data = response.json()
55+
price = data.get('bpi').get(currency, {}).get('rate_float', None)
56+
if price:
57+
converted_btc = amount/price
58+
return converted_btc
59+
return None
60+
return None
61+
62+
def convert_btc_to_cur(self, coins, currency):
63+
url = 'https://api.coindesk.com/v1/bpi/currentprice/{}.json'.format(currency)
64+
response = requests.get(url)
65+
if response.status_code == 200:
66+
data = response.json()
67+
price = data.get('bpi').get(currency, {}).get('rate_float', None)
68+
if price:
69+
converted_amount = coins * price
70+
return converted_amount
71+
return None
72+
return None
73+
74+
def convert_to_btc_on(self, amount, currency, date_obj):
75+
start = date_obj.strftime('%Y-%m-%d')
76+
end = date_obj.strftime('%Y-%m-%d')
77+
url = (
78+
'https://api.coindesk.com/v1/bpi/historical/close.json'
79+
'?start={}&end={}&currency={}'.format(
80+
start, end, currency
81+
)
82+
)
83+
response = requests.get(url)
84+
if response.status_code == 200:
85+
data = response.json()
86+
price = data.get('bpi', {}).get(start, None)
87+
if price:
88+
converted_btc = amount/price
89+
return converted_btc
90+
return None
91+
return None
92+
93+
def convert_btc_to_cur_on(self, coins, currency, date_obj):
94+
start = date_obj.strftime('%Y-%m-%d')
95+
end = date_obj.strftime('%Y-%m-%d')
96+
url = (
97+
'https://api.coindesk.com/v1/bpi/historical/close.json'
98+
'?start={}&end={}&currency={}'.format(
99+
start, end, currency
100+
)
101+
)
102+
response = requests.get(url)
103+
if response.status_code == 200:
104+
data = response.json()
105+
price = data.get('bpi', {}).get(start, None)
106+
if price:
107+
converted_btc = coins*price
108+
return converted_btc
109+
return None
110+
return None
111+
112+
def get_symbol(self):
113+
return "\u0E3F"

0 commit comments

Comments
 (0)