From 42bdce7dd2eada396454b79a6a7f40dfc7721ff6 Mon Sep 17 00:00:00 2001 From: Luois45 Date: Wed, 9 Aug 2023 09:50:13 +0200 Subject: [PATCH 1/2] Added support for sending bought items to a discord webhook --- default-config.json | 3 ++- skinbaron.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/default-config.json b/default-config.json index 4b5a9ed..ba980ca 100644 --- a/default-config.json +++ b/default-config.json @@ -1,5 +1,6 @@ { - "apikey": "Your API Key", + "apikey": "YOUR_SKINBARON_API_KEY", + "discord_webhook": "YOUR_DISCORD_WEBHOOK_URL", "interval": 60, "buying": [ { diff --git a/skinbaron.py b/skinbaron.py index be786bf..817e58f 100644 --- a/skinbaron.py +++ b/skinbaron.py @@ -14,6 +14,38 @@ def load_config() -> dict: return json.load(configFile) +def send_discord_embed(items: list): + """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']}" for item in items]), + "color": 10233776 # Corresponding to RGB(156,39,176) or #9C27B0 + }] + + 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""" @@ -86,6 +118,10 @@ def offers_buyitems(buy_offer_ids: list, total) -> list: response_json = response.json() logging.info("Bought %s items", len(response_json["items"])) items = response_json["items"] + + # Send a message to the Discord webhook + send_discord_embed(items) + return items From f46b69f20e3f76f96e8f6ddb621bad88060d9037 Mon Sep 17 00:00:00 2001 From: Luois45 Date: Wed, 9 Aug 2023 10:11:14 +0200 Subject: [PATCH 2/2] Changed teh discord webhook embed and added exception logging --- skinbaron.py | 157 +++++++++++++++++++++++++++------------------------ 1 file changed, 82 insertions(+), 75 deletions(-) diff --git a/skinbaron.py b/skinbaron.py index 817e58f..8026e66 100644 --- a/skinbaron.py +++ b/skinbaron.py @@ -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: @@ -14,37 +14,40 @@ def load_config() -> dict: return json.load(configFile) -def send_discord_embed(items: list): - """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']}" for item in items]), - "color": 10233776 # Corresponding to RGB(156,39,176) or #9C27B0 - }] - - 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) +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) + if response.status_code != 204: + logging.error("Failed to send message to Discord. Status Code: %s", response.status_code) def account_get_balance() -> float: @@ -53,8 +56,8 @@ def account_get_balance() -> float: 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 @@ -66,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 @@ -103,59 +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) - + 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)