cs50x avatar

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

cs50x

Published: 09 Nov 2021 › Updated: 09 Nov 2021[Python] Code to calculate your total earned DEC per day

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

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

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

Written by

Read more #hive-13323 posts


Best Posts From cs50x

We have not curated any of cs50x's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From cs50x