Skip to content

Commit

Permalink
Improved Article formatting to contain stats
Browse files Browse the repository at this point in the history
- The emails are now sent with information about the difficulty, source and read time.
  • Loading branch information
tfnribeiro committed Sep 17, 2024
1 parent 0b81dc8 commit 453cb50
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
13 changes: 10 additions & 3 deletions tools/send_subscription_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
from zeeguu.core.emailer.zeeguu_mailer import ZeeguuMailer
from zeeguu.core.model.search_subscription import SearchSubscription
from zeeguu.api.app import create_app
from zeeguu.core.util.reading_time_estimator import estimate_read_time

app = create_app()
app.app_context().push()

def format_article_info(article):
art_info = article.article_info()
return f"""<b>{art_info["title"]}</b>\n
<p>&nbsp;&nbsp;{estimate_read_time(art_info["metrics"]["word_count"])}min, {art_info["metrics"]["cefr_level"]}, <a href="{art_info["url"]}">read</a> on <a href="{article.feed.url.domain.domain_name}">{article.feed.url.domain.domain_name}</a></p>\n"""


def send_mail_new_articles_search(to_email, new_content_dict):
body = "\r\n".join(
Expand All @@ -19,8 +25,8 @@ def send_mail_new_articles_search(to_email, new_content_dict):
" "
])

for keyword, titles in new_content_dict.items():
body += "\r\n".join([" ", f"Search: '{keyword}': "] + [f"""- <a href="{url}">{t}</a>""" for t, url in titles]) + "\n"
for keyword, articles in new_content_dict.items():
body += "\r\n".join([" ", f"<h3>{keyword}</h3>", " "] + [format_article_info(a) for a in articles]) + "\n"

body += "\r\n".join([
" ",
Expand All @@ -31,6 +37,7 @@ def send_mail_new_articles_search(to_email, new_content_dict):
)

subject = f"New articles for {"'" + "','".join(new_content_dict.keys()) + "'"}"
print(body)
emailer = ZeeguuMailer(subject, body, to_email)
emailer.send()

Expand Down Expand Up @@ -59,7 +66,7 @@ def send_subscription_emails():
]
if new_articles_found:
updated_dict = user_subscriptions.get(user.email, {})
updated_dict[subscription.search.keywords] = [(article.title, article.url) for article in new_articles_found]
updated_dict[subscription.search.keywords] = [article for article in new_articles_found]
user_subscriptions[user.email] = updated_dict
for user_email, new_content_dict in user_subscriptions.items():
send_mail_new_articles_search(user_email, new_content_dict)
Expand Down
1 change: 1 addition & 0 deletions zeeguu/core/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from zeeguu.core.util.encoding import JSONSerializable, encode, encode_error
from zeeguu.core.util.hash import text_hash, password_hash
from zeeguu.core.util.time import get_server_time_utc
from zeeguu.core.util.reading_time_estimator import estimate_read_time
9 changes: 9 additions & 0 deletions zeeguu/core/util/reading_time_estimator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import math


def estimate_read_time(word_count: int):
"""
Returns the estimated reading time in minutes, assuming
a reading rate of 160WPM.
"""
return math.ceil(word_count / 160)

0 comments on commit 453cb50

Please sign in to comment.