<commit_before>import re
from base import BaseMatcher
import os
import requests
import json
import time
import hashlib
import logging
from random import Random

IMG_REGEX = r'\S+(?:\.jpe?g|\.gif|\.png)\b'
API_USER = os.environ.get('SNIPT_API_USER')
API_KEY = os.environ.get('SNIPT_API_KEY')
URL = 'https://snipt.net/api/private/%s'
KEYWORDS = ['fail', 'suck', 'win', 'kill', 'jump', 'cry', \
    'seeth', 'curl', 'crazy', 'anger', 'idiot', 'dumb', \
    'stupid', 'funny']
HEADERS = {
    'Authorization': 'ApiKey {0}:{1}'.format(API_USER, API_KEY),
    'Accept': 'application/json',
    'Content-Type': 'application/json',
}

class GifterMatcher(BaseMatcher):

    name = 'gifter'
    request_regex = r'\.?(show\s)?(me\s)?\s?(the\s)?gif\.?'
    _previous_message = ''

    def respond(self, message, user=None):
        # parse and save
        img_found = self.save(message, user)
        self._previous_message = message
        # parse for request - only if direct message
        if user:
            # check for random request
            if re.match(self.request_regex, message, re.IGNORECASE):
                message = self.random()
                self.speak(message)
            # check for keywords
            keywords = [x for x in message.split() if x.lower() in KEYWORDS]
            if keywords:
                msg = self.random(keywords[0])
                if msg and not img_found:
                    self.speak(msg)

    def save(self, text, author):
        """
        Saves all images found in text.  Called from Nigel.

        :param text: Text to search
        :param author: Author of message

        """
        # save imgs
        imgs = re.findall(IMG_REGEX, text, re.IGNORECASE)
        return [self.save_link(x, author, text) for x in imgs]

    def get_suggested_tags(self, messages=[]):
        """
        Parses text and returns suggested tags

        :param messages: Messages to look for tags<commit_msg>fixed bug in gifter if no random results are found<commit_after>import re
from base import BaseMatcher
import os
import requests
import json
import time
import hashlib
import logging
from random import Random

IMG_REGEX = r'\S+(?:\.jpe?g|\.gif|\.png)\b'
API_USER = os.environ.get('SNIPT_API_USER')
API_KEY = os.environ.get('SNIPT_API_KEY')
URL = 'https://snipt.net/api/private/%s'
KEYWORDS = ['fail', 'suck', 'win', 'kill', 'jump', 'cry', \
    'seeth', 'curl', 'crazy', 'anger', 'idiot', 'dumb', \
    'stupid', 'funny']
HEADERS = {
    'Authorization': 'ApiKey {0}:{1}'.format(API_USER, API_KEY),
    'Accept': 'application/json',
    'Content-Type': 'application/json',
}

class GifterMatcher(BaseMatcher):

    name = 'gifter'
    request_regex = r'\.?(show\s)?(me\s)?\s?(the\s)?gif\.?'
    _previous_message = ''

    def respond(self, message, user=None):
        # parse and save
        img_found = self.save(message, user)
        self._previous_message = message
        # parse for request - only if direct message
        if user:
            # check for random request
            if re.match(self.request_regex, message, re.IGNORECASE):
                message = self.random()
                if not message:
                    return
                self.speak(message)
            # check for keywords
            keywords = [x for x in message.split() if x.lower() in KEYWORDS]
            if keywords:
                msg = self.random(keywords[0])
                if msg and not img_found:
                    self.speak(msg)

    def save(self, text, author):
        """
        Saves all images found in text.  Called from Nigel.

        :param text: Text to search
        :param author: Author of message

        """
        # save imgs
        imgs = re.findall(IMG_REGEX, text, re.IGNORECASE)
        return [self.save_link(x, author, text) for x in imgs]

    def get_suggested_tags(self, messages=[]):
        """
        Parses text and returns suggested tags

        :param messages: Messages to look for tags
{'fraction_non_alphanumeric': 0.12857142857142856, 'fraction_numerical': 0.0015037593984962407, 'mean_word_length': 2.342546063651591, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '4892724', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1237, 'n_words': 350}