check if a line has the same day + month as today

This commit is contained in:
missytake 2023-09-03 13:49:07 +02:00
parent 190e6cf863
commit 0f4dbb0b07
3 changed files with 44 additions and 1 deletions

View file

@ -3,6 +3,7 @@ from remember_remember_bot.util import (
chat_is_active, chat_is_active,
get_file_path, get_file_path,
get_lines_from_file, get_lines_from_file,
today_lines,
) )
@ -17,7 +18,9 @@ def remind_chat(chat: deltachat.Chat):
if chat_is_active(messages): if chat_is_active(messages):
file_path = get_file_path(messages) file_path = get_file_path(messages)
lines = get_lines_from_file(file_path) lines = get_lines_from_file(file_path)
print(lines) message_text = "\n".join(today_lines(lines))
print(message_text)
chat.send_text(message_text)
def activate_chat(msg: deltachat.Message): def activate_chat(msg: deltachat.Message):

View file

@ -59,3 +59,25 @@ def get_lines_from_file(file_path: str) -> [str]:
except (FileNotFoundError, TypeError): except (FileNotFoundError, TypeError):
return [f"File not found: {file_path}"] return [f"File not found: {file_path}"]
return [line.strip("\n") for line in lines] return [line.strip("\n") for line in lines]
def today_lines(lines: [str]):
"""Out of several lines from the file, only return those with the same date as today.
:param lines: a list of lines with a date in the beginning.
:return: a list of lines, all from today.
"""
result = []
for line in lines:
try:
day = int(line.split(".")[0])
month = int(line.split(".")[1])
except ValueError:
print(f"wrong format: {line}")
continue
if (
day == datetime.datetime.now().day
and month == datetime.datetime.now().month
):
result.append(line)
return result

View file

@ -3,8 +3,10 @@ from remember_remember_bot.util import (
update_day, update_day,
chat_is_active, chat_is_active,
get_file_path, get_file_path,
today_lines,
) )
import pytest import pytest
import datetime
def test_update_day(): def test_update_day():
@ -53,3 +55,19 @@ def test_get_file_path(messages: [], tmp_file_path: str):
"%file_path%", tmp_file_path "%file_path%", tmp_file_path
) )
assert get_file_path(messages) == tmp_file_path assert get_file_path(messages) == tmp_file_path
def test_today_lines():
day = datetime.datetime.now().day
month = datetime.datetime.now().month
year = datetime.datetime.now().year
lines = [
f"{day}.{month}.{year}\tToday I had a great day!",
f"{day}.{month}.2020\tI didn't go outside all day :) no appointments",
"03.13.2024\tNice how they invented a 13th month extra for the Satanists!",
]
result = today_lines(lines)
print(result)
assert len(result) == 2
assert result[0] == lines[0]
assert result[1] == lines[1]