Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions messages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FIRST_TIME_MESSAGES:
['Надо что-нибудь опубликовать в канале',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

по моему массив в yml можно задать покрасивее. Через дефисы -

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можешь переименовать этот файл в messages.sample.yml плз
По аналогии с конфигом

'Пора что-нибудь поставить в телеграм',
'Пришло время опубликовать новый пост в канале',
'Нам надо опубликовать что-нибудь новое',
'В канале ${daysCount} дней ничего не было, поставьте пост',
'Напишите что-нибудь для телеграма']
22 changes: 22 additions & 0 deletions src/messages_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

параметр не описан

"""A function that creates random message to notify
"""
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

5 changes: 3 additions & 2 deletions src/tasks/xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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