50 lines
1.2 KiB
Python
Executable File
50 lines
1.2 KiB
Python
Executable File
#!/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()
|