Update app.py
Browse files
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 |
-
|
|
|
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 |
-
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
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:
|