<commit_before># -*- coding: utf-8 -*-
import json
import thread
import time
from socket import error as socket_error

import paho.mqtt.client as mqtt

from pokemongo_bot.event_manager import EventHandler

DEBUG_ON = False


class MyMQTTClass:
    def __init__(self, bot, clientid=None):
        self.bot = bot
        self.client_id = clientid
        self.bot.mqtt_pokemon_list = []
        self._mqttc = None

    def mqtt_on_connect(self, mqttc, obj, flags, rc):
        if rc is 0:
            self._mqttc.subscribe("pgo/#", 1)
        if DEBUG_ON:
            print "rc: " + str(rc)

    def mqtt_on_message(self, mqttc, obj, msg):
        if DEBUG_ON:
            print 'on message: {}'.format(msg.payload)

        pokemon = json.loads(msg.payload)
        if pokemon and 'encounter_id' in pokemon:
            new_list = [x for x in self.bot.mqtt_pokemon_list if x['encounter_id'] is pokemon['encounter_id']]
            if not (new_list and len(new_list) > 0):
                self.bot.mqtt_pokemon_list.append(pokemon)

    def on_disconnect(self, client, userdata, rc):
        self._mqttc.unsubscribe("pgo/#")
        if DEBUG_ON:
            print 'on_disconnect'
            if rc != 0:
                print "Unexpected disconnection."

    def mqtt_on_publish(self, mqttc, obj, mid):
        if DEBUG_ON:
            print "mid: " + str(mid)

    def mqtt_on_subscribe(self, mqttc, obj, mid, granted_qos):
        if DEBUG_ON:
            print "Subscribed: " + str(mid) + " " + str(granted_qos)

    # def mqtt_on_log(self, mqttc, obj, level, string):
    #    print string

    def publish(self, channel, message):
        if self._mqttc:
            try:
                self._mqttc.publish(channel, message)
            except UnicodeDecodeError:
                pass

    def connect_to_mqtt(self):
        try:
            if DEBUG_ON:
                print 'connect again'

            self._mqttc = mqtt.Client(None)
            self._mqttc.on_message = self.mqtt_on_message
            self._mqttc.on_connect = self.mqtt_on_connect
            self._mqttc.on_subscribe = self.mqtt_on_subscribe
            self._mqttc.on_publish = self.mqtt_on_publish
            self._mqttc.on_disconnect = self.on_disconnect

            self._mqttc.connect("broker.pikabot.org", 1883, 60)
            # Enable this line if you are doing the snip code, off stress
            # self._mqttc.loop_start()
        except TypeError:
            print 'Connect to mqtter error'
            return

    def run(self):
        while True:
            try:
                self._mqttc.loop_forever(timeout=30.0, max_packets=100, retry_first_connection=False)
                print 'Oops disconnected ?'
                time.sleep(20)
            except UnicodeDecodeError:
                time.sleep(1)


class SocialHandler(EventHandler):
    def __init__(self, bot):
        super(SocialHandler, self).__init__()
        self.bot = bot
        self.mqttc = None

    def handle_event(self, event, sender, level, formatted_msg, data):
        if self.mqttc is None:
            try:
                if DEBUG_ON:<commit_msg>Switch the keep alive timeout from 60 to 10 to reduce the chance of remote disconnect event. (#5283)<commit_after># -*- coding: utf-8 -*-
import json
import thread
import time
from socket import error as socket_error

import paho.mqtt.client as mqtt

from pokemongo_bot.event_manager import EventHandler

DEBUG_ON = False


class MyMQTTClass:
    def __init__(self, bot, clientid=None):
        self.bot = bot
        self.client_id = clientid
        self.bot.mqtt_pokemon_list = []
        self._mqttc = None

    def mqtt_on_connect(self, mqttc, obj, flags, rc):
        if rc is 0:
            self._mqttc.subscribe("pgo/#", 1)
        if DEBUG_ON:
            print "rc: " + str(rc)

    def mqtt_on_message(self, mqttc, obj, msg):
        if DEBUG_ON:
            print 'on message: {}'.format(msg.payload)

        pokemon = json.loads(msg.payload)
        if pokemon and 'encounter_id' in pokemon:
            new_list = [x for x in self.bot.mqtt_pokemon_list if x['encounter_id'] is pokemon['encounter_id']]
            if not (new_list and len(new_list) > 0):
                self.bot.mqtt_pokemon_list.append(pokemon)

    def on_disconnect(self, client, userdata, rc):
        self._mqttc.unsubscribe("pgo/#")
        if DEBUG_ON:
            print 'on_disconnect'
            if rc != 0:
                print "Unexpected disconnection."

    def mqtt_on_publish(self, mqttc, obj, mid):
        if DEBUG_ON:
            print "mid: " + str(mid)

    def mqtt_on_subscribe(self, mqttc, obj, mid, granted_qos):
        if DEBUG_ON:
            print "Subscribed: " + str(mid) + " " + str(granted_qos)

    # def mqtt_on_log(self, mqttc, obj, level, string):
    #    print string

    def publish(self, channel, message):
        if self._mqttc:
            try:
                self._mqttc.publish(channel, message)
            except UnicodeDecodeError:
                pass

    def connect_to_mqtt(self):
        try:
            if DEBUG_ON:
                print 'connect again'

            self._mqttc = mqtt.Client(None)
            self._mqttc.on_message = self.mqtt_on_message
            self._mqttc.on_connect = self.mqtt_on_connect
            self._mqttc.on_subscribe = self.mqtt_on_subscribe
            self._mqttc.on_publish = self.mqtt_on_publish
            self._mqttc.on_disconnect = self.on_disconnect

            self._mqttc.connect("broker.pikabot.org", 1883, 10)
            # Enable this line if you are doing the snip code, off stress
            # self._mqttc.loop_start()
        except TypeError:
            print 'Connect to mqtter error'
            return

    def run(self):
        while True:
            try:
                self._mqttc.loop_forever(timeout=30.0, max_packets=100, retry_first_connection=False)
                print 'Oops disconnected ?'
                time.sleep(20)
            except UnicodeDecodeError:
                time.sleep(1)


class SocialHandler(EventHandler):
    def __init__(self, bot):
        super(SocialHandler, self).__init__()
        self.bot = bot
        self.mqttc = None

    def handle_event(self, event, sender, level, formatted_msg, data):
        if self.mqttc is None:
            try:
                if DEBUG_ON:
{'fraction_non_alphanumeric': 0.0923853066372379, 'fraction_numerical': 0.007567397130695255, 'mean_word_length': 2.005210800568451, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3129783', 'n_tokens_mistral': 2094, 'n_tokens_neox': 2002, 'n_words': 482}