Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ import time
|
|
5 |
import datetime
|
6 |
import traceback
|
7 |
import fractions
|
8 |
-
import requests
|
9 |
|
10 |
from fastapi import FastAPI, Request
|
11 |
import av
|
@@ -15,9 +14,7 @@ app = FastAPI()
|
|
15 |
# -------------------------------------------------------------------
|
16 |
# Configuration & Global Variables
|
17 |
# -------------------------------------------------------------------
|
18 |
-
# (No
|
19 |
-
# All functions will simply return JSON responses from the webhook endpoint.
|
20 |
-
|
21 |
# Conversation state
|
22 |
user_inputs = {}
|
23 |
# The conversation fields will depend on the mode.
|
@@ -52,8 +49,9 @@ stream_thread = None
|
|
52 |
live_log_thread = None
|
53 |
|
54 |
# Live logging globals
|
55 |
-
live_log_lines = []
|
56 |
-
|
|
|
57 |
|
58 |
# -------------------------------------------------------------------
|
59 |
# Enhanced Logging Setup
|
@@ -84,7 +82,7 @@ logger.addHandler(list_handler)
|
|
84 |
# Utility Functions & UI Helpers
|
85 |
# -------------------------------------------------------------------
|
86 |
def create_html_message(text: str):
|
87 |
-
# Wrap text in <pre> tags for monospaced output
|
88 |
return {"parse_mode": "HTML", "text": f"<pre>{text}</pre>"}
|
89 |
|
90 |
def get_inline_keyboard_for_stream():
|
@@ -130,8 +128,8 @@ def help_text():
|
|
130 |
)
|
131 |
|
132 |
def send_guide_message(chat_id, message):
|
|
|
133 |
logging.info(f"Sending message to chat {chat_id}: {message}")
|
134 |
-
# Return a JSON response that would be sent back to Telegram via webhook.
|
135 |
return {
|
136 |
"method": "sendMessage",
|
137 |
"chat_id": chat_id,
|
@@ -159,41 +157,23 @@ def validate_inputs():
|
|
159 |
return True, ""
|
160 |
|
161 |
# -------------------------------------------------------------------
|
162 |
-
#
|
163 |
# -------------------------------------------------------------------
|
164 |
def notify_error(chat_id, error_message):
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
"chat_id": chat_id,
|
170 |
-
"text": f"⚠️ *Streaming Error Occurred:*\n\n{error_message}\n\nPlease check the live logs for details.",
|
171 |
-
"parse_mode": "Markdown"
|
172 |
-
}
|
173 |
-
logging.error(f"Notifying error to chat {chat_id}: {error_message}")
|
174 |
-
return payload
|
175 |
|
176 |
# -------------------------------------------------------------------
|
177 |
-
# Live
|
178 |
# -------------------------------------------------------------------
|
179 |
-
def live_log_updater(
|
180 |
-
global
|
181 |
try:
|
182 |
-
# Simulate sending an initial live log message in HTML format
|
183 |
-
payload = {
|
184 |
-
"chat_id": chat_id,
|
185 |
-
"text": "<pre>Live Logs:\nInitializing...</pre>",
|
186 |
-
"parse_mode": "HTML"
|
187 |
-
}
|
188 |
-
# Since we are not making an outgoing call, simply log and assign a dummy message ID.
|
189 |
-
live_log_message_id = 1 # Dummy value for simulation
|
190 |
-
logging.info("Simulated live log message sent with dummy id 1")
|
191 |
-
# Update live log every 1 second until streaming stops
|
192 |
while streaming_state in ["streaming", "paused"]:
|
193 |
-
#
|
194 |
-
|
195 |
-
# Instead of calling an external API, log the update that would be sent.
|
196 |
-
logging.info(f"Live log update for chat {chat_id}: {log_text}")
|
197 |
time.sleep(1)
|
198 |
except Exception as e:
|
199 |
logging.error(f"Error in live log updater: {e}")
|
@@ -202,8 +182,12 @@ def live_log_updater(chat_id):
|
|
202 |
# Logs History Handler (/logs)
|
203 |
# -------------------------------------------------------------------
|
204 |
def logs_history(chat_id):
|
205 |
-
# Return the last 50 log lines in HTML format
|
206 |
log_text = "<pre>" + "\n".join(live_log_lines[-50:]) + "</pre>"
|
|
|
|
|
|
|
|
|
207 |
return {
|
208 |
"method": "sendMessage",
|
209 |
"chat_id": chat_id,
|
@@ -339,6 +323,13 @@ def stream_to_youtube(input_url, quality_settings, video_codec, audio_codec, out
|
|
339 |
|
340 |
logging.info("Streaming started successfully.")
|
341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
# Stream loop: process packets until state changes
|
343 |
while streaming_state in ["streaming", "paused"]:
|
344 |
for packet in input_stream.demux():
|
@@ -388,11 +379,11 @@ def stream_to_youtube(input_url, quality_settings, video_codec, audio_codec, out
|
|
388 |
error_message = f"An error occurred during streaming: {str(e)}\n\n{traceback.format_exc()}"
|
389 |
logging.error(error_message)
|
390 |
streaming_state = "idle"
|
391 |
-
# Notify the user about the error by
|
392 |
-
|
393 |
|
394 |
def start_streaming(chat_id):
|
395 |
-
global stream_thread,
|
396 |
valid, msg = validate_inputs()
|
397 |
if not valid:
|
398 |
return send_guide_message(chat_id, f"Validation error: {msg}")
|
@@ -415,24 +406,20 @@ def start_streaming(chat_id):
|
|
415 |
stream_thread.start()
|
416 |
logging.info("Streaming thread started.")
|
417 |
|
418 |
-
#
|
419 |
-
|
420 |
-
live_log_thread.daemon = True
|
421 |
-
live_log_thread.start()
|
422 |
-
logging.info("Live log updater started.")
|
423 |
-
|
424 |
-
# Immediately inform the user that streaming has started
|
425 |
return {
|
426 |
"method": "sendMessage",
|
427 |
"chat_id": chat_id,
|
428 |
-
"text": "🚀 *Streaming initiated!* Live logs are now updating
|
429 |
"reply_markup": get_inline_keyboard_for_stream(),
|
430 |
"parse_mode": "Markdown"
|
431 |
}
|
432 |
except Exception as e:
|
433 |
error_message = f"Failed to start streaming: {str(e)}"
|
434 |
logging.error(error_message)
|
435 |
-
|
|
|
436 |
|
437 |
# -------------------------------------------------------------------
|
438 |
# Stream Control Handlers
|
|
|
5 |
import datetime
|
6 |
import traceback
|
7 |
import fractions
|
|
|
8 |
|
9 |
from fastapi import FastAPI, Request
|
10 |
import av
|
|
|
14 |
# -------------------------------------------------------------------
|
15 |
# Configuration & Global Variables
|
16 |
# -------------------------------------------------------------------
|
17 |
+
# (No token or outgoing calls are used in this version.)
|
|
|
|
|
18 |
# Conversation state
|
19 |
user_inputs = {}
|
20 |
# The conversation fields will depend on the mode.
|
|
|
49 |
live_log_thread = None
|
50 |
|
51 |
# Live logging globals
|
52 |
+
live_log_lines = [] # Rolling list (max 50 lines)
|
53 |
+
live_log_display = "" # Global variable updated every second by live_log_updater
|
54 |
+
error_notification = "" # Global variable to hold any error notification message
|
55 |
|
56 |
# -------------------------------------------------------------------
|
57 |
# Enhanced Logging Setup
|
|
|
82 |
# Utility Functions & UI Helpers
|
83 |
# -------------------------------------------------------------------
|
84 |
def create_html_message(text: str):
|
85 |
+
# Wrap text in <pre> tags for monospaced output using HTML parse mode
|
86 |
return {"parse_mode": "HTML", "text": f"<pre>{text}</pre>"}
|
87 |
|
88 |
def get_inline_keyboard_for_stream():
|
|
|
128 |
)
|
129 |
|
130 |
def send_guide_message(chat_id, message):
|
131 |
+
# Return a response dictionary (to be sent as the webhook response)
|
132 |
logging.info(f"Sending message to chat {chat_id}: {message}")
|
|
|
133 |
return {
|
134 |
"method": "sendMessage",
|
135 |
"chat_id": chat_id,
|
|
|
157 |
return True, ""
|
158 |
|
159 |
# -------------------------------------------------------------------
|
160 |
+
# Error Notification Helper
|
161 |
# -------------------------------------------------------------------
|
162 |
def notify_error(chat_id, error_message):
|
163 |
+
global error_notification
|
164 |
+
error_notification = error_message
|
165 |
+
# (This error will be available for the next webhook update; no outgoing call is made.)
|
166 |
+
logging.error(f"Error notified to user in chat {chat_id}: {error_message}")
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
# -------------------------------------------------------------------
|
169 |
+
# Live Log Updater (Background Thread)
|
170 |
# -------------------------------------------------------------------
|
171 |
+
def live_log_updater():
|
172 |
+
global live_log_display, streaming_state
|
173 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
while streaming_state in ["streaming", "paused"]:
|
175 |
+
# Update the live log display variable with the last 15 log lines in HTML format
|
176 |
+
live_log_display = "<pre>" + "\n".join(live_log_lines[-15:]) + "</pre>"
|
|
|
|
|
177 |
time.sleep(1)
|
178 |
except Exception as e:
|
179 |
logging.error(f"Error in live log updater: {e}")
|
|
|
182 |
# Logs History Handler (/logs)
|
183 |
# -------------------------------------------------------------------
|
184 |
def logs_history(chat_id):
|
185 |
+
# Return the last 50 log lines (if available) in HTML format
|
186 |
log_text = "<pre>" + "\n".join(live_log_lines[-50:]) + "</pre>"
|
187 |
+
# If an error notification exists, prepend it to the log text.
|
188 |
+
global error_notification
|
189 |
+
if error_notification:
|
190 |
+
log_text = f"<pre>ERROR: {error_notification}\n\n" + log_text[5:]
|
191 |
return {
|
192 |
"method": "sendMessage",
|
193 |
"chat_id": chat_id,
|
|
|
323 |
|
324 |
logging.info("Streaming started successfully.")
|
325 |
|
326 |
+
# Start the live log updater in a background thread
|
327 |
+
global live_log_thread
|
328 |
+
live_log_thread = threading.Thread(target=live_log_updater)
|
329 |
+
live_log_thread.daemon = True
|
330 |
+
live_log_thread.start()
|
331 |
+
logging.info("Live log updater thread started.")
|
332 |
+
|
333 |
# Stream loop: process packets until state changes
|
334 |
while streaming_state in ["streaming", "paused"]:
|
335 |
for packet in input_stream.demux():
|
|
|
379 |
error_message = f"An error occurred during streaming: {str(e)}\n\n{traceback.format_exc()}"
|
380 |
logging.error(error_message)
|
381 |
streaming_state = "idle"
|
382 |
+
# Notify the user about the error by storing it in a global variable
|
383 |
+
notify_error(chat_id, error_message)
|
384 |
|
385 |
def start_streaming(chat_id):
|
386 |
+
global stream_thread, stream_chat_id
|
387 |
valid, msg = validate_inputs()
|
388 |
if not valid:
|
389 |
return send_guide_message(chat_id, f"Validation error: {msg}")
|
|
|
406 |
stream_thread.start()
|
407 |
logging.info("Streaming thread started.")
|
408 |
|
409 |
+
# Immediately inform the user that streaming has started;
|
410 |
+
# live log updates will be available via the global variable and /logs command.
|
|
|
|
|
|
|
|
|
|
|
411 |
return {
|
412 |
"method": "sendMessage",
|
413 |
"chat_id": chat_id,
|
414 |
+
"text": "🚀 *Streaming initiated!* Live logs are now updating. Use the inline keyboard to control the stream.",
|
415 |
"reply_markup": get_inline_keyboard_for_stream(),
|
416 |
"parse_mode": "Markdown"
|
417 |
}
|
418 |
except Exception as e:
|
419 |
error_message = f"Failed to start streaming: {str(e)}"
|
420 |
logging.error(error_message)
|
421 |
+
notify_error(chat_id, error_message)
|
422 |
+
return send_guide_message(chat_id, error_message)
|
423 |
|
424 |
# -------------------------------------------------------------------
|
425 |
# Stream Control Handlers
|