analyze user's files
This commit is contained in:
parent
726417c2f7
commit
67a94e390a
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,3 +4,4 @@ __pycache__/
|
||||||
/.tox/
|
/.tox/
|
||||||
/build/
|
/build/
|
||||||
/venv/
|
/venv/
|
||||||
|
remember.db/
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,21 @@
|
||||||
import deltachat
|
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:
|
def setup(email: str, password: str, db: str, debug: bool) -> deltachat.Account:
|
||||||
ac = deltachat.account.Account(db_path=db)
|
ac = deltachat.account.Account(db_path=db)
|
||||||
ac.run_account(email, password, show_ffi=debug)
|
ac.run_account(email, password, show_ffi=debug)
|
||||||
return ac
|
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)
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,20 @@
|
||||||
import time
|
import time
|
||||||
import datetime
|
|
||||||
|
|
||||||
import deltachat
|
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):
|
def loop(ac: deltachat.Account):
|
||||||
current_day = 0
|
current_day = 0
|
||||||
while True:
|
while True:
|
||||||
if check_new_day(current_day):
|
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)
|
print(current_day)
|
||||||
current_day = update_day()
|
current_day = update_day()
|
||||||
time.sleep(1)
|
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
|
|
||||||
|
|
|
||||||
50
src/remember_remember_bot/util.py
Normal file
50
src/remember_remember_bot/util.py
Normal file
|
|
@ -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]
|
||||||
|
|
@ -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)
|
|
||||||
40
tests/test_util.py
Normal file
40
tests/test_util.py
Normal file
|
|
@ -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
|
||||||
Loading…
Reference in a new issue