[Python] List the lowest rental prices for each CP

avatar

Code

import enum
import requests
import pandas as pd

CP_MAP = {
    "regular": {
        "alpha": {"legendary": 3000, "epic": 600, "rare": 120, "common": 30},
        "beta": {"legendary": 1500, "epic": 300, "rare": 60, "common": 15},
        "dice": {"legendary": 1000, "epic": 200, "rare": 40, "common": 10},
        "promo": {"legendary": 3000, "epic": 600, "rare": 120, "common": 30},
        "untamed": {"legendary": 1000, "epic": 200, "rare": 40, "common": 10},
        "chaos": {"legendary": 500, "epic": 100, "rare": 20, "common": 5}
    },
    "gold": {
        "alpha": {"legendary": 150000, "epic": 30000, "rare": 6000, "common": 1500},
        "beta": {"legendary": 75000, "epic": 15000, "rare": 3000, "common": 750},
        "dice": {"legendary": 50000, "epic": 10000, "rare": 2000, "common": 500},
        "promo": {"legendary": 150000, "epic": 30000, "rare": 6000, "common": 1500},
        "untamed": {"legendary": 50000, "epic": 10000, "rare": 2000, "common": 500},
        "chaos": {"legendary": 12500, "epic": 2500, "rare": 500, "common": 125}
    }
}

class Rarity(enum.IntEnum):
    Common = 1
    Rare = 2
    Epic = 3
    Legendary = 4

class Edition(enum.IntEnum):
    Alpha = 0
    Beta = 1
    Promo = 2
    Reward = 3
    Untamed = 4
    Dice = 5

    def to_slug(self, card_id):
        slug = self.name.lower()
        if slug == "reward":
            if card_id <= 223:
                return "beta"
            elif 224 <= card_id and card_id <= 298:
                return "dice"
            elif 299 <= card_id and card_id <= 330:
                return "gradius"
            elif 331 <= card_id and card_id <= 351:
                return "chaos"
        else:
            return slug

def get_data():
    market = requests.get(f"https://api2.splinterlands.com/market/for_rent_grouped").json()
    card_details = requests.get("https://api.splinterlands.io/cards/get_details").json()
    result = {}
    for card in market:
        gold_slug = "gold" if card["gold"] else "regular"
        edition_slug = Edition(card["edition"]).to_slug(card["card_detail_id"])
        rarity_id = [card_detail["rarity"] for card_detail in card_details if card_detail["id"] == card["card_detail_id"]][0]
        rarity_slug = Rarity(rarity_id).name.lower()
        card["cp"] = CP_MAP[gold_slug][edition_slug][rarity_slug]
        card["cp_per_dec"] = round(float(card["cp"]) / float(card["low_price"]), 3)
        if card["cp"] in result and float(card["low_price"]) < float(result[card["cp"]]["low_price"]):
            result[card["cp"]] = card
        else:
            result[card["cp"]] = card
    return result

data = get_data()
df = pd.DataFrame(data.values())
df = df.set_index("cp")
df = df.sort_index()
df[15:][["card_detail_id", "gold", "edition", "low_price", "cp_per_dec"]]

Result

Screenshot

You can run the code in Google Colab.

https://colab.research.google.com/drive/16tsx765O_Zl9txHNlA8Qe1bY5QMnUIzg?usp=sharing



0
0
0.000
1 comments
avatar

Congratulations @cs50x! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 10 upvotes.
Your next target is to reach 50 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 the last post from @hivebuzz:

HiveFest⁶ Meetings Contest

0
0
0.000