-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
39 lines (33 loc) · 1.33 KB
/
main.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
import requests
# Create a CryptoData class to store the data and methods to get and print the data
class CryptoData:
def __init__(self, api_key, conversion_currency='USDT'):
self.api_key = api_key
self.base_url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
self.headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': self.api_key
}
self.params = {
'start': '1',
'limit': '3',
'convert': conversion_currency
}
# Get the data
def get_data(self):
response = requests.get(self.base_url, params=self.params, headers=self.headers)
response.raise_for_status() # Error Handling
return response.json()['data']
# Helper function to format numbers with commas
def intcomma(self, value):
return "{:,}".format(value)
# Print the data
def print_crypto_data(self):
data = self.get_data()
print("{:<10} {:<10} {:<20}".format("COIN", "PRICE", "MARKET CAP"))
for i in data:
print("{:<10} {:<10.2f} {:<20} USDT".format(i['symbol'], i['quote']['USDT']['price'], self.intcomma(i['quote']['USDT']['market_cap'])))
# Create a CryptoData object
crypto_data = CryptoData('YOUR_API_KEY')
# Print the data
crypto_data.print_crypto_data()