<commit_before>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2016-07-26 11:04:34
import datetime
import urllib
import urllib2
from abc import ABCMeta, abstractmethod

import util
import package_tracking.component.model as model

logger = util.get_logger("MoJoQQ")

# api 地址
# https://github.com/sjdy521/Mojo-Webqq/blob/master/API.md


class MoJoQQComponent:
    def __init__(self):
        pass

    __metaclass__ = ABCMeta

    @abstractmethod
    def send_qq_msg(self, qq_no, msg):
        pass

    @abstractmethod
    def send_group_msg(self, group_no, msg, mention):
        pass

    @abstractmethod
    def send_discuss_group_msg(self, discuss_id, msg, mention):
        pass


class MoJoQQComponentImpl(MoJoQQComponent):
    def __init__(self, mojo_host='127.0.0.1', mojo_port='5000'):
        """
        MojoQQ webqq的perl实现可以使用它发送、接收qq消息

        Args:
            mojo_host (str): mojoqq 主机地址
            mojo_host (str): mojoqq 服务端口
        """
        self.mojo_host = mojo_host
        self.mojo_port = mojo_port
        self.mojo_url = 'http://%s:%s' % (self.mojo_host, self.mojo_port)

    def send_qq_msg(self, qq_no, msg):
        """
        发送qq好友信息

        Args:
            qq_no (str): qq号
            msg (str): 消息
        Returns:
            str: json
        """
        service_path = '/openqq/send_message'

        logger.debug("[MoJoQQ] - 发送消息到 qq %s 消息：%s " % (qq_no, msg))

        data = {
            'qq': qq_no,
            'content': msg
        }

        encoded_data = urllib.urlencode(data)

        resp = urllib2.urlopen(self.mojo_url + service_path, encoded_data).read()

        logger.debug("[MoJoQQ] - 返回信息 %s " % resp)

    def send_group_msg(self, group_no, msg, mention):
        """
        发送qq群组消息

        Args:
            group_no (str): qq群号
            msg (str): 消息
            mention (str): 需要提到的人
        Returns:
            str: json
        """
        service_path = '/openqq/send_group_message'

        logger.debug("[MoJoQQ] - 发送消息到 qq群 %s 消息：%s 提到的人: %s " % (group_no, msg, mention))

        data = {
            'gnumber': group_no,
            'content': '@'+mention+' '+msg if mention else msg
        }

        encoded_data = urllib.urlencode(data)

        resp = urllib2.urlopen(self.mojo_url + service_path, encoded_data).read()

        logger.debug("[MoJoQQ] - 返回信息 %s " % resp)

    def send_discuss_group_msg(self, discuss_id, msg, mention):
        """
        发送qq讨论组消息

        Args:
            discuss_id (str): 讨论组id
            msg (str): 消息
            mention (str): 需要提到的人
        Returns:
            str: json
        """
        service_path = '/openqq/send_discuss_message'

        logger.debug("[MoJoQQ] - 发送消息到 qq讨论组 %s 消息：%s 提到的人: %s".encode('utf-8') % (discuss_id, msg, mention))

        data = {
            'did': discuss_id,
            'content': '@' + mention + ' ' + msg if mention else msg
        }

        encoded_data = urllib.urlencode(data)

        resp = urllib2.urlopen(self.mojo_url + service_path, encoded_data).read()

        logger.debug("[MoJoQQ] - 返回信息 %s " % resp)

<commit_msg>* 发送消息失败后重试一次<commit_after>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2016-07-26 11:04:34
import datetime
import urllib
import urllib2
from abc import ABCMeta, abstractmethod

import util
import package_tracking.component.model as model

logger = util.get_logger("MoJoQQ")

# api 地址
# https://github.com/sjdy521/Mojo-Webqq/blob/master/API.md


class MoJoQQComponent:
    def __init__(self):
        pass

    __metaclass__ = ABCMeta

    @abstractmethod
    def send_qq_msg(self, qq_no, msg):
        pass

    @abstractmethod
    def send_group_msg(self, group_no, msg, mention):
        pass

    @abstractmethod
    def send_discuss_group_msg(self, discuss_id, msg, mention):
        pass


class MoJoQQComponentImpl(MoJoQQComponent):
    def __init__(self, mojo_host='127.0.0.1', mojo_port='5000'):
        """
        MojoQQ webqq的perl实现可以使用它发送、接收qq消息

        Args:
            mojo_host (str): mojoqq 主机地址
            mojo_host (str): mojoqq 服务端口
        """
        self.mojo_host = mojo_host
        self.mojo_port = mojo_port
        self.mojo_url = 'http://%s:%s' % (self.mojo_host, self.mojo_port)

    def send_qq_msg(self, qq_no, msg):
        """
        发送qq好友信息

        Args:
            qq_no (str): qq号
            msg (str): 消息
        Returns:
            str: json
        """
        service_path = '/openqq/send_message'

        logger.debug("[MoJoQQ] - 发送消息到 qq %s 消息：%s " % (qq_no, msg))

        data = {
            'qq': qq_no,
            'content': msg
        }

        encoded_data = urllib.urlencode(data)

        resp = urllib2.urlopen(self.mojo_url + service_path, encoded_data).read()

        logger.debug("[MoJoQQ] - 返回信息 %s " % resp)

    def send_group_msg(self, group_no, msg, mention):
        """
        发送qq群组消息

        Args:
            group_no (str): qq群号
            msg (str): 消息
            mention (str): 需要提到的人
        Returns:
            str: json
        """
        service_path = '/openqq/send_group_message'

        logger.debug("[MoJoQQ] - 发送消息到 qq群 %s 消息：%s 提到的人: %s " % (group_no, msg, mention))

        data = {
            'gnumber': group_no,
            'content': '@'+mention+' '+msg if mention else msg
        }

        encoded_data = urllib.urlencode(data)

        try:
            resp = urllib2.urlopen(self.mojo_url + service_path, encoded_data).read()
        except Exception as e:
            logger.debug("[MoJoQQ] - 发送qq消息失败正在重试 原因 %s " % e.message)
            resp = urllib2.urlopen(self.mojo_url + service_path, encoded_data).read()

        logger.debug("[MoJoQQ] - 返回信息 %s " % resp)

    def send_discuss_group_msg(self, discuss_id, msg, mention):
        """
        发送qq讨论组消息

        Args:
            discuss_id (str): 讨论组id
            msg (str): 消息
            mention (str): 需要提到的人
        Returns:
            str: json
        """
        service_path = '/openqq/send_discuss_message'

        logger.debug("[MoJoQQ] - 发送消息到 qq讨论组 %s 消息：%s 提到的人: %s".encode('utf-8') % (discuss_id, msg, mention))

        data = {
            'did': discuss_id,
            'content': '@' + mention + ' ' + msg if mention else msg
        }

        encoded_data = urllib.urlencode(data)

        resp = urllib2.urlopen(self.mojo_url + service_path, encoded_data).read()

        logger.debug("[MoJoQQ] - 返回信息 %s " % resp)


{'fraction_non_alphanumeric': 0.12094898433865715, 'fraction_numerical': 0.010389207629089781, 'mean_word_length': 2.0041918956683746, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4034313', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2402, 'n_words': 461}