[Python] Code to calculate your total earned DEC per day

Words
33
Reading
1 min
Listen
Play
5y

I wrote a Python code to calculate the total number of DEC won per day, the number of games won, and the average number of DEC won per game.

Code

import sys
from datetime import datetime
from pprint import pprint
import requests

username = sys.argv[-1]
url = "https://api2.splinterlands.com/players/balance_history"
params = f"?token_type=DEC&offset=0&limit=1000&username={username}"
rows = requests.get(url + params).json()
d = []

for row in rows:
    if row["type"] == "dec_reward":
        dt = datetime.fromisoformat(row["created_date"].split('T')[0])
        date_key = int(f"{dt.year}{str(dt.month).zfill(2)}{str(dt.day).zfill(2)}")
        r = [r for r in d if r["date"] == date_key]
        if r:
            r[0]["total"] = round(float(row["amount"]) + r[0]["total"], 3)
            r[0]["wins"] += 1
            r[0]["average"] = round(r[0]["total"] / r[0]["wins"], 3)
        else:
            d.append({
                "date": date_key,
                "total": round(float(row["amount"]), 3),
                "wins": 1
            })
pprint(d)

Result

Screenshot

[Python] Code to calculate your total earned DEC per day | Ecency