Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions messages.sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FIRST_TIME_MESSAGES:
- 'Надо что-нибудь опубликовать в канале'
- 'Пора что-нибудь поставить в телеграм'
- 'Пришло время опубликовать новый пост в канале'
- 'Нам надо опубликовать что-нибудь новое'
- 'Напишите что-нибудь для телеграма'
23 changes: 23 additions & 0 deletions src/messages_creator.py
Original file line number Diff line number Diff line change
@@ -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):
Copy link
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
: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

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