#!/bin/env python

import aiohttp
import discord
from discord.ext import commands
from bs4 import BeautifulSoup
from utils import aiohttp_wrap as aw


class AskAsk:
    def __init__(self, bot):
        self.bot = bot
        self.aio_session = bot.aio_session
        self.scrape_uri = 'http://www.ask.com/web?q={}&o=0&qo=homepageSearchBox'

    def _get_ask_links(html):
        """ Gets all result links from [REDACTED] """
        soup = BeautifulSoup(html, 'lxml')

        link_list = []

        for link in soup.find_all('a', {'class': 'result-link'}, href=True):
            if not link['href'].startswith('//'):
                link_list.append(link['href'])

        if not link_list:
            return None

        return link_list

    @commands.command(name='ask', aliases=['g'])
    async def ask_search(self, ctx, *, query):
        """ Get search results from [REDACTED], now that Google hates me. """

        # Handle no input
        if not query:
            return await ctx.send('Feel free to search something.')

        # Format query for search url
        search_query = query.replace(' ', '+')

        # Get response and store links
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}
        resp_html = await aw.aio_get_text(self.aio_session, self.scrape_uri.format(search_query), headers=headers)

        link_list = AskAsk._get_ask_links(resp_html)

        if not link_list:
            return await ctx.send("Sorry, I couldn't find anything for `{}``.".format(query))

        if len(link_list) >= 3:
            await ctx.send(f'**Top result:**\n{link_list[0]}\n**See Also:**\n1. <{link_list[1]}>\n2. <{link_list[2]}>')
        elif len(link_list) >= 2:
            await ctx.send(f'**Top result:**\n{link_list[0]}\n**See Also:**\n1. <{link_list[1]}>')
        else:
            await ctx.send(f'**Top result:**\n{link_list[0]}')


def setup(bot):
    bot.add_cog(AskAsk(bot))

@@ -24,29 +24,10 @@
 rt a
-iohttp%0Aimport discord
+st
 %0Afro
@@ -234,16 +234,61 @@
 session%0A
+        self.redis_client = bot.redis_client%0A
         
@@ -359,16 +359,158 @@
 archBox'
+%0A        self.headers = %7B'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'%7D
 %0A%0A    de
@@ -1299,174 +1299,344 @@
   # 
-Get response and store links%0A        headers = %7B'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'%7D%0A
+Check if this has been searched in the cache%0A        if await self.redis_client.exists(f'ask:%7Bsearch_query%7D'):%0A            link_list_str = await self.redis_client.get(f'ask:%7Bsearch_query%7D')%0A            link_list = ast.literal_eval(link_list_str)%0A%0A        # Actually request page html and store the link list for 6 hrs%0A        else:%0A    
     
@@ -1746,17 +1746,20 @@
 eaders)%0A
-%0A
+    
         
@@ -1812,32 +1812,143 @@
         
-if not link_list
+    if link_list:%0A                await self.redis_client.set(f'ask:%7Bsearch_query%7D', f'%7Blink_list%7D', ex=21600)%0A            else
 :%0A      
@@ -1945,32 +1945,36 @@
 se:%0A            
+ 
+   
 return await ctx
@@ -1979,16 +1979,17 @@
 tx.send(
+f
 %22Sorry, 
@@ -2023,27 +2023,18 @@
 r %60%7B
+query
 %7D%60%60.%22
-.format(query)
 )%0A%0A 

add caching