<commit_before>from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

from lib.command import Command
from lib.utils import telegram_escape


class GoogleCommand(Command):
    name = 'google'
    aliases = ['g', 'search']
    description = 'Searches Google for a set of search terms.'

    def __init__(self, bot, config):
        super().__init__(bot, config)
        if not self._config or 'api_key' not in self._config or 'custom_search_id' not in self._config:
            self._logger.error('Google API keys are not configured.')
            self._config = None
            return
        else:
            try:
                self.__service = build('customsearch', 'v1', developerKey=self._config['api_key'])
            except HttpError as ex:
                self._logger.exception(ex)
                self._config = None
                return

    def run(self, message, args):
        if not self._config:
            self.reply(message, 'Google command is not configured!')
            return

        if not args:
            self.reply(message, 'Please supply some search terms!')
            return

        try:
            self.bot.telegram.send_chat_action(message.chat.id, 'typing')
            res = self.__service.cse().list(q=' '.join(args), cx=self._config['custom_search_id'], num=5).execute()
        except HttpError as ex:
            self.reply(message, 'Error occurred while fetching search results!')
            self._logger.exception(ex)
            return

        try:
            results = res['items']
            search_information = res['searchInformation']
            reply = 'About {0} results ({1} seconds)\n'.format(search_information['formattedTotalResults'],
                                                               search_information['formattedSearchTime'])
            for idx, result in enumerate(results):
                title = result['title']
                url = result['link']
                display_url = result['displayLink']
                reply += '<b>{0}.</b> <a href="{1}">{2}</a>\n<code>{3}</code>\n'.format(idx + 1, telegram_escape(url),
                                                                                        telegram_escape(title),
                                                                                        telegram_escape(display_url))
        except KeyError:
            try:
                self.reply(message, res['error']['message'])
            except KeyError:
                self.reply(message, 'No results found')
                return

        self.reply(message, reply, parse_mode='HTML')<commit_msg>google: always return on error<commit_after>from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

from lib.command import Command
from lib.utils import telegram_escape


class GoogleCommand(Command):
    name = 'google'
    aliases = ['g', 'search']
    description = 'Searches Google for a set of search terms.'

    def __init__(self, bot, config):
        super().__init__(bot, config)
        if not self._config or 'api_key' not in self._config or 'custom_search_id' not in self._config:
            self._logger.error('Google API keys are not configured.')
            self._config = None
            return
        else:
            try:
                self.__service = build('customsearch', 'v1', developerKey=self._config['api_key'])
            except HttpError as ex:
                self._logger.exception(ex)
                self._config = None
                return

    def run(self, message, args):
        if not self._config:
            self.reply(message, 'Google command is not configured!')
            return

        if not args:
            self.reply(message, 'Please supply some search terms!')
            return

        try:
            self.bot.telegram.send_chat_action(message.chat.id, 'typing')
            res = self.__service.cse().list(q=' '.join(args), cx=self._config['custom_search_id'], num=5).execute()
        except HttpError as ex:
            self.reply(message, 'Error occurred while fetching search results!')
            self._logger.exception(ex)
            return

        try:
            results = res['items']
            search_information = res['searchInformation']
            reply = 'About {0} results ({1} seconds)\n'.format(search_information['formattedTotalResults'],
                                                               search_information['formattedSearchTime'])
            for idx, result in enumerate(results):
                title = result['title']
                url = result['link']
                display_url = result['displayLink']
                reply += '<b>{0}.</b> <a href="{1}">{2}</a>\n<code>{3}</code>\n'.format(idx + 1, telegram_escape(url),
                                                                                        telegram_escape(title),
                                                                                        telegram_escape(display_url))
        except KeyError:
            try:
                self.reply(message, res['error']['message'])
            except KeyError:
                self.reply(message, 'No results found')
            finally:
                return

        self.reply(message, reply, parse_mode='HTML')
{'fraction_non_alphanumeric': 0.10370647697491576, 'fraction_numerical': 0.0033695245226506927, 'mean_word_length': 1.6741741741741742, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3062132', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1266, 'n_words': 334}