diff --git a/crypto b/crypto index 05a665d..96eb78b 100755 --- a/crypto +++ b/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( diff --git a/cryptograph b/cryptograph new file mode 100755 index 0000000..e34fa25 --- /dev/null +++ b/cryptograph @@ -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()