#!/usr/bin/env python3 # Set up logging configuration before imports import logging class AsifFilter(logging.Filter): def filter(self, record): # Block messages from asif module or if they start with "Joined channel" return not (record.module == 'asif' or record.name.startswith('asif') or (isinstance(record.msg, str) and record.msg.startswith('Joined channel'))) # Set up base logging configuration logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%H:%M:%S' ) # Apply filter to root logger logging.getLogger().addFilter(AsifFilter()) # Also apply to asif's logger specifically asif_logger = logging.getLogger('asif') asif_logger.addFilter(AsifFilter()) asif_logger.setLevel(logging.CRITICAL) asif_logger.propagate = False from asif import Client # Set up error logging for asif error_handler = logging.FileHandler('ERROR.log') error_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) # Patch asif's Client class to use error logger def silent_client_init(self, *args, **kwargs): # Create a logger that writes to ERROR.log error_logger = logging.getLogger('asif.client') error_logger.addHandler(error_handler) error_logger.propagate = False # Don't send to console error_logger.setLevel(logging.INFO) # Capture all messages # Store the logger self._log = error_logger # Call the original __init__ original_init(self, *args, **kwargs) # Save original __init__ and replace it original_init = Client.__init__ Client.__init__ = silent_client_init import asyncio import re import aiohttp import json import time import argparse import yaml from pathlib import Path from typing import List, Optional # ANSI color codes class Colors: COLORS = [ '\033[94m', # BLUE '\033[96m', # CYAN '\033[92m', # GREEN '\033[93m', # YELLOW '\033[95m', # MAGENTA '\033[91m', # RED ] ENDC = '\033[0m' BOLD = '\033[1m' # Track used colors used_colors = {} # botname -> color mapping @classmethod def get_color_for_bot(cls, botname: str) -> str: """Get a consistent color for a bot based on its name.""" # If this bot already has a color, return it if botname in cls.used_colors: return cls.used_colors[botname] # If we still have unused colors, use the next available one unused_colors = [c for c in cls.COLORS if c not in cls.used_colors.values()] if unused_colors: color = unused_colors[0] else: # If we're out of unique colors, fall back to hash-based selection color = cls.COLORS[hash(botname) % len(cls.COLORS)] cls.used_colors[botname] = color return color class BotLoggerAdapter(logging.LoggerAdapter): # Class variables to track maximum lengths max_nick_length = 0 max_endpoint_length = 0 instances = [] # Keep track of all instances to update padding def __init__(self, logger, extra): super().__init__(logger, extra) botname = extra['botname'] nick, endpoint = botname.split('@') self.nick = nick self.endpoint = endpoint # Update max lengths (without ANSI codes) old_max_nick = BotLoggerAdapter.max_nick_length old_max_endpoint = BotLoggerAdapter.max_endpoint_length BotLoggerAdapter.max_nick_length = max( BotLoggerAdapter.max_nick_length, len(nick) ) BotLoggerAdapter.max_endpoint_length = max( BotLoggerAdapter.max_endpoint_length, len(endpoint) ) # If max lengths changed, update all existing instances if (old_max_nick != BotLoggerAdapter.max_nick_length or old_max_endpoint != BotLoggerAdapter.max_endpoint_length): for instance in BotLoggerAdapter.instances: instance.update_padding() # Add self to instances list BotLoggerAdapter.instances.append(self) # Initial padding calculation self.update_padding() def update_padding(self): """Update the colored botname with current padding requirements.""" # Right-align nick, then @ symbol, then colored endpoint nick_padding = " " * (BotLoggerAdapter.max_nick_length - len(self.nick)) endpoint_padding = " " * (BotLoggerAdapter.max_endpoint_length - len(self.endpoint)) self.colored_botname = f"{nick_padding}{self.nick}@{Colors.BOLD}{Colors.get_color_for_bot(self.nick+'@'+self.endpoint)}{self.endpoint}{Colors.ENDC}{endpoint_padding}" def process(self, msg, kwargs): return f'[{self.colored_botname}] {msg}', kwargs class IcecastBot: def __init__(self, config_path: Optional[str] = None): # Load config self.config = self.load_config(config_path) # Set up bot-specific logger self.logger = BotLoggerAdapter( logging.getLogger(__name__), {'botname': f'{self.config["irc"]["nick"]}@{self.config["stream"]["endpoint"]}'} ) # Initialize IRC bot with config self.bot = Client( host=self.config['irc']['host'], port=self.config['irc']['port'], user=self.config['irc']['user'], realname=self.config['irc']['realname'], nick=self.config['irc']['nick'] ) # Set up instance variables from config self.channel_name = self.config['irc']['channel'] self.stream_url = self.config['stream']['url'] self.stream_endpoint = self.config['stream']['endpoint'] self.current_song = "Unknown" self.reply = self.config['announce']['format'] self.ignore_patterns = self.config['announce']['ignore_patterns'] self.channel = None self.last_health_check = time.time() self.health_check_interval = self.config['stream']['health_check_interval'] self.setup_handlers() @staticmethod def load_config(config_path: Optional[str] = None) -> dict: """Load configuration from file and/or command line arguments.""" if config_path is None: config_path = Path(__file__).parent / 'config.yaml' # Load config file try: with open(config_path) as f: config = yaml.safe_load(f) except FileNotFoundError: # Create a temporary logger for config loading temp_logger = logging.getLogger(__name__) temp_logger.warning(f"Config file not found at {config_path}, using defaults") config = { 'irc': {}, 'stream': {}, 'announce': { 'format': "\x02Now playing:\x02 {song}", 'ignore_patterns': ['Unknown', 'Unable to fetch metadata'] } } return config def should_announce_song(self, song: str) -> bool: """Check if the song should be announced based on ignore patterns.""" return not any(pattern.lower() in song.lower() for pattern in self.ignore_patterns) def setup_handlers(self): @self.bot.on_connected() async def connected(): try: self.channel = await self.bot.join(self.channel_name) self.logger.info(f"Connected to IRC and joined {self.channel_name}") except Exception as e: self.logger.error(f"Error joining channel: {e}") asyncio.create_task(self.monitor_metadata()) @self.bot.on_join() async def on_join(channel): # Silently store the channel without logging if not self.channel: self.channel = channel @self.bot.on_message(re.compile("^!np")) async def now_playing(message): await message.reply(self.reply.format(song=self.current_song)) async def fetch_json_metadata(self): try: async with aiohttp.ClientSession() as session: url = f"{self.stream_url}/status-json.xsl" async with session.get(url) as response: if response.status == 200: data = await response.text() json_data = json.loads(data) if 'icestats' in json_data: sources = json_data['icestats'].get('source', []) if isinstance(sources, list): for src in sources: if src['listenurl'].endswith(f'{self.stream_endpoint}'): source = src else: source = sources title = source.get('title') or source.get('song') or source.get('current_song') if title: return title return "Unable to fetch metadata" except Exception as e: self.logger.error(f"Error fetching JSON metadata: {e}") return "Error fetching metadata" async def monitor_metadata(self): await asyncio.sleep(5) while True: try: cmd = [ 'curl', '-s', '-H', 'Icy-MetaData: 1', '--no-buffer', f"{self.stream_url}/{self.stream_endpoint}" ] process = await asyncio.create_subprocess_exec( *cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) self.logger.info("Started stream monitoring") buffer = b"" last_json_check = time.time() json_check_interval = 60 # Fallback interval if ICY updates fail while True: chunk = await process.stdout.read(8192) if not chunk: break buffer += chunk current_time = time.time() # Periodic health check if current_time - self.last_health_check >= self.health_check_interval: self.logger.info("Monitor status: Active - processing stream data") self.last_health_check = current_time # Look for metadata marker but fetch from JSON if b"StreamTitle='" in buffer: new_song = await self.fetch_json_metadata() if new_song and new_song != self.current_song and "Unable to fetch metadata" not in new_song: self.logger.info(f"Now Playing: {new_song}") self.current_song = new_song await self.announce_song(new_song) # Clear buffer after metadata marker buffer = buffer[buffer.find(b"';", buffer.find(b"StreamTitle='")) + 2:] last_json_check = current_time # Keep buffer size reasonable if len(buffer) > 65536: buffer = buffer[-32768:] # Fallback JSON check if ICY updates aren't coming through if current_time - last_json_check >= json_check_interval: new_song = await self.fetch_json_metadata() if "Unable to fetch metadata" in new_song: break if new_song and new_song != self.current_song: self.logger.info(f"Now Playing (fallback): {new_song}") self.current_song = new_song await self.announce_song(new_song) last_json_check = current_time await asyncio.sleep(0.1) await process.wait() self.logger.warning("Stream monitor ended, restarting...") await asyncio.sleep(5) except Exception as e: self.logger.error(f"Stream monitor error: {e}") await asyncio.sleep(5) async def announce_song(self, song: str): """Announce song if it doesn't match any ignore patterns.""" try: if self.channel and self.should_announce_song(song): await self.channel.message(self.reply.format(song=song)) except Exception as e: self.logger.error(f"Error announcing song: {e}") async def start(self): await self.bot.run() async def run_multiple_bots(config_paths: List[str]): """Run multiple bot instances concurrently.""" bots = [IcecastBot(config_path) for config_path in config_paths] await asyncio.gather(*(bot.start() for bot in bots)) if __name__ == "__main__": parser = argparse.ArgumentParser(description='Icecast IRC Bot') parser.add_argument('configs', nargs='*', help='Paths to config files') parser.add_argument('--config', type=str, help='Path to single config file') parser.add_argument('--irc-host', type=str, help='IRC server host') parser.add_argument('--irc-port', type=int, help='IRC server port') parser.add_argument('--irc-nick', type=str, help='IRC nickname') parser.add_argument('--irc-channel', type=str, help='IRC channel') parser.add_argument('--stream-url', type=str, help='Icecast stream URL (base url; do not include /stream or .mp3)') parser.add_argument('--stream-endpoint', type=str, help='Stream endpoint (e.g. /stream)') args = parser.parse_args() if args.configs: # Multi-bot mode asyncio.run(run_multiple_bots(args.configs)) else: # Single-bot mode bot = IcecastBot(args.config) # Apply any command line overrides to the config if args.irc_host: bot.config['irc']['host'] = args.irc_host if args.irc_port: bot.config['irc']['port'] = args.irc_port if args.irc_nick: bot.config['irc']['nick'] = args.irc_nick if args.irc_channel: bot.config['irc']['channel'] = args.irc_channel if args.stream_url: bot.config['stream']['url'] = args.stream_url if args.stream_endpoint: bot.config['stream']['endpoint'] = args.stream_endpoint asyncio.run(bot.start())