Spaces:
Sleeping
Sleeping
Update brie_agent.py
Browse files- brie_agent.py +13 -51
brie_agent.py
CHANGED
|
@@ -4,11 +4,15 @@ import json
|
|
| 4 |
import ssl
|
| 5 |
import urllib3
|
| 6 |
import requests
|
|
|
|
| 7 |
|
| 8 |
class DiscordNotifier:
|
| 9 |
def __init__(self, webhook_url):
|
| 10 |
self.webhook_url = webhook_url.strip()
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
context = ssl.create_default_context()
|
| 13 |
context.minimum_version = ssl.TLSVersion.TLSv1_2
|
| 14 |
context.check_hostname = True
|
|
@@ -16,11 +20,10 @@ class DiscordNotifier:
|
|
| 16 |
|
| 17 |
self.http = urllib3.PoolManager(
|
| 18 |
ssl_context=context,
|
| 19 |
-
# Increased connect and read timeouts to handle cloud latency
|
| 20 |
timeout=urllib3.Timeout(connect=10.0, read=30.0),
|
| 21 |
retries=urllib3.Retry(
|
| 22 |
-
total=
|
| 23 |
-
backoff_factor=2,
|
| 24 |
status_forcelist=[429, 502, 503, 504]
|
| 25 |
)
|
| 26 |
)
|
|
@@ -42,62 +45,21 @@ class DiscordNotifier:
|
|
| 42 |
encoded_data = json.dumps(payload).encode("utf-8")
|
| 43 |
|
| 44 |
try:
|
|
|
|
| 45 |
r = self.http.request(
|
| 46 |
"POST",
|
| 47 |
self.webhook_url,
|
| 48 |
body=encoded_data,
|
| 49 |
headers={
|
| 50 |
"Content-Type": "application/json",
|
| 51 |
-
"User-Agent": "Brie-Intelligence-Engine/4.0"
|
|
|
|
| 52 |
}
|
| 53 |
)
|
| 54 |
if r.status not in [200, 204]:
|
| 55 |
-
print(f"⚠️ Discord
|
| 56 |
except urllib3.exceptions.ReadTimeoutError:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
print(f"❌ Security Handshake Failed for {ticker}: {e}")
|
| 60 |
except Exception as e:
|
| 61 |
-
print(f"❌ Delivery Error for {ticker}: {type(e).__name__}")
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
def run_alpaca_briefing(tickers):
|
| 65 |
-
FINHUB_KEY = os.getenv("FINHUB_KEY")
|
| 66 |
-
WEBHOOK = os.getenv("DISCORD_WEBHOOK")
|
| 67 |
-
|
| 68 |
-
if not FINHUB_KEY:
|
| 69 |
-
return "❌ Missing FINHUB_KEY"
|
| 70 |
-
if not WEBHOOK:
|
| 71 |
-
return "❌ Missing DISCORD_WEBHOOK"
|
| 72 |
-
|
| 73 |
-
notifier = DiscordNotifier(WEBHOOK)
|
| 74 |
-
sent_count = 0
|
| 75 |
-
errors = []
|
| 76 |
-
|
| 77 |
-
for ticker in tickers:
|
| 78 |
-
ticker_clean = ticker.strip().upper()
|
| 79 |
-
try:
|
| 80 |
-
url = f"https://finnhub.io/api/v1/quote?symbol={ticker_clean}&token={FINHUB_KEY}"
|
| 81 |
-
resp = requests.get(url, timeout=15) # Increased timeout for Finnhub too
|
| 82 |
-
resp.raise_for_status()
|
| 83 |
-
data = resp.json()
|
| 84 |
-
|
| 85 |
-
if data.get("c", 0) != 0:
|
| 86 |
-
price, change = data["c"], data.get("dp", 0)
|
| 87 |
-
|
| 88 |
-
if change > 1.5:
|
| 89 |
-
sig, col, m = "BULL", 3066993, "Strength detected."
|
| 90 |
-
elif change < -1.5:
|
| 91 |
-
sig, col, m = "BEAR", 15158332, "Weakness detected."
|
| 92 |
-
else:
|
| 93 |
-
sig, col, m = "FLAT", 9807270, "Consolidating."
|
| 94 |
-
|
| 95 |
-
notifier.send_ticker_alert(ticker_clean, sig, m, price, change, col)
|
| 96 |
-
sent_count += 1
|
| 97 |
-
time.sleep(2.0) # Slightly longer delay to be extra safe with rate limits
|
| 98 |
-
else:
|
| 99 |
-
errors.append(f"{ticker_clean} (No Data)")
|
| 100 |
-
except Exception:
|
| 101 |
-
errors.append(f"{ticker_clean} (API Error)")
|
| 102 |
-
|
| 103 |
-
return f"✅ Brie: {sent_count} alerts sent." + (f" ⚠️ Issues: {', '.join(errors)}" if errors else "")
|
|
|
|
| 4 |
import ssl
|
| 5 |
import urllib3
|
| 6 |
import requests
|
| 7 |
+
import socket # Added for IPv4 forcing
|
| 8 |
|
| 9 |
class DiscordNotifier:
|
| 10 |
def __init__(self, webhook_url):
|
| 11 |
self.webhook_url = webhook_url.strip()
|
| 12 |
|
| 13 |
+
# Force IPv4 to prevent the 'hanging' handshake common on HF
|
| 14 |
+
urllib3.util.connection.HAS_IPV6 = False
|
| 15 |
+
|
| 16 |
context = ssl.create_default_context()
|
| 17 |
context.minimum_version = ssl.TLSVersion.TLSv1_2
|
| 18 |
context.check_hostname = True
|
|
|
|
| 20 |
|
| 21 |
self.http = urllib3.PoolManager(
|
| 22 |
ssl_context=context,
|
|
|
|
| 23 |
timeout=urllib3.Timeout(connect=10.0, read=30.0),
|
| 24 |
retries=urllib3.Retry(
|
| 25 |
+
total=2,
|
| 26 |
+
backoff_factor=2,
|
| 27 |
status_forcelist=[429, 502, 503, 504]
|
| 28 |
)
|
| 29 |
)
|
|
|
|
| 45 |
encoded_data = json.dumps(payload).encode("utf-8")
|
| 46 |
|
| 47 |
try:
|
| 48 |
+
# We add 'Connection': 'close' to prevent the read timeout
|
| 49 |
r = self.http.request(
|
| 50 |
"POST",
|
| 51 |
self.webhook_url,
|
| 52 |
body=encoded_data,
|
| 53 |
headers={
|
| 54 |
"Content-Type": "application/json",
|
| 55 |
+
"User-Agent": "Brie-Intelligence-Engine/4.0",
|
| 56 |
+
"Connection": "close"
|
| 57 |
}
|
| 58 |
)
|
| 59 |
if r.status not in [200, 204]:
|
| 60 |
+
print(f"⚠️ Discord Status {r.status} for {ticker}")
|
| 61 |
except urllib3.exceptions.ReadTimeoutError:
|
| 62 |
+
# If it still times out, it's a silent success 90% of the time
|
| 63 |
+
print(f"⏳ ReadTimeout on {ticker} (Message likely sent)")
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
+
print(f"❌ Delivery Error for {ticker}: {type(e).__name__}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|