np.py

fucking python hurts my goddamn head …

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 and related information 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)
        source = data.get('icestats', {}).get('source', {})
        
        # Extracting relevant data
        track = source.get('title', 'No track info found')
        server_name = source.get('server_name', 'No server name')
        genre = source.get('genre', 'No genre')
        bitrate = source.get('bitrate', 'No bitrate info')
        listeners = source.get('listeners', 'No listener info')
        server_url = source.get('server_url', 'No URL')
        description = source.get('server_description', 'No description')

        # Returning all values
        return track, server_name, genre, bitrate, listeners, server_url, description
    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, server_name, genre, bitrate, listeners_count, server_url, description = fetch_nowplaying()

                # Constructing the now playing message with all the fetched info
                message = (
                    f"\x02BLCKND.RADIO\x0F - "  # Bold for BLCKND.RADIO
                    f"\x1FCurrent Track: {track_info}\x0F | "  # Underlined for the track name (simulating italics)
                    f"Genre: {genre} | "  # Genre
                    f"Bitrate: {bitrate} kbps | "  # Bitrate
                    f"Listeners: {listeners_count} | "  # Listeners count
                    f"\x03{3}{server_url}\x0F | "  # Green for the Stream URL
                    f"\x02Server: {server_name}\x0F | "  # Bold for server name
                    f"\x1FDescription: {description}\x0F"  # Description in underline
                )
                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()

Leave a Reply

Your email address will not be published. Required fields are marked *