-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBitcoinAPI.py
More file actions
136 lines (123 loc) · 5.76 KB
/
BitcoinAPI.py
File metadata and controls
136 lines (123 loc) · 5.76 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
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
from typing import Tuple
import requests
import json
from chart import chart
import discord
TIMEOUT = 10
coincap_rates = "https://api.coincap.io/v2/rates/"
coincap_btc = "https://api.coincap.io/v2/assets/bitcoin"
def get_current_price() -> tuple[None, str] | tuple[float, None]:
"""
Retrieve the current price of Bitcoin from the CoinCap API.
This function sends a GET request to the CoinCap API to fetch the current
price of Bitcoin. If the request is successful, it parses the response to
extract the price in USD. In case of any exceptions during the request, it
captures the exception and returns an error message.
Returns:
tuple[float, None]: On success, the price of Bitcoin in USD and None for error.
tuple[None, str]: On failure, None for the price and an error message.
"""
error = None
try:
response = requests.get(coincap_btc, timeout=TIMEOUT)
response.raise_for_status()
bitcoin_price = float(response.json()["data"]["priceUsd"])
except requests.RequestException as e:
error = f"Failed to fetch price with error: {e}"
return None, error
return bitcoin_price, error
def get_currency_rates(currency: str) -> tuple[None, None, str] | tuple[str, str, str]:
"""
Retrieve the exchange rate for a specified currency from the CoinCap API.
This function fetches the exchange rates from the CoinCap API and searches
for the specified currency in the response data. If the currency is found,
it returns the currency symbol and its rate in USD. If the currency is not found
or if the request fails, an error message is returned.
Parameters:
currency (str): The symbol of the currency (e.g., 'EUR', 'GBP') for which
the exchange rate is requested.
Returns:
tuple[str, str, str]: On success, the currency symbol, the exchange rate in USD,
and None for the error.
tuple[None, None, str]: On failure, None for the currency symbol and rate,
and an error message.
"""
error = None
currency_symbol = None
rateUsd = None
currency = currency.upper()
try:
response = requests.get(coincap_rates, timeout=TIMEOUT)
response.raise_for_status()
rates = response.json()
for rate in rates["data"]:
if rate["symbol"] == currency and (rate["type"] == "fiat" or rate["symbol"] == "BTC"):
currency_symbol = rate["currencySymbol"]
rateUsd = rate["rateUsd"]
break
else:
error = f"Currency does not exist"
except requests.RequestException as e:
error = f"Failed to fetch rates with error: {e}"
return None, None, error
return currency_symbol, rateUsd, error
def get_current_price_in_currency(currency: str) -> tuple[None, None, str] | tuple[float, str, None]:
"""
Retrieve the current price of Bitcoin in a specified currency.
This function first gets the exchange rate for the given currency against USD
and the current price of Bitcoin in USD. It then calculates the price of Bitcoin
in the given currency.
Parameters:
currency (str): The symbol of the currency (e.g., 'EUR', 'GBP') in which to
convert the Bitcoin price.
Returns:
tuple[float, str, None]: On success, the price of Bitcoin in the specified
currency, the currency symbol, and None for the error.
tuple[None, None, str]: On failure, None for the price and currency symbol,
and an error message.
"""
currency_symbol, rateUsd, error = get_currency_rates(currency)
if error:
return None, None, error
price_in_usd, error = get_current_price()
if error:
return None, None, error
price_in_currency = float(price_in_usd) / float(rateUsd)
return price_in_currency, currency_symbol, None
def get_bitcoin_ath(currency) -> tuple[None, None, str] | tuple[str, float, None]:
"""
Retrieve the all-time high price of Bitcoin from the CoinGecko API.
This function sends a GET request to the CoinGecko API to fetch the all-time
high price of Bitcoin. If the request is successful, it parses the response to
extract the price in USD. In case of any exceptions during the request, it
captures the exception and returns an error message.
Returns:
tuple[float, None]: On success, the all-time high price of Bitcoin in USD and None for error.
tuple[None, str]: On failure, None for the price and an error message.
"""
error = None
try:
coingecko = f"https://api.coingecko.com/api/v3/coins/markets?vs_currency={currency}&ids=bitcoin&order=market_cap_desc&per_page=100&page=1&sparkline=false"
response = requests.get(coingecko, timeout=TIMEOUT)
response.raise_for_status()
bitcoin_ath_float = float(response.json()[0]["ath"])
currency_symbol, _, _ = get_currency_rates(currency)
bitcoin_ath = "{:,.2f}".format(bitcoin_ath_float)
if currency_symbol == None:
currency_symbol = "";
bitcoin_ath = f"{currency_symbol}{bitcoin_ath} {currency.upper()}"
except requests.RequestException as e:
error = f"Failed to fetch price with error"
return None, None, error
return bitcoin_ath, bitcoin_ath_float, error
def get_chart(name, timespan="10weeks"):
error = None
file = None
try:
response = requests.get('https://api.blockchain.info/charts/' + name + '?timespan=' + timespan+ "&format=json", timeout=TIMEOUT)
responseJson = json.loads(response.content)
file = discord.File(chart(responseJson), "chart.png")
except requests.RequestException as e:
error = f"Failed to fetch chart with error: {e}"
return None, error
return file, error