np.py

Python
import socket
import ssl
import subprocess
import json
import time

# Configuration
SERVER = "irc.blcknd.network"
PORT = 6697
CHANNEL = "#blcknd"
NICKNAME = "fuckoff"  # Your bot's nickname
REALNAME = "Python IRC Bot"
IDENT = "fuckoff"
USER = "fuckoff"
PASSWORD = "password"  # Set this to your actual IRC server password, if any

def fetch_nowplaying():
    """Fetch the current track from the Icecast server using curl."""
    command = ['curl', '-s', 'http://blcknd.stream:8000/status-json.xsl']
    try:
        result = subprocess.run(command, capture_output=True, text=True, check=True)
        data = json.loads(result.stdout)
        track = data.get('icestats', {}).get('source', {}).get('title', 'No track info found')
        return track
    except subprocess.CalledProcessError as e:
        return f"Error fetching track info: {e}"
    except json.JSONDecodeError:
        return "Error parsing JSON response."

def send_message(ssl_sock, message):
    """Send a message to the IRC channel."""
    ssl_sock.send(f"PRIVMSG {CHANNEL} :{message}\r\n".encode('utf-8'))

def connect_irc():
    """Establish a secure SSL connection to the IRC server."""
    raw_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ssl_sock = ssl.wrap_socket(raw_sock)
    ssl_sock.connect((SERVER, PORT))
    return ssl_sock

def register_with_server(ssl_sock):
    """Send the registration commands to the IRC server."""
    ssl_sock.send(f"NICK {NICKNAME}\r\n".encode('utf-8'))
    ssl_sock.send(f"USER {USER} 0 * :{REALNAME}\r\n".encode('utf-8'))
    time.sleep(2)  # Allow time for registration to complete

def handle_irc(ssl_sock):
    """Handle incoming IRC messages."""
    while True:
        data = ssl_sock.recv(4096).decode('utf-8', errors='ignore')
        if data:
            print(f"Server response: {data.strip()}")
            if "PING" in data:
                ssl_sock.send(f"PONG {data.split()[1]}\r\n".encode('utf-8'))
            elif "!nowplaying" in data:
                print("Nowplaying command detected!")
                track_info = fetch_nowplaying()
                # Format the track info with colors and bold text
                # Bold for BLCKND.RADIO, track name simulated with underline, and green for the URL
                message = (
                    f"\x02BLCKND.RADIO\x0F - "  # Bold for BLCKND.RADIO
                    f"\x1FCurrent Track: {track_info}\x0F "  # Underlined for the track name (simulating italics)
                    f"[ \x03{3}https://blcknd.stream\x0F ]"  # Green for the URL
                )
                send_message(ssl_sock, message)

def main():
    """Main function to connect to the IRC server and listen for commands."""
    print("Attempting to connect to IRC server...")
    try:
        ssl_sock = connect_irc()
        print("Connected to the IRC server.")
        register_with_server(ssl_sock)
        ssl_sock.send(f"JOIN {CHANNEL}\r\n".encode('utf-8'))
        print(f"Joined {CHANNEL}")
        handle_irc(ssl_sock)
    except Exception as e:
        print(f"Error connecting to the server: {e}")
        print("Failed to connect to the IRC server.")

if __name__ == "__main__":
    main()