<commit_before>import logging
import re
import random
import hashlib
import urllib.parse
import datetime
import collections
import time
import json

import discord

from discord.ext import commands

from .common import Cog

log = logging.getLogger(__name__)

RXKCD_ENDPOINT = 'http://0.0.0.0:8080/search'

class Extra(Cog):
    """Extra commands that don't fit in any other cogs."""
    def __init__(self, bot):
        super().__init__(bot)

        self.description_coll = self.config.jose_db['descriptions']

        self.socket_stats = collections.Counter()
        self.sock_start = time.monotonic()

    async def on_socket_response(self, data):
        self.socket_stats[data.get('t')] += 1

    @commands.command()
    async def avatar(self, ctx, person: discord.User = None):
        """Get someone's avatar."""
        if person is None:
            person = ctx.author

        await ctx.send(person.avatar_url.replace('webp', 'png'))

    @commands.command()
    async def xkcd(self, ctx, number: str = ''):
        """Get XKCD shit."""

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

        with ctx.typing():
            url = 'https://xkcd.com/info.0.json'
            info = await self.get_json(url)

            try:
                as_int = int(number)
            except:
                as_int = -1

            if number == 'rand':
                random_num = random.randint(0, info['num'])
                info = await self.get_json(f'https://xkcd.com/{random_num}/info.0.json')
            elif as_int > 0:
                info = await self.get_json(f'https://xkcd.com/{number}/info.0.json')

        await ctx.send(f'xkcd {info["num"]} => {info["img"]}')

    async def _do_rxkcd(self, ctx, terms):
        async with self.bot.session.post(RXKCD_ENDPOINT, json={'search': terms}) as r:
            if r.status != 200:
                raise self.SayException(f'Got a not good error code: {r.code}')

            data = await r.text()
            data = json.loads(data)
            if not data['success']:
                raise self.SayException(f'XKCD retrieval failed: {data["message"]!r}')

            if len(data['results']) < 1:
                raise self.SayException('No comics found.')

            comic = data['results'][0]
            
            em = discord.Embed(title=f'Relevant XKCD for {terms!r}')
            em.description = f'XKCD {comic["number"]}, {comic["title"]}'
            em.set_image(url=comic['image'])

            await ctx.send(embed=em)

    @commands.command()
    async def rxkcd(self, ctx, *, terms: str):
        """Get a Relevant XKCD.
        
        Made with https://github.com/adtac/relevant-xkcd
        """
        try:<commit_msg>extra: debug rxkcd<commit_after>import logging
import re
import random
import hashlib
import urllib.parse
import datetime
import collections
import time
import json

import discord

from discord.ext import commands

from .common import Cog

log = logging.getLogger(__name__)

RXKCD_ENDPOINT = 'http://0.0.0.0:8080/search'

class Extra(Cog):
    """Extra commands that don't fit in any other cogs."""
    def __init__(self, bot):
        super().__init__(bot)

        self.description_coll = self.config.jose_db['descriptions']

        self.socket_stats = collections.Counter()
        self.sock_start = time.monotonic()

    async def on_socket_response(self, data):
        self.socket_stats[data.get('t')] += 1

    @commands.command()
    async def avatar(self, ctx, person: discord.User = None):
        """Get someone's avatar."""
        if person is None:
            person = ctx.author

        await ctx.send(person.avatar_url.replace('webp', 'png'))

    @commands.command()
    async def xkcd(self, ctx, number: str = ''):
        """Get XKCD shit."""

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

        with ctx.typing():
            url = 'https://xkcd.com/info.0.json'
            info = await self.get_json(url)

            try:
                as_int = int(number)
            except:
                as_int = -1

            if number == 'rand':
                random_num = random.randint(0, info['num'])
                info = await self.get_json(f'https://xkcd.com/{random_num}/info.0.json')
            elif as_int > 0:
                info = await self.get_json(f'https://xkcd.com/{number}/info.0.json')

        await ctx.send(f'xkcd {info["num"]} => {info["img"]}')

    async def _do_rxkcd(self, ctx, terms):
        async with self.bot.session.post(RXKCD_ENDPOINT, json={'search': terms}) as r:
            if r.status != 200:
                raise self.SayException(f'Got a not good error code: {r.code}')

            data = await r.text()
            data = json.loads(data)
            if not data['success']:
                raise self.SayException(f'XKCD retrieval failed: {data["message"]!r}')

            if len(data['results']) < 1:
                raise self.SayException('No comics found.')

            comic = data['results'][0]
            
            em = discord.Embed(title=f'Relevant XKCD for {terms!r}')
            em.description = f'XKCD {comic["number"]}, {comic["title"]}'
            em.set_image(url=comic['image'])

            await ctx.send(embed=em)

    async def _do_rxkcd_debug(self, ctx):
        async with self.bot.session.post(RXKCD_ENDPOINT, json={'search': 'standards'}) as r:
            await ctx.send(repr(r))
            await ctx.send(await r.text())

    @commands.command()
    async def rxkcd(self, ctx, *, terms: str):
        """Get a Relevant XKCD.
        
        Made with https://github.com/adtac/relevant-xkcd
        """
        try:
            if '-debug' in terms:
                return await self._do_rxkcd_debug(ctx)

{'fraction_non_alphanumeric': 0.1362850224835697, 'fraction_numerical': 0.0069180214458664825, 'mean_word_length': 2.1395222584147664, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4685777', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1787, 'n_words': 420}