awacke1 commited on
Commit
304a90b
ยท
verified ยท
1 Parent(s): 09bf8cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -19
app.py CHANGED
@@ -61,18 +61,33 @@ server_task = None
61
  def get_node_name():
62
  """๐ŸŒ - Naming Node with Code - Spins a name, oh so bold!"""
63
  action = "๐ŸŒ - Naming Node with Code - Spins a name, oh so bold!"
64
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
65
  parser = argparse.ArgumentParser(description='Start a chat node with a specific name')
66
  parser.add_argument('--node-name', type=str, default=None)
67
  parser.add_argument('--port', type=int, default=8501)
68
  args = parser.parse_args()
69
  return args.node_name or f"node-{uuid.uuid4().hex[:8]}", args.port
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  def save_chat_entry(username, message):
72
  """๐Ÿ“ - Chat Snap Trap - Logs your yap, no cap! โœ๏ธ๐Ÿง """
73
  action = "๐Ÿ“ - Chat Snap Trap - Logs your yap, no cap! โœ๏ธ๐Ÿง " # Double rhyme bonus!
74
- with open(CHAT_FILE, 'a') as f:
75
- f.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {username}: {action}\n")
76
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
77
  entry = f"[{timestamp}] {username}: {message}"
78
  with open(CHAT_FILE, 'a') as f:
@@ -81,7 +96,8 @@ def save_chat_entry(username, message):
81
  def load_chat():
82
  """๐Ÿ“– - Chat Fetch Quest - Grabs the log, no jest!"""
83
  action = "๐Ÿ“– - Chat Fetch Quest - Grabs the log, no jest!"
84
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
85
  if not os.path.exists(CHAT_FILE):
86
  with open(CHAT_FILE, 'w') as f:
87
  f.write(f"# {START_ROOM} Chat\n\nWelcome to the cosmic hub - start chatting! ๐ŸŽค\n")
@@ -93,7 +109,8 @@ def load_chat():
93
  def get_user_list(chat_content):
94
  """๐Ÿ‘ฅ - Crew Clue Brew - Spots whoโ€™s who in the crew!"""
95
  action = "๐Ÿ‘ฅ - Crew Clue Brew - Spots whoโ€™s who in the crew!"
96
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
97
  users = set()
98
  for line in chat_content.split('\n'):
99
  if line.strip() and ': ' in line:
@@ -104,13 +121,15 @@ def get_user_list(chat_content):
104
  def has_joined_before(client_id, chat_content):
105
  """๐Ÿšช - Join Check Trek - Sees whoโ€™s back, no wreck!"""
106
  action = "๐Ÿšช - Join Check Trek - Sees whoโ€™s back, no wreck!"
107
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
108
  return any(f"Client-{client_id} has joined" in line for line in chat_content.split('\n'))
109
 
110
  def get_message_suggestions(chat_content, prefix):
111
  """๐Ÿ” - Suggest Jest Chest - Finds old quips, the best!"""
112
  action = "๐Ÿ” - Suggest Jest Chest - Finds old quips, the best!"
113
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
114
  lines = chat_content.split('\n')
115
  messages = [line.split(': ', 1)[1] for line in lines if ': ' in line and line.strip()]
116
  return [msg for msg in messages if msg.lower().startswith(prefix.lower())][:5]
@@ -118,7 +137,8 @@ def get_message_suggestions(chat_content, prefix):
118
  def load_quotes(source="famous"):
119
  """๐Ÿ“œ - Quote Tote Note - Wise words float, we gloat!"""
120
  action = "๐Ÿ“œ - Quote Tote Note - Wise words float, we gloat!"
121
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
122
  famous_quotes = [
123
  "The true sign of intelligence is not knowledge but imagination. โ€“ Albert Einstein",
124
  ]
@@ -130,7 +150,7 @@ def load_quotes(source="famous"):
130
  def save_vote(file, item, user_hash, username, comment=""):
131
  """๐Ÿ‘ - Vote Note Float - Cheers rise, we gloat!"""
132
  action = "๐Ÿ‘ - Vote Note Float - Cheers rise, we gloat!"
133
- save_chat_entry(username, action)
134
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
135
  entry = f"[{timestamp}] {user_hash} voted for {item}"
136
  with open(file, 'a') as f:
@@ -145,7 +165,8 @@ def save_vote(file, item, user_hash, username, comment=""):
145
  def load_votes(file):
146
  """๐Ÿ† - Tally Rally Call - Counts the cheers, no stall!"""
147
  action = "๐Ÿ† - Tally Rally Call - Counts the cheers, no stall!"
148
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
149
  if not os.path.exists(file):
150
  with open(file, 'w') as f:
151
  f.write("# Vote Tally\n\nNo votes yet - get clicking! ๐Ÿ–ฑ๏ธ\n")
@@ -166,7 +187,8 @@ def load_votes(file):
166
  def generate_user_hash():
167
  """๐Ÿ”‘ - Hash Dash Bash - Crafts a code, so brash!"""
168
  action = "๐Ÿ”‘ - Hash Dash Bash - Crafts a code, so brash!"
169
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
170
  if 'user_hash' not in st.session_state:
171
  st.session_state.user_hash = hashlib.md5(str(random.getrandbits(128)).encode()).hexdigest()[:8]
172
  return st.session_state.user_hash
@@ -174,7 +196,8 @@ def generate_user_hash():
174
  def save_pasted_image(image_data):
175
  """๐Ÿ“ธ - Snap Cap Trap - Saves your pic, no flap!"""
176
  action = "๐Ÿ“ธ - Snap Cap Trap - Saves your pic, no flap!"
177
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
178
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
179
  filename = f"paste_{timestamp}.png"
180
  filepath = os.path.join('./', filename)
@@ -188,14 +211,16 @@ def save_pasted_image(image_data):
188
  def get_video_html(video_path, width="100%"):
189
  """๐ŸŽฅ - Reel Deal Steal - Plays your flick, so real!"""
190
  action = "๐ŸŽฅ - Reel Deal Steal - Plays your flick, so real!"
191
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
192
  video_url = f"data:video/mp4;base64,{base64.b64encode(open(video_path, 'rb').read()).decode()}"
193
  return f'<video width="{width}" controls autoplay muted loop><source src="{video_url}" type="video/mp4">Your browser does not support the video tag.</video>'
194
 
195
  def get_audio_html(audio_path, width="100%"):
196
  """๐ŸŽถ - Tune Moon Boom - Drops a beat, so groom!"""
197
  action = "๐ŸŽถ - Tune Moon Boom - Drops a beat, so groom!"
198
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
199
  audio_url = f"data:audio/mpeg;base64,{base64.b64encode(open(audio_path, 'rb').read()).decode()}"
200
  return f'<audio controls style="width: {width};"><source src="{audio_url}" type="audio/mpeg">Your browser does not support the audio element.</audio>'
201
 
@@ -204,7 +229,8 @@ active_connections = {}
204
  async def websocket_handler(websocket, path):
205
  """๐ŸŒ - Web Sock Jock - Links the chat, no block!"""
206
  action = "๐ŸŒ - Web Sock Jock - Links the chat, no block!"
207
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
208
  try:
209
  client_id = str(uuid.uuid4())
210
  room_id = "chat"
@@ -228,7 +254,8 @@ async def websocket_handler(websocket, path):
228
  async def broadcast_message(message, room_id):
229
  """๐Ÿ“ข - Shout Out Bout - Blasts the word, no doubt!"""
230
  action = "๐Ÿ“ข - Shout Out Bout - Blasts the word, no doubt!"
231
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
232
  if room_id in active_connections:
233
  disconnected = []
234
  for client_id, ws in active_connections[room_id].items():
@@ -242,7 +269,8 @@ async def broadcast_message(message, room_id):
242
  async def run_websocket_server():
243
  """๐Ÿ–ฅ๏ธ - Server Ferver Verve - Spins the web, with nerve!"""
244
  action = "๐Ÿ–ฅ๏ธ - Server Ferver Verve - Spins the web, with nerve!"
245
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
246
  global server_running, server_task
247
  if not server_running:
248
  server = await websockets.serve(websocket_handler, '0.0.0.0', 8765)
@@ -252,7 +280,8 @@ async def run_websocket_server():
252
  def create_streamlit_interface():
253
  """๐ŸŽจ - UI Brew Crew - Builds the view, so new!"""
254
  action = "๐ŸŽจ - UI Brew Crew - Builds the view, so new!"
255
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
256
 
257
  # Dynamic title based on username
258
  if 'username' in st.session_state:
@@ -437,7 +466,8 @@ def create_streamlit_interface():
437
  async def main():
438
  """๐ŸŽฎ - Game Fame Claim - Starts the fun, no shame!"""
439
  action = "๐ŸŽฎ - Game Fame Claim - Starts the fun, no shame!"
440
- save_chat_entry(st.session_state.get('username', 'System ๐ŸŒŸ'), action)
 
441
  global NODE_NAME, server_task
442
  NODE_NAME, port = get_node_name()
443
  if server_task is None:
 
61
  def get_node_name():
62
  """๐ŸŒ - Naming Node with Code - Spins a name, oh so bold!"""
63
  action = "๐ŸŒ - Naming Node with Code - Spins a name, oh so bold!"
64
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
65
+ log_action(username, action)
66
  parser = argparse.ArgumentParser(description='Start a chat node with a specific name')
67
  parser.add_argument('--node-name', type=str, default=None)
68
  parser.add_argument('--port', type=int, default=8501)
69
  args = parser.parse_args()
70
  return args.node_name or f"node-{uuid.uuid4().hex[:8]}", args.port
71
 
72
+ def log_action(username, action):
73
+ """Helper to log actions with deduplication"""
74
+ if 'action_log' not in st.session_state:
75
+ st.session_state.action_log = {}
76
+ user_log = st.session_state.action_log.setdefault(username, {})
77
+ current_time = time.time()
78
+ # Clean up old entries (older than 10 seconds)
79
+ user_log = {k: v for k, v in user_log.items() if current_time - v < 10}
80
+ st.session_state.action_log[username] = user_log
81
+ # Log if action hasn't been seen recently
82
+ if action not in user_log:
83
+ with open(CHAT_FILE, 'a') as f:
84
+ f.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {username}: {action}\n")
85
+ user_log[action] = current_time
86
+
87
  def save_chat_entry(username, message):
88
  """๐Ÿ“ - Chat Snap Trap - Logs your yap, no cap! โœ๏ธ๐Ÿง """
89
  action = "๐Ÿ“ - Chat Snap Trap - Logs your yap, no cap! โœ๏ธ๐Ÿง " # Double rhyme bonus!
90
+ log_action(username, action)
 
91
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
92
  entry = f"[{timestamp}] {username}: {message}"
93
  with open(CHAT_FILE, 'a') as f:
 
96
  def load_chat():
97
  """๐Ÿ“– - Chat Fetch Quest - Grabs the log, no jest!"""
98
  action = "๐Ÿ“– - Chat Fetch Quest - Grabs the log, no jest!"
99
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
100
+ log_action(username, action)
101
  if not os.path.exists(CHAT_FILE):
102
  with open(CHAT_FILE, 'w') as f:
103
  f.write(f"# {START_ROOM} Chat\n\nWelcome to the cosmic hub - start chatting! ๐ŸŽค\n")
 
109
  def get_user_list(chat_content):
110
  """๐Ÿ‘ฅ - Crew Clue Brew - Spots whoโ€™s who in the crew!"""
111
  action = "๐Ÿ‘ฅ - Crew Clue Brew - Spots whoโ€™s who in the crew!"
112
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
113
+ log_action(username, action)
114
  users = set()
115
  for line in chat_content.split('\n'):
116
  if line.strip() and ': ' in line:
 
121
  def has_joined_before(client_id, chat_content):
122
  """๐Ÿšช - Join Check Trek - Sees whoโ€™s back, no wreck!"""
123
  action = "๐Ÿšช - Join Check Trek - Sees whoโ€™s back, no wreck!"
124
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
125
+ log_action(username, action)
126
  return any(f"Client-{client_id} has joined" in line for line in chat_content.split('\n'))
127
 
128
  def get_message_suggestions(chat_content, prefix):
129
  """๐Ÿ” - Suggest Jest Chest - Finds old quips, the best!"""
130
  action = "๐Ÿ” - Suggest Jest Chest - Finds old quips, the best!"
131
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
132
+ log_action(username, action)
133
  lines = chat_content.split('\n')
134
  messages = [line.split(': ', 1)[1] for line in lines if ': ' in line and line.strip()]
135
  return [msg for msg in messages if msg.lower().startswith(prefix.lower())][:5]
 
137
  def load_quotes(source="famous"):
138
  """๐Ÿ“œ - Quote Tote Note - Wise words float, we gloat!"""
139
  action = "๐Ÿ“œ - Quote Tote Note - Wise words float, we gloat!"
140
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
141
+ log_action(username, action)
142
  famous_quotes = [
143
  "The true sign of intelligence is not knowledge but imagination. โ€“ Albert Einstein",
144
  ]
 
150
  def save_vote(file, item, user_hash, username, comment=""):
151
  """๐Ÿ‘ - Vote Note Float - Cheers rise, we gloat!"""
152
  action = "๐Ÿ‘ - Vote Note Float - Cheers rise, we gloat!"
153
+ log_action(username, action)
154
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
155
  entry = f"[{timestamp}] {user_hash} voted for {item}"
156
  with open(file, 'a') as f:
 
165
  def load_votes(file):
166
  """๐Ÿ† - Tally Rally Call - Counts the cheers, no stall!"""
167
  action = "๐Ÿ† - Tally Rally Call - Counts the cheers, no stall!"
168
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
169
+ log_action(username, action)
170
  if not os.path.exists(file):
171
  with open(file, 'w') as f:
172
  f.write("# Vote Tally\n\nNo votes yet - get clicking! ๐Ÿ–ฑ๏ธ\n")
 
187
  def generate_user_hash():
188
  """๐Ÿ”‘ - Hash Dash Bash - Crafts a code, so brash!"""
189
  action = "๐Ÿ”‘ - Hash Dash Bash - Crafts a code, so brash!"
190
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
191
+ log_action(username, action)
192
  if 'user_hash' not in st.session_state:
193
  st.session_state.user_hash = hashlib.md5(str(random.getrandbits(128)).encode()).hexdigest()[:8]
194
  return st.session_state.user_hash
 
196
  def save_pasted_image(image_data):
197
  """๐Ÿ“ธ - Snap Cap Trap - Saves your pic, no flap!"""
198
  action = "๐Ÿ“ธ - Snap Cap Trap - Saves your pic, no flap!"
199
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
200
+ log_action(username, action)
201
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
202
  filename = f"paste_{timestamp}.png"
203
  filepath = os.path.join('./', filename)
 
211
  def get_video_html(video_path, width="100%"):
212
  """๐ŸŽฅ - Reel Deal Steal - Plays your flick, so real!"""
213
  action = "๐ŸŽฅ - Reel Deal Steal - Plays your flick, so real!"
214
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
215
+ log_action(username, action)
216
  video_url = f"data:video/mp4;base64,{base64.b64encode(open(video_path, 'rb').read()).decode()}"
217
  return f'<video width="{width}" controls autoplay muted loop><source src="{video_url}" type="video/mp4">Your browser does not support the video tag.</video>'
218
 
219
  def get_audio_html(audio_path, width="100%"):
220
  """๐ŸŽถ - Tune Moon Boom - Drops a beat, so groom!"""
221
  action = "๐ŸŽถ - Tune Moon Boom - Drops a beat, so groom!"
222
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
223
+ log_action(username, action)
224
  audio_url = f"data:audio/mpeg;base64,{base64.b64encode(open(audio_path, 'rb').read()).decode()}"
225
  return f'<audio controls style="width: {width};"><source src="{audio_url}" type="audio/mpeg">Your browser does not support the audio element.</audio>'
226
 
 
229
  async def websocket_handler(websocket, path):
230
  """๐ŸŒ - Web Sock Jock - Links the chat, no block!"""
231
  action = "๐ŸŒ - Web Sock Jock - Links the chat, no block!"
232
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
233
+ log_action(username, action)
234
  try:
235
  client_id = str(uuid.uuid4())
236
  room_id = "chat"
 
254
  async def broadcast_message(message, room_id):
255
  """๐Ÿ“ข - Shout Out Bout - Blasts the word, no doubt!"""
256
  action = "๐Ÿ“ข - Shout Out Bout - Blasts the word, no doubt!"
257
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
258
+ log_action(username, action)
259
  if room_id in active_connections:
260
  disconnected = []
261
  for client_id, ws in active_connections[room_id].items():
 
269
  async def run_websocket_server():
270
  """๐Ÿ–ฅ๏ธ - Server Ferver Verve - Spins the web, with nerve!"""
271
  action = "๐Ÿ–ฅ๏ธ - Server Ferver Verve - Spins the web, with nerve!"
272
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
273
+ log_action(username, action)
274
  global server_running, server_task
275
  if not server_running:
276
  server = await websockets.serve(websocket_handler, '0.0.0.0', 8765)
 
280
  def create_streamlit_interface():
281
  """๐ŸŽจ - UI Brew Crew - Builds the view, so new!"""
282
  action = "๐ŸŽจ - UI Brew Crew - Builds the view, so new!"
283
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
284
+ log_action(username, action)
285
 
286
  # Dynamic title based on username
287
  if 'username' in st.session_state:
 
466
  async def main():
467
  """๐ŸŽฎ - Game Fame Claim - Starts the fun, no shame!"""
468
  action = "๐ŸŽฎ - Game Fame Claim - Starts the fun, no shame!"
469
+ username = st.session_state.get('username', 'System ๐ŸŒŸ')
470
+ log_action(username, action)
471
  global NODE_NAME, server_task
472
  NODE_NAME, port = get_node_name()
473
  if server_task is None: