From 67a94e390a31be4fa56a01bc9e97acf61f06f53c Mon Sep 17 00:00:00 2001 From: missytake Date: Fri, 1 Sep 2023 17:01:03 +0200 Subject: [PATCH] analyze user's files --- .gitignore | 1 + src/remember_remember_bot/commands.py | 14 ++++++++ src/remember_remember_bot/loop.py | 20 +++++------ src/remember_remember_bot/util.py | 50 +++++++++++++++++++++++++++ tests/test_loop.py | 8 ----- tests/test_util.py | 40 +++++++++++++++++++++ 6 files changed, 113 insertions(+), 20 deletions(-) create mode 100644 src/remember_remember_bot/util.py delete mode 100644 tests/test_loop.py create mode 100644 tests/test_util.py diff --git a/.gitignore b/.gitignore index 7c463ec..6bf3d41 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ __pycache__/ /.tox/ /build/ /venv/ +remember.db/ diff --git a/src/remember_remember_bot/commands.py b/src/remember_remember_bot/commands.py index 0fd6e17..affb6ff 100644 --- a/src/remember_remember_bot/commands.py +++ b/src/remember_remember_bot/commands.py @@ -1,7 +1,21 @@ import deltachat +from remember_remember_bot.util import ( + chat_is_active, + get_file_path, + get_lines_from_file, +) def setup(email: str, password: str, db: str, debug: bool) -> deltachat.Account: ac = deltachat.account.Account(db_path=db) ac.run_account(email, password, show_ffi=debug) return ac + + +def remind_user(user: deltachat.Contact): + chat = user.get_chat() + messages = [msg.text for msg in chat.get_messages()] + if chat_is_active(messages): + file_path = get_file_path(messages) + lines = get_lines_from_file(file_path) + print(lines) diff --git a/src/remember_remember_bot/loop.py b/src/remember_remember_bot/loop.py index c1fecd5..2b79a3d 100644 --- a/src/remember_remember_bot/loop.py +++ b/src/remember_remember_bot/loop.py @@ -1,24 +1,20 @@ import time -import datetime import deltachat +from remember_remember_bot.commands import remind_user +from remember_remember_bot.util import check_new_day, update_day + def loop(ac: deltachat.Account): current_day = 0 while True: if check_new_day(current_day): - pass + for user in ac.get_contacts(): + if ( + user.get_chat() + ): # does this return None if there is no existing chat? + remind_user(user) print(current_day) current_day = update_day() time.sleep(1) - - -def check_new_day(current_day): - if current_day != datetime.datetime.now().day: - return True - return False - - -def update_day(): - return datetime.datetime.now().day diff --git a/src/remember_remember_bot/util.py b/src/remember_remember_bot/util.py new file mode 100644 index 0000000..9a25bfd --- /dev/null +++ b/src/remember_remember_bot/util.py @@ -0,0 +1,50 @@ +import datetime + + +def check_new_day(current_day): + if current_day != datetime.datetime.now().day: + return True + return False + + +def update_day(): + return datetime.datetime.now().day + + +def chat_is_active(messages: [str]) -> bool: + """Check if a user activated the bot. + + :param messages: the text of all messages with the user + :return: True if the user wants to receive reminders. + """ + active = False + for message_text in messages: + if "/start" in message_text: + active = True + if "/stop" in message_text: + active = False + return active + + +def get_file_path(messages: [str]) -> str: + """Get the file path from the user's chat. + + :param messages: the text of all messages with the user + :return: the file path with their reminders + """ + file_path = None + for message_text in messages: + if "file" in message_text: + file_path = message_text.split("file ", 1)[1] + return file_path + + +def get_lines_from_file(file_path: str) -> [str]: + """Get the lines from the file with the user's reminders. + + :param file_path: the path to the file with the user's reminders + :return: a list with all the user's reminders + """ + with open(file_path, "r", encoding="utf-8") as f: + lines = f.readlines() + return [line.strip("\n") for line in lines] diff --git a/tests/test_loop.py b/tests/test_loop.py deleted file mode 100644 index 061f8f0..0000000 --- a/tests/test_loop.py +++ /dev/null @@ -1,8 +0,0 @@ -from remember_remember_bot.loop import check_new_day, update_day - - -def test_update_day() -> None: - current_day = 0 - assert check_new_day(current_day) - current_day = update_day() - assert not check_new_day(current_day) diff --git a/tests/test_util.py b/tests/test_util.py new file mode 100644 index 0000000..6badae8 --- /dev/null +++ b/tests/test_util.py @@ -0,0 +1,40 @@ +from remember_remember_bot.util import ( + check_new_day, + update_day, + chat_is_active, + get_file_path, +) +import pytest + + +def test_update_day(): + current_day = 0 + assert check_new_day(current_day) + current_day = update_day() + assert not check_new_day(current_day) + + +@pytest.mark.parametrize( + ("messages", "active"), + [ + (["/start"], True), + (["/start", "/stop"], False), + (["/start", "/stop", "Let's get this party started"], False), + (["/start", "/stop", "/start"], True), + ], +) +def test_chat_is_active(messages, active): + assert chat_is_active(messages) == active + + +@pytest.mark.parametrize( + ("messages", "file_path"), + [ + (["/file /home/user/test"], "/home/user/test"), + (["/file /home/user/test", "/stop"], "/home/user/test"), + (["I stored your reminders at the file /home/user/test"], "/home/user/test"), + (["/start", "/stop", "/start"], None), + ], +) +def test_get_file_path(messages: [], file_path: str): + assert get_file_path(messages) == file_path