<commit_before>from __future__ import print_function
from collections import defaultdict
from threading import Thread, Event, Lock
import pickle
import argparse
import re
import copy
import io

from repoze.lru import LRUCache

import linot.config as config
from .twitch_engine import TwitchEngine
from linot.services.service_base import ServiceBase
import linot.logger
logger = linot.logger.get().getLogger(__name__)


class Checker(Thread):
    def __init__(self, period, twitch, get_sublist):
        super(Checker, self).__init__()
        self._stop = Event()
        self._polling = Event()
        self._period = period
        self._twitch = twitch
        self._get_sublist = get_sublist
        self._status_lock = Lock()
        self._logger = logger.getChild(self.__class__.__name__)

    def run(self):
        self._logger.info('Twitch Checker is started')
        # Skip 1st notify if channels are already live before plugin load
        self._set_live_channels(self._twitch.get_live_channels())

        while(not self._stop.is_set()):
            self._logger.debug('Wait polling {} sec.'.format(self._period))
            self._polling.wait(self._period)
            self._polling.clear()

            self._logger.debug('Try get live channels')
            current_live_channels = self._twitch.get_live_channels()
            local_live_channels = self.get_live_channels()

            off_channels = local_live_channels.viewkeys() - current_live_channels.viewkeys()
            for ch in off_channels:
                # TODO do we have to notify user the channel went off?
                del local_live_channels[ch]

            # Send live notifications to subcribers
            new_live_channels = current_live_channels.viewkeys() - local_live_channels.viewkeys()
            self._logger.debug('New live channels:' + str(new_live_channels))
            for ch in new_live_channels:
                local_live_channels[ch] = current_live_channels[ch]
                local_sublist = self._get_sublist()
                check_name = ch.lower()
                for user in local_sublist:
                    if check_name in local_sublist[user]:
                        msg = io.StringIO()
                        print(u'{} is now streamming!!'.format(unicode(ch)), file=msg)
                        print(u'[Title] {}'.format(unicode(current_live_channels[ch]['status'])), file=msg)
                        print(u'[Playing] {}'.format(unicode(current_live_channels[ch]['game'])), file=msg)
                        print(current_live_channels[ch]['url'], file=msg)
                        user.send_message(msg.getvalue())

            self._set_live_channels(local_live_channels)

        self._stop.clear()
        self._logger.info('Twitch Checker is stopped')

    def _set_live_channels(self, ch_list):
        self._status_lock.acquire(True)
        self._live_channels = ch_list
        self._status_lock.release()

    def refresh(self):
        self._logger.debug('Trigger refresh')
        self._polling.set()

    def get_live_channels(self):
        self._status_lock.acquire(True)
        ch_stat = copy.copy(self._live_channels)
        self._status_lock.release()
        return ch_stat

    def async_stop(self):
        self._logger.debug('stop is called')
        self._polling.set()
        self._stop.set()

    def stop(self):
        self.async_stop()
        self._logger.debug('waiting for thread end')
        self.join()

    def is_stopped(self):
        return self._stop.is_set()


class Service(ServiceBase):
    CMD = 'twitch'
    SUB_FILE = 'twitch_sublist.p'
    CHECK_PERIOD = 300

    def __init__(self, name_cache_size=512):
        ServiceBase.__init__(self)
        self._sublist_lock = Lock()
        self._twitch = TwitchEngine()
        self._channel_name_cache = LRUCache(name_cache_size)

    def _setup_argument(self, cmd_group):
        cmd_group.add_argument('-subscribe', nargs='+', func=self._subscribe,
                               help='Subscribe channels')
        cmd_group.add_argument('-unsubscribe', nargs='+', func=self._unsubscribe,
                               help='Unsubscribe channels')
        cmd_group.add_argument('-listchannel', action='store_true', func=self._list_channel,
                               help="List channels you've subscribed")
        cmd_group.add_argument('-refresh', action='store_true', func=self._refresh,
                               help=argparse.SUPPRESS)
        cmd_group.add_argument('-listusers', action='store_true', func=self._list_users,
                               help=argparse.SUPPRESS)
        cmd_group.add_direct_command(self._sub_by_url, 'twitch\.tv/(\w+)[\s\t,]*', re.IGNORECASE)

    def _start(self):
        # Load subscribe list
        try:
            logger.debug('Loading subscribe list from file')
            self._sublist = pickle.load(open(self.SUB_FILE, 'rb'))
            self._calculate_channel_sub_count()
        except IOError:
            logger.debug('Subscribe list file not found, create empty.')
            self._sublist = defaultdict(list)
            self._channel_sub_count = defaultdict(int)
        self._check_thread = Checker(
            self.CHECK_PERIOD, self._twitch, self.get_sublist)
        self._check_thread.start()<commit_msg>make twitch help text a little bit clear<commit_after>from __future__ import print_function
from collections import defaultdict
from threading import Thread, Event, Lock
import pickle
import argparse
import re
import copy
import io

from repoze.lru import LRUCache

import linot.config as config
from .twitch_engine import TwitchEngine
from linot.services.service_base import ServiceBase
import linot.logger
logger = linot.logger.get().getLogger(__name__)


class Checker(Thread):
    def __init__(self, period, twitch, get_sublist):
        super(Checker, self).__init__()
        self._stop = Event()
        self._polling = Event()
        self._period = period
        self._twitch = twitch
        self._get_sublist = get_sublist
        self._status_lock = Lock()
        self._logger = logger.getChild(self.__class__.__name__)

    def run(self):
        self._logger.info('Twitch Checker is started')
        # Skip 1st notify if channels are already live before plugin load
        self._set_live_channels(self._twitch.get_live_channels())

        while(not self._stop.is_set()):
            self._logger.debug('Wait polling {} sec.'.format(self._period))
            self._polling.wait(self._period)
            self._polling.clear()

            self._logger.debug('Try get live channels')
            current_live_channels = self._twitch.get_live_channels()
            local_live_channels = self.get_live_channels()

            off_channels = local_live_channels.viewkeys() - current_live_channels.viewkeys()
            for ch in off_channels:
                # TODO do we have to notify user the channel went off?
                del local_live_channels[ch]

            # Send live notifications to subcribers
            new_live_channels = current_live_channels.viewkeys() - local_live_channels.viewkeys()
            self._logger.debug('New live channels:' + str(new_live_channels))
            for ch in new_live_channels:
                local_live_channels[ch] = current_live_channels[ch]
                local_sublist = self._get_sublist()
                check_name = ch.lower()
                for user in local_sublist:
                    if check_name in local_sublist[user]:
                        msg = io.StringIO()
                        print(u'{} is now streamming!!'.format(unicode(ch)), file=msg)
                        print(u'[Title] {}'.format(unicode(current_live_channels[ch]['status'])), file=msg)
                        print(u'[Playing] {}'.format(unicode(current_live_channels[ch]['game'])), file=msg)
                        print(current_live_channels[ch]['url'], file=msg)
                        user.send_message(msg.getvalue())

            self._set_live_channels(local_live_channels)

        self._stop.clear()
        self._logger.info('Twitch Checker is stopped')

    def _set_live_channels(self, ch_list):
        self._status_lock.acquire(True)
        self._live_channels = ch_list
        self._status_lock.release()

    def refresh(self):
        self._logger.debug('Trigger refresh')
        self._polling.set()

    def get_live_channels(self):
        self._status_lock.acquire(True)
        ch_stat = copy.copy(self._live_channels)
        self._status_lock.release()
        return ch_stat

    def async_stop(self):
        self._logger.debug('stop is called')
        self._polling.set()
        self._stop.set()

    def stop(self):
        self.async_stop()
        self._logger.debug('waiting for thread end')
        self.join()

    def is_stopped(self):
        return self._stop.is_set()


class Service(ServiceBase):
    CMD = 'twitch'
    SUB_FILE = 'twitch_sublist.p'
    CHECK_PERIOD = 300

    def __init__(self, name_cache_size=512):
        ServiceBase.__init__(self)
        self._sublist_lock = Lock()
        self._twitch = TwitchEngine()
        self._channel_name_cache = LRUCache(name_cache_size)

    def _setup_argument(self, cmd_group):
        cmd_group.add_argument('-subscribe', nargs='+', func=self._subscribe,
                               help='Subscribe channels and receive notification when channel goes live.\n'
                               'ex: {} -subscribe kaydada'.format(self.CMD))
        cmd_group.add_argument('-unsubscribe', nargs='+', func=self._unsubscribe,
                               help='Unsubscribe channels.\n'
                               'ex: {} -unsubscribe kaydada'.format(self.CMD))
        cmd_group.add_argument('-listchannel', action='store_true', func=self._list_channel,
                               help="List channels you've subscribed")

        # below, admin only
        cmd_group.add_argument('-refresh', action='store_true', func=self._refresh,
                               help=argparse.SUPPRESS)
        cmd_group.add_argument('-listusers', action='store_true', func=self._list_users,
                               help=argparse.SUPPRESS)
        cmd_group.add_direct_command(self._sub_by_url, 'twitch\.tv/(\w+)[\s\t,]*', re.IGNORECASE)

    def _start(self):
        # Load subscribe list
        try:
            logger.debug('Loading subscribe list from file')
            self._sublist = pickle.load(open(self.SUB_FILE, 'rb'))
            self._calculate_channel_sub_count()
        except IOError:
            logger.debug('Subscribe list file not found, create empty.')
            self._sublist = defaultdict(list)
            self._channel_sub_count = defaultdict(int)
        self._check_thread = Checker(
            self.CHECK_PERIOD, self._twitch, self.get_sublist)
        self._check_thread.start()
{'fraction_non_alphanumeric': 0.10130538701967273, 'fraction_numerical': 0.001287001287001287, 'mean_word_length': 2.3743796526054592, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4029916', 'n_tokens_mistral': 3133, 'n_tokens_neox': 3053, 'n_words': 610}