AstraOS commited on
Commit
a9bd12e
·
verified ·
1 Parent(s): 2d7178c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import os
2
  import logging
3
  import threading
4
  import time
@@ -14,7 +13,7 @@ app = FastAPI()
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,9 +48,9 @@ stream_thread = None
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
@@ -118,7 +117,7 @@ def help_text():
118
  "*/start* - Begin setup for streaming (simple mode: only Input & Output URL)\n"
119
  "*/setting* - Enter advanced settings (Input URL, Quality Settings, Video Codec, Audio Codec, Output URL)\n"
120
  "*/help* - Display this help text\n"
121
- "*/logs* - Show the log history\n\n"
122
  "After inputs are collected, press the inline *Start Streaming* button.\n\n"
123
  "While streaming, you can use inline buttons or commands:\n"
124
  "*/pause* - Pause the stream\n"
@@ -128,7 +127,7 @@ def help_text():
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",
@@ -162,8 +161,7 @@ def validate_inputs():
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)
@@ -172,7 +170,7 @@ 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:
@@ -182,12 +180,16 @@ def live_log_updater():
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,
@@ -200,7 +202,7 @@ def logs_history(chat_id):
200
  # -------------------------------------------------------------------
201
  def handle_start(chat_id):
202
  global current_step, user_inputs, conversation_fields, advanced_mode
203
- # By default, simple mode (unless advanced_mode was set via /setting)
204
  user_inputs = {}
205
  if not advanced_mode:
206
  conversation_fields = ["input_url", "output_url"]
@@ -323,12 +325,13 @@ def stream_to_youtube(input_url, quality_settings, video_codec, audio_codec, out
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"]:
@@ -407,7 +410,7 @@ def start_streaming(chat_id):
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,
 
 
1
  import logging
2
  import threading
3
  import time
 
13
  # -------------------------------------------------------------------
14
  # Configuration & Global Variables
15
  # -------------------------------------------------------------------
16
+ # (No token or outgoing HTTP calls are used in this version.)
17
  # Conversation state
18
  user_inputs = {}
19
  # The conversation fields will depend on the mode.
 
48
  live_log_thread = None
49
 
50
  # Live logging globals
51
+ live_log_lines = [] # Rolling list (max 50 log lines)
52
+ live_log_display = "" # Global variable updated every second by live_log_updater
53
+ error_notification = "" # Global variable to hold error details if any
54
 
55
  # -------------------------------------------------------------------
56
  # Enhanced Logging Setup
 
117
  "*/start* - Begin setup for streaming (simple mode: only Input & Output URL)\n"
118
  "*/setting* - Enter advanced settings (Input URL, Quality Settings, Video Codec, Audio Codec, Output URL)\n"
119
  "*/help* - Display this help text\n"
120
+ "*/logs* - Show the log history (live log display)\n\n"
121
  "After inputs are collected, press the inline *Start Streaming* button.\n\n"
122
  "While streaming, you can use inline buttons or commands:\n"
123
  "*/pause* - Pause the stream\n"
 
127
  )
128
 
129
  def send_guide_message(chat_id, message):
130
+ # Return a response dictionary to be sent as the webhook reply
131
  logging.info(f"Sending message to chat {chat_id}: {message}")
132
  return {
133
  "method": "sendMessage",
 
161
  def notify_error(chat_id, error_message):
162
  global error_notification
163
  error_notification = error_message
164
+ logging.error(f"Error for chat {chat_id}: {error_message}")
 
165
 
166
  # -------------------------------------------------------------------
167
  # Live Log Updater (Background Thread)
 
170
  global live_log_display, streaming_state
171
  try:
172
  while streaming_state in ["streaming", "paused"]:
173
+ # Update the global live_log_display with the last 15 log lines in HTML format
174
  live_log_display = "<pre>" + "\n".join(live_log_lines[-15:]) + "</pre>"
175
  time.sleep(1)
176
  except Exception as e:
 
180
  # Logs History Handler (/logs)
181
  # -------------------------------------------------------------------
182
  def logs_history(chat_id):
183
+ global live_log_display, error_notification
184
+ # Use the global live_log_display if available; otherwise, show a default message.
185
+ log_text = live_log_display if live_log_display else "<pre>No logs available yet.</pre>"
186
+ # If an error occurred, prepend the error notification.
187
  if error_notification:
188
+ # Remove any opening <pre> tag from log_text and prepend error message
189
+ if log_text.startswith("<pre>"):
190
+ log_text = f"<pre>ERROR: {error_notification}\n\n" + log_text[5:]
191
+ else:
192
+ log_text = f"<pre>ERROR: {error_notification}\n\n{log_text}</pre>"
193
  return {
194
  "method": "sendMessage",
195
  "chat_id": chat_id,
 
202
  # -------------------------------------------------------------------
203
  def handle_start(chat_id):
204
  global current_step, user_inputs, conversation_fields, advanced_mode
205
+ # By default, use simple mode unless advanced_mode is set via /setting
206
  user_inputs = {}
207
  if not advanced_mode:
208
  conversation_fields = ["input_url", "output_url"]
 
325
 
326
  logging.info("Streaming started successfully.")
327
 
328
+ # Start the live log updater in a background thread if not already running.
329
  global live_log_thread
330
+ if live_log_thread is None or not live_log_thread.is_alive():
331
+ live_log_thread = threading.Thread(target=live_log_updater)
332
+ live_log_thread.daemon = True
333
+ live_log_thread.start()
334
+ logging.info("Live log updater thread started.")
335
 
336
  # Stream loop: process packets until state changes
337
  while streaming_state in ["streaming", "paused"]:
 
410
  logging.info("Streaming thread started.")
411
 
412
  # Immediately inform the user that streaming has started;
413
+ # live log updates are available via the global variable and the /logs command.
414
  return {
415
  "method": "sendMessage",
416
  "chat_id": chat_id,