-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoingecko.py
88 lines (75 loc) · 2.64 KB
/
coingecko.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
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
# An implementation of the CoinGecko API that aims to be simpler and easier to understand
# By Henry Chedd and Amy(@00p513-dev)
# Licensed under the Don't Be A Dick Public License
import json
import requests
#######################################################
# Check if you can connect to CoinGecko API #
# Returns an array with connection result in index 0, #
# and api response in index 1 #
#######################################################
def connectionTest():
connected = False
while not connected:
r = requests.get('https://api.coingecko.com/api/v3/ping/')
if r.status_code == 200:
data = r.text
jdata = json.loads(data)
connected = True
return [True, jdata['gecko_says']]
else:
try:
data = r.text
jdata = json.loads(data)
return [False, jdata]
except:
return [False, 'Failed to recieve API data']
#######################################################
# Get price for one unit of crypto in a fiat currency #
#######################################################
def getCoinValue(crypto, fiat):
url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=" + fiat + "&ids=" + crypto
s = requests.get(url)
if s.status_code == 200:
data = s.text
jdata = json.loads(data)
return jdata[0]['current_price']
########################################
# Covvert fiat in to a crypto currency #
########################################
def convertToCrypto(crypto, fiat, amount):
url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=" + fiat + "&ids=" + crypto
s = requests.get(url)
if s.status_code == 200:
data = s.text
jdata = json.loads(data)
conversionRate = jdata[0]['current_price']
price = amount*conversionRate
return price
########################################
# Covvert crypto in to a fiat currency #
########################################
def convertToFiat(crypto, fiat, amount):
url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=" + fiat + "&ids=" + crypto
s = requests.get(url)
if s.status_code == 200:
data = s.text
jdata = json.loads(data)
conversionRate = jdata[0]['current_price']
price = amount/conversionRate
return price
######################################
# List of supported currencies #
# Pretty version prints on new lines #
######################################
def supportedVsCurrencies():
url = "https://api.coingecko.com/api/v3/simple/supported_vs_currencies"
s = requests.get(url)
if s.status_code == 200:
data = s.text
jdata = json.loads(data)
return jdata
def supportedVsCurrenciesPretty():
supported = supportedVsCurrencies()
supported = ', '.join(supported)
return supported