-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitcoin-track.py
44 lines (32 loc) · 1.17 KB
/
bitcoin-track.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
from requests import Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
# The url to the API for the data request
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
# What is needed
parameters = {
'start':'1', # CMC Rank/ Crypto Choice
'limit':'1', # Limit the data requested
'convert':'ZAR'
}
# API validation so I can get access
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': 'API_KEY_GOES_HERE', # Get your api key from here https://coinmarketcap.com/api/
}
# Create a session
session = Session()
# Update the header by using the session
session.headers.update(headers)
try:
# variable = get the url and parameters from the session
response = session.get(url, params=parameters)
# variable = loads the response as text from the json
data = json.loads(response.text)
# variable = list[main column][index of subcolumns][other column]
name = data['data'][0]['name']
quote = data['data'][0]['quote']['ZAR']['price']
print("Name: " + str(name))
print("Price: R" + str(quote))
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)