import os from datetime import datetime import requests import json base_url = 'https://discordapp.com/api/webhooks/' def create_message(name, timestamp, price, quantity, current_quantity=0, forecast_previous="", forecast_latest="", npc_drop=False): """ Creates the string to be broadcast :param name: :param timestamp: :param price: :param quantity: :param current_quantity: :param forecast_previous: :param forecast_latest: :param npc_drop: :return: """ this_time = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M') this_drop_decimal = quantity * price / 1e9 npc = '' if npc_drop: npc = '*NPC* ' this_message = "{}: {}{} dropped {:,} shares at ${:,.2f} for a grand total of ${:,.1f}B [{}]".format( this_time, npc, name, quantity, price, this_drop_decimal, current_quantity) if forecast_previous and forecast_latest: this_message = "{}: @{} changed from {} to {} with {:,} shares available at ${:,.2f}".format( this_time, name, forecast_previous, forecast_latest, current_quantity, price) return this_message def broadcast(this_message): """ :param this_message: :return: """ # development only # load_dotenv() # url = os.environ["BROADCAST_URL"] webhooks_json = os.environ["DISCORD_TOKENS"] webhooks = json.loads(webhooks_json) content = {'content': this_message} for webhook_key in webhooks: webhook = webhooks[webhook_key] url = base_url + webhook response = requests.post(url, json=content) if response.status_code in [200, 204]: print("Webhook executed") else: print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))