If you don't know how to rent a card through API, please go to my last blog
@wohefengyiyang/splinterlands-apibot-learning-2-how-to-write-a-program-to-realize-automatic-card-rental-by-using-python
In my last blog, I explained how to rent a card. So how to rent multi cards?
1. The easiest way is to rent a card many times
cards_sorted_one = get_rent_cards_xp(335, 3, False, 1, 3)
for card_sorted in cards_sorted:
if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
rent_card(username, [card_sorted.market_id], 1, 'DEC')
break
cards_sorted_two = get_rent_cards_xp(339, 3, False, 1, 3)
for card_sorted in cards_sorted_two:
if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
rent_card(username, [card_sorted.market_id], 1, 'DEC')
break
cards_sorted_three = get_rent_cards_xp(333, 3, False, 1, 3)
for card_sorted in cards_sorted_three:
if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
rent_card(username, [card_sorted.market_id], 1, 'DEC')
break
2. Using the for loop
HaHaHa, the way of 1 seems too stupid, so use the loop
# Define a Class an array to save your rental card settings
class RentalSetting:
card_id = ''
edition = 3
gold = False
xp = 1
price = 0
def __init__(self, c, e, g, x, p):
self.card_id = c
self.edition = e
self.gold = g
self.xp = x
self.price = p
rental_setting = [RentalSetting(333,3,False,1,1),RentalSetting(335,3,True,1,1),RentalSetting(339,3,False,1,1)]
for r_setting in rental_setting:
cards_sorted = get_rent_cards_xp(r_setting.card_id, r_setting.edition, r_setting.gold, r_setting.xp, r_setting.price)
for card_sorted in cards_sorted:
if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
rent_card(username, [card_sorted.market_id], 1, 'DEC')
break
3. Rent multiple identical cards
What should we do iff we want to rent multiple identical cards?
# Define a Class an array to save your rental card settings and amounts
class RentalSetting:
card_id = ''
edition = 3
gold = False
xp = 1
price = 0
amount = 1
def __init__(self, c, e, g, x, p, a):
self.card_id = c
self.edition = e
self.gold = g
self.xp = x
self.price = p
self.amount = a
rental_setting = [RentalSetting(333,3,False,1,1,3),RentalSetting(335,3,True,1,1,4),RentalSetting(339,3,False,1,1,5)]
for r_setting in rental_setting:
cards_sorted = get_rent_cards_xp(r_setting.card_id, r_setting.edition, r_setting.gold, r_setting.xp, r_setting.price)
cards_amount = []
amount = r_setting.amount
index = 0
for card_sorted in cards_sorted:
index = index + 1
if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
cards_amount.append(card_sorted.market_id)
amount = amount - 1
if amount == 0 or index ==len(cards_sorted):
if len(cards_amount) > 0:
rent_card(username, cards_amount, 1, 'DEC')
break
4. All codes: you can copy and test it
import requests
from beem import Hive
API2 = "https://api2.splinterlands.com"
# 👇👇👇👇👇👇👇👇👇👇👇 your username
username = 'username'
# 👇👇👇👇👇👇👇👇👇👇👇 your posting keyword and active keyword
passwords = ['aaa', 'bbb']
hive = Hive(keys=passwords)
class Card:
market_id = ''
uid = ''
detail_id = 0
price = 0
def __init__(self, m, u, d, p):
self.market_id = m
self.uid = u
self.detail_id = d
self.price = p
def __lt__(self, other):
return self.price < other.price
# Define a Class an array to save your rental card settings and amounts
class RentalSetting:
card_id = ''
edition = 3
gold = False
xp = 1
price = 0
amount = 1
def __init__(self, c, e, g, x, p, a):
self.card_id = c
self.edition = e
self.gold = g
self.xp = x
self.price = p
self.amount = a
def get_rent_cards_xp(card_id: int, edition: int, gold: bool, xp: int, price: float) -> list:
url = API2 + "/market/for_rent_by_card"
request: dict = {"card_detail_id": card_id,
"gold": gold,
"edition": edition}
rent_cards = requests.get(url, params=request).json()
cards = [card for card in rent_cards if card.get("xp") >= xp and float(card.get("buy_price")) <= price]
v_cards = []
for c in cards:
v_cards.append(Card(c.get('market_id'), c.get('uid'), c.get('card_detail_id'), float(c.get('buy_price'))))
v_cards.sort()
return v_cards
def verify(market_id: str, uid: str, card_detail_id: int) -> bool:
url = API2 + "/market/validateListing"
request: dict = {"card_detail_id": card_detail_id,
"uid": uid,
"market_id": market_id}
return requests.get(url, params=request).json().get('isValid')
def rent_card(player: str, card_ids: list[str], days: int, currency: str):
data: dict = {"items": card_ids,
"currency": currency,
"days": days,
"app": "splinterlands/0.7.139"}
hive.custom_json("sm_market_rent", data, required_auths=[player], required_posting_auths=[])
print(player, 'rent cards success')
rental_setting = [RentalSetting(333, 3, False, 1, 1, 3), RentalSetting(335, 3, True, 1, 1, 4),
RentalSetting(339, 3, False, 1, 1, 5)]
for r_setting in rental_setting:
cards_sorted = get_rent_cards_xp(r_setting.card_id, r_setting.edition, r_setting.gold, r_setting.xp,
r_setting.price)
cards_amount = []
amount = r_setting.amount
index = 0
for card_sorted in cards_sorted:
index = index + 1
if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
cards_amount.append(card_sorted.market_id)
amount = amount - 1
if amount == 0 or index == len(cards_sorted):
if len(cards_amount) > 0:
rent_card(username, cards_amount, 1, 'DEC')
print(username, 'rent cards', cards_amount)
break
Next period notice
Setting a power and BCX(power/dec), and automatic card rental