Python Libraries: Simple... Display your Splinterlands DEC

image.png

In an attempt to write something ‘useful’ without stealing it from the internet I set about trying out the Splinterlands API for the first time.

There are scripts on GitHub that do a lot more than what I show you here, but lifting them won't teach you Python.

You need to write them from nothing and without looking if you want it all to sink in.


image.png
...'the output after running this script, it displays the data I am looking for and nothing else'...

I have SEVEN Splinterlands accounts that are DEC collectors. You read that correctly, it’s unrelated to tax collecting and DEC is one of the native Splinterlands tokens.

I realise this is unusual but can imagine some players/investors have more than a single account. To check quickly on the status of my accounts, I wanted to have a desktop shortcut I could click to reveal my balances.

It couldn’t be that hard surely?

Using the Splinterlands API was merely a call using a variable to pipe the data into.

image.png

Nice and easy but the URL variable held a lot more than just my DEC totals and I needed to suffix the call with my account. That’s what the + is for.

As there are SEVEN of them, it made sense to use of a List data structure and further down the script use a For loop to cycle through them.

image.png

I noted the data being filtered into the 'url' variable appeared to be formatted but occasionally contained strange characters.

Being intent on mastering this myself, I tried several ways to separate the data into something more readable and eventually came to the conclusion it was json data.

image.png

This did the trick and now I could interrogate it correctly. To utilise the json.loads method, python requires a couple of libraries to be accessible.

image.png

Simply add these at the top of your script.

Now I had some readable data to work with, I needed another loop with an embedded If statement within it to get at the DEC data.

image.png

data[‘player’] and data [‘balance’] were what I needed to display in some readable form. The issue was that my account names within the data['player'] variable were all of different lengths.

Simply printing them to the console was displaying it but not in the neat straight lines I wanted.

Some padding was needed and this is where this kind of complex-looking code comes into play.

image.png

The spaces variable holds several spaces (CHR$ 32 for ASCII buffs), and that number is 20 minus the length of the Splinterlands account name.

It does look a little cryptic but can be figured out if you stare long enough at it while drinking lots of coffee and occasionally pounding your head against the nearest wall.

image.png

The final line is something I am still getting used to with Python. Adding 3 x {} separated by a comma, with the next 3 elements dictates what will be within them.

Note the middle one contains the variable 'spaces' which is simply padding so then when the script is run, it looks nice and ordered.

I do intend to create a GitHub repository for these scripts. As I still haven’t done this, please feel free to copy and paste the entire script if you find it useful.

Replace my accounts with yours unless you simply want to spy on my DEC totals. The script could easily be modified to display much more than DEC, and that one line pipes a lot of data into the 'url' variable.

RedLine.png

SplinterlandsDECBalances.py

import json, requests

splinterlands_accounts = ["account1", "account2", "account3"]

print("----------------------------------")
print("Account               DEC Total")
print("----------------------------------")

for account in splinterlands_accounts:

    url = "https://api2.splinterlands.com/players/balances?username=" + account
    spldata = json.loads(requests.get(url).text)

    for data in spldata:
        if data["token"] == "DEC":
            spaces = " " * (20-len(data['player']))
            print("{} {} {}".format(data['player'], spaces, data['balance']))

RedLine.png

  • Earn currency while you play brewing virtual beer with CryptoBrewMaster
  • Earn currency while you play and become a global Rock Star with Rising Star


CurieCurator.jpg

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center