<commit_before>import time
import re
import os
import pprint
import traceback

import threading
import hangups.ui.utils
from slackclient import SlackClient
from websocket import WebSocketConnectionClosedException

import asyncio
import logging
import hangups
import json

import emoji
import urllib
import slacker

""" SlackRTM plugin for listening to hangouts and slack and syncing messages between the two.
config.json will have to be configured as follows:
"slackrtm": [{
  "name": "SlackNameForLoggingEtc",
  "key": "SLACK_API_KEY",
  "synced_conversations": [
  ["SLACK_CHANNEL_ID1", "CONV_ID1", "optHONameForSenderDisplay"],
  ["SLACK_CHANNEL_ID1", "CONV_ID1", "optHONameForSenderDisplay"]
  ]
}]

You can (theoretically) set up as many slack sinks per bot as you like, by extending the list"""


def unicodeemoji2text(text):
    out = u''
    for c in text[:]:
        try:
            c = emoji.decode(c)
        except:
            pass
        out = out + c
    return out

def chatMessageEvent2SlackText(event):
    def renderTextSegment(segment):
        out = ''
        if segment.is_bold:
            out += ' *'
        if segment.is_italic:
            out += ' _'
        out += unicodeemoji2text(segment.text)
        if segment.is_italic:
            out += '_ '
        if segment.is_bold:
            out += '* '
        return out

    lines = ['']
    for segment in event.segments:
        if segment.type_ == hangups.schemas.SegmentType.TEXT:
            lines[-1] += renderTextSegment(segment)
        elif segment.type_ == hangups.schemas.SegmentType.LINK:
            # unfortunately slack does not format links :-(
            lines[-1] += segment.text
        elif segment.type_ == hangups.schemas.SegmentType.LINE_BREAK:
            lines.append('')
        else:
            print('slackrtm: Ignoring unknown chat message segment type: %s' % segment.type_)
    lines.extend(event.attachments)
    return '\n'.join(lines)


class ParseError(Exception):
    pass

class SlackMessage(object):
    def __init__(self, slackrtm, reply):
        self.text = None
        self.username = None
        self.edited = None
        self.from_ho_id = None
        self.sender_id = None
        self.channel = None
        self.file_attachment = None

        if not 'type' in reply:
            print("slackrtm: No 'type' in reply:")
            print("slackrtm: "+str(reply))
            raise ParseError('No "type" in reply:\n%s' % str(reply))
    
        if reply['type'] in ['pong', 'presence_change',  'user_typing', 'file_shared', 'file_public', 'file_comment_added', 'file_comment_deleted']:
            # we ignore pong's as they are only answers for our pings
            raise ParseError('Not a "message" type reply: type=%s' % reply['type'])

        text = u''
        username = ''
        edited = ''
        from_ho_id = ''
        sender_id = ''
        channel = None
        # only used during parsing<commit_msg>also ignoring type=message_deleted replies from Slack<commit_after>import time
import re
import os
import pprint
import traceback

import threading
import hangups.ui.utils
from slackclient import SlackClient
from websocket import WebSocketConnectionClosedException

import asyncio
import logging
import hangups
import json

import emoji
import urllib
import slacker

""" SlackRTM plugin for listening to hangouts and slack and syncing messages between the two.
config.json will have to be configured as follows:
"slackrtm": [{
  "name": "SlackNameForLoggingEtc",
  "key": "SLACK_API_KEY",
  "synced_conversations": [
  ["SLACK_CHANNEL_ID1", "CONV_ID1", "optHONameForSenderDisplay"],
  ["SLACK_CHANNEL_ID1", "CONV_ID1", "optHONameForSenderDisplay"]
  ]
}]

You can (theoretically) set up as many slack sinks per bot as you like, by extending the list"""


def unicodeemoji2text(text):
    out = u''
    for c in text[:]:
        try:
            c = emoji.decode(c)
        except:
            pass
        out = out + c
    return out

def chatMessageEvent2SlackText(event):
    def renderTextSegment(segment):
        out = ''
        if segment.is_bold:
            out += ' *'
        if segment.is_italic:
            out += ' _'
        out += unicodeemoji2text(segment.text)
        if segment.is_italic:
            out += '_ '
        if segment.is_bold:
            out += '* '
        return out

    lines = ['']
    for segment in event.segments:
        if segment.type_ == hangups.schemas.SegmentType.TEXT:
            lines[-1] += renderTextSegment(segment)
        elif segment.type_ == hangups.schemas.SegmentType.LINK:
            # unfortunately slack does not format links :-(
            lines[-1] += segment.text
        elif segment.type_ == hangups.schemas.SegmentType.LINE_BREAK:
            lines.append('')
        else:
            print('slackrtm: Ignoring unknown chat message segment type: %s' % segment.type_)
    lines.extend(event.attachments)
    return '\n'.join(lines)


class ParseError(Exception):
    pass

class SlackMessage(object):
    def __init__(self, slackrtm, reply):
        self.text = None
        self.username = None
        self.edited = None
        self.from_ho_id = None
        self.sender_id = None
        self.channel = None
        self.file_attachment = None

        if not 'type' in reply:
            print("slackrtm: No 'type' in reply:")
            print("slackrtm: "+str(reply))
            raise ParseError('No "type" in reply:\n%s' % str(reply))
    
        if reply['type'] in ['pong', 'presence_change',  'user_typing', 'file_shared', 'file_public', 'file_comment_added', 'file_comment_deleted', 'message_deleted']:
            # we ignore pong's as they are only answers for our pings
            raise ParseError('Not a "message" type reply: type=%s' % reply['type'])

        text = u''
        username = ''
        edited = ''
        from_ho_id = ''
        sender_id = ''
        channel = None
        # only used during parsing
{'fraction_non_alphanumeric': 0.10330370618816032, 'fraction_numerical': 0.003018614791212477, 'mean_word_length': 2.5103001765744555, 'pattern_counts': {'":': 8, '<': 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': '6176405', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1765, 'n_words': 499}