From 453cb5087744a42547f8b0081dd149a254a32fa2 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Tue, 17 Sep 2024 10:59:45 +0200 Subject: [PATCH] Improved Article formatting to contain stats - The emails are now sent with information about the difficulty, source and read time. --- tools/send_subscription_emails.py | 13 ++++++++++--- zeeguu/core/util/__init__.py | 1 + zeeguu/core/util/reading_time_estimator.py | 9 +++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 zeeguu/core/util/reading_time_estimator.py diff --git a/tools/send_subscription_emails.py b/tools/send_subscription_emails.py index b0fd90a9..13986989 100644 --- a/tools/send_subscription_emails.py +++ b/tools/send_subscription_emails.py @@ -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"""{art_info["title"]}\n +

  {estimate_read_time(art_info["metrics"]["word_count"])}min, {art_info["metrics"]["cefr_level"]}, read on {article.feed.url.domain.domain_name}

\n""" + def send_mail_new_articles_search(to_email, new_content_dict): body = "\r\n".join( @@ -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"""- {t}""" for t, url in titles]) + "\n" + for keyword, articles in new_content_dict.items(): + body += "\r\n".join([" ", f"

{keyword}

", " "] + [format_article_info(a) for a in articles]) + "\n" body += "\r\n".join([ " ", @@ -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() @@ -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) diff --git a/zeeguu/core/util/__init__.py b/zeeguu/core/util/__init__.py index dc00aeac..e594112f 100644 --- a/zeeguu/core/util/__init__.py +++ b/zeeguu/core/util/__init__.py @@ -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 diff --git a/zeeguu/core/util/reading_time_estimator.py b/zeeguu/core/util/reading_time_estimator.py new file mode 100644 index 00000000..feee43e3 --- /dev/null +++ b/zeeguu/core/util/reading_time_estimator.py @@ -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)