diff --git a/README.md b/README.md index 8129615..10db5d2 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ The main idea of this project is an increasing of projects performance inside of scenario: "xpath" # path to file wuth messages list to be send - messages: "./messages.yml" + messages: messages.sample.yml # page that will be parsed url: "https://t.me/s/codex_team" diff --git a/messages.sample.yml b/messages.sample.yml new file mode 100644 index 0000000..f761053 --- /dev/null +++ b/messages.sample.yml @@ -0,0 +1,6 @@ +FIRST_TIME_MESSAGES: + - 'Надо что-нибудь опубликовать в канале' + - 'Пора что-нибудь поставить в телеграм' + - 'Пришло время опубликовать новый пост в канале' + - 'Нам надо опубликовать что-нибудь новое' + - 'Напишите что-нибудь для телеграма' diff --git a/src/messages_creator.py b/src/messages_creator.py new file mode 100644 index 0000000..2cc3d63 --- /dev/null +++ b/src/messages_creator.py @@ -0,0 +1,23 @@ +import random +from logging import getLogger +from yaml import safe_load +from os import path + +logger = getLogger("general") + + +def create_message(name_file_with_messages): + """A function that creates random message to notify + :param name_file_with_messages: it is a path if yaml file with messages + """ + message_name = path.join(path.dirname(path.dirname(path.abspath(__file__))), name_file_with_messages) + with open(message_name, 'r', encoding='utf-8') as message_file: + message = '' + try: + message_dict = safe_load(message_file.read()) + first_time_messages = message_dict.get("FIRST_TIME_MESSAGES") + message = random.choice(first_time_messages) + except: + logger.exception("Failed to load info from configuration file.") + return message + diff --git a/src/tasks/xpath.py b/src/tasks/xpath.py index 900289e..f6a354b 100644 --- a/src/tasks/xpath.py +++ b/src/tasks/xpath.py @@ -7,6 +7,7 @@ from src.settings import MONGO_CLIENT, DATABASE_NAME from src.tasks.base import BaseTask +from src.messages_creator import create_message class XpathTask(BaseTask): @@ -57,7 +58,7 @@ def __init__(self, name, schedule, notifier, scenario, **kwargs): :param params: A name, schedule, notifier, scenario of a task and dict with xpath and url. """ super().__init__(name, schedule, notifier, scenario, **kwargs) - self._arg_names += ["task_id", "max_secs_without_changes", "notify_url"] + self._arg_names += ["task_id", "max_secs_without_changes", "notify_url", "messages_url"] self.task_id: str = self.__get_hash(self.params['url'] + self.params['xpath']) def get_element(self, document: str): @@ -145,5 +146,5 @@ def run(self): return False if (datetime.now() - old_timestamp["timestamp"]).seconds >= self.max_secs_without_changes: notifier = self.notifier(self.notify_url) - notifier.notify("Hello") + notifier.notify(create_message(self.messages_url)) return True