feat: cryptograph

This commit is contained in:
AngeD 2023-07-22 13:27:58 +02:00
parent 0f6f9b7dec
commit 401cae43da
2 changed files with 52 additions and 4 deletions

5
crypto
View File

@ -14,15 +14,14 @@ FORMAT = "{coin}=${price:.0f}"
def main(): 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" url = "api.coingecko.com"
path = f"/api/v3/simple/price?ids={coin_ids}&vs_currencies={CURRENCY}" path = f"/api/v3/simple/price?ids={coin_ids}&vs_currencies={CURRENCY}"
# {'bitcoin': {'usd': 69420}}
client = http.HTTPSConnection(url) client = http.HTTPSConnection(url)
client.request("GET", path) client.request("GET", path)
# {'bitcoin': {'usd': 69420}}
prices = json.loads(client.getresponse().read()) prices = json.loads(client.getresponse().read())
print( print(

49
cryptograph Executable file
View 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()