-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.py
More file actions
467 lines (419 loc) · 15.2 KB
/
mail.py
File metadata and controls
467 lines (419 loc) · 15.2 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
import os
import json
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime
from pathlib import Path
import markdown
from dotenv import load_dotenv
load_dotenv()
SENT_LOG_FILE = Path("sent_summaries.json")
QUEUE_FILE = Path("summary_queue.json")
def load_sent_log():
if SENT_LOG_FILE.exists():
with open(SENT_LOG_FILE, 'r') as f:
return json.load(f)
return {}
def save_sent_log(log_data):
with open(SENT_LOG_FILE, 'w') as f:
json.dump(log_data, indent=2, fp=f)
def load_queue():
if QUEUE_FILE.exists():
with open(QUEUE_FILE, 'r') as f:
return json.load(f)
return {"queue": [], "sent_order": []}
def save_queue(queue_data):
with open(QUEUE_FILE, 'w') as f:
json.dump(queue_data, indent=2, fp=f)
def get_summary_hash(content):
import hashlib
return hashlib.md5(content.encode()).hexdigest()
def get_all_summaries():
"""Get all SUMMARY.md files with their metadata."""
docs_dir = Path("github_docs")
if not docs_dir.exists():
return {}
summaries = {}
for summary_file in docs_dir.rglob("SUMMARY.md"):
repo_name = summary_file.parent.name
with open(summary_file, 'r', encoding='utf-8') as f:
content = f.read()
summaries[repo_name] = {
"path": str(summary_file),
"hash": get_summary_hash(content),
"content": content,
"modified_at": summary_file.stat().st_mtime
}
return summaries
def build_queue():
"""Build/update the send queue based on available summaries."""
all_summaries = get_all_summaries()
queue_data = load_queue()
existing_queue = queue_data.get("queue", [])
sent_order = queue_data.get("sent_order", [])
existing_queue_repos = {item["repo_name"] for item in existing_queue}
all_repo_names = set(all_summaries.keys())
# Detect new repos not in queue or sent_order
new_repos = all_repo_names - existing_queue_repos - set(sent_order)
# Add new repos to front of queue
new_entries = [
{"repo_name": r, "added_at": datetime.now().isoformat()}
for r in sorted(new_repos, key=lambda r: all_summaries[r]["modified_at"], reverse=True)
]
final_queue = new_entries + existing_queue
# If queue is empty, start new cycle with all repos
if not final_queue:
print("Queue empty - starting new cycle with all repos.")
sent_order_repos = [r for r in sent_order if r in all_summaries]
final_queue = [
{"repo_name": r, "added_at": datetime.now().isoformat()}
for r in sent_order_repos
]
sent_order.clear()
queue_data["queue"] = final_queue
queue_data["sent_order"] = sent_order
save_queue(queue_data)
print(f"Queue status: {len(final_queue)} repos pending")
if new_repos:
print(f"New repos: {list(new_repos)}")
return queue_data
def get_next_to_send():
"""Get the next repo from queue to send."""
all_summaries = get_all_summaries()
queue_data = build_queue()
queue = queue_data.get("queue", [])
if not queue:
print("No summaries to send.")
return None, None, queue_data
next_item = queue[0]
repo_name = next_item["repo_name"]
if repo_name not in all_summaries:
print(f"Repo {repo_name} not found, skipping.")
queue_data["queue"].pop(0)
save_queue(queue_data)
return None, None, queue_data
return all_summaries[repo_name]["content"], repo_name, queue_data
def enhance_markdown_content(content: str) -> str:
"""Enhance markdown content with special formatting."""
lines = content.split('\n')
enhanced_lines = []
in_feature_section = False
for line in lines:
# Add icons to main sections
if line.startswith('## 1. Project Overview'):
line = '## 🎯 Project Overview & Purpose'
elif line.startswith('## 2. Key Features'):
line = '## ⚡ Key Features & Capabilities'
in_feature_section = True
elif line.startswith('## 3. Architecture'):
line = '## 🏗️ Architecture & Technical Design'
in_feature_section = False
elif line.startswith('## 4.') or line.startswith('## 5.'):
in_feature_section = False
# Enhance "What it does" style sections
if '**What it does**:' in line:
line = '<div class="info-box">💡 <strong>What it does:</strong> ' + line.split('**What it does**:')[1]
elif '**Why it\'s useful**:' in line or "**Why it's useful**:" in line:
line = '</div><div class="info-box">✨ <strong>Why it\'s useful:</strong> ' + line.split(':')[1]
elif '**When to use it**:' in line:
line = '</div><div class="info-box">🎯 <strong>When to use it:</strong> ' + line.split('**When to use it**:')[1]
elif '**Limitations**:' in line:
line = '</div><div class="warning-box">⚠️ <strong>Limitations:</strong> ' + line.split('**Limitations**:')[1] + '</div>'
# Add icons to subsections
if line.startswith('### What is this project'):
line = '### 📖 What is this project and what problem does it solve?'
elif line.startswith('### Target audience'):
line = '### 👥 Target audience and use cases'
elif line.startswith('### Project history'):
line = '### 📅 Project history, maturity, and current status'
elif line.startswith('### Key differentiators'):
line = '### 🌟 Key differentiators from similar projects'
elif line.startswith('### Major Features'):
line = '### 🚀 Major Features'
elif line.startswith('### Overall system architecture'):
line = '### 🔧 Overall system architecture'
elif line.startswith('### Design patterns'):
line = '### 🎨 Design patterns and principles used'
elif line.startswith('### Data flow'):
line = '### 🔄 Data flow and component interactions'
enhanced_lines.append(line)
return '\n'.join(enhanced_lines)
def create_html_email(summary_content: str, repo_name: str, queue_position: int, queue_total: int) -> str:
"""Create an attractive HTML email with improved styling."""
enhanced_content = enhance_markdown_content(summary_content)
# Convert markdown to HTML
html_content = markdown.markdown(
enhanced_content,
extensions=['extra', 'codehilite', 'tables', 'toc']
)
html = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 900px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}}
.container {{
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
overflow: hidden;
}}
.header {{
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
text-align: center;
}}
.header h1 {{
margin: 0;
font-size: 28px;
font-weight: 600;
}}
.header p {{
margin: 10px 0 0 0;
opacity: 0.9;
font-size: 14px;
}}
.progress {{
background: rgba(255,255,255,0.2);
padding: 8px 16px;
border-radius: 20px;
font-size: 13px;
margin-top: 10px;
display: inline-block;
}}
.content {{
padding: 40px;
}}
.repo-badge {{
display: inline-block;
background-color: #f0f0f0;
color: #333;
padding: 8px 16px;
border-radius: 20px;
font-size: 14px;
font-weight: 600;
margin-bottom: 20px;
}}
h1 {{
color: #1a202c;
font-size: 24px;
margin-top: 30px;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 3px solid #667eea;
}}
h2 {{
color: #2d3748;
font-size: 20px;
margin-top: 25px;
margin-bottom: 12px;
padding-left: 10px;
border-left: 4px solid #667eea;
}}
h3 {{
color: #4a5568;
font-size: 18px;
margin-top: 20px;
margin-bottom: 10px;
}}
h4 {{
color: #667eea;
font-size: 16px;
margin-top: 15px;
margin-bottom: 8px;
}}
ul, ol {{
margin: 10px 0;
padding-left: 25px;
}}
li {{
margin: 8px 0;
}}
code {{
background-color: #f7fafc;
padding: 2px 6px;
border-radius: 3px;
font-family: 'Courier New', monospace;
font-size: 0.9em;
color: #e53e3e;
}}
pre {{
background-color: #2d3748;
color: #f7fafc;
padding: 16px;
border-radius: 6px;
overflow-x: auto;
margin: 15px 0;
}}
pre code {{
background-color: transparent;
color: inherit;
padding: 0;
}}
blockquote {{
border-left: 4px solid #667eea;
padding-left: 20px;
margin: 20px 0;
color: #4a5568;
background-color: #f7fafc;
padding: 15px 20px;
border-radius: 4px;
}}
.info-box {{
background-color: #ebf8ff;
border-left: 4px solid #4299e1;
padding: 15px;
margin: 15px 0;
border-radius: 4px;
}}
.warning-box {{
background-color: #fffaf0;
border-left: 4px solid #f6ad55;
padding: 15px;
margin: 15px 0;
border-radius: 4px;
}}
.success-box {{
background-color: #f0fff4;
border-left: 4px solid #48bb78;
padding: 15px;
margin: 15px 0;
border-radius: 4px;
}}
.footer {{
background-color: #f7fafc;
padding: 20px 40px;
text-align: center;
color: #718096;
font-size: 14px;
border-top: 1px solid #e2e8f0;
}}
.footer a {{
color: #667eea;
text-decoration: none;
}}
.footer a:hover {{
text-decoration: underline;
}}
hr {{
border: none;
border-top: 1px solid #e2e8f0;
margin: 30px 0;
}}
table {{
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}}
th, td {{
padding: 12px;
text-align: left;
border-bottom: 1px solid #e2e8f0;
}}
th {{
background-color: #f7fafc;
font-weight: 600;
color: #2d3748;
}}
strong {{
color: #2d3748;
}}
em {{
color: #4a5568;
font-style: italic;
}}
.divider {{
height: 2px;
background: linear-gradient(to right, transparent, #667eea, transparent);
margin: 30px 0;
border: none;
}}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>📚 Daily Documentation Summary</h1>
<p>{datetime.now().strftime("%B %d, %Y")}</p>
</div>
<div class="content">
<div class="repo-badge">🔖 Repository: {repo_name}</div>
<hr class="divider">
{html_content}
</div>
<div class="footer">
<p>🤖 Auto-generated by GitHub Actions</p>
<p><a href="https://github.com/{repo_name}">📂 View Repository</a></p>
<p style="margin-top: 10px; font-size: 12px;">Generated with ❤️ by your documentation automation system</p>
</div>
</div>
</body>
</html>
"""
return html
def send_email(subject: str, html_content: str):
"""Send email using SMTP."""
smtp_api = os.getenv('SMTP_API')
smtp_host = os.getenv('SMTP_HOST')
smtp_port = int(os.getenv('SMTP_PORT', '587'))
gmail_user = os.getenv('GMAIL_USER')
recipient = os.getenv('RECIPIENT_EMAIL')
if not all([smtp_api, smtp_host, gmail_user, recipient]):
print("Missing required environment variables for email")
return False
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = gmail_user
msg['To'] = recipient
# Attach HTML content
html_part = MIMEText(html_content, 'html')
msg.attach(html_part)
try:
with smtplib.SMTP(smtp_host, smtp_port) as server:
server.starttls()
server.login(gmail_user, smtp_api)
server.send_message(msg)
print(f"Email sent successfully to {recipient}")
return True
except Exception as e:
print(f"Failed to send email: {e}")
return False
def main():
print("Starting daily email summary task...")
summary_content, repo_name, queue_data = get_next_to_send()
if not summary_content or not repo_name:
print("Nothing to send today.")
return
print(f"Sending summary for: {repo_name}")
html_content = create_html_email(summary_content, repo_name, 0, 0)
subject = f"📚 Doc Summary: {repo_name}"
if send_email(subject, html_content):
# Update queue and sent_order
queue = queue_data.get("queue", [])
queue_data["queue"] = [i for i in queue if i["repo_name"] != repo_name]
queue_data["sent_order"].append(repo_name)
save_queue(queue_data)
# Log as sent
sent_log = load_sent_log()
sent_log[repo_name] = {
"hash": get_summary_hash(summary_content),
"sent_at": datetime.now().isoformat(),
}
save_sent_log(sent_log)
print(f"Sent and logged: {repo_name}")
else:
print(f"Failed to send: {repo_name}")
if __name__ == "__main__":
main()