-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_data.py
67 lines (46 loc) · 2.17 KB
/
crypto_data.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
import requests
import json
import datetime
import matplotlib.pyplot as plt
from typing import List, Dict
def pprint(data_struct) -> None:
print(json.dumps(data_struct, indent=4))
def get_all_coins() -> List[Dict[str, str]]:
return json.loads(requests.get("https://api.coingecko.com/api/v3/coins/list").text)
def get_all_coin_names() -> List[str]:
coins = get_all_coins()
return [coin["name"] for coin in coins]
def get_supported_vs_currencies() -> List[str]:
return json.loads(requests.get("https://api.coingecko.com/api/v3/simple/supported_vs_currencies").text)
def get_coin_id(coin_name: str, available_coins: List[Dict[str, str]]) -> str:
return next(coin for coin in available_coins if coin["name"] == coin_name)["id"]
def get_coin_symbol(coin_name: str, available_coins: List[Dict[str, str]]) -> str:
return next(coin for coin in available_coins if coin["name"] == coin_name)["symbol"]
def plot_coin_chart(coin_id: str, vs_currency: str, num_of_days: str) -> None:
if vs_currency not in get_supported_vs_currencies():
with open("supported_vs_currencies.json", "w") as f:
f.write(json.dumps(get_supported_vs_currencies(), indent=4))
raise ValueError(f"error: {vs_currency} is not a supported vs currency.")
coin_data_url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/?vs_currency={vs_currency}&days={num_of_days}"
json_coin_data = requests.get(coin_data_url)
coin_data = json.loads(json_coin_data.text)
date_stamps = [
datetime.datetime.fromtimestamp(stamp[0] / 1000)
for stamp in coin_data["prices"]
]
coin_value = [stamp[1] for stamp in coin_data["prices"]]
plt.plot(date_stamps, coin_value)
plt.title(f"{coin_id} vs {vs_currency}")
plt.xlabel(f"Last {num_of_days} Days")
plt.ylabel(f"{vs_currency}")
plt.xlim([min(date_stamps), max(date_stamps)])
plt.ylim([min(coin_value), max(coin_value)])
plt.show()
def main() -> None:
coins = get_all_coins()
coin_name = "Bitcoin"
vs_currency_id = "usd"
num_of_days = 10
plot_coin_chart(get_coin_id(coin_name, coins), vs_currency_id, num_of_days)
if __name__ == "__main__":
main()