refactor(bot): add logging and flow tweaks
This commit is contained in:
+17
-3
@@ -1,5 +1,9 @@
|
||||
import logging
|
||||
|
||||
from typing import Set, List
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ProcessedPostsManager:
|
||||
def __init__(self, file_path: str = "processed.txt"):
|
||||
@@ -9,9 +13,16 @@ class ProcessedPostsManager:
|
||||
def _load(self) -> Set[str]:
|
||||
try:
|
||||
with open(self.file_path, "r", encoding="utf-8") as processed:
|
||||
return {line.strip() for line in processed if line.strip()}
|
||||
ids = {line.strip() for line in processed if line.strip()}
|
||||
logger.info(f"Загружено {len(ids)} обработанных постов из {self.file_path}.")
|
||||
return ids
|
||||
|
||||
except:
|
||||
except FileNotFoundError:
|
||||
logger.info(f"Файл {self.file_path} не найден, начинаем с пустого списка.")
|
||||
return set()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при загрузке {self.file_path}: {e}. Начинаем с пустого списка.")
|
||||
return set()
|
||||
|
||||
def _save(self):
|
||||
@@ -31,9 +42,12 @@ class ProcessedPostsManager:
|
||||
self.processed_posts.add(str_id)
|
||||
with open(self.file_path, "a", encoding="utf-8") as f:
|
||||
f.write(str_id + "\n")
|
||||
logger.debug(f"Пост {post_id} отмечен как обработанный.")
|
||||
|
||||
def add_existing_posts(self, post_ids: List[int]):
|
||||
before = len(self.processed_posts)
|
||||
for post_id in post_ids:
|
||||
self.processed_posts.add(str(post_id))
|
||||
|
||||
self._save()
|
||||
added = len(self.processed_posts) - before
|
||||
logger.info(f"Добавлено {added} новых записей в список обработанных (всего: {len(self.processed_posts)}).")
|
||||
|
||||
Reference in New Issue
Block a user