<commit_before>import os
import redis
import yaml
import random
import re
import requests
from gendo import Gendo

path = os.path.dirname(os.path.abspath(__file__))
path_to_yaml = os.path.join(path, 'config.yaml')

with open(path_to_yaml, 'r') as ymlfile:
    cfg = yaml.load(ymlfile)

channel = cfg.get('gendo', {}).get('channel')
token = cfg.get('gendo', {}).get('auth_token')
gendo = Gendo(__name__, token, channel)

db = None


def setup_redis():
    global db
    db = redis.StrictRedis(host='localhost', port=6379, db=0)


@gendo.listen_for('cookies')
def cookies(user, message):
    return "I *LOVE* COOOOOOOOKIES!!!!"


@gendo.listen_for('morning')
def morning(user, message):
    return "mornin' @{0}".format(gendo.get_user_name(user))


@gendo.listen_for('image me')
def image_me(user, message):
    matches = re.findall('image me(.*)', message)
    if not matches:
        return
    message = matches[0].strip()
    url = 'https://ajax.googleapis.com/ajax/services/search/images'
    resp = requests.get(url, params={'v': '1.0', 'q': message})
    if resp.ok:
        results = resp.json().get('responseData', {}).get('results', [])
        if results:
            # make sure we get a working image
            while True:
                image_url = _get_random_image(results)
                is_working = requests.get(image_url).ok
                if is_working:
                    return image_url


def _get_random_image(results):
    min_int, max_int = (0, len(results) - 1)
    idx = random.randint(min_int, max_int)
    return results[idx].get('url')


@gendo.listen_for('sneaky fish count')
def sneaky_fish_counts(user, message):
    report = []
    if not get_total_sneaky_fishes():
        return "no sneaky fishes"
    for user_id, count in get_all_sneaky_fishes():
        suffix = ('' if count == 1 else 's')
        report.append("{0} has {1} accusation{2}.".format(
            gendo.get_user_name(user_id), int(count), suffix))
    return '```' + '\n'.join(report) + '```'


@gendo.listen_for('is a sneaky fish')
def sneaky_fish(user, message):
    fishes = re.findall('<\@([0-9A-Z]*)>', message)<commit_msg>make sure image is in content-type<commit_after>import os
import redis
import yaml
import random
import re
import requests
from gendo import Gendo

path = os.path.dirname(os.path.abspath(__file__))
path_to_yaml = os.path.join(path, 'config.yaml')

with open(path_to_yaml, 'r') as ymlfile:
    cfg = yaml.load(ymlfile)

channel = cfg.get('gendo', {}).get('channel')
token = cfg.get('gendo', {}).get('auth_token')
gendo = Gendo(__name__, token, channel)

db = None


def setup_redis():
    global db
    db = redis.StrictRedis(host='localhost', port=6379, db=0)


@gendo.listen_for('selfie me')
def selfie_me(user, message):
    matches = re.findall('selfie me(.*)', message)
    if not matches:
        return
    message = matches[0].strip()


@gendo.listen_for('cookies')
def cookies(user, message):
    return "I *LOVE* COOOOOOOOKIES!!!!"


@gendo.listen_for('morning')
def morning(user, message):
    return "mornin' @{0}".format(gendo.get_user_name(user))


@gendo.listen_for('image me')
def image_me(user, message):
    matches = re.findall('image me(.*)', message)
    if not matches:
        return
    message = matches[0].strip()
    url = 'https://ajax.googleapis.com/ajax/services/search/images'
    resp = requests.get(url, params={'v': '1.0', 'q': message})
    if resp.ok:
        results = resp.json().get('responseData', {}).get('results', [])
        if results:
            # make sure we get a working image
            while True:
                image_url = _get_random_image(results)
                resp = requests.get(image_url)
                is_working = resp.ok
                content_type = resp.headers.get('Content-Type', '')
                if is_working and 'image' in content_type:
                    return image_url


def _get_random_image(results):
    min_int, max_int = (0, len(results) - 1)
    idx = random.randint(min_int, max_int)
    return results[idx].get('url')


@gendo.listen_for('sneaky fish count')
def sneaky_fish_counts(user, message):
    report = []
    if not get_total_sneaky_fishes():
        return "no sneaky fishes"
    for user_id, count in get_all_sneaky_fishes():
        suffix = ('' if count == 1 else 's')
        report.append("{0} has {1} accusation{2}.".format(
            gendo.get_user_name(user_id), int(count), suffix))
    return '```' + '\n'.join(report) + '```'


@gendo.listen_for('is a sneaky fish')
def sneaky_fish(user, message):
    fishes = re.findall('<\@([0-9A-Z]*)>', message)
{'fraction_non_alphanumeric': 0.15402998044753421, 'fraction_numerical': 0.007603736693460786, 'mean_word_length': 3.255083179297597, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '578188', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1574, 'n_words': 359}