<commit_before># -*- coding: UTF-8 -*-

from django.core.management.base import BaseCommand
from optparse import make_option
import unicodedata
import shout
import daemon
import daemon.pidfile
from signal import SIGTSTP
import sys, os
import jukebox_core.api


class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option(
            "--start",
            action="store_true",
            dest="start",
            help="Start shoutcast streaming"
        ),
        make_option(
            "--stop",
            action="store_true",
            dest="stop",
            help="Stop shoutcast streaming"
        ),
        make_option(
            "--host",
            action="store",
            dest="host",
            help="Host of shoutcast server"
        ),
        make_option(
            "--port",
            action="store",
            dest="port",
            help="Port of shoutcast server"
        ),
        make_option(
            "--password",
            action="store",
            dest="password",
            help="Source password of shoutcast server"
        ),
    )

    def handle(self, *args, **options):
        pidFile = os.path.dirname(
            os.path.abspath(__file__)
        ) + "/../../daemon.pid"

        if options["start"]:
            if (options["host"] is None or
                options["port"] is None or
                options["password"] is None
            ):
                print "Required arguments: host, port, password"
                self.print_help("jukebox_shout", "help")
                return

            if os.path.exists(pidFile):
                print "Daemon already running, pid file exists"
                return

            pid = daemon.pidfile.TimeoutPIDLockFile(
                pidFile,
                10
            )

            print "Starting jukebox_shout daemon..."
            self.daemon = daemon.DaemonContext(
                uid=os.getuid(),
                gid=os.getgid(),
                pidfile=pid,
                working_directory=os.getcwd(),
                detach_process=True,
                signal_map={
                    SIGTSTP: self.shutdown
                }
            )

            with self.daemon:
                self.stream(
                    channel_mount="/stream",
                    station_url="http://" + options["host"],
                    genre="Mixed",
                    name="Democratic Jukebox",
                    description="Your democratic music player",
                    hostname=options["host"],
                    port=int(options["port"]),
                    password=options["password"],
                )
        elif options["stop"]:
            if not os.path.exists(pidFile):
                print "Daemon not running"
                return

            print "Stopping daemon..."
            pid = int(open(pidFile).read())
            os.kill(pid, SIGTSTP)
        else:
            self.print_help("jukebox_shout", "help")

    def shutdown(self, signal, action):
        self.shout.close()
        self.daemon.close()
        sys.exit(0)

    def stream(
        self,
        channel_mount,
        station_url,
        genre, name,
        description,
        hostname,
        port,
        password
    ):
        self.shout = shout.Shout()
        print "Using libshout version %s" % shout.version()

        self.shout.audio_info = {
            shout.SHOUT_AI_BITRATE: "128",
            shout.SHOUT_AI_SAMPLERATE: "44100",
            shout.SHOUT_AI_CHANNELS: "2"
        }
        self.shout.name = name
        self.shout.url = station_url
        self.shout.mount = channel_mount
        self.shout.port = port
        self.shout.user = "source"
        self.shout.password = password
        self.shout.genre = genre
        self.shout.description = description
        self.shout.host = hostname
        self.shout.ogv = 0
        self.shout.format = "mp3"
        self.shout.open()

        songs_api = jukebox_core.api.songs()
        while 1:
            self.sendfile(songs_api.getNextSong())


    def sendfile(self, song_instance):<commit_msg>jukebox_shout: improved error handling<commit_after># -*- coding: UTF-8 -*-

from django.core.management.base import BaseCommand
from optparse import make_option
import unicodedata
import shout
import daemon
import daemon.pidfile
from signal import SIGTSTP
import sys, os
import jukebox_core.api


class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option(
            "--start",
            action="store_true",
            dest="start",
            help="Start shoutcast streaming"
        ),
        make_option(
            "--stop",
            action="store_true",
            dest="stop",
            help="Stop shoutcast streaming"
        ),
        make_option(
            "--host",
            action="store",
            dest="host",
            help="Host of shoutcast server"
        ),
        make_option(
            "--port",
            action="store",
            dest="port",
            help="Port of shoutcast server"
        ),
        make_option(
            "--password",
            action="store",
            dest="password",
            help="Source password of shoutcast server"
        ),
    )

    def handle(self, *args, **options):
        pidFile = os.path.dirname(
            os.path.abspath(__file__)
        ) + "/../../daemon.pid"

        if options["start"]:
            if (options["host"] is None or
                options["port"] is None or
                options["password"] is None
            ):
                print "Required arguments: host, port, password"
                self.print_help("jukebox_shout", "help")
                return

            if os.path.exists(pidFile):
                print "Daemon already running, pid file exists"
                return

            pid = daemon.pidfile.TimeoutPIDLockFile(
                pidFile,
                10
            )

            self.shout = shout.Shout()
            print "Using libshout version %s" % shout.version()

            self.shout.audio_info = {
                shout.SHOUT_AI_BITRATE: "128",
                shout.SHOUT_AI_SAMPLERATE: "44100",
                shout.SHOUT_AI_CHANNELS: "2"
            }
            self.shout.name = "Democratic Jukebox"
            self.shout.url = "http://" + options["host"]
            self.shout.mount = "/stream"
            self.shout.port = int(options["port"])
            self.shout.user = "source"
            self.shout.password = options["password"]
            self.shout.genre = "Mixed"
            self.shout.description = "Your democratic music player"
            self.shout.host = options["host"]
            self.shout.ogv = 0
            self.shout.format = "mp3"
            try:
                self.shout.open()
                self.shout.close()
            except shout.ShoutException as exception:
                print "Error: " + str(exception)
                return

            print "Starting jukebox_shout daemon..."
            self.daemon = daemon.DaemonContext(
                uid=os.getuid(),
                gid=os.getgid(),
                pidfile=pid,
                working_directory=os.getcwd(),
                detach_process=True,
                signal_map={
                    SIGTSTP: self.shutdown
                }
            )

            with self.daemon:
                self.shout.open()
                songs_api = jukebox_core.api.songs()
                while 1:
                    self.sendfile(songs_api.getNextSong())

        elif options["stop"]:
            if not os.path.exists(pidFile):
                print "Daemon not running"
                return

            print "Stopping daemon..."
            pid = int(open(pidFile).read())
            os.kill(pid, SIGTSTP)
        else:
            self.print_help("jukebox_shout", "help")

    def shutdown(self, signal, action):
        self.shout.close()
        self.daemon.close()
        sys.exit(0)

    def sendfile(self, song_instance):
{'fraction_non_alphanumeric': 0.1060923076923077, 'fraction_numerical': 0.003938461538461538, 'mean_word_length': 1.3928150765606595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2557948', 'n_tokens_mistral': 2265, 'n_tokens_neox': 2160, 'n_words': 453}