This commit is contained in:
+43
-9
@@ -19,6 +19,14 @@ VK_API_VERSION = os.getenv('VK_API_VERSION', '5.131')
|
||||
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')
|
||||
VK_NO_REPOST_TAG = os.getenv('VK_NO_REPOST_TAG', '') # Тег для отключения автопубликации
|
||||
# Минимум букв (isalpha), чтобы пост не считался пустым (видео без описания и т.п.)
|
||||
try:
|
||||
VK_POST_MIN_LETTERS = int((os.getenv('VK_POST_MIN_LETTERS') or '5').strip().strip('"').strip("'"))
|
||||
except ValueError:
|
||||
VK_POST_MIN_LETTERS = 5
|
||||
if VK_POST_MIN_LETTERS < 0:
|
||||
VK_POST_MIN_LETTERS = 0
|
||||
EMPTY_POST_SHORTNAME = "- = Пустой пост = -"
|
||||
LOG_FILE = os.getenv('LOG_FILE', 'vk_loader.log') # Путь к лог-файлу
|
||||
LOG_PREFIX = "VK_loader" # Уникальный префикс для идентификации скрипта
|
||||
|
||||
@@ -86,6 +94,12 @@ def get_vk_posts():
|
||||
log_message(error_msg)
|
||||
raise Exception(error_msg)
|
||||
|
||||
|
||||
def count_letters(text):
|
||||
"""Количество букв в тексте (Unicode isalpha), без цифр и знаков."""
|
||||
return sum(1 for c in (text or "") if c.isalpha())
|
||||
|
||||
|
||||
def process_post(post):
|
||||
"""Извлекает необходимые данные из поста VK, включая обработку репостов"""
|
||||
post_id = post['id']
|
||||
@@ -144,11 +158,15 @@ def process_post(post):
|
||||
|
||||
# Формируем текст поста: если это репост, добавляем префикс и информацию об оригинале
|
||||
post_text = source_post.get('text', '')
|
||||
own_text = post.get('text', '')
|
||||
letter_count = count_letters(post_text)
|
||||
if is_repost:
|
||||
letter_count += count_letters(own_text)
|
||||
# Если у репоста есть свой текст, добавляем его перед текстом оригинала
|
||||
if post.get('text', '').strip():
|
||||
post_text = f"{post['text']}\n\n---\n\n{post_text}"
|
||||
if own_text.strip():
|
||||
post_text = f"{own_text}\n\n---\n\n{post_text}"
|
||||
post_text = repost_prefix + post_text + repost_info
|
||||
is_empty_text = letter_count < VK_POST_MIN_LETTERS
|
||||
|
||||
return {
|
||||
'vk_post_id': post_id,
|
||||
@@ -161,7 +179,9 @@ def process_post(post):
|
||||
'poll_options': poll_options,
|
||||
'poll_multiple': poll_multiple,
|
||||
'poll_end_date': poll_end_date,
|
||||
'is_repost': is_repost
|
||||
'is_repost': is_repost,
|
||||
'is_empty_text': is_empty_text,
|
||||
'letter_count': letter_count,
|
||||
}
|
||||
|
||||
def save_to_database(posts):
|
||||
@@ -175,9 +195,14 @@ def save_to_database(posts):
|
||||
try:
|
||||
# Проверка наличия тега VK_NO_REPOST_TAG в тексте поста
|
||||
post_text = post.get('text', '')
|
||||
is_empty_text = post.get('is_empty_text', False)
|
||||
should_auto_publish = AUTO_PUBLISH
|
||||
shortname = None
|
||||
|
||||
if VK_NO_REPOST_TAG and VK_NO_REPOST_TAG in post_text:
|
||||
if is_empty_text:
|
||||
should_auto_publish = False
|
||||
shortname = EMPTY_POST_SHORTNAME
|
||||
elif VK_NO_REPOST_TAG and VK_NO_REPOST_TAG in post_text:
|
||||
# Если тег найден, отключаем автопубликацию независимо от AUTO_PUBLISH
|
||||
should_auto_publish = False
|
||||
|
||||
@@ -201,16 +226,21 @@ def save_to_database(posts):
|
||||
int(post['poll_multiple']),
|
||||
post['poll_end_date'],
|
||||
IS_EVENT,
|
||||
int(should_auto_publish), # Значение с учетом проверки тега
|
||||
None, # shortname - пока не используется, устанавливаем NULL
|
||||
None # action_number - пока не используется, устанавливаем NULL
|
||||
int(should_auto_publish),
|
||||
shortname,
|
||||
None # action_number
|
||||
))
|
||||
if cursor.rowcount > 0:
|
||||
new_posts_count += 1
|
||||
# Логируем добавление нового поста с указанием причины отключения автопубликации
|
||||
auto_publish_reason = 'Да'
|
||||
if not should_auto_publish:
|
||||
if VK_NO_REPOST_TAG and VK_NO_REPOST_TAG in post_text:
|
||||
if is_empty_text:
|
||||
auto_publish_reason = (
|
||||
f'Нет (пустой текст: {post.get("letter_count", 0)} букв '
|
||||
f'< {VK_POST_MIN_LETTERS})'
|
||||
)
|
||||
elif VK_NO_REPOST_TAG and VK_NO_REPOST_TAG in post_text:
|
||||
auto_publish_reason = 'Нет (найден тег ' + VK_NO_REPOST_TAG + ')'
|
||||
else:
|
||||
auto_publish_reason = 'Нет'
|
||||
@@ -246,7 +276,11 @@ def vk_load_10():
|
||||
Возвращает количество добавленных постов
|
||||
"""
|
||||
# Стартовая информация
|
||||
start_msg = f"Старт скрипта | База: {DB_CONFIG['database']}@{DB_CONFIG['host']} | IS_EVENT: {IS_EVENT} | AUTO_PUBLISH: {AUTO_PUBLISH}"
|
||||
start_msg = (
|
||||
f"Старт скрипта | База: {DB_CONFIG['database']}@{DB_CONFIG['host']} | "
|
||||
f"IS_EVENT: {IS_EVENT} | AUTO_PUBLISH: {AUTO_PUBLISH} | "
|
||||
f"VK_POST_MIN_LETTERS: {VK_POST_MIN_LETTERS}"
|
||||
)
|
||||
print(start_msg)
|
||||
log_message(start_msg)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user