import discord
from discord.ext import commands
import aiohttp
import urbandictionary as ud
import wolframalpha
import xml.etree.ElementTree as ET
import requests


client = wolframalpha.Client('T3HV89-2PK4L5Q32Q')


class Web:
    def __init__(self, bot):
        self.bot = bot


    @commands.command()
    async def kat(self, ctx):
        root = ET.fromstring(requests.get('http://thecatapi.com/api/images/get?format=xml').text)
        url = root[0][0][0][0]
        embed = discord.Embed(title=f'Hier is een kat', color=0xFF69B4)
        embed.set_image(url=url.text)
        await ctx.send(embed=embed)


    @commands.command()
    async def hond(self, ctx):
        async with aiohttp.ClientSession() as sess:
            async with sess.get('https://randomdog.ovh/randomdog') as resp:
                data = await resp.json()
                embed = discord.Embed(title=f'Hier is een hond', color=0xFF69B4)
                embed.set_image(url=data['url'])
                await ctx.send(embed=embed)


    @commands.command()
    async def ud(self, ctx, *query):
        f_query = ''
        for p in query:
            f_query += ' ' + p
        defs = ud.define(f_query)
        def_string = defs[0].definition
        await ctx.send(f'_{def_string}_')


    @commands.command(aliases=['wa'])
    async def wolframalpha(self, ctx, *query):
        f_query = ''
        for p in query:
            f_query += ' ' + p
        res = client.query(f_query)
        res_text = next(res.results).text
        await ctx.send(f'`{res_text}`')


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