fix(telegram): async API calls & pagination
This commit is contained in:
+19
-14
@@ -67,13 +67,13 @@ class TelegramStarsBot:
|
||||
post_ids = 0
|
||||
for post in all_posts:
|
||||
post_id = post.get("post_id")
|
||||
has_comments = self.lolz_api.has_comments(post=post)
|
||||
has_comments = await self.lolz_api.has_comments(post=post)
|
||||
|
||||
if has_comments:
|
||||
self.processed_manager.mark_processed(post_id)
|
||||
post_ids += 1
|
||||
|
||||
logger.info(f"Добавлено {len(post_ids)} постов в список обработанных.")
|
||||
logger.info(f"Добавлено {post_ids} постов в список обработанных.")
|
||||
|
||||
else:
|
||||
logger.info("Не найдено постов для добавления в обработанные.")
|
||||
@@ -87,18 +87,20 @@ class TelegramStarsBot:
|
||||
for attempt in range(self.config.max_retries):
|
||||
try:
|
||||
if message_id is None:
|
||||
history = await self.client.get_chat_history(
|
||||
fetched_id = None
|
||||
async for msg in self.client.get_chat_history(
|
||||
chat_id=channel, limit=1
|
||||
)
|
||||
):
|
||||
fetched_id = msg.id
|
||||
break
|
||||
|
||||
if len(history) == 0:
|
||||
if fetched_id is None:
|
||||
logger.error(
|
||||
f"Не удалось найти сообщения в канале https://t.me/{channel}"
|
||||
)
|
||||
return False
|
||||
|
||||
message = history[0]
|
||||
message_id = message.id
|
||||
message_id = fetched_id
|
||||
|
||||
await self.client.send_paid_reaction(
|
||||
chat_id=channel,
|
||||
@@ -111,8 +113,9 @@ class TelegramStarsBot:
|
||||
return True
|
||||
|
||||
except FloodWait as e:
|
||||
logger.warning(f"FloodWait: необходимо подождать {e.x + 2} секунд.")
|
||||
await asyncio.sleep(e.x + 2)
|
||||
wait_time = getattr(e, "value", getattr(e, "x", 10)) + 2
|
||||
logger.warning(f"FloodWait: необходимо подождать {wait_time} секунд.")
|
||||
await asyncio.sleep(wait_time)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
@@ -138,7 +141,7 @@ class TelegramStarsBot:
|
||||
logger.info(f"Найден новый пост для обработки: ID {post_id}")
|
||||
|
||||
if self.config.skip_commented:
|
||||
if await self.lolz_api.has_comments(post_id):
|
||||
if await self.lolz_api.has_comments(post=post):
|
||||
logger.info(
|
||||
f"Пост {post_id} уже имеет комментарии. Пропускаю обработку."
|
||||
)
|
||||
@@ -189,13 +192,17 @@ class TelegramStarsBot:
|
||||
logger.info(
|
||||
f"Проверка новых постов в теме {self.config.lolz_thread_id} начиная со страницы {self.start_page}..."
|
||||
)
|
||||
posts = await self.lolz_api.get_thread_posts(
|
||||
posts, last_page = await self.lolz_api.get_thread_posts(
|
||||
self.config.lolz_thread_id, self.start_page
|
||||
)
|
||||
|
||||
if posts:
|
||||
for post in posts:
|
||||
await self._process_single_post(post)
|
||||
# Next cycle starts from the last page that had posts —
|
||||
# new posts always appear there or on a new page beyond it,
|
||||
# never on earlier pages.
|
||||
self.start_page = last_page
|
||||
else:
|
||||
logger.info("Новых постов для обработки не найдено.")
|
||||
|
||||
@@ -216,9 +223,7 @@ class TelegramStarsBot:
|
||||
if is_first_login:
|
||||
logger.info("Сессия Telegram не найдена. Запускаю процесс входа...")
|
||||
else:
|
||||
choice = self.config.default_choice
|
||||
|
||||
if choice == "2":
|
||||
if self.config.default_choice == 2:
|
||||
logger.info("Выбран режим парсинга существующих постов.")
|
||||
self.client = Client(
|
||||
name=self.SESSION_NAME,
|
||||
|
||||
Reference in New Issue
Block a user