feat: Add AniDB image support for Telegram success notifications

- Extract AniDB ID from folder path patterns {anidb4-XXXX}
- Fetch and send anime images with rename_success Telegram messages
- Fallback to text-only if image unavailable
- Add aiohttp dependency for async HTTP requests
This commit is contained in:
2026-02-05 17:28:33 +07:00
parent 92f10b228c
commit bbcd9b7d9c
3 changed files with 48 additions and 8 deletions

View File

@@ -1,7 +1,10 @@
import asyncio
import os
import re
from typing import Optional
from telegram import Bot
from telegram.constants import ParseMode
import aiohttp
class TelegramNotifier:
def __init__(self, config):
@@ -18,17 +21,40 @@ class TelegramNotifier:
"""This method has been removed for security reasons."""
return None
def notify(self, event_type: str, message: str, details: dict = None):
def _extract_anidb_id(self, folder_path: str) -> Optional[str]:
"""Extract AniDB ID from folder path (e.g., {anidb4-18874} -> 18874)"""
match = re.search(r'\{anidb\d+-(\d+)\}', folder_path)
if match:
return match.group(1)
return None
async def _fetch_anidb_image(self, anidb_id: str) -> Optional[str]:
"""Fetch anime image URL from AniDB"""
# AniDB image URL pattern
image_url = f"https://img7.anidb.net/pics/anime/{anidb_id}.jpg"
# Verify the image exists
try:
async with aiohttp.ClientSession() as session:
async with session.head(image_url, allow_redirects=True) as response:
if response.status == 200:
return image_url
except Exception:
pass
return None
def notify(self, event_type: str, message: str, details: dict = None, folder_path: str = None):
"""Sync wrapper for async telegram send"""
if not self.enabled or event_type not in self.notify_on:
return
try:
asyncio.run(self._send_message(event_type, message, details))
asyncio.run(self._send_message(event_type, message, details, folder_path))
except Exception as e:
print(f"Failed to send Telegram notification: {e}")
async def _send_message(self, event_type: str, message: str, details: dict):
async def _send_message(self, event_type: str, message: str, details: dict, folder_path: str = None):
emoji_map = {
'no_match': '',
'copy_error': '📁',
@@ -49,11 +75,24 @@ class TelegramNotifier:
details_text = '\n'.join([f"• *{k}*: `{fmt(v)}`" for k, v in details.items()])
text += f"\n\n*Details:*\n{details_text}"
# Add memory content for rename_success events
if event_type == 'rename_success':
pass
try:
# Fetch and send image for rename_success events
image_path = None
if event_type == 'rename_success' and folder_path:
anidb_id = self._extract_anidb_id(folder_path)
if anidb_id:
image_url = await self._fetch_anidb_image(anidb_id)
if image_url:
# Send message with photo
await self.bot.send_photo(
chat_id=self.chat_id,
photo=image_url,
caption=text,
parse_mode=ParseMode.MARKDOWN
)
return
# Fallback to text-only message
await self.bot.send_message(
chat_id=self.chat_id,
text=text,

View File

@@ -263,7 +263,7 @@ class FileProcessor:
'folder': str(folder),
'rule_id': rule['id'],
'output_preview': result.stdout[:200] if result.stdout else ''
})
}, folder_path=str(folder))
except subprocess.TimeoutExpired:
msg = f"Renamer timeout for {folder}"

View File

@@ -1,6 +1,7 @@
watchdog>=3.0.0
flask>=2.3.0
python-telegram-bot>=20.0
aiohttp>=3.9.0
toml>=0.10.2
Werkzeug>=2.3.0
requests>=2.32.5