<commit_before>import logging
import asyncio
import random
import datetime

import discord
import wolframalpha
import pyowm

from discord.ext import commands

from .common import Cog

log = logging.getLogger(__name__)

W_CLEAR_SKY =           ':sunny:'
W_FEW_CLOUDS =          ':white_sun_small_cloud:'
W_SCATTERED_CLOUDS =    ':partly_sunny:'
W_BROKEN_CLOUDS =       ':cloud:'
W_SHOWER_RAIN =         ':cloud_rain:'
W_RAIN =                ':cloud_rain:'
W_THUNDERSTORM =        ':thunder_cloud_rain:'
W_SNOW =                ':snowflake:'
W_MIST =                ':foggy:'

OWM_ICONS = {
    '01d': W_CLEAR_SKY,
    '02d': W_FEW_CLOUDS,
    '03d': W_SCATTERED_CLOUDS,
    '04d': W_BROKEN_CLOUDS,
    '09d': W_SHOWER_RAIN,
    '10d': W_RAIN,
    '11d': W_THUNDERSTORM,
    '13d': W_SNOW,
    '50d': W_MIST,
}

W_API_ICONS = {
    'tornado': ':cloud_tornado:',
    'tropical-storm': ':cyclone:',
    'thunderstorm': ':thunder_cloud_rain:',
    'rain-snow': ':cloud_rain: :snowflake:',
    'rain-hail': ':cloud_rain: *(hail)*',
    'freezing-drizzle': ':cloud_rain: *(freezing)*',
    'scattered-showers': ':white_sun_rain_cloud:',
    'rain': ':cloud_rain:',
    'flurries': ':cloud_snow:',
    'snow': ':cloud_snow:',
    'blowing-snow': ':cloud_snow: :dash:',
    'hail': '*(hail)*',
    'fog': ':fog: *(fog)*',
    'wind': ':dash:',
    'cloudy': ':cloud:',
    'mostly-cloudy-night': ':partly_sunny:',
    'mostly-cloudy': ':partly_sunny:',
    'partly-cloudy-night': ':white_sun_small_cloud:',
    'partly-cloudy': ':white_sun_small_cloud:',
    'clear-night': ':full_moon:',
    'sunny': ':sunny:',
    'mostly-clear-night': ':full_moon: :cloud: *(partly cloudy)*',
    'mostly-sunny': ':white_sun_small_cloud:',
    'isolated-thunderstorms': ':thunder_cloud_rain:',
    'scattered-thunderstorms': ':thunder_cloud_rain:',
    'heavy-rain': ':cloud_rain: *(heavy)*',
    'scattered-snow': ':cloud_snow: *(scattered)*',
    'heavy-snow': ':cloud_snow: *(heavy)*',
    'na': ':no_entry_sign: *(na)*',
    'scattered-showers-night': ':white_sun_rain_cloud:',
    'scattered-snow-night': ':cloud_snow: *(scattered)*',
    'scattered-thunderstorms-night': ':thunder_cloud_rain:'
}

class Math(Cog):
    """Math related commands."""
    def __init__(self, bot):
        super().__init__(bot)

        self.wac = wolframalpha.Client(self.bot.config.WOLFRAMALPHA_APP_ID)
        self.owm = pyowm.OWM(self.bot.config.OWM_APIKEY)
        self.w_config = getattr(self.bot.config, 'WEATHER_API', None)
        
        if self.w_config is not None:
            self.w_api_base_url = self.w_config['base_url']
            self.w_api_base_payload = {
                'key': self.w_config['key'],
                'secret': self.w_config['secret'],
            }


    @commands.command(aliases=['wa'])
    async def wolframalpha(self, ctx, *, term: str):
        """Query Wolfram|Alpha"""
        if len(term) < 1:
            await ctx.send("haha no")
            return

        await self.jcoin.pricing(ctx, self.prices['API'])

        log.info('Wolfram|Alpha: %s', term)

        future = self.loop.run_in_executor(None, self.wac.query, term)
        res = None

        async with ctx.typing():
            try:
                res = await asyncio.wait_for(future, 13)
            except asyncio.TimeoutError:
                await ctx.send('⏳ Timeout reached')
                return
            except Exception as err:
                await ctx.send(f':cry: {err!r}')
                return

        if res is None:
            return

        if getattr(res, 'results', False):
            pods = (pod for pod in res.pods)
            pod = next(pods)
            while pod.title == 'Input interpretation':
                pod = next(pods)
            text = None

            if getattr(pod, 'text', False):
                text = pod.text
            elif pod.get('subpod', False):
                subpod = pod['subpod']
                text = subpod['img']['@src']
            else:
                text = None

            if text is not None:
                await ctx.send(f'{term}:\n{text}')
            else:
                await ctx.send(f':poop: `{pod!r}`')
            return
        else:
            await ctx.send(f'{ctx.author.mention}, :cyclone: No answer :cyclone:')
            return

    #@commands.command(aliases=['temperature'])
    async def weather(self, ctx, location: str):
        """Get weather data for a location."""

        await self.jcoin.pricing(ctx, self.prices['API'])

        try:
            future = self.loop.run_in_executor(None, \
                self.owm.weather_at_place, location)
            observation = await future
        except:
            await ctx.send('Error retrieving weather data')
            return

        w = observation.get_weather()
        _wg = lambda t: w.get_temperature(t)['temp']

        _icon = w.get_weather_icon_name()
        icon = OWM_ICONS.get(_icon, '*<no icon>*')
        status = w.get_detailed_status()

        res = [f'{location}']
        res.append(f' - Status: `{status}` : {icon}')
        res.append(f' - `{_wg("celsius")} °C, {_wg("fahrenheit")} °F, {_wg("kelvin")} °K`')

        await ctx.send('\n'.join(res))

    @commands.command()
    async def money(self, ctx, amount: str, currency_from: str = '', currency_to: str = ''):
        """Convert currencies."""

        currency_from = currency_from.upper()
        currency_to = currency_to.upper()

        if amount == 'list':
            data = await self.get_json('http://api.fixer.io/latest')
            res = ' '.join(data["rates"].keys())
            await ctx.send(f'```\n{res}\n```')
            return

        try:<commit_msg>math: revamp owm<commit_after>import logging
import asyncio
import random
import datetime

import discord
import wolframalpha
import pyowm

from discord.ext import commands

from .common import Cog

log = logging.getLogger(__name__)

W_CLEAR_SKY =           ':sunny:'
W_FEW_CLOUDS =          ':white_sun_small_cloud:'
W_SCATTERED_CLOUDS =    ':partly_sunny:'
W_BROKEN_CLOUDS =       ':cloud:'
W_SHOWER_RAIN =         ':cloud_rain:'
W_RAIN =                ':cloud_rain:'
W_THUNDERSTORM =        ':thunder_cloud_rain:'
W_SNOW =                ':snowflake:'
W_MIST =                ':foggy:'

OWM_ICONS = {
    '01d': W_CLEAR_SKY,
    '02d': W_FEW_CLOUDS,
    '03d': W_SCATTERED_CLOUDS,
    '04d': W_BROKEN_CLOUDS,
    '09d': W_SHOWER_RAIN,
    '10d': W_RAIN,
    '11d': W_THUNDERSTORM,
    '13d': W_SNOW,
    '50d': W_MIST,
}

W_API_ICONS = {
    'tornado': ':cloud_tornado:',
    'tropical-storm': ':cyclone:',
    'thunderstorm': ':thunder_cloud_rain:',
    'rain-snow': ':cloud_rain: :snowflake:',
    'rain-hail': ':cloud_rain: *(hail)*',
    'freezing-drizzle': ':cloud_rain: *(freezing)*',
    'scattered-showers': ':white_sun_rain_cloud:',
    'rain': ':cloud_rain:',
    'flurries': ':cloud_snow:',
    'snow': ':cloud_snow:',
    'blowing-snow': ':cloud_snow: :dash:',
    'hail': '*(hail)*',
    'fog': ':fog: *(fog)*',
    'wind': ':dash:',
    'cloudy': ':cloud:',
    'mostly-cloudy-night': ':partly_sunny:',
    'mostly-cloudy': ':partly_sunny:',
    'partly-cloudy-night': ':white_sun_small_cloud:',
    'partly-cloudy': ':white_sun_small_cloud:',
    'clear-night': ':full_moon:',
    'sunny': ':sunny:',
    'mostly-clear-night': ':full_moon: :cloud: *(partly cloudy)*',
    'mostly-sunny': ':white_sun_small_cloud:',
    'isolated-thunderstorms': ':thunder_cloud_rain:',
    'scattered-thunderstorms': ':thunder_cloud_rain:',
    'heavy-rain': ':cloud_rain: *(heavy)*',
    'scattered-snow': ':cloud_snow: *(scattered)*',
    'heavy-snow': ':cloud_snow: *(heavy)*',
    'na': ':no_entry_sign: *(na)*',
    'scattered-showers-night': ':white_sun_rain_cloud:',
    'scattered-snow-night': ':cloud_snow: *(scattered)*',
    'scattered-thunderstorms-night': ':thunder_cloud_rain:'
}

class Math(Cog):
    """Math related commands."""
    def __init__(self, bot):
        super().__init__(bot)

        self.wac = wolframalpha.Client(self.bot.config.WOLFRAMALPHA_APP_ID)
        self.owm = pyowm.OWM(self.bot.config.OWM_APIKEY)
        self.w_config = getattr(self.bot.config, 'WEATHER_API', None)
        
        if self.w_config is not None:
            self.w_api_base_url = self.w_config['base_url']
            self.w_api_base_payload = {
                'key': self.w_config['key'],
                'secret': self.w_config['secret'],
            }


    @commands.command(aliases=['wa'])
    async def wolframalpha(self, ctx, *, term: str):
        """Query Wolfram|Alpha"""
        if len(term) < 1:
            await ctx.send("haha no")
            return

        await self.jcoin.pricing(ctx, self.prices['API'])

        log.info('Wolfram|Alpha: %s', term)

        future = self.loop.run_in_executor(None, self.wac.query, term)
        res = None

        async with ctx.typing():
            try:
                res = await asyncio.wait_for(future, 13)
            except asyncio.TimeoutError:
                await ctx.send('⏳ Timeout reached')
                return
            except Exception as err:
                await ctx.send(f':cry: {err!r}')
                return

        if res is None:
            return

        if getattr(res, 'results', False):
            pods = (pod for pod in res.pods)
            pod = next(pods)
            while pod.title == 'Input interpretation':
                pod = next(pods)
            text = None

            if getattr(pod, 'text', False):
                text = pod.text
            elif pod.get('subpod', False):
                subpod = pod['subpod']
                text = subpod['img']['@src']
            else:
                text = None

            if text is not None:
                await ctx.send(f'{term}:\n{text}')
            else:
                await ctx.send(f':poop: `{pod!r}`')
            return
        else:
            await ctx.send(f'{ctx.author.mention}, :cyclone: No answer :cyclone:')
            return

    <EMAIL>.command(aliases=['temperature'])
    async def weather(self, ctx, location: str):
        """Get weather data for a location."""

        await self.jcoin.pricing(ctx, self.prices['API'])

        try:
            future = self.loop.run_in_executor(None, \
                self.owm.weather_at_place, location)
            observation = await future
        except:
            await ctx.send('Error retrieving weather data')
            return

        w = observation.get_weather()
        _wg = lambda t: w.get_temperature(t)['temp']

        _icon = w.get_weather_icon_name()
        icon = OWM_ICONS.get(_icon, '*<no icon>*')
        status = w.get_detailed_status()

        em = discord.Embed(title=f"Weather for '{location}'")

        o_location = observation.get_location()

        em.add_field(name='Location', value=f'{o_location.get_name()}')
        em.add_field(name='Situation', value=f'{status} {icon}')
        em.add_field(name='Temperature', value=f'`{_wg("celsius")} °C, {_wg("fahrenheit")} °F, {_wg("kelvin")} °K`')

        await ctx.send(embed=em)

    @commands.command()
    async def money(self, ctx, amount: str, currency_from: str = '', currency_to: str = ''):
        """Convert currencies."""

        currency_from = currency_from.upper()
        currency_to = currency_to.upper()

        if amount == 'list':
            data = await self.get_json('http://api.fixer.io/latest')
            res = ' '.join(data["rates"].keys())
            await ctx.send(f'```\n{res}\n```')
            return

        try:
{'fraction_non_alphanumeric': 0.15977508650519032, 'fraction_numerical': 0.003633217993079585, 'mean_word_length': 2.345196759259259, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, 'https://': 0, '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': '1112146', 'n_tokens_mistral': 4257, 'n_tokens_neox': 3982, 'n_words': 744}