Добавление тэга отмены автопубликации ВК-поста
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2025-10-31 16:36:22 +03:00
parent faa5d6dd6f
commit b760035af9
2 changed files with 20 additions and 3 deletions
+2
View File
@@ -55,6 +55,8 @@ RESPONDER_BOT_NAME = "posto_1638_bot"
# Tag to detect org info # Tag to detect org info
# Org info is normally long, publish without picture # Org info is normally long, publish without picture
ORG_MESSAGE_TAG = "#оргинфаЗиланткона" ORG_MESSAGE_TAG = "#оргинфаЗиланткона"
# tag to detect posts to not repost from VK
VK_NO_REPOST_TAG = "#окоЗиланта"
# Minutes delay before publishing an event # Minutes delay before publishing an event
+18 -3
View File
@@ -18,6 +18,7 @@ VK_ACCESS_TOKEN = os.getenv('VK_ACCESS_TOKEN', '')
VK_API_VERSION = os.getenv('VK_API_VERSION', '5.131') VK_API_VERSION = os.getenv('VK_API_VERSION', '5.131')
IS_EVENT = os.getenv('IS_EVENT', 'false').lower() in ('true', '1', 'yes', 'on') IS_EVENT = os.getenv('IS_EVENT', 'false').lower() in ('true', '1', 'yes', 'on')
AUTO_PUBLISH = os.getenv('AUTO_PUBLISH', 'false').lower() in ('true', '1', 'yes', 'on') AUTO_PUBLISH = os.getenv('AUTO_PUBLISH', 'false').lower() in ('true', '1', 'yes', 'on')
VK_NO_REPOST_TAG = os.getenv('VK_NO_REPOST_TAG', '') # Тег для отключения автопубликации
LOG_FILE = os.getenv('LOG_FILE', 'vk_loader.log') # Путь к лог-файлу LOG_FILE = os.getenv('LOG_FILE', 'vk_loader.log') # Путь к лог-файлу
LOG_PREFIX = "VK_loader" # Уникальный префикс для идентификации скрипта LOG_PREFIX = "VK_loader" # Уникальный префикс для идентификации скрипта
@@ -144,6 +145,14 @@ def save_to_database(posts):
new_posts_count = 0 new_posts_count = 0
for post in posts: for post in posts:
try: try:
# Проверка наличия тега VK_NO_REPOST_TAG в тексте поста
post_text = post.get('text', '')
should_auto_publish = AUTO_PUBLISH
if VK_NO_REPOST_TAG and VK_NO_REPOST_TAG in post_text:
# Если тег найден, отключаем автопубликацию независимо от AUTO_PUBLISH
should_auto_publish = False
# Добавляем поля is_event, marked_for_publication, shortname и action_number # Добавляем поля is_event, marked_for_publication, shortname и action_number
cursor.execute(''' cursor.execute('''
INSERT INTO posts ( INSERT INTO posts (
@@ -164,14 +173,20 @@ def save_to_database(posts):
int(post['poll_multiple']), int(post['poll_multiple']),
post['poll_end_date'], post['poll_end_date'],
IS_EVENT, IS_EVENT,
int(AUTO_PUBLISH), # Значение из .env int(should_auto_publish), # Значение с учетом проверки тега
None, # shortname - пока не используется, устанавливаем NULL None, # shortname - пока не используется, устанавливаем NULL
None # action_number - пока не используется, устанавливаем NULL None # action_number - пока не используется, устанавливаем NULL
)) ))
if cursor.rowcount > 0: if cursor.rowcount > 0:
new_posts_count += 1 new_posts_count += 1
# Логируем добавление нового поста # Логируем добавление нового поста с указанием причины отключения автопубликации
log_message(f"Добавлен новый пост: ID {post['vk_post_id']}, Дата: {post['published_at']}, Автопубликация: {'Да' if AUTO_PUBLISH else 'Нет'}") auto_publish_reason = 'Да'
if not should_auto_publish:
if VK_NO_REPOST_TAG and VK_NO_REPOST_TAG in post_text:
auto_publish_reason = 'Нет (найден тег ' + VK_NO_REPOST_TAG + ')'
else:
auto_publish_reason = 'Нет'
log_message(f"Добавлен новый пост: ID {post['vk_post_id']}, Дата: {post['published_at']}, Автопубликация: {auto_publish_reason}")
except mysql.connector.Error as err: except mysql.connector.Error as err:
error_msg = f"Ошибка при вставке поста {post['vk_post_id']}: {err}" error_msg = f"Ошибка при вставке поста {post['vk_post_id']}: {err}"
log_message(error_msg) log_message(error_msg)