feat: cryptograph
This commit is contained in:
parent
0f6f9b7dec
commit
401cae43da
7
crypto
7
crypto
@ -6,7 +6,7 @@ import http.client as http
|
||||
|
||||
# curl https://api.coingecko.com/api/v3/coins/list
|
||||
COINS = [
|
||||
# [id, symbol]
|
||||
# [id, symbol]
|
||||
["bitcoin", "BTC"],
|
||||
]
|
||||
CURRENCY = "usd"
|
||||
@ -14,15 +14,14 @@ FORMAT = "{coin}=${price:.0f}"
|
||||
|
||||
|
||||
def main():
|
||||
coin_ids = ",".join([coin for coin in list(zip(*COINS))[0]])
|
||||
coin_ids = ",".join([coin[0] for coin in COINS])
|
||||
url = "api.coingecko.com"
|
||||
path = f"/api/v3/simple/price?ids={coin_ids}&vs_currencies={CURRENCY}"
|
||||
|
||||
# {'bitcoin': {'usd': 69420}}
|
||||
|
||||
client = http.HTTPSConnection(url)
|
||||
client.request("GET", path)
|
||||
|
||||
# {'bitcoin': {'usd': 69420}}
|
||||
prices = json.loads(client.getresponse().read())
|
||||
|
||||
print(
|
||||
|
49
cryptograph
Executable file
49
cryptograph
Executable file
@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import http.client as http
|
||||
import json
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from matplotlib.dates import DateFormatter
|
||||
|
||||
# 1 = */5 * * * *
|
||||
# < 90 = 0 * * * *
|
||||
# > 90 = 0 0 * * *
|
||||
DAYS = 30
|
||||
COIN = "bitcoin"
|
||||
CURRENCY = "usd"
|
||||
|
||||
|
||||
def create_graph(dates: list[datetime], values: list[float]):
|
||||
_, ax = plt.subplots()
|
||||
|
||||
plt.rcParams.update({"font.weight": "bold"})
|
||||
ax.xaxis.set_major_formatter(DateFormatter("%m-%d")) # pyright: ignore
|
||||
ax.plot(dates, values, linewidth=4.0, color="red") # pyright: ignore
|
||||
plt.tight_layout()
|
||||
plt.savefig("foo.png", transparent=True)
|
||||
|
||||
|
||||
def get_time_range(days: int) -> list[datetime]:
|
||||
now = datetime.now()
|
||||
time_change = timedelta(hours=1)
|
||||
|
||||
return [now + time_change * hour for hour in range(-days * 24, 1)]
|
||||
|
||||
|
||||
def main():
|
||||
url = "api.coingecko.com"
|
||||
path = f"/api/v3/coins/{COIN}/market_chart?vs_currency={CURRENCY}&days={DAYS}&precision=2"
|
||||
|
||||
client = http.HTTPSConnection(url)
|
||||
client.request("GET", path)
|
||||
|
||||
# dict("prices", "market_caps", "total_volumes")
|
||||
market_chart = json.loads(client.getresponse().read())
|
||||
|
||||
create_graph(get_time_range(DAYS), list(zip(*market_chart["prices"]))[1])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user