diff --git a/scrapers/routes.py b/scrapers/routes.py index 5b32319..4b86dd5 100644 --- a/scrapers/routes.py +++ b/scrapers/routes.py @@ -575,10 +575,28 @@ async def block_torrent(block_data: schemas.BlockTorrent): torrent_stream.is_blocked = True try: await torrent_stream.save() + # Send Telegram notification + if settings.telegram_bot_token: + metadata = await MediaFusionMetaData.get_motor_collection().find_one( + {"_id": torrent_stream.meta_id}, projection={"title": 1, "type": 1} + ) + title = metadata.get("title") + meta_type = metadata.get("type") + poster = f"{settings.poster_host_url}/poster/{meta_type}/{torrent_stream.meta_id}.jpg" + await telegram_notifier.send_block_notification( + info_hash=block_data.info_hash, + meta_id=torrent_stream.meta_id, + title=title, + meta_type=meta_type, + poster=poster, + torrent_name=torrent_stream.torrent_name, + ) + except Exception as e: raise HTTPException( status_code=500, detail=f"Failed to block torrent: {str(e)}" ) + return {"status": f"Torrent {block_data.info_hash} has been successfully blocked."} @@ -614,12 +632,19 @@ async def migrate_id(migrate_data: schemas.MigrateID): await MediaFusionMetaData.get_motor_collection().delete_one( {"_id": migrate_data.mediafusion_id} ) - if migrate_data.media_type == "series": - await get_series_data_by_id(migrate_data.imdb_id) - else: - await get_movie_data_by_id(migrate_data.imdb_id) await update_meta_stream(migrate_data.imdb_id) + # Send notification + if settings.telegram_bot_token: + new_poster = f"{settings.poster_host_url}/poster/{migrate_data.media_type}/{migrate_data.imdb_id}.jpg" + await telegram_notifier.send_migration_notification( + old_id=migrate_data.mediafusion_id, + new_id=migrate_data.imdb_id, + title=old_title, + meta_type=migrate_data.media_type, + poster=new_poster, + ) + return { "status": f"Successfully migrated {migrate_data.mediafusion_id} to {migrate_data.imdb_id}." } diff --git a/utils/telegram_bot.py b/utils/telegram_bot.py index 4e90dca..f258acb 100644 --- a/utils/telegram_bot.py +++ b/utils/telegram_bot.py @@ -73,14 +73,73 @@ class TelegramNotifier: # Add block link message += f"\n\n[🚫 Block Torrent]({block_url})" + await self._send_photo_message(poster, message) + + async def send_block_notification( + self, + info_hash: str, + meta_id: str, + title: str, + meta_type: str, + poster: str, + torrent_name: str, + ): + """Send notification when a torrent is blocked""" + if not self.enabled: + logger.warning("Telegram notifications are disabled. Check bot token.") + return + + meta_id_data = ( + f"*IMDb*: [{meta_id}](https://www.imdb.com/title/{meta_id}/)\n" + if meta_id.startswith("tt") + else f"Meta ID: {meta_id}\n" + ) + + message = ( + f"🚫 Torrent Blocked\n\n" + f"*Title*: {title}\n" + f"*Type*: {meta_type.title()}\n" + f"{meta_id_data}" + f"*Torrent Name*: `{torrent_name}`\n" + f"*Info Hash*: `{info_hash}`\n" + f"*Poster*: [View]({poster})" + ) + + await self._send_photo_message(poster, message) + + async def send_migration_notification( + self, + old_id: str, + new_id: str, + title: str, + meta_type: str, + poster: str, + ): + """Send notification when an ID is migrated""" + if not self.enabled: + logger.warning("Telegram notifications are disabled. Check bot token.") + return + + message = ( + f"🔄 ID Migration Complete\n\n" + f"*Title*: {title}\n" + f"*Type*: {meta_type.title()}\n" + f"*Old ID*: `{old_id}`\n" + f"*New IMDb ID*: [{new_id}](https://www.imdb.com/title/{new_id}/)\n" + f"*Poster*: [View]({poster})" + ) + + await self._send_photo_message(poster, message) + + async def _send_photo_message(self, photo_url: str, message: str): + """Send a message with photo, falling back to text-only if photo fails""" try: async with aiohttp.ClientSession() as session: - # Send a single message with photo and caption async with session.post( f"{self.base_url}/sendPhoto", json={ "chat_id": self.chat_id, - "photo": poster, + "photo": photo_url, "caption": message, "parse_mode": "Markdown", },