-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
294 lines (247 loc) · 9.63 KB
/
app2.py
File metadata and controls
294 lines (247 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# app.py
import os
import sys
import time
import json
import signal
import logging
import sqlite3
import subprocess
import importlib
from datetime import datetime, timedelta
import streamlit as st
import pandas as pd
import pytz
import plotly.express as px
from dotenv import load_dotenv, set_key
from summary_engine import generate_summary
from scheduler import DigestScheduler, get_next_email_time
from email_sender import send_email
import discord_bot
from base_config import (
DISCORD_CONFIG,
OPENAI_CONFIG,
EMAIL_CONFIG,
DB_CONFIG
)
# === Configuration and Setup ===
def setup_logging():
"""Configure logging for the application."""
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
return logging.getLogger('DiscordDigest')
def init_streamlit():
"""Initialize Streamlit configuration and session state."""
st.set_page_config(
page_title="Discord Digest Control Panel",
layout="wide"
)
# Initialize session state variables
session_vars = ['message_cache', 'last_refresh', 'bot_process', 'scheduler_process']
for var in session_vars:
if var not in st.session_state:
st.session_state[var] = None
# === Bot Management ===
class BotManager:
@staticmethod
def check_status():
"""Check if the bot is running and connected."""
try:
process_running = (
st.session_state.bot_process is not None and
st.session_state.bot_process.poll() is None
)
if process_running:
importlib.reload(discord_bot)
return discord_bot.check_connection_status()
return False
except Exception as e:
logger.error(f"Error checking bot status: {e}", exc_info=True)
return False
@staticmethod
def start():
"""Start the Discord bot process."""
try:
if not BotManager.check_status():
python_executable = sys.executable
st.session_state.bot_process = subprocess.Popen(
[python_executable, 'discord_bot.py'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
time.sleep(5)
if st.session_state.bot_process.poll() is None:
if discord_bot.check_connection_status():
return True
BotManager.stop()
else:
_, stderr = st.session_state.bot_process.communicate()
logger.error(f"Bot failed to start. Error: {stderr}")
st.session_state.bot_process = None
return False
except Exception as e:
logger.error(f"Error starting bot: {e}", exc_info=True)
return False
@staticmethod
def stop():
"""Stop the Discord bot process."""
try:
if BotManager.check_status():
st.session_state.bot_process.send_signal(signal.SIGTERM)
for _ in range(10):
if st.session_state.bot_process.poll() is not None:
break
time.sleep(1)
if st.session_state.bot_process.poll() is None:
st.session_state.bot_process.kill()
st.session_state.bot_process.wait()
st.session_state.bot_process = None
st.session_state.message_cache = None
st.session_state.last_refresh = None
return True
except Exception as e:
logger.error(f"Error stopping bot: {e}", exc_info=True)
if st.session_state.bot_process:
try:
st.session_state.bot_process.kill()
st.session_state.bot_process = None
except:
pass
return False
# === Data Management ===
class DataManager:
@staticmethod
def load_messages(days=1):
"""Load messages from the database with caching."""
try:
cache_age = (
datetime.now() - st.session_state.last_refresh
if st.session_state.last_refresh else None
)
if (st.session_state.message_cache is not None and
cache_age and cache_age.seconds < 300):
return st.session_state.message_cache
conn = sqlite3.connect('messages.db')
cutoff_date = datetime.utcnow() - timedelta(days=days)
query = """
SELECT content, author, timestamp, channel_id
FROM messages
WHERE timestamp >= ?
ORDER BY timestamp DESC
"""
df = pd.read_sql_query(
query,
conn,
params=(cutoff_date.strftime('%Y-%m-%d %H:%M:%S'),)
)
conn.close()
st.session_state.message_cache = df
st.session_state.last_refresh = datetime.now()
return df
except Exception as e:
logger.error(f"Error loading messages: {e}", exc_info=True)
return pd.DataFrame(columns=['content', 'author', 'timestamp', 'channel_id'])
# === Page Components ===
class PageComponents:
@staticmethod
def show_dashboard():
"""Render the dashboard page."""
st.title("Discord Digest Dashboard")
if st.button("🔄 Refresh Data"):
st.session_state.message_cache = None
col1, col2, col3 = st.columns(3)
try:
df = DataManager.load_messages(1)
df_week = DataManager.load_messages(7)
with col1:
st.metric("Messages Today", len(df))
with col2:
st.metric("Messages This Week", len(df_week))
with col3:
unique_authors = df_week['author'].nunique() if not df_week.empty else 0
st.metric("Active Users", unique_authors)
PageComponents._render_activity_charts(df_week)
except Exception as e:
logger.error(f"Error in dashboard: {e}", exc_info=True)
st.error(f"Error loading dashboard: {str(e)}")
@staticmethod
def _render_activity_charts(df_week):
"""Render activity charts for the dashboard."""
if not df_week.empty:
df_week['date'] = pd.to_datetime(df_week['timestamp']).dt.date
daily_counts = df_week.groupby('date').size().reset_index(name='count')
fig = px.line(daily_counts, x='date', y='count',
title='Daily Message Count')
st.plotly_chart(fig, use_container_width=True)
user_counts = df_week['author'].value_counts().head(5)
fig2 = px.bar(x=user_counts.index, y=user_counts.values,
title='Most Active Users',
labels={'x': 'User', 'y': 'Messages'})
st.plotly_chart(fig2, use_container_width=True)
@staticmethod
def show_message_viewer():
"""Render the message viewer page."""
st.title("Message Viewer")
days = st.slider("Select time range (days)", 1, 30, 1)
df = DataManager.load_messages(days)
search_term = st.text_input("Search messages")
if search_term and not df.empty:
df = df[df['content'].str.contains(search_term, case=False, na=False)]
PageComponents._render_message_table(df)
@staticmethod
def _render_message_table(df):
"""Render paginated message table with export option."""
ROWS_PER_PAGE = 50
total_pages = (len(df) // ROWS_PER_PAGE) + (1 if len(df) % ROWS_PER_PAGE > 0 else 0)
if total_pages > 0:
page_num = st.number_input('Page', min_value=1, max_value=total_pages, value=1)
start_idx = (page_num - 1) * ROWS_PER_PAGE
end_idx = start_idx + ROWS_PER_PAGE
st.dataframe(df.iloc[start_idx:end_idx], height=500)
if st.button("Export to CSV"):
csv = df.to_csv(index=False)
st.download_button(
"Download CSV",
csv,
"discord_messages.csv",
"text/csv"
)
else:
st.info("No messages found")
# === Main Application ===
def main():
"""Main application entry point."""
logger = setup_logging()
load_dotenv()
init_streamlit()
# Sidebar setup
st.sidebar.title("Discord Digest Controls")
st.sidebar.subheader("Connect to the Discord Server")
if st.sidebar.button("Connect"):
with st.spinner("Starting bot..."):
if BotManager.start():
st.sidebar.success("Bot connected successfully!")
else:
st.sidebar.error("Failed to connect bot!")
# Page navigation
page = st.sidebar.selectbox(
"Select Page",
["Dashboard", "Message Viewer", "Email Controls", "Settings"]
)
# Page routing
if page == "Dashboard":
PageComponents.show_dashboard()
elif page == "Message Viewer":
PageComponents.show_message_viewer()
elif page == "Email Controls":
PageComponents.show_email_controls()
elif page == "Settings":
PageComponents.show_settings()
# Footer
st.sidebar.markdown("---")
st.sidebar.markdown("Discord Digest Bot v2.0")
if __name__ == "__main__":
main()