extend discord bot

This commit is contained in:
MuslemRahimi 2025-04-16 15:53:36 +02:00
parent 40e2f5c82d
commit 9bc68e7bd0

View File

@ -29,6 +29,7 @@ ANALYST_REPORT_WEBHOOK_URL = os.getenv("DISCORD_ANALYST_REPORT_WEBHOOK")
WIIM_WEBHOOK_URL = os.getenv('DISCORD_WIIM_WEBHOOK') WIIM_WEBHOOK_URL = os.getenv('DISCORD_WIIM_WEBHOOK')
CONGRESS_TRADING_WEBHOOK_URL = os.getenv("DISCORD_CONGRESS_TRADING_WEBHOOK") CONGRESS_TRADING_WEBHOOK_URL = os.getenv("DISCORD_CONGRESS_TRADING_WEBHOOK")
BENZINGA_API_KEY = os.getenv('BENZINGA_API_KEY') BENZINGA_API_KEY = os.getenv('BENZINGA_API_KEY')
headers = {"accept": "application/json"} headers = {"accept": "application/json"}
@ -88,6 +89,22 @@ def abbreviate_number(n):
else: else:
return f"{n/1_000_000_000:.1f}B" return f"{n/1_000_000_000:.1f}B"
def extract_first_value(s):
"""
Extract the first value from a string like "$1K-$15K" or "$1M-$5M"
and return it as an integer.
"""
s = s.upper().replace('$', '')
first_part = s.split('-')[0]
if 'K' in first_part:
return int(float(first_part.replace('K', '')) * 1_000)
elif 'M' in first_part:
return int(float(first_part.replace('M', '')) * 1_000_000)
else:
# If no K or M, just try converting directly
return int(first_part)
def remove_duplicates(elements): def remove_duplicates(elements):
seen = set() seen = set()
unique_elements = [] unique_elements = []
@ -672,15 +689,15 @@ def wiim():
def congress_trading(): def congress_trading():
try: try:
with open(f"json/discord/wiim.json", "r") as file: with open(f"json/discord/congress_trading.json", "r") as file:
seen_list = orjson.loads(file.read()) seen_list = orjson.loads(file.read())
seen_list = [item for item in seen_list if datetime.fromisoformat(item['date']).date() == today] seen_list = [item for item in seen_list if datetime.fromisoformat(item['date']).date() == today]
except: except:
seen_list = [] seen_list = []
with open(f"json/dashboard/data.json", 'rb') as file: with open(f"json/congress-trading/rss-feed/data.json", 'rb') as file:
data = orjson.loads(file.read())['wiim'] data = orjson.loads(file.read())
data = [item for item in data if datetime.fromisoformat(item['date']).date() == today] data = [item for item in data if datetime.fromisoformat(item['disclosureDate']).date() == today]
res_list = [] res_list = []
for item in data: for item in data:
@ -688,18 +705,17 @@ def congress_trading():
with open(f"json/quote/{item['ticker']}.json","r") as file: with open(f"json/quote/{item['ticker']}.json","r") as file:
quote_data = orjson.loads(file.read()) quote_data = orjson.loads(file.read())
item['price'] = round(quote_data.get('price',0),2) item['name'] = quote_data.get('name','n/a')
item['changesPercentage'] = round(quote_data.get('changesPercentage',0),2)
item['marketCap'] = quote_data.get('marketCap',0)
item['eps'] = round(quote_data.get('eps',0),2)
unique_str = f"{item['date']}-{item['ticker']}-{item.get('text','')}" item['amountInt'] = extract_first_value(item['amount'])
unique_str = f"{item['disclosureDate']}-{item['transactionDate']}-{item['amount']}-{item['representative']}"
item['id'] = hashlib.md5(unique_str.encode()).hexdigest() item['id'] = hashlib.md5(unique_str.encode()).hexdigest()
if item['marketCap'] > 1E9: if item['amountInt'] >= 50_000:
res_list.append(item) res_list.append(item)
except:
pass except Exception as e:
print(e)
if res_list: if res_list:
@ -708,32 +724,36 @@ def congress_trading():
else: else:
seen_ids = {} seen_ids = {}
for item in res_list: for item in res_list[:1]:
try: try:
if item != None and item['id'] not in seen_ids and item['marketCap']: if item != None and item['id'] not in seen_ids:
symbol = item['ticker'] symbol = item['ticker']
price = item['price'] name = item['name']
changes_percentage = round(item['changesPercentage'],2) representative = item['representative']
eps = item['eps'] amount = item['amount']
market_cap = abbreviate_number(item['marketCap']) transaction_type = item['type']
description = item['text'] transaction_date = datetime.strptime(item['transactionDate'], "%Y-%m-%d").strftime("%d/%m/%Y")
disclosure_date = datetime.strptime(item['disclosureDate'], "%Y-%m-%d").strftime("%d/%m/%Y")
message_timestamp = int((datetime.now() - timedelta(minutes=0)).timestamp()) message_timestamp = int((datetime.now() - timedelta(minutes=0)).timestamp())
color = 0x39FF14 if 'higher' in description else 0xFF0000 if 'lower' in description else 0xFFFFFF color = 0x39FF14 if transaction_type == 'Bought' else 0xFF0000
embed = { embed = {
"color": color, "color": color,
"thumbnail": {"url": "https://stocknear.com/pwa-64x64.png"}, "thumbnail": {"url": "https://stocknear.com/pwa-64x64.png"},
"title": "Why Priced Moved", "title": "Congress Trading",
"fields": [ "fields": [
{"name": "Politician", "value": representative, "inline": True},
{"name": "", "value": "", "inline": True},
{"name": "Amount", "value": amount, "inline": True},
{"name": "Symbol", "value": symbol, "inline": True}, {"name": "Symbol", "value": symbol, "inline": True},
{"name": "", "value": "", "inline": True}, {"name": "", "value": "", "inline": True},
{"name": "Market Cap", "value": market_cap, "inline": True}, {"name": "Side", "value": transaction_type, "inline": True},
{"name": "Price", "value": str(price), "inline": True}, {"name": "Trade Date", "value": transaction_date, "inline": True},
{"name": "", "value": "", "inline": True}, {"name": "", "value": "", "inline": True},
{"name": "% Change", "value": str(changes_percentage)+"%", "inline": True}, {"name": "Filing Date", "value": disclosure_date, "inline": True},
{"name": "", "value": "", "inline": False}, {"name": "", "value": "", "inline": False},
{"name": f"{description}", "value": "", "inline": False},
{"name": f"Data by Stocknear - <t:{message_timestamp}:R>", "value": "", "inline": False}, {"name": f"Data by Stocknear - <t:{message_timestamp}:R>", "value": "", "inline": False},
], ],
"footer": {"text": ""} "footer": {"text": ""}
@ -744,10 +764,10 @@ def congress_trading():
"embeds": [embed] "embeds": [embed]
} }
response = requests.post(WIIM_WEBHOOK_URL, json=payload) response = requests.post(CONGRESS_TRADING_WEBHOOK_URL, json=payload)
if response.status_code in (200, 204): if response.status_code in (200, 204):
seen_list.append({'date': item['date'], 'id': item['id'], 'symbol': symbol}) seen_list.append({'date': item['disclosureDate'], 'id': item['id'], 'symbol': symbol})
print("Embed sent successfully!") print("Embed sent successfully!")
else: else:
@ -761,7 +781,7 @@ def congress_trading():
except Exception as e: except Exception as e:
print(e) print(e)
try: try:
with open("json/discord/wiim.json","wb") as file: with open("json/discord/congress_trading.json","wb") as file:
file.write(orjson.dumps(seen_list)) file.write(orjson.dumps(seen_list))
except Exception as e: except Exception as e:
print(e) print(e)
@ -773,4 +793,5 @@ if __name__ == "__main__":
recent_earnings() recent_earnings()
executive_order_message() executive_order_message()
analyst_report() analyst_report()
wiim() wiim()
congress_trading()