Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
357 lines (295 loc) · 11.8 KB
/
Copy pathserver.py
File metadata and controls
357 lines (295 loc) · 11.8 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
import subprocess
import threading
from flask import Flask, jsonify, send_file, request
from flask_cors import CORS
import os
import json
from pathlib import Path
import logging
import imaplib
import requests
from dotenv import load_dotenv
import smtplib
from email.mime.text import MIMEText
def notify_ntfy(success: bool, count: int = 0, error: str = None) -> None:
"""Send a push notification via ntfy.sh."""
topic = os.getenv("NTFY_TOPIC")
if not topic:
log.warning("NTFY_TOPIC not set — skipping push notification")
return
if success:
title = "Digest ready"
message = f"{count} newsletter(s) résumée(s)"
tags = "white_check_mark"
priority = "default"
else:
title = "Erreur pipeline"
message = error or "Une erreur est survenue"
tags = "x"
priority = "high"
try:
requests.post(
f"https://ntfy.sh/{topic}",
headers={
"Title": title,
"Tags": tags,
"Priority": priority,
},
data=message.encode("utf-8"),
timeout=10,
)
log.info("ntfy notification sent: %s", title)
except Exception as exc:
log.error("ntfy notification failed: %s", exc)
def notify_email(success: bool, count: int = 0, error: str = None) -> None:
"""Send a summary email via Proton Mail Bridge SMTP."""
smtp_host = os.getenv("IMAP_HOST", "127.0.0.1")
smtp_port = int(os.getenv("SMTP_PORT", 1025))
username = os.getenv("PROTON_USERNAME")
password = os.getenv("PROTON_BRIDGE_PASSWORD")
recipient = os.getenv("NOTIFY_EMAIL")
if not all([username, password, recipient]):
log.warning("SMTP credentials incomplete — skipping email notification")
return
if success:
subject = f"Digest prêt — {count} newsletter(s) résumée(s)"
body = (
f"Le pipeline newsletter-digest s'est terminé avec succès.\n\n"
f"{count} newsletter(s) ont été résumées et sont disponibles dans le digest."
)
else:
subject = "Erreur pipeline newsletter-digest"
body = (
f"Le pipeline newsletter-digest a rencontré une erreur.\n\n"
f"Détail : {error or 'inconnu'}"
)
msg = MIMEText(body, "plain", "utf-8")
msg["Subject"] = subject
msg["From"] = username
msg["To"] = recipient
try:
with smtplib.SMTP(smtp_host, smtp_port) as smtp:
smtp.login(username, password)
smtp.sendmail(username, recipient, msg.as_string())
log.info("Email notification sent to %s", recipient)
except Exception as exc:
log.error("Email notification failed: %s", exc)
load_dotenv()
log = logging.getLogger(__name__)
app = Flask(__name__)
CORS(app)
# --- Pipeline state ---
pipeline_state = {
"status": "idle", # idle | running | done | error
"error": None,
}
state_lock = threading.Lock()
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DIGEST_PATH = os.path.join(BASE_DIR, "output", "index.html")
# Ensure output directory exists
os.makedirs(os.path.join(BASE_DIR, "output"), exist_ok=True)
current_process = {"proc": None}
def run_pipeline():
"""Run export_emails.py then summarize_newsletters.py sequentially."""
with state_lock:
pipeline_state["status"] = "running"
pipeline_state["error"] = None
# Clear stale progress file from previous run immediately
progress_path = Path(BASE_DIR) / "output" / "progress.json"
progress_path.unlink(missing_ok=True)
# Clear previous digest and index
(Path(BASE_DIR) / "output" / "index.html").unlink(missing_ok=True)
(Path(BASE_DIR) / "output" / "index.json").unlink(missing_ok=True)
# Clear previous .eml files
for eml_file in (Path(BASE_DIR) / "emails").glob("*.eml"):
eml_file.unlink()
scripts = [
["python3", os.path.join(BASE_DIR, "export_emails.py")],
["python3", os.path.join(BASE_DIR, "summarize_newsletters.py"),
"--eml-dir", os.path.join(BASE_DIR, "emails"),
"--output", DIGEST_PATH],
]
for script in scripts:
proc = subprocess.Popen(script, cwd=BASE_DIR)
current_process["proc"] = proc
proc.wait() # blocks until script finishes or is killed
current_process["proc"] = None
if proc.returncode not in (0, -9, -15): # -9/-15 = killed signals
error_msg = f"Échec : {os.path.basename(script[1])}"
with state_lock:
pipeline_state["status"] = "error"
pipeline_state["error"] = error_msg
notify_ntfy(success=False, error=error_msg)
# notify_email(success=False, error=error_msg) # Send notification e-mail
return
with state_lock:
# Only mark done if not already cancelled
if pipeline_state["status"] == "running":
pipeline_state["status"] = "done"
# Count summarized newsletters from index.json
count = 0
index_path = Path(BASE_DIR) / "output" / "index.json"
if index_path.exists():
try:
index = json.loads(index_path.read_text(encoding="utf-8"))
count = sum(1 for nl in index if nl.get("summary"))
except Exception:
pass
notify_ntfy(success=True, count=count)
# notify_email(success=True, count=count) # Send notification e-mail
@app.route("/health")
def health():
"""Basic health check."""
return jsonify({"status": "ok"})
@app.route("/run", methods=["POST"])
def run():
"""Trigger the pipeline. Rejects if already running."""
with state_lock:
if pipeline_state["status"] == "running":
return jsonify({"error": "Pipeline déjà en cours"}), 409
thread = threading.Thread(target=run_pipeline, daemon=True)
thread.start()
return jsonify({"status": "running"}), 202
@app.route("/status")
def status():
# Read progress file written by summarize_newsletters.py if it exists
progress_path = Path(BASE_DIR) / "output" / "progress.json"
progress_str = None
progress_total = None
if pipeline_state["status"] == "running" and progress_path.exists():
try:
data = json.loads(progress_path.read_text(encoding="utf-8"))
current = data.get("current", 0)
total = data.get("total", 0)
subject = data.get("subject", "")
# Expose total immediately even before first summary
progress_total = total
progress_str = f"[{current}/{total}] {subject}" if current > 0 else None
except Exception:
pass
return jsonify({
"status": pipeline_state["status"],
"error": pipeline_state.get("error"),
"progress": progress_str,
"total": progress_total,
})
@app.route("/cancel", methods=["POST"])
def cancel():
"""Kill the running pipeline thread immediately."""
with state_lock:
if pipeline_state["status"] != "running":
return jsonify({"error": "Aucun pipeline en cours"}), 409
# subprocess.run is blocking — we track the process to kill it
if current_process["proc"] is not None:
current_process["proc"].kill()
current_process["proc"] = None
print("Pipeline cancelled by user.")
# Clean up progress file
progress_path = Path(BASE_DIR) / "output" / "progress.json"
progress_path.unlink(missing_ok=True)
with state_lock:
pipeline_state["status"] = "idle"
pipeline_state["error"] = None
return jsonify({"status": "idle"})
@app.route("/index")
def index_json():
"""Serve the generated index.json for the React frontend."""
index_path = os.path.join(BASE_DIR, "output", "index.json")
if not os.path.exists(index_path):
return jsonify({"error": "index.json non disponible"}), 404
return send_file(index_path, mimetype="application/json")
@app.route("/digest")
def digest():
"""Serve the generated HTML digest."""
if not os.path.exists(DIGEST_PATH):
return jsonify({"error": "Digest non disponible"}), 404
return send_file(DIGEST_PATH)
@app.route("/email", methods=["DELETE"])
def delete_email():
"""
Delete selected newsletters:
1. Remove local .eml files
2. Move messages to Trash on Proton Mail via IMAP
3. Update index.json and uid_map.json
"""
data = request.get_json()
if not data or "ids" not in data or "uids" not in data:
return jsonify({"error": "Missing ids or uids"}), 400
ids = data["ids"] # list of relative .eml file paths
uids = data["uids"] # list of IMAP UIDs (integers)
# ── 1. Delete local .eml files
deleted_files = []
for eml_id in ids:
eml_path = Path(BASE_DIR) / eml_id
if eml_path.exists():
eml_path.unlink()
deleted_files.append(eml_path.name)
log.info("Deleted local file: %s", eml_path.name)
# ── 2. Move messages to Trash via IMAP
imap_errors = []
if uids:
try:
conn = imaplib.IMAP4(
os.getenv("IMAP_HOST", "127.0.0.1"),
int(os.getenv("IMAP_PORT", 1143)),
)
conn.login(
os.getenv("PROTON_USERNAME"),
os.getenv("PROTON_BRIDGE_PASSWORD"),
)
folder = os.getenv("PROTON_FOLDER")
status, _ = conn.select(f'"{folder}"')
if status != "OK":
raise Exception(f"Cannot select folder: {folder}")
# Check if server supports MOVE command
capabilities = conn.capability()[1][0].decode().upper()
supports_move = "MOVE" in capabilities
# Build comma-separated UID list for IMAP command
uid_set = ",".join(str(u) for u in uids)
if supports_move:
# Atomic move — preferred
conn.uid("move", uid_set, "Trash")
log.info("Moved UIDs %s to Trash (MOVE)", uid_set)
else:
# Fallback: copy to Trash, mark as deleted, expunge
conn.uid("copy", uid_set, "Trash")
conn.uid("store", uid_set, "+FLAGS", "\\Deleted")
conn.expunge()
log.info("Moved UIDs %s to Trash (COPY+EXPUNGE)", uid_set)
conn.logout()
except Exception as exc:
log.error("IMAP error: %s", exc)
imap_errors.append(str(exc))
# ── 3. Update index.json
index_path = Path(BASE_DIR) / "output" / "index.json"
if index_path.exists():
try:
index = json.loads(index_path.read_text(encoding="utf-8"))
index = [nl for nl in index if nl["id"] not in ids]
index_path.write_text(
json.dumps(index, ensure_ascii=False, indent=2),
encoding="utf-8",
)
log.info("index.json updated — %d entry/entries removed", len(ids))
except Exception as exc:
log.error("Failed to update index.json: %s", exc)
# ── 4. Update uid_map.json
uid_map_path = Path(BASE_DIR) / "emails" / "uid_map.json"
if uid_map_path.exists():
try:
uid_map = json.loads(uid_map_path.read_text(encoding="utf-8"))
deleted_names = {Path(eml_id).name for eml_id in ids}
uid_map = {k: v for k, v in uid_map.items() if k not in deleted_names}
uid_map_path.write_text(
json.dumps(uid_map, ensure_ascii=False, indent=2),
encoding="utf-8",
)
log.info("uid_map.json updated")
except Exception as exc:
log.error("Failed to update uid_map.json: %s", exc)
response = {"deleted": deleted_files}
if imap_errors:
response["imap_errors"] = imap_errors
return jsonify(response), 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)