web1 / scheduler.py
iq7se2's picture
Upload 9 files
d043a72 verified
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from database import get_all_bookmarks_for_notification, update_bookmark_chapter, get_manga_slug
from database import get_weekly_stats, build_weekly_report_text
from scraper import get_manga_chapters_api
from config import logger
import asyncio
async def check_new_chapters(bot):
bookmarks = get_all_bookmarks_for_notification()
for user_id, manga_url, manga_title, last_ch_id in bookmarks:
try:
chaps = await get_manga_chapters_api(manga_url)
if not chaps:
continue
latest_id = chaps[0]["id"]
if last_ch_id and latest_id == last_ch_id:
continue
update_bookmark_chapter(user_id, manga_url, latest_id)
slug = get_manga_slug(manga_url)
title = manga_title or slug
try:
await bot.send_message(
user_id,
f"πŸ”” *New Chapter Available!*\n\n"
f"πŸ“š *{title}*\n"
f"πŸ“– Latest: *{chaps[0]['title']}*\n\n"
f"Tap below to read:",
reply_markup=InlineKeyboardMarkup(inline_keyboard=[[
InlineKeyboardButton(
text=f"πŸ“₯ Read {chaps[0]['title']}",
callback_data=f"search_open_{slug}"
)
]])
)
except Exception as e:
logger.warning(f"Notification failed for user {user_id}: {e}")
await asyncio.sleep(0.3)
except Exception as e:
logger.error(f"Notification check error for {manga_url}: {e}")
async def send_weekly_report(bot, admins: set):
try:
stats = get_weekly_stats()
text = build_weekly_report_text(stats)
for admin_id in admins:
try:
await bot.send_message(admin_id, text)
except Exception as e:
logger.error(f"Weekly report to {admin_id} failed: {e}")
except Exception as e:
logger.error(f"Weekly report error: {e}")