Skip to content

Commit f981fe9

Browse files
author
Ravi kumar
committed
Merge pull request #15 from ravigadila/bitcoin
Bitcoin support
2 parents 6a09907 + 07289f5 commit f981fe9

3 files changed

Lines changed: 260 additions & 2 deletions

File tree

forex_python/bitcoin.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import requests
2+
3+
4+
class BtcConverter(object):
5+
"""
6+
Get bit coin rates and convertion
7+
"""
8+
9+
def get_latest_price(self, currency):
10+
"""
11+
Get Lates price of one bitcoin to valid Currency 1BTC => X USD
12+
"""
13+
url = 'https://api.coindesk.com/v1/bpi/currentprice/{}.json'.format(currency)
14+
response = requests.get(url)
15+
if response.status_code == 200:
16+
data = response.json()
17+
price = data.get('bpi').get(currency, {}).get('rate_float', None)
18+
return price
19+
return None
20+
21+
def get_previous_price(self, currency, date_obj):
22+
"""
23+
Get Price for one bit coin on given date
24+
"""
25+
start = date_obj.strftime('%Y-%m-%d')
26+
end = date_obj.strftime('%Y-%m-%d')
27+
url = (
28+
'https://api.coindesk.com/v1/bpi/historical/close.json'
29+
'?start={}&end={}&currency={}'.format(
30+
start, end, currency
31+
)
32+
)
33+
response = requests.get(url)
34+
if response.status_code == 200:
35+
data = response.json()
36+
price = data.get('bpi', {}).get(start, None)
37+
return price
38+
return None
39+
40+
def get_previous_price_list(self, currency, start_date, end_date):
41+
"""
42+
Get List of prices between two dates
43+
"""
44+
start = start_date.strftime('%Y-%m-%d')
45+
end = end_date.strftime('%Y-%m-%d')
46+
url = (
47+
'https://api.coindesk.com/v1/bpi/historical/close.json'
48+
'?start={}&end={}&currency={}'.format(
49+
start, end, currency
50+
)
51+
)
52+
response = requests.get(url)
53+
if response.status_code == 200:
54+
data = response.json()
55+
price_dict = data.get('bpi', {})
56+
return price_dict
57+
return {}
58+
59+
def convert_to_btc(self, amount, currency):
60+
"""
61+
Convert X amount to Bit Coins
62+
"""
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_btc = amount/price
70+
return converted_btc
71+
return None
72+
return None
73+
74+
def convert_btc_to_cur(self, coins, currency):
75+
"""
76+
Convert X bit coins to valid currency amount
77+
"""
78+
url = 'https://api.coindesk.com/v1/bpi/currentprice/{}.json'.format(currency)
79+
response = requests.get(url)
80+
if response.status_code == 200:
81+
data = response.json()
82+
price = data.get('bpi').get(currency, {}).get('rate_float', None)
83+
if price:
84+
converted_amount = coins * price
85+
return converted_amount
86+
return None
87+
return None
88+
89+
def convert_to_btc_on(self, amount, currency, date_obj):
90+
"""
91+
Convert X amount to BTC based on given date rate
92+
"""
93+
start = date_obj.strftime('%Y-%m-%d')
94+
end = date_obj.strftime('%Y-%m-%d')
95+
url = (
96+
'https://api.coindesk.com/v1/bpi/historical/close.json'
97+
'?start={}&end={}&currency={}'.format(
98+
start, end, currency
99+
)
100+
)
101+
response = requests.get(url)
102+
if response.status_code == 200:
103+
data = response.json()
104+
price = data.get('bpi', {}).get(start, None)
105+
if price:
106+
converted_btc = amount/price
107+
return converted_btc
108+
return None
109+
return None
110+
111+
def convert_btc_to_cur_on(self, coins, currency, date_obj):
112+
"""
113+
Convert X BTC to valid currency amount based on given date
114+
"""
115+
start = date_obj.strftime('%Y-%m-%d')
116+
end = date_obj.strftime('%Y-%m-%d')
117+
url = (
118+
'https://api.coindesk.com/v1/bpi/historical/close.json'
119+
'?start={}&end={}&currency={}'.format(
120+
start, end, currency
121+
)
122+
)
123+
response = requests.get(url)
124+
if response.status_code == 200:
125+
data = response.json()
126+
price = data.get('bpi', {}).get(start, None)
127+
if price:
128+
converted_btc = coins*price
129+
return converted_btc
130+
return None
131+
return None
132+
133+
def get_symbol(self):
134+
"""
135+
Here is Unicode symbol for bit coin
136+
"""
137+
return "\u0E3F"

tests/test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import datetime
22
from unittest import TestCase
3-
from forex_python.converter import CurrencyRates, CurrencyCodes
4-
from forex_python.converter import RatesNotAvailableError
3+
from forex_python.converter import CurrencyRates, CurrencyCodes, RatesNotAvailableError
54

65

76
class TestGetRates(TestCase):

tests/test_bitcoin.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import datetime
2+
from unittest import TestCase
3+
from forex_python.bitcoin import BtcConverter
4+
5+
6+
class TestCommon(TestCase):
7+
"""
8+
Common class with setUp method for all test cases
9+
"""
10+
def setUp(self):
11+
self.b = BtcConverter()
12+
13+
14+
class TestLatestPrice(TestCommon):
15+
"""
16+
Test get latest price using currency code
17+
"""
18+
def test_latest_price_valid_currency(self):
19+
price = self.b.get_latest_price('USD')
20+
self.assertEqual(type(price), float)
21+
22+
def test_latest_price_invalid_currency(self):
23+
price = self.b.get_latest_price('XYZ')
24+
self.assertFalse(price)
25+
26+
27+
class TestPreviousPrice(TestCommon):
28+
"""
29+
Test Price with date input
30+
"""
31+
def test_previous_price_valid_currency(self):
32+
date_obj = datetime.datetime.today() - datetime.timedelta(days=15)
33+
price = self.b.get_previous_price('USD', date_obj)
34+
self.assertEqual(type(price), float)
35+
36+
def test_previous_price_invalid_currency(self):
37+
date_obj = datetime.datetime.today() - datetime.timedelta(days=15)
38+
price = self.b.get_previous_price('XYZ', date_obj)
39+
self.assertFalse(price)
40+
41+
42+
class TestPreviousPriceList(TestCommon):
43+
"""
44+
Test previous price list for a currency
45+
"""
46+
def test_previous_price_list_with_valid_currency(self):
47+
start_date = datetime.datetime.today() - datetime.timedelta(days=15)
48+
end_date = datetime.datetime.today()
49+
price_list = self.b.get_previous_price_list('USD', start_date, end_date)
50+
self.assertTrue(price_list)
51+
self.assertEqual(type(price_list), dict)
52+
53+
def test_previous_price_list_with_invalid_currency(self):
54+
start_date = datetime.datetime.today() - datetime.timedelta(days=15)
55+
end_date = datetime.datetime.today()
56+
price_list = self.b.get_previous_price_list('XYZ', start_date, end_date)
57+
self.assertFalse(price_list)
58+
self.assertEqual(type(price_list), dict)
59+
60+
61+
class TestConvertBtc(TestCommon):
62+
"""
63+
Test Converting amount to Bit coins
64+
"""
65+
def test_convet_to_btc_with_valid_currency(self):
66+
coins = self.b.convert_to_btc(250, 'USD')
67+
self.assertEqual(type(coins), float)
68+
69+
def test_convet_to_btc_with_invalid_currency(self):
70+
coins = self.b.convert_to_btc(250, 'XYZ')
71+
self.assertFalse(coins)
72+
73+
74+
class TestConvertBtcToCur(TestCommon):
75+
"""
76+
Convert Bit Coins to Valid Currency amount
77+
"""
78+
def test_convert_btc_to_cur_valid_currency(self):
79+
amount = self.b.convert_btc_to_cur(2, 'USD')
80+
self.assertEqual(type(amount), float)
81+
82+
def test_convert_btc_to_cur_invalid_currency(self):
83+
amount = self.b.convert_btc_to_cur(2, 'XYZ')
84+
self.assertFalse(amount)
85+
86+
87+
class TestConvertToBtcOn(TestCommon):
88+
"""
89+
Convert To bit coin based on previous dates
90+
"""
91+
def test_convert_to_btc_on_with_valid_currency(self):
92+
date_obj = datetime.datetime.today() - datetime.timedelta(days=15)
93+
coins = self.b.convert_to_btc_on(300, 'USD', date_obj)
94+
self.assertEqual(type(coins), float)
95+
96+
def test_convert_to_btc_on_with_invalid_currency(self):
97+
date_obj = datetime.datetime.today() - datetime.timedelta(days=15)
98+
coins = self.b.convert_to_btc_on(300, 'XYZ', date_obj)
99+
self.assertFalse(coins)
100+
101+
102+
class TestConvertBtcToCurOn(TestCommon):
103+
"""
104+
Convert BitCoins to valid Currency
105+
"""
106+
def test_convert_to_btc_on_with_valid_currency(self):
107+
date_obj = datetime.datetime.today() - datetime.timedelta(days=15)
108+
amount = self.b.convert_btc_to_cur_on(3, 'USD', date_obj)
109+
self.assertEqual(type(amount), float)
110+
111+
def test_convert_to_btc_on_with_invalid_currency(self):
112+
date_obj = datetime.datetime.today() - datetime.timedelta(days=15)
113+
amount = self.b.convert_btc_to_cur_on(3, 'XYZ', date_obj)
114+
self.assertFalse(amount)
115+
116+
117+
class TestBitCoinSymbol(TestCommon):
118+
"""
119+
Bit Coin symbol
120+
"""
121+
def test_bitcoin_symbol(self):
122+
self.assertEqual(self.b.get_symbol(), "\u0E3F")

0 commit comments

Comments
 (0)