Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -674,63 +674,18 @@ def send_notifications_for_events(new_events):
|
|
| 674 |
|
| 675 |
total_sent = 0
|
| 676 |
|
| 677 |
-
|
| 678 |
-
import threading
|
| 679 |
-
import time
|
| 680 |
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
now = time.time()
|
| 689 |
-
|
| 690 |
-
# 馃敟 STRICT: 1 mail per second globally
|
| 691 |
-
if now - last_sent_time < 1:
|
| 692 |
-
time.sleep(1 - (now - last_sent_time))
|
| 693 |
-
|
| 694 |
-
last_sent_time = time.time()
|
| 695 |
-
|
| 696 |
-
return send_email(mail, subject, html)
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
# ================= REPLACE LOOP =================
|
| 700 |
-
|
| 701 |
-
max_workers = 5
|
| 702 |
-
batch_size = 20
|
| 703 |
-
total_sent = 0
|
| 704 |
-
|
| 705 |
-
for i in range(0, len(users), batch_size):
|
| 706 |
-
batch = users[i:i + batch_size]
|
| 707 |
-
|
| 708 |
-
print(f"馃摝 Sending batch {i//batch_size + 1} ({len(batch)} users)")
|
| 709 |
-
|
| 710 |
-
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
| 711 |
-
futures = []
|
| 712 |
-
|
| 713 |
-
for uid, mail, unsubscribe_token in batch:
|
| 714 |
-
|
| 715 |
-
html = f"""
|
| 716 |
-
<h2>馃摙 New BIP Events Alert</h2>
|
| 717 |
-
<p>{len(new_events)} new events have been added:</p>
|
| 718 |
-
{events_html}
|
| 719 |
-
<hr>
|
| 720 |
-
<a href="{HF_URL}?unsubscribe={unsubscribe_token}">Unsubscribe</a>
|
| 721 |
-
"""
|
| 722 |
-
|
| 723 |
-
futures.append(
|
| 724 |
-
executor.submit(rate_limited_send, mail, subject, html)
|
| 725 |
-
)
|
| 726 |
-
|
| 727 |
-
for future in as_completed(futures):
|
| 728 |
-
if future.result():
|
| 729 |
-
total_sent += 1
|
| 730 |
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
time.sleep(3)
|
| 734 |
|
| 735 |
cur.close()
|
| 736 |
db.close()
|
|
|
|
| 674 |
|
| 675 |
total_sent = 0
|
| 676 |
|
| 677 |
+
for uid, mail, unsubscribe_token in users:
|
|
|
|
|
|
|
| 678 |
|
| 679 |
+
html = f"""
|
| 680 |
+
<h2>馃摙 New BIP Events Alert</h2>
|
| 681 |
+
<p>{len(new_events)} new events have been added:</p>
|
| 682 |
+
{events_html}
|
| 683 |
+
<hr>
|
| 684 |
+
<a href="{HF_URL}?unsubscribe={unsubscribe_token}">Unsubscribe</a>
|
| 685 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 686 |
|
| 687 |
+
if send_email(mail, subject, html):
|
| 688 |
+
total_sent += 1
|
|
|
|
| 689 |
|
| 690 |
cur.close()
|
| 691 |
db.close()
|