Skip to content

Commit

Permalink
Merge pull request #13 from Luois45/dev
Browse files Browse the repository at this point in the history
Added Discord webhook support and added logging of generalErrors when buying items
  • Loading branch information
louisa-uno authored Aug 9, 2023
2 parents 6016c01 + f46b69f commit 8bd4018
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 44 deletions.
3 changes: 2 additions & 1 deletion default-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"apikey": "Your API Key",
"apikey": "YOUR_SKINBARON_API_KEY",
"discord_webhook": "YOUR_DISCORD_WEBHOOK_URL",
"interval": 60,
"buying": [
{
Expand Down
129 changes: 86 additions & 43 deletions skinbaron.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Configure logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
format='%(asctime)s - %(levelname)s - %(message)s')


def load_config() -> dict:
Expand All @@ -14,15 +14,50 @@ def load_config() -> dict:
return json.load(configFile)


def send_discord_embed(items: list, total):
"""Send an embed to Discord containing bought items information."""
webhook_url = config.get("discord_webhook")

# If webhook_url is None or empty, return early
if not webhook_url:
logging.warning("Discord webhook URL not found in config. Skipping sending message to Discord.")
return

# Construct the embed
embeds = [{
"title": "Items Bought",
"description": "\n".join([f"Item: {item['name']} - Price: {item['price']:.2f} €" for item in items]),
"color": 10233776, # Corresponding to RGB(156,39,176) or #9C27B0
"footer": {
"text": f"Total: {total:.2f} €"
}
}]

payload = {
"content": "New items have been bought!",
"embeds": embeds,
"username": "SkinbaronBot_v2 by Louis_45"
}

headers = {
'Content-Type': 'application/json',
}

response = requests.post(webhook_url, json=payload, headers=headers)

# Log an error if the request failed
if response.status_code != 204:
logging.error("Failed to send message to Discord. Status Code: %s", response.status_code)


def account_get_balance() -> float:
"""Get the balance from SkinBaron"""
# Set up the API URL and payload
url = "https://api.skinbaron.de/GetBalance"
payload = json.dumps({"apikey": apikey})
headers = {
'Content-Type': 'application/json',
'x-requested-with': 'XMLHttpRequest'
'Content-Type': 'application/json',
'x-requested-with': 'XMLHttpRequest'
}

# Send the request to the API
Expand All @@ -34,28 +69,28 @@ def account_get_balance() -> float:


def offers_search(appid: int = 0,
search_item: str = "string",
min_search = 0,
max_search = 0,
tradelocked: bool = True,
after_saleid: str = "string",
items_per_page: int = 0) -> list:
search_item: str = "string",
min_search = 0,
max_search = 0,
tradelocked: bool = True,
after_saleid: str = "string",
items_per_page: int = 0) -> list:
"""Search for offers on SkinBaron"""
# Set up the API URL and payload
url = "https://api.skinbaron.de/SearchOffers"
payload = json.dumps({
"apikey": apikey,
"appid": appid,
"search_item": search_item,
"min": min_search,
"max": max_search,
"tradelocked": tradelocked,
"after_saleid": after_saleid,
"items_per_page": items_per_page
"apikey": apikey,
"appid": appid,
"search_item": search_item,
"min": min_search,
"max": max_search,
"tradelocked": tradelocked,
"after_saleid": after_saleid,
"items_per_page": items_per_page
})
headers = {
'Content-Type': 'application/json',
'x-requested-with': 'XMLHttpRequest'
'Content-Type': 'application/json',
'x-requested-with': 'XMLHttpRequest'
}

# Send the request to the API
Expand All @@ -71,55 +106,63 @@ def offers_buyitems(buy_offer_ids: list, total) -> list:
# Set up the API URL and payload
url = "https://api.skinbaron.de/BuyItems"
payload = json.dumps({
"apikey": apikey,
"total": total,
"toInventory": True,
"saleids": buy_offer_ids
"apikey": apikey,
"total": total,
"toInventory": True,
"saleids": buy_offer_ids
})
headers = {
'Content-Type': 'application/json',
'x-requested-with': 'XMLHttpRequest'
'Content-Type': 'application/json',
'x-requested-with': 'XMLHttpRequest'
}

# Send the request to the API
response = requests.request("POST", url, headers=headers, data=payload)
response_json = response.json()
logging.info("Bought %s items", len(response_json["items"]))
# Check for generalErrors in the response and log them
if "generalErrors" in response_json:
for error in response_json["generalErrors"]:
logging.info("Received Error when trying to buy items: %s", error)
return []
items = response_json["items"]
logging.info("Bought %s items", len(items))

# Send a message to the Discord webhook
send_discord_embed(items, total)
return items


def buy_offers_search(enabled: bool = True,
appid: int = 0,
search_item: str = "string",
min_search = 0,
max_search = 0,
tradelocked: bool = True,
after_saleid: str = "string",
items_per_page: int = 0,
max_buy = 0,
max_buy_total = 0) -> None:
appid: int = 0,
search_item: str = "string",
min_search = 0,
max_search = 0,
tradelocked: bool = True,
after_saleid: str = "string",
items_per_page: int = 0,
max_buy = 0,
max_buy_total = 0) -> None:
"""Buy offers on SkinBaron"""
if not enabled:
return
offers = offers_search(appid=appid,
search_item=search_item,
min_search=min_search,
max_search=max_search,
tradelocked=tradelocked,
after_saleid=after_saleid,
items_per_page=items_per_page)
search_item=search_item,
min_search=min_search,
max_search=max_search,
tradelocked=tradelocked,
after_saleid=after_saleid,
items_per_page=items_per_page)
buy_offer_ids = []
total = 0
for offer in offers:
if offer["price"] <= max_buy:
logging.info("Buying offer: %s - %s €", offer["market_name"],
offer["price"])
offer["price"])
buy_offer_ids.append(offer["id"])
total += offer["price"]
else:
logging.info("Offer too expensive: %s - %s €",
offer["market_name"], offer["price"])
offer["market_name"], offer["price"])
if total > 0:
if total > max_buy_total:
logging.info("Total too expensive: %s €", total)
Expand Down

0 comments on commit 8bd4018

Please sign in to comment.