<commit_before>#!/usr/bin/env python

"""

    rfk.liquidsoaphandler
    ~~~~~~~~~~~~~~~~~~~~~

    ALL YOUR LIQUID SOAP ARE BELONG TO US

    Flow is like this:
        Auth: streaming client tries to authenticate
        Connect: streaming of data starts
        Meta: Metadata (ATTENTION: not all clients send metadata!)
        Disconnect: streaming client disconnects

"""

import argparse
import json
import sys
import base64

import os
basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(basedir, 'lib'))

import rfk
import rfk.database
from rfk.database.base import User, Loop
from rfk.database.show import Show, Tag, UserShow
from rfk.database.track import Track
from rfk.database.streaming import Listener
from rfk.liquidsoap import LiquidInterface
from rfk import exc as rexc
from rfk.helper import get_path
from rfk.log import init_db_logging

username_delimiter = '|'
logger = None


def kick():
    """Shorthand method for kicking the currently connected user
    
    returns True if someone was kicked
    returns False if harbor was empty or kick failed
    """

    logger.info('kick')
    liquidsoap = LiquidInterface()
    liquidsoap.connect()
    kicked = liquidsoap.kick_harbor()
    liquidsoap.close()
    logger.info('kick result: %s' % (kicked))
    return kicked


def init_show(user):
    """Initializes a show

        it either takes a planned show or an unplanned show if it's still running
        if non of them is found a new unplanned show is added and initialized
        if a new show was initialized the old one will be ended and the streamer status will be resetted
    """

    show = Show.get_current_show(user)
    if show is None:
        show = Show()
        if user.get_setting(code='use_icy'):
            show.add_tags(Tag.parse_tags(user.get_setting(code='icy_show_genre') or ''))
            show.description = user.get_setting(code='icy_show_description') or ''
            show.name = user.get_setting(code='icy_show_name') or ''
        else:
            show.add_tags(Tag.parse_tags(user.get_setting(code='show_def_tags') or ''))
            show.description = user.get_setting(code='show_def_desc') or ''
            show.name = user.get_setting(code='show_def_name') or ''
            show.logo = user.get_setting(code='show_def_logo') or None
        show.flags = Show.FLAGS.UNPLANNED
        show.add_user(user)
    elif show.flags == Show.FLAGS.UNPLANNED:
        # just check if there is a planned show to transition to
        s = Show.get_current_show(user, only_planned=True)
        if s is not None:
            show = s
    us = show.get_usershow(user)
    us.status = UserShow.STATUS.STREAMING
    rfk.database.session.commit()
    unfinished_shows = UserShow.query.filter(UserShow.status == UserShow.STATUS.STREAMING,
                                             UserShow.show != show).all()
    for us in unfinished_shows:
        if us.show.flags & Show.FLAGS.UNPLANNED:
            us.show.end_show()<commit_msg>ok, better place it here :3<commit_after>#!/usr/bin/env python

"""

    rfk.liquidsoaphandler
    ~~~~~~~~~~~~~~~~~~~~~

    ALL YOUR LIQUID SOAP ARE BELONG TO US

    Flow is like this:
        Auth: streaming client tries to authenticate
        Connect: streaming of data starts
        Meta: Metadata (ATTENTION: not all clients send metadata!)
        Disconnect: streaming client disconnects

"""

import argparse
import json
import sys
import base64

import os
basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(basedir, 'lib'))

import rfk
import rfk.database
from rfk.database.base import User, Loop
from rfk.database.show import Show, Tag, UserShow
from rfk.database.track import Track
from rfk.database.streaming import Listener
from rfk.liquidsoap import LiquidInterface
from rfk import exc as rexc
from rfk.helper import get_path
from rfk.log import init_db_logging

username_delimiter = '|'
logger = None


def kick():
    """Shorthand method for kicking the currently connected user
    
    returns True if someone was kicked
    returns False if harbor was empty or kick failed
    """

    logger.info('kick')
    liquidsoap = LiquidInterface()
    liquidsoap.connect()
    kicked = liquidsoap.kick_harbor()
    liquidsoap.close()
    logger.info('kick result: %s' % (kicked))
    return kicked


def init_show(user):
    """Initializes a show

        it either takes a planned show or an unplanned show if it's still running
        if non of them is found a new unplanned show is added and initialized
        if a new show was initialized the old one will be ended and the streamer status will be resetted
    """

    show = Show.get_current_show(user)
    if show is None:
        show = Show()
        if user.get_setting(code='use_icy'):
            show.add_tags(Tag.parse_tags(user.get_setting(code='icy_show_genre') or ''))
            show.description = user.get_setting(code='icy_show_description') or ''
            show.name = user.get_setting(code='icy_show_name') or ''
        else:
            show.add_tags(Tag.parse_tags(user.get_setting(code='show_def_tags') or ''))
            show.description = user.get_setting(code='show_def_desc') or ''
            show.name = user.get_setting(code='show_def_name') or ''
        show.logo = user.get_setting(code='show_def_logo') or None
        show.flags = Show.FLAGS.UNPLANNED
        show.add_user(user)
    elif show.flags == Show.FLAGS.UNPLANNED:
        # just check if there is a planned show to transition to
        s = Show.get_current_show(user, only_planned=True)
        if s is not None:
            show = s
    us = show.get_usershow(user)
    us.status = UserShow.STATUS.STREAMING
    rfk.database.session.commit()
    unfinished_shows = UserShow.query.filter(UserShow.status == UserShow.STATUS.STREAMING,
                                             UserShow.show != show).all()
    for us in unfinished_shows:
        if us.show.flags & Show.FLAGS.UNPLANNED:
            us.show.end_show()
{'fraction_non_alphanumeric': 0.0962425840474621, 'fraction_numerical': 0.0008239947264337508, 'mean_word_length': 2.9054054054054053, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3839077', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1793, 'n_words': 528}