| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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, stock_id=""):
- """
- 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:
- :param stock_id:
- :return:
- """
- this_time = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M')
- this_drop_decimal = quantity * price / 1e9
- npc = ''
- if npc_drop:
- npc = '*NPC* '
- name = ('<@!'+stock_id+'>' if stock_id else name)
- 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")))
|