Ejemplo n.º 1
0
 async def i(self, ctx, *, query):
     """Google image search. [p]i Lillie pokemon sun and moon"""
     await ctx.message.delete()
     config = load_optional_config()
     if query[0].isdigit():
         item = int(query[0])
         query = query[1:]
     else:
         item = 0
     async with aiohttp.ClientSession() as session:
         async with session.get("https://www.googleapis.com/customsearch/v1?q=" + urllib.parse.quote_plus(query) + "&start=" + '1' + "&key=" + config['google_api_key'] + "&cx=" + config['custom_search_engine'] + "&searchType=image") as resp:
             if resp.status != 200:
                 if not config['google_api_key'] or not config['custom_search_engine']:
                     return await ctx.send(self.bot.bot_prefix + "You don't seem to have image searching configured properly. Refer to the wiki for details.")
                 return await ctx.send(self.bot.bot_prefix + 'Google failed to respond.')
             else:
                 result = json.loads(await resp.text())
                 try:
                     result['items']
                 except:
                     return await ctx.send(self.bot.bot_prefix + 'There were no results to your search. Use more common search query or make sure you have image search enabled for your custom search engine.')
                 if len(result['items']) < 1:
                     return await ctx.send(self.bot.bot_prefix + 'There were no results to your search. Use more common search query or make sure you have image search enabled for your custom search engine.')
                 em = discord.Embed()
                 if embed_perms(ctx.message):
                     em.set_image(url=result['items'][item]['link'])
                     show_search = get_config_value("optional_config", "show_search_term")
                     if show_search == "True":
                         em.set_footer(text="Search term: \"" + query + "\"")
                     await ctx.send(content=None, embed=em)
                 else:
                     await ctx.send(result['items'][item]['link'])
                     await ctx.send("Search term: \"" + query + "\"")
Ejemplo n.º 2
0
    async def g(self, ctx, *, query):
        """Google web search. Ex: [p]g what is discordapp?"""
        if not embed_perms(ctx.message):
            config = load_optional_config()
            async with aiohttp.ClientSession() as session:
                async with session.get("https://www.googleapis.com/customsearch/v1?q=" + urllib.parse.quote_plus(query) + "&start=" + '1' + "&key=" + config['google_api_key'] + "&cx=" + config['custom_search_engine']) as resp:
                    result = json.loads(await resp.text())
            return await ctx.send(result['items'][0]['link'])

        try:
            entries, root = await get_google_entries(query)
            card_node = root.find(".//div[@id='topstuff']")
            card = self.parse_google_card(card_node)
        except RuntimeError as e:
            await ctx.send(str(e))
        else:
            if card:
                value = '\n'.join(entries[:2])
                if value:
                    card.add_field(name='Search Results', value=value, inline=False)
                return await ctx.send(embed=card)
            if len(entries) == 0:
                return await ctx.send('No results.')
            next_two = entries[1:3]
            if next_two:
                formatted = '\n'.join(map(lambda x: '<%s>' % x, next_two))
                msg = '{}\n\n**See also:**\n{}'.format(entries[0], formatted)
            else:
                msg = entries[0]
            await ctx.send(msg)
Ejemplo n.º 3
0
 async def i(self, ctx, *, query):
     """Google image search. [p]i Lillie pokemon sun and moon"""
     await ctx.message.delete()
     config = load_optional_config()
     if query[0].isdigit():
         item = int(query[0])
         query = query[1:]
     else:
         item = 0
     async with self.bot.session.get("https://www.googleapis.com/customsearch/v1?q=" + urllib.parse.quote_plus(query) + "&start=" + '1' + "&key=" + config['google_api_key'] + "&cx=" + config['custom_search_engine'] + "&searchType=image") as resp:
         if resp.status != 200:
             if not config['google_api_key'] or not config['custom_search_engine']:
                 return await ctx.send(self.bot.bot_prefix + "You don't seem to have image searching configured properly. Refer to the wiki for details.")
             return await ctx.send(self.bot.bot_prefix + 'Google failed to respond.')
         else:
             result = json.loads(await resp.text())
             try:
                 result['items']
             except:
                 return await ctx.send(self.bot.bot_prefix + 'There were no results to your search. Use more common search query or make sure you have image search enabled for your custom search engine.')
             if len(result['items']) < 1:
                 return await ctx.send(self.bot.bot_prefix + 'There were no results to your search. Use more common search query or make sure you have image search enabled for your custom search engine.')
             em = discord.Embed()
             if embed_perms(ctx.message):
                 em.set_image(url=result['items'][item]['link'])
                 show_search = get_config_value("optional_config", "show_search_term")
                 if show_search == "True":
                     em.set_footer(text="Search term: \"" + query + "\"")
                 await ctx.send(content=None, embed=em)
             else:
                 await ctx.send(result['items'][item]['link'])
                 await ctx.send("Search term: \"" + query + "\"")
Ejemplo n.º 4
0
    async def g(self, ctx, *, query):
        """Google web search. Ex: [p]g what is discordapp?"""
        if not embed_perms(ctx.message):
            config = load_optional_config()
            async with self.bot.session.get("https://www.googleapis.com/customsearch/v1?q=" + urllib.parse.quote_plus(query) + "&start=1" + "&key=" + config['google_api_key'] + "&cx=" + config['custom_search_engine']) as resp:
                result = json.loads(await resp.text())
            return await ctx.send(result['items'][0]['link'])

        try:
            entries, root = await get_google_entries(query, session=self.bot.session)
            card_node = root.find(".//div[@id='topstuff']")
            card = self.parse_google_card(card_node)
        except RuntimeError as e:
            await ctx.send(str(e))
        else:
            if card:
                value = '\n'.join(entries[:2])
                if value:
                    card.add_field(name='Search Results', value=value, inline=False)
                return await ctx.send(embed=card)
            if not entries:
                return await ctx.send('No results.')
            next_two = entries[1:3]
            if next_two:
                formatted = '\n'.join(map(lambda x: '<%s>' % x, next_two))
                msg = '{}\n\n**See also:**\n{}'.format(entries[0], formatted)
            else:
                msg = entries[0]
            await ctx.send(msg)
Ejemplo n.º 5
0
    async def google_results(type, query):
        loop = asyncio.get_event_loop()
        config = load_optional_config()
        try:
            entries, root = await get_google_entries('site:myanimelist.net {} {}'.format(type, query))
            result = entries[0]
        except RuntimeError:
            try:
                search_url = "https://www.googleapis.com/customsearch/v1?q=site:myanimelist.net {} {} ".format(type, query) + "&start=" + '1' + "&key=" + \
                            config['google_api_key'] + "&cx=" + config[
                                'custom_search_engine']
                r = await loop.run_in_executor(None, requests.get, search_url)
                response = r.content.decode('utf-8')
                result = json.loads(response)['items'][0]['link']
            except:
                return False, None

        return True, result
Ejemplo n.º 6
0
    async def google_results(type, query):
        loop = asyncio.get_event_loop()
        config = load_optional_config()
        try:
            entries, root = await get_google_entries('site:myanimelist.net {} {}'.format(type, query))
            result = entries[0]
        except RuntimeError:
            try:
                search_url = "https://www.googleapis.com/customsearch/v1?q=site:myanimelist.net {} {} ".format(type, query) + "&start=" + '1' + "&key=" + \
                            config['google_api_key'] + "&cx=" + config[
                                'custom_search_engine']
                r = await loop.run_in_executor(None, requests.get, search_url)
                response = r.content.decode('utf-8')
                result = json.loads(response)['items'][0]['link']
            except:
                return False, None

        return True, result