First commit to spltools, a python package for working with Splinterlands data.

Image created with Bing image generator

I have spent quite a bit of time working with Splintelands data over the last years. This has included mining battle data, analyzing card stats, guild data etc. Something I have long been thinking about is to compile these various scripts, functions and classes into a python package, in case someone else could have use for it.

I finally pulled the trigger and created a Github repository for it. I named it spltools, because I am not particularly creative with these things. Anyway, the link to the repo is:


https://github.com/Kalkulus2/spltools

For now, it only contains two things.

  • A function to download Splinterlands card information
  • A guild class

I plan to slowly add in new functionality to this package over time. Updates will likely be accompanied by a Hive post.

separator.png

Downloading card details: getCardData()

The main content of the package currently is the getCardData function, which downloads information about all Splinterlands cards from https://api2.splinterlands.com/cards/get_details. (Some browsers, like Firefox, allow you to view the data in that url conveniently in the browser).

image.png

The function is very simple. There is no input argument, and all it does is to ask splinterlands for the card information data, and then use the json library to build a python dictionary from the returned data. If the data request is successful, we get the card information in the carddata variable.

I then add an id filter with a list comprehension, to remove all the Soulkeep cards from the data. The Soulkeep cards have ids larger than 10000, while Splinterlands cards currently go up to 706.


The data returned from this api endpoint is a list of cards, where each card is a dictionary with a number of data fields. For example, the first card in the list is the Goblin Shaman, and the card info looks like this ---->

 

Here we have the name, id, color etc. The stats field is another dictionary with keys "attack", "ranged", etc, which are lists containing the card stats at each level.

 

We also get information about the card distribution. Goblin Shaman has 185254 total printed.

separator.png

Example usage

I have a goal with this package to provide examples for most of the functions and classes it contains. To start with, I have made this tiny program to call the getCardData function. This script, printAllNamesAndIds.py, can be found in the examples folder:


image.png

This program imports the getCardData function, calls it, and then loops over the cards and prints their names and ids. I am a big fan of the fstring functionality in Python, which allows me to easily insert the names and ids into the output string . The output looks like this:


image.png

separator.png

Final words

If you have not yet joined Splinterlands please click the referral link below to get started.


Join Splinterlands

Best wishes
@Kalkulus

separator.png



0
0
0.000
9 comments
avatar

Hi @kalkulus,

Nice tool also a big fan of python 😍.

question why do you use the
with urllib.request.urlopen("https://api2.splinterlands.com/cards/get_details") as request:

I like to use this strucure:

base_url = 'https://api2.splinterlands.com/'
land_url = 'https://vapi.splinterlands.com/'
prices_url = 'https://prices.splinterlands.com/'

retry_strategy = LogRetry(
    total=10,
    status_forcelist=[429, 500, 502, 503, 504],
    backoff_factor=2,  # wait will be [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
    allowed_methods=['HEAD', 'GET', 'OPTIONS']
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount('https://', adapter)


def get_card_details():
    address = base_url + 'cards/get_details'
    return pd.DataFrame(http.get(address).json()).set_index('id')

Not sure why one is better than the other.

Some feedback:
For Python, the PEP 8 style guide recommends using snake_case for function and variable names.

I would recommend using a base_url to abstract the url.

Might consider a Retry mechanism when users are overloading the spl API. Not sure if it needs in this code or the caller?

Keep up the great work 💪

Here is my spl api i use for my statistics tool.
https://github.com/gamerbeaker007/splinterlands-statistics/blob/main/src/api/spl.py

0
0
0.000
avatar
(Edited)

Awesome, thanks for the feedback.

I was not aware that snake_case was a PEP. I have just at some point gotten used to CamelCase or mixedCase so those are my defaults. I will adapt to snake_case for Python then.

The HTTPAdapter setup is appropriate when you call the API repeatedly right? I've not tried using that class but it looks simple enough. When I get to calls like that I might just steal your code ;)

0
0
0.000
avatar

Especially when you get to code to retrieve all balances for example.

This needs more than one call to the spl api at this point you need to address a back-off/retry strategy. Also if for some reason the spl api gives a other return then successful like 502.

No problem with using my code as inspiration or copy/paste 😁, I'm also not official developer just learning as wel.

I also like camelcase more for my java programs 🤣.

This is what chatgpt think PEP8 should look like for class functions and variables.

class MyClassName:
    """A simple example class following PEP 8 style."""

    def __init__(self, first_value, second_value):
        """Initialize the class with two variables."""
        self.first_value = first_value
        self.second_value = second_value

    def calculate_sum(self):
        """Calculate the sum of the two values."""
        return self.first_value + self.second_value

# Usage
my_object = MyClassName(first_value=10, second_value=20)
result = my_object.calculate_sum()

print(f"The sum is: {result}")

But using classes and going object oriented (OOP) with python is another great topic on what todo with it😁.

0
0
0.000
avatar

Congratulations @kalkulus! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You distributed more than 21000 upvotes.
Your next target is to reach 22000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

Our Hive Power Delegations to the September PUM Winners
Feedback from the October Hive Power Up Day
Hive Power Up Month Challenge - September 2024 Winners List
0
0
0.000
avatar

👍

!BEER
!BBH
!WINE

0
0
0.000
avatar

@kalkulus! @eii likes your content! so I just sent 1 BBH to your account on behalf of @eii. (2/5)

(html comment removed: )

0
0
0.000