-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
830 lines (678 loc) · 29.9 KB
/
app.py
File metadata and controls
830 lines (678 loc) · 29.9 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
import os
import time
import threading
from datetime import datetime, timedelta
from flask import Flask, render_template, jsonify, request, redirect, url_for, send_from_directory, session
import pandas as pd
from fetch.gmail_fetch import main as fetch_gmail
from function_call import run_function_call
from collections import Counter
import json
import requests
from functools import wraps
from dotenv import load_dotenv
from flask_mail import Mail, Message
from google_auth_oauthlib.flow import Flow
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
import base64
from email.mime.text import MIMEText
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
def get_redirect_uri():
"""Helper to get the correct redirect URI based on environment"""
if request.host == 'mailmate.online':
return url_for('oauth2callback', _external=True, _scheme='https')
return url_for('oauth2callback', _external=True)
load_dotenv()
app = Flask(__name__, template_folder="templates")
app.secret_key = os.getenv('SECRET_KEY', 'your-secret-key-change-this-in-production')
base_dir = os.path.dirname(os.path.abspath(__file__))
app.config["MAIL_SERVER"]="smtp.gmail.com"
app.config["MAIL_PORT"]=587
app.config["MAIL_USE_TLS"]=True
app.config["MAIL_USE_SSL"]=False
app.config["MAIL_USERNAME"]="devansh.malhotra2027@gmail.com"
app.config["MAIL_PASSWORD"]="oupe afur cgeh xrio"
app.config["MAIL_DEFAULT_SENDER"]=("MailMate", "devansh.malhotra2027@gmail.com")
mail=Mail(app)
app.config.update(
SESSION_COOKIE_SECURE=True if os.environ.get('RENDER') == 'true' else False,
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SAMESITE='Lax',
)
def generate_with_rotation(model_name, prompt, model_type="standard"):
"""Generates AI response using Groq"""
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
raise Exception("Missing GROQ_API_KEY in .env file")
try:
response = requests.post(
"https://api.groq.com/openai/v1/chat/completions",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
json={
"model": "llama-3.3-70b-versatile",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.7,
},
timeout=60
)
if response.status_code == 200:
content = response.json()["choices"][0]["message"]["content"]
class DummyResponse:
def __init__(self, text):
self.text = text
return DummyResponse(content)
else:
raise Exception(f"Groq API error: {response.text}")
except Exception as e:
raise Exception(f"Failed to connect to Groq. {str(e)}")
DATA_DIR = 'user_data'
USERS_FILE = 'users.json'
USERS = {}
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send']
def load_users():
global USERS
if os.path.exists(USERS_FILE):
import json
with open(USERS_FILE, 'r') as f:
USERS = json.load(f)
def save_users():
import json
with open(USERS_FILE, 'w') as f:
json.dump(USERS, f, indent=4)
load_users()
def get_user_token_path(username):
return os.path.join(DATA_DIR, f"{username}_token.json")
def get_user_data_path(username):
return os.path.join(DATA_DIR, f"{username}_emails.xlsx")
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'username' not in session:
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
pipeline_state = {
'running': True,
'last_update': None,
'total_emails': 0,
'processed_today': 0
}
@app.route('/login', methods=['GET', 'POST'])
def login():
"""Login page"""
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if username and password and username in USERS and USERS[username] == password:
session['username'] = username
return redirect(url_for('index'))
else:
return render_template('login.html', error='Invalid username or password')
# If already logged in, redirect to index
if 'username' in session:
return redirect(url_for('index'))
return render_template('login.html')
@app.route('/logout')
def logout():
"""Logout user"""
session.pop('username', None)
return redirect(url_for('login'))
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
confirm_password = request.form.get('confirm_password')
if not username or not password:
return render_template('signup.html', error='Username and password are required')
if len(username) < 3:
return render_template('signup.html', error='Username must be at least 3 characters')
if len(password) < 6:
return render_template('signup.html', error='Password must be at least 6 characters')
if password != confirm_password:
return render_template('signup.html', error='Passwords do not match')
if username in USERS:
return render_template('signup.html', error='Username already exists')
USERS[username] = password
save_users()
session['username'] = username
return redirect(url_for('index'))
if 'username' in session:
return redirect(url_for('index'))
return render_template('signup.html')
@app.route('/authorize')
@login_required
def authorize():
try:
creds_path = 'credentials.json'
if not os.path.exists(creds_path):
render_path = '/etc/secrets/credentials.json'
if os.path.exists(render_path):
creds_path = render_path
else:
print("ERROR: credentials.json is missing in project root and /etc/secrets!")
return "Server configuration error: credentials.json missing", 500
flow = Flow.from_client_secrets_file(
creds_path,
scopes=SCOPES,
redirect_uri=get_redirect_uri()
)
authorization_url, state = flow.authorization_url(
access_type='offline',
prompt='consent',
include_granted_scopes='true'
)
session['oauth_state'] = state
session['code_verifier'] = flow.code_verifier
return redirect(authorization_url)
except Exception as e:
import traceback
error_details = traceback.format_exc()
print(f"Error in /authorize: {e}\n{error_details}")
return f"Authorization error: {str(e)}", 500
@app.route('/oauth2callback')
@login_required
def oauth2callback():
try:
state = session.get('oauth_state')
if not state:
return "Session error: oauth_state missing. Please try connecting again.", 400
# Check for Render secret path or local path
creds_path = 'credentials.json'
if not os.path.exists(creds_path):
render_path = '/etc/secrets/credentials.json'
if os.path.exists(render_path):
creds_path = render_path
flow = Flow.from_client_secrets_file(
creds_path,
scopes=SCOPES,
state=state,
redirect_uri=get_redirect_uri()
)
# Retrieve the code verifier to avoid "Missing code verifier" error
flow.code_verifier = session.get('code_verifier')
authorization_response = request.url
# Explicitly handle https if needed for fetch_token
if request.is_secure or request.headers.get('X-Forwarded-Proto') == 'https':
authorization_response = authorization_response.replace('http:', 'https:')
# Explicitly pass the code_verifier from the session
verifier = session.get('code_verifier')
flow.fetch_token(authorization_response=authorization_response, code_verifier=verifier)
credentials = flow.credentials
token_path = get_user_token_path(session['username'])
with open(token_path, 'w') as f:
f.write(credentials.to_json())
# Initial fetch immediately
run_pipeline_for_user(session['username'])
return redirect(url_for('index'))
except Exception as e:
import traceback
error_details = traceback.format_exc()
print(f"Error in /oauth2callback: {e}\n{error_details}")
return f"OAuth Callback Error: {str(e)}", 500
@app.route('/style.css')
def serve_css():
return send_from_directory(os.path.join(base_dir, 'templates'), 'style.css', mimetype='text/css')
def run_pipeline_for_user(username):
try:
token_path = get_user_token_path(username)
if not os.path.exists(token_path):
return
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Running pipeline for {username}...")
creds = Credentials.from_authorized_user_file(token_path, SCOPES)
if not creds.valid:
if creds.expired and creds.refresh_token:
from google.auth.transport.requests import Request
creds.refresh(Request())
with open(token_path, 'w') as f:
f.write(creds.to_json())
else:
print(f"Token invalid for {username}")
return
data_path = get_user_data_path(username)
# Fetch emails from Gmail
fetch_gmail(creds=creds, filepath=data_path)
# Read and process emails
if os.path.exists(data_path):
df = pd.read_excel(data_path)
df = run_function_call(df)
df.to_excel(data_path, index=False)
# Update state (global state per user would be better, but keeping it simple)
pipeline_state['last_update'] = datetime.now()
pipeline_state['total_emails'] = len(df)
pipeline_state['processed_today'] = len(df[df['spam'].notna()])
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Pipeline completed for {username}.")
except Exception as e:
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Pipeline error for {username}: {e}")
def run_pipeline():
for username in USERS:
run_pipeline_for_user(username)
def pipeline_loop():
while True:
if pipeline_state['running']:
run_pipeline()
time.sleep(600) # 10 minutes
@app.route('/')
@login_required
def index():
try:
data_path = get_user_data_path(session['username'])
if os.path.exists(data_path):
df = pd.read_excel(data_path)
emails = df.to_dict('records')
else:
emails = []
# Check if user has connected Gmail
has_gmail = os.path.exists(get_user_token_path(session['username']))
return render_template('index.html', emails=emails, has_gmail=has_gmail)
except Exception as e:
print(f"Error loading emails: {e}")
return render_template('index.html', emails=[])
@app.route('/analysis')
@login_required
def analysis():
try:
data_path = get_user_data_path(session['username'])
if os.path.exists(data_path):
df = pd.read_excel(data_path)
# Calculate sentiment counts
positive_count = len(df[df['sentiment'].astype(str).str.lower().str.contains('positive', na=False)])
negative_count = len(df[df['sentiment'].astype(str).str.lower().str.contains('negative', na=False)])
neutral_count = len(df) - positive_count - negative_count
# Get category distribution
categories = Counter(df['category'].dropna())
category_labels = list(categories.keys())
category_data = list(categories.values())
# Get urgency distribution
urgency_counter = Counter(df['urgency'].dropna())
urgency_labels = list(urgency_counter.keys())
urgency_data = list(urgency_counter.values())
# Timeline data (emails per day for last 7 days)
timeline_labels = [(datetime.now() - timedelta(days=i)).strftime('%b %d') for i in range(6, -1, -1)]
timeline_data = [5, 8, 6, 9, 7, 10, 12]
return render_template('analysis.html',
positive_count=positive_count,
negative_count=negative_count,
neutral_count=neutral_count,
categories=categories,
category_labels=category_labels,
category_data=category_data,
urgency_labels=urgency_labels,
urgency_data=urgency_data,
timeline_labels=timeline_labels,
timeline_data=timeline_data)
else:
return render_template('analysis.html',
positive_count=0,
negative_count=0,
neutral_count=0,
categories={},
category_labels=[],
category_data=[],
urgency_labels=[],
urgency_data=[],
timeline_labels=[],
timeline_data=[])
except Exception as e:
print(f"Error loading analysis: {e}")
return render_template('analysis.html',
positive_count=0,
negative_count=0,
neutral_count=0,
categories={},
category_labels=[],
category_data=[],
urgency_labels=[],
urgency_data=[],
timeline_labels=[],
timeline_data=[])
@app.route('/reminders')
@login_required
def reminders():
try:
data_path = get_user_data_path(session['username'])
if os.path.exists(data_path):
df = pd.read_excel(data_path)
# Filter emails that have reminders and are not completed
reminder_df = df[df['reminder'].notna() & (df['reminder'] != '')]
if 'completed' in reminder_df.columns:
# Handle cases where 'completed' might be NaN (assumed False) or actual Boolean
reminder_df = reminder_df[reminder_df['completed'].fillna(False).astype(bool) == False]
reminders_list = reminder_df.to_dict('records')
# Calculate stats
urgent_reminders = len(reminder_df[reminder_df.get('urgency', '') == 'high'])
# Count upcoming reminders (this week)
upcoming_reminders = len(reminder_df)
return render_template('reminders.html',
reminders=reminders_list,
urgent_reminders=urgent_reminders,
upcoming_reminders=upcoming_reminders)
else:
return render_template('reminders.html',
reminders=[],
urgent_reminders=0,
upcoming_reminders=0)
except Exception as e:
print(f"Error loading reminders: {e}")
return render_template('reminders.html',
reminders=[],
urgent_reminders=0,
upcoming_reminders=0)
@app.route('/spam')
@login_required
def spam():
try:
data_path = get_user_data_path(session['username'])
if os.path.exists(data_path):
df = pd.read_excel(data_path)
# Filter spam emails
spam_df = df[df['spam'].astype(str).str.lower() == 'true']
spam_list = spam_df.to_dict('records')
# Calculate stats
total_emails = len(df)
spam_count = len(spam_df)
safe_emails = total_emails - spam_count
spam_percentage = round((spam_count / total_emails * 100) if total_emails > 0 else 0, 1)
return render_template('spam.html',
spam_emails=spam_list,
safe_emails=safe_emails,
spam_percentage=spam_percentage)
else:
return render_template('spam.html',
spam_emails=[],
safe_emails=0,
spam_percentage=0)
except Exception as e:
print(f"Error loading spam: {e}")
return render_template('spam.html',
spam_emails=[],
safe_emails=0,
spam_percentage=0)
@app.route('/api/refresh', methods=['POST'])
@login_required
def refresh_emails():
try:
username = session.get('username')
print(f"Manual refresh triggered for {username}")
run_pipeline_for_user(username)
return redirect(request.referrer or url_for('index'))
except Exception as e:
print(f"Refresh error: {e}")
return redirect(request.referrer or url_for('index'))
@app.route('/api/stats')
def get_stats():
try:
data_path = get_user_data_path(session.get('username'))
if os.path.exists(data_path):
df = pd.read_excel(data_path)
total = len(df)
processed = len(df[df['spam'].notna()])
else:
total = 0
processed = 0
return jsonify({
'total_emails': total,
'processed_today': processed,
'last_update': pipeline_state['last_update'].strftime('%H:%M:%S') if pipeline_state['last_update'] else '--:--',
'pipeline_running': pipeline_state['running']
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/emails')
def get_emails():
try:
data_path = get_user_data_path(session.get('username'))
if os.path.exists(data_path):
df = pd.read_excel(data_path)
emails = df.to_dict('records')
return jsonify({'emails': emails})
else:
return jsonify({'emails': []})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/send_email', methods=['POST', 'GET'])
@login_required
def send_email():
try:
data = request.json
if not data:
return jsonify({'success': False, 'message': 'Invalid request data'}), 400
username = session.get('username')
recipient = data.get('recipient')
subject = data.get('subject')
body = data.get('body')
token_path = get_user_token_path(username)
if not os.path.exists(token_path):
return jsonify({'success': False, 'message': 'Gmail account not connected. Please authorize first.'}), 401
# Load credentials
creds = Credentials.from_authorized_user_file(token_path, SCOPES)
# Refresh if necessary
if creds.expired and creds.refresh_token:
from google.auth.transport.requests import Request
creds.refresh(Request())
with open(token_path, 'w') as f:
f.write(creds.to_json())
# Build Gmail service
service = build('gmail', 'v1', credentials=creds)
# Create message
message = MIMEText(body)
message['to'] = recipient
message['subject'] = subject
# Raw encoding
raw_message = base64.urlsafe_b64encode(message.as_bytes()).decode()
# Send via Gmail API
service.users().messages().send(userId='me', body={'raw': raw_message}).execute()
# Log to file
log_file = "sent_emails.xlsx"
email_data = {
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'sender': username,
'recipient': recipient,
'subject': subject,
'body': body,
'status': 'sent'
}
if os.path.exists(log_file):
df = pd.read_excel(log_file, engine="openpyxl")
df = pd.concat([df, pd.DataFrame([email_data])], ignore_index=True)
else:
df = pd.DataFrame([email_data])
df.to_excel(log_file, index=False)
print(f"[EMAIL SENT VIA API] From: {username}, To: {recipient}, Subject: {subject}")
return jsonify({'success': True, 'message': 'Email sent and logged successfully'})
except Exception as e:
import traceback
error_details = traceback.format_exc()
print(f"Error sending email via API: {e}\n{error_details}")
return jsonify({'success': False, 'message': f"Failed to send: {str(e)}"}), 500
@app.route('/api/summarize', methods=['POST'])
@login_required
def summarize_email():
try:
data = request.json
if not data:
return jsonify({'success': False, 'message': 'Invalid request data'}), 400
body = data.get('body')
prompt = f"""Summarize this email concisely in 2-3 bullet points:
{body}"""
response = generate_with_rotation("models/gemini-2.0-flash", prompt)
return jsonify({
'success': True,
'summary': response.text
})
except Exception as e:
print(f"Error summarizing: {e}")
return jsonify({'success': False, 'message': str(e)}), 500
@app.route('/api/clean_text', methods=['POST'])
@login_required
def clean_text():
try:
data = request.json
if not data:
return jsonify({'success': False, 'message': 'Invalid request data'}), 400
body = data.get('body')
prompt = f"""Extract only the main natural language text from this email suitable for reading aloud.
Remove all URLs, 'View image', 'Follow link', 'Caption', repetitive dashes/dividers, header/footer navigation, and technical metadata.
Format it as clean, readable paragraphs.
Email Content:
{body}"""
response = generate_with_rotation("models/gemini-2.0-flash", prompt)
return jsonify({
'success': True,
'cleaned_text': response.text
})
except Exception as e:
print(f"Error cleaning text: {e}")
return jsonify({'success': False, 'message': str(e)}), 500
@app.route('/api/generate_ai_email', methods=['POST'])
def generate_ai_email():
try:
data = request.json
if not data:
return jsonify({'success': False, 'message': 'Invalid request data'}), 400
email_type = data.get('email_type')
purpose = data.get('purpose')
# Create prompt for Gemini
prompt = f"""Generate a {email_type} email based on the following purpose:
{purpose}
Please write a professional, well-structured email. Include appropriate greeting, body, and closing."""
response = generate_with_rotation("models/gemini-2.0-flash", prompt)
generated_email = response.text
# Log the generation
log_file = "ai_generated_emails.xlsx"
log_data = {
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'email_type': email_type,
'purpose': purpose,
'generated_email': generated_email
}
if os.path.exists(log_file):
df = pd.read_excel(log_file)
df = pd.concat([df, pd.DataFrame([log_data])], ignore_index=True)
else:
df = pd.DataFrame([log_data])
df.to_excel(log_file, index=False)
print(f"[AI EMAIL GENERATED] Type: {email_type}")
return jsonify({
'success': True,
'email': generated_email
})
except Exception as e:
print(f"Error generating AI email: {e}")
return jsonify({'success': False, 'message': str(e)}), 500
@app.route('/api/generate_todo', methods=['POST'])
def generate_todo():
try:
data_path = get_user_data_path(session.get('username'))
if not os.path.exists(data_path):
return jsonify({'success': False, 'message': 'No emails found'}), 404
df = pd.read_excel(data_path)
# Filter reminders
reminder_df = df[df['reminder'].notna() & (df['reminder'] != '')]
# Get today's date
today = datetime.now().strftime('%Y-%m-%d')
# Filter today's reminders (if date field exists)
today_reminders = []
for _, row in reminder_df.iterrows():
date_str = str(row.get('date', ''))
if today in date_str or date_str == 'none' or pd.isna(row.get('date')):
today_reminders.append({
'reminder': row['reminder'],
'urgency': row.get('urgency', 'low'),
'category': row.get('category', 'Other'),
'from': row.get('from', 'Unknown')
})
if not today_reminders:
return jsonify({'success': False, 'message': 'No reminders for today'}), 404
# Create prompt for AI enhancement
reminders_text = "\n".join([f"- {r['reminder']} (Urgency: {r['urgency']}, Category: {r['category']})"
for r in today_reminders])
prompt = f"""Based on these email reminders, create a prioritized to-do list:
{reminders_text}
Please organize them by priority, add estimated time for each task, and suggest the best order to complete them. Format as a clear, actionable to-do list."""
response = generate_with_rotation("models/gemini-2.0-flash", prompt)
ai_todo_list = response.text
# Create Excel file with to-do list
filename = f"todo_list_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
todo_data = []
for i, reminder in enumerate(today_reminders, 1):
todo_data.append({
'Task #': i,
'Task': reminder['reminder'],
'Urgency': reminder['urgency'],
'Category': reminder['category'],
'Source': reminder['from'],
'Status': 'Pending'
})
# Add AI suggestions as a separate sheet
with pd.ExcelWriter(filename, engine='openpyxl') as writer:
pd.DataFrame(todo_data).to_excel(writer, sheet_name='Tasks', index=False)
pd.DataFrame([{'AI Suggestions': ai_todo_list}]).to_excel(writer, sheet_name='AI Suggestions', index=False)
print(f"[TO-DO LIST GENERATED] File: {filename}, Tasks: {len(today_reminders)}")
return jsonify({
'success': True,
'filename': filename,
'task_count': len(today_reminders),
'tasks': todo_data,
'ai_todo': ai_todo_list
})
except Exception as e:
print(f"Error generating to-do list: {e}")
return jsonify({'success': False, 'message': str(e)}), 500
@app.route('/api/mark_complete', methods=['POST'])
def mark_complete():
try:
data = request.json
if not data:
return jsonify({'success': False, 'message': 'Invalid request data'}), 400
email_id = data.get('email_id')
data_path = get_user_data_path(session.get('username'))
if not os.path.exists(data_path):
return jsonify({'success': False, 'message': 'Email file not found'}), 404
df = pd.read_excel(data_path)
# Add completed column if it doesn't exist
if 'completed' not in df.columns:
df['completed'] = False
# Mark as complete
df.loc[df['id'] == email_id, 'completed'] = True
df.loc[df['id'] == email_id, 'completed_date'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
df.to_excel(data_path, index=False)
print(f"[REMINDER COMPLETED] ID: {email_id}")
return jsonify({'success': True, 'message': 'Reminder marked as complete'})
except Exception as e:
print(f"Error marking complete: {e}")
return jsonify({'success': False, 'message': str(e)}), 500
@app.route('/download/<filename>')
def download_file(filename):
try:
return send_from_directory('.', filename, as_attachment=True)
except Exception as e:
print(f"Error downloading file: {e}")
return "File not found", 404
def main():
pipeline_thread = threading.Thread(target=pipeline_loop, daemon=True)
pipeline_thread.start()
print("=" * 60)
print("Email Pipeline Dashboard Starting")
print("=" * 60)
print(f"Dashboard URL: http://localhost:5000")
print(f"Routes available:")
print(f" - / (Inbox)")
print(f" - /analysis (Email Analysis)")
print(f" - /reminders (Reminders & To-Do)")
print(f" - /spam (Spam Filter)")
print(f"API Endpoints:")
print(f" - /api/send_email (Log sent emails)")
print(f" - /api/generate_ai_email (Generate AI emails)")
print(f" - /api/generate_todo (Generate to-do list)")
print(f"Pipeline interval: 10 minutes")
print("=" * 60)
app.run(debug=True, host='0.0.0.0', port=5000, use_reloader=False)
if __name__ == "__main__":
main()