Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -115,9 +115,11 @@ def check_telegram_messages():
|
|
| 115 |
print(f"[{datetime.now().strftime('%I:%M:%S %p')}] 🔄 Cookies received via Telegram! Resuming checks...")
|
| 116 |
return cookies_updated
|
| 117 |
except Exception as e:
|
|
|
|
| 118 |
return False
|
| 119 |
|
| 120 |
def send_telegram_message(text: str):
|
|
|
|
| 121 |
if not TELEGRAM_BOT_TOKEN or not TELEGRAM_CHAT_ID:
|
| 122 |
print("\n[!] Telegram not configured. The following alert would have been sent:\n")
|
| 123 |
print(text)
|
|
@@ -133,10 +135,13 @@ def send_telegram_message(text: str):
|
|
| 133 |
}
|
| 134 |
|
| 135 |
try:
|
|
|
|
| 136 |
r = requests.post(url, json=payload, timeout=10)
|
|
|
|
| 137 |
r.raise_for_status()
|
| 138 |
return True
|
| 139 |
except Exception as e:
|
|
|
|
| 140 |
print(f"[!] Failed to send telegram message: {e}")
|
| 141 |
return False
|
| 142 |
|
|
@@ -176,19 +181,24 @@ def send_event_alerts(events):
|
|
| 176 |
# SCRAPER LOGIC
|
| 177 |
# ==============================================================================
|
| 178 |
def fetch_bip_events(xsrf_token, bip_session, page=1):
|
|
|
|
| 179 |
cookies = {
|
| 180 |
"XSRF-TOKEN": xsrf_token,
|
| 181 |
"bip_session": bip_session
|
| 182 |
}
|
| 183 |
params = {"perPage": 10, "page": page}
|
| 184 |
try:
|
|
|
|
| 185 |
r = requests.get(BIP_API, params=params, headers=HEADERS, cookies=cookies, timeout=20)
|
|
|
|
| 186 |
# Check if session expired based on HTML redirect instead of JSON
|
| 187 |
if "text/html" in r.headers.get("Content-Type", "") or r.status_code == 401 or r.status_code == 403:
|
|
|
|
| 188 |
return None, "Session expired or invalid cookies."
|
| 189 |
r.raise_for_status()
|
| 190 |
return r.json(), None
|
| 191 |
except Exception as e:
|
|
|
|
| 192 |
return None, str(e)
|
| 193 |
|
| 194 |
def parse_event(resource):
|
|
@@ -237,6 +247,7 @@ def check_new_events(last_id, xsrf_token, bip_session):
|
|
| 237 |
# ==============================================================================
|
| 238 |
def process_tick():
|
| 239 |
global LAST_EVENT_ID, LAST_EVENT_CODE, SESSION_EXPIRED, EXPIRED_XSRF, EXPIRED_BIP
|
|
|
|
| 240 |
# Reload environment variables on every tick so user can update .env without restarting
|
| 241 |
load_dotenv(override=True)
|
| 242 |
xsrf = os.getenv("XSRF_TOKEN", "")
|
|
|
|
| 115 |
print(f"[{datetime.now().strftime('%I:%M:%S %p')}] 🔄 Cookies received via Telegram! Resuming checks...")
|
| 116 |
return cookies_updated
|
| 117 |
except Exception as e:
|
| 118 |
+
print(f"[DEBUG] Network/Request EXCEPTION in check_telegram_messages: {type(e).__name__} - {e}")
|
| 119 |
return False
|
| 120 |
|
| 121 |
def send_telegram_message(text: str):
|
| 122 |
+
print(f"\n[DEBUG] --> send_telegram_message called")
|
| 123 |
if not TELEGRAM_BOT_TOKEN or not TELEGRAM_CHAT_ID:
|
| 124 |
print("\n[!] Telegram not configured. The following alert would have been sent:\n")
|
| 125 |
print(text)
|
|
|
|
| 135 |
}
|
| 136 |
|
| 137 |
try:
|
| 138 |
+
print(f"[DEBUG] Sending alert to Telegram API...")
|
| 139 |
r = requests.post(url, json=payload, timeout=10)
|
| 140 |
+
print(f"[DEBUG] Telegram responded with HTTP {r.status_code}")
|
| 141 |
r.raise_for_status()
|
| 142 |
return True
|
| 143 |
except Exception as e:
|
| 144 |
+
print(f"[DEBUG] Network/Request EXCEPTION in send_telegram_message: {type(e).__name__} - {e}")
|
| 145 |
print(f"[!] Failed to send telegram message: {e}")
|
| 146 |
return False
|
| 147 |
|
|
|
|
| 181 |
# SCRAPER LOGIC
|
| 182 |
# ==============================================================================
|
| 183 |
def fetch_bip_events(xsrf_token, bip_session, page=1):
|
| 184 |
+
print(f"\n[DEBUG] --> fetch_bip_events(page={page})")
|
| 185 |
cookies = {
|
| 186 |
"XSRF-TOKEN": xsrf_token,
|
| 187 |
"bip_session": bip_session
|
| 188 |
}
|
| 189 |
params = {"perPage": 10, "page": page}
|
| 190 |
try:
|
| 191 |
+
print(f"[DEBUG] Fetching strictly from {BIP_API} ...")
|
| 192 |
r = requests.get(BIP_API, params=params, headers=HEADERS, cookies=cookies, timeout=20)
|
| 193 |
+
print(f"[DEBUG] BIP API responded with HTTP {r.status_code}")
|
| 194 |
# Check if session expired based on HTML redirect instead of JSON
|
| 195 |
if "text/html" in r.headers.get("Content-Type", "") or r.status_code == 401 or r.status_code == 403:
|
| 196 |
+
print(f"[DEBUG] Session expired detected! Content-type: {r.headers.get('Content-Type')}, Status: {r.status_code}")
|
| 197 |
return None, "Session expired or invalid cookies."
|
| 198 |
r.raise_for_status()
|
| 199 |
return r.json(), None
|
| 200 |
except Exception as e:
|
| 201 |
+
print(f"[DEBUG] Network/Request EXCEPTION in fetch_bip_events: {type(e).__name__} - {e}")
|
| 202 |
return None, str(e)
|
| 203 |
|
| 204 |
def parse_event(resource):
|
|
|
|
| 247 |
# ==============================================================================
|
| 248 |
def process_tick():
|
| 249 |
global LAST_EVENT_ID, LAST_EVENT_CODE, SESSION_EXPIRED, EXPIRED_XSRF, EXPIRED_BIP
|
| 250 |
+
print("\n[DEBUG] --- process_tick starting ---")
|
| 251 |
# Reload environment variables on every tick so user can update .env without restarting
|
| 252 |
load_dotenv(override=True)
|
| 253 |
xsrf = os.getenv("XSRF_TOKEN", "")
|