Ejemplo n.º 1
0
def test_async_413(mocked_aio_response):
    loop = asyncio.get_event_loop()

    mocked_aio_response.post(URL_PATTERN, status=413)

    aio_saucenao = AIOSauceNao()
    with pytest.raises(BadFileSizeError):
        loop.run_until_complete(aio_saucenao.from_url('https://example.com/'))
Ejemplo n.º 2
0
def test_async_429_long_limit_unregister(mocked_aio_response):
    loop = asyncio.get_event_loop()

    mocked_aio_response.post(URL_PATTERN,
                             status=429,
                             payload=e.LongLimitUnregister)

    aio_saucenao = AIOSauceNao()
    with pytest.raises(LongLimitReachedError):
        loop.run_until_complete(aio_saucenao.from_url('https://example.com/'))
Ejemplo n.º 3
0
def test_async_from_url(mocked_aio_response):
    loop = asyncio.get_event_loop()

    def request_callback(url, **kwargs):
        assert kwargs['params']['url'] == 'https://example.com/'
        return CallbackResult(status=500, headers={}, body='')

    mocked_aio_response.post(URL_PATTERN, callback=request_callback)

    aio_saucenao = AIOSauceNao()
    with pytest.raises(UnknownApiError):
        loop.run_until_complete(aio_saucenao.from_url('https://example.com/'))
Ejemplo n.º 4
0
def test_async_bad_api_key(mocked_aio_response):
    loop = asyncio.get_event_loop()

    mocked_aio_response.post(URL_PATTERN,
                             payload={'header': {
                                 'user_id': '0',
                                 'status': 0
                             }})

    aio_saucenao = AIOSauceNao('BadToken')
    with pytest.raises(BadKeyError):
        loop.run_until_complete(aio_saucenao.from_url('https://example.com/'))
Ejemplo n.º 5
0
def test_async_user_id_below_zero(mocked_aio_response):
    loop = asyncio.get_event_loop()

    mocked_aio_response.post(
        URL_PATTERN, payload={'header': {
            'user_id': '-12',
            'status': 0
        }})

    aio_saucenao = AIOSauceNao()
    with pytest.raises(UnknownServerError):
        loop.run_until_complete(aio_saucenao.from_url('https://example.com/'))
Ejemplo n.º 6
0
def test_async_from_file(mocked_aio_response):
    loop = asyncio.get_event_loop()

    def request_callback(url, **kwargs):
        assert bin_file == kwargs['data']['file']
        return CallbackResult(status=500, headers={}, body='')

    mocked_aio_response.post(URL_PATTERN, callback=request_callback)

    with open('tests/test_suite.py', 'rb') as f:
        bin_file = f.read()
        f.seek(0)
        aio_saucenao = AIOSauceNao()
        with pytest.raises(UnknownApiError):
            loop.run_until_complete(aio_saucenao.from_file(bin_file))
Ejemplo n.º 7
0
def test_async_optional_params(mocked_aio_response):
    loop = asyncio.get_event_loop()

    def request_callback(url, **kwargs):
        assert kwargs['params']['api_key'] == 'SauceNAO'
        assert kwargs['params']['dbmask'] == 12
        assert kwargs['params']['dbmaski'] == 918

        return CallbackResult(status=500, headers={}, body='')

    mocked_aio_response.post(URL_PATTERN, callback=request_callback)

    aio_saucenao = AIOSauceNao('SauceNAO', dbmask=12, dbmaski=918)
    with pytest.raises(UnknownApiError):
        loop.run_until_complete(aio_saucenao.from_url('https://example.com/'))
Ejemplo n.º 8
0
def test_async_long_remaining_below_zero(mocked_aio_response):
    loop = asyncio.get_event_loop()

    r = {
        'header': {
            'user_id': '0',
            'status': 0,
            'short_remaining': 0,
            'long_remaining': -1
        }
    }
    mocked_aio_response.post(URL_PATTERN, payload=r)

    aio_saucenao = AIOSauceNao()
    with pytest.raises(LongLimitReachedError):
        loop.run_until_complete(aio_saucenao.from_url('https://example.com/'))
Ejemplo n.º 9
0
    async def saucenao(self, ctx, skip=0):
        """Performs a reverse image query using saucenao on the last uploaded or embedded image"""

        await ctx.trigger_typing()

        async with AIOSauceNao(__saucenao_api_key__) as aio:

            urls = await self.get_recent_image_urls(ctx, skip)

            if not urls:
                return await ctx.send("> couldn't find a recent image.")

            url = urls[0]
            try:

                entries = await aio.from_url(url)

            except (errors.LongLimitReachedError,
                    errors.ShortLimitReachedError, errors.BadFileSizeError,
                    errors.UnknownClientError, errors.UnknownServerError) as e:
                return await ctx.send(str(e))

            if entries.long_remaining == 0:
                return await ctx.send(
                    f"> Bot's saucenao daily quota has reached for the day try tomorrow."
                )

            pages = ctx.menu(self.sauce_source(entries))
            await pages.start(ctx)
Ejemplo n.º 10
0
class SauceWrapper():
    key = b64decode('ZTZlNjFhY2RhYzE4Y2Q4ZWZhOWZhZWQ2MDY3MWY1Njc2NDY2NjE4Zg==')
    client = AIOSauceNao(key.decode('utf-8'))

    async def get_sauce(self, url: str):
        async with self.client as aio:
            results = await aio.from_url(url)

        return results or None

    def generate_embed(self,
                       sauce,
                       max_index: int,
                       curr_index: int,
                       disable_components: bool = False):
        components = [
            Button(custom_id='sauce_first',
                   color='blurple',
                   emoji=UI.first_arrow,
                   disabled=curr_index == 1 or disable_components),
            Button(custom_id='sauce_prev',
                   color='blurple',
                   emoji=UI.previous_arrow,
                   disabled=curr_index == 1 or disable_components),
            Button(custom_id='delete',
                   color='red',
                   label='Delete',
                   disabled=False),
            Button(custom_id='sauce_next',
                   color='blurple',
                   emoji=UI.next_arrow,
                   disabled=max_index == 1 or (curr_index == max_index)
                   or disable_components),
            Button(custom_id='sauce_last',
                   color='blurple',
                   emoji=UI.last_arrow,
                   disabled=max_index == 1 or (curr_index == max_index)
                   or disable_components),
        ]

        embed = Embed(color=randint(0, 0xffffff), title='Hasbi Image Search')
        embed.add_field(name='Author', value=sauce.author, inline=True)
        embed.add_field(name='Title', value=sauce.title, inline=True)
        embed.add_field(name='HD URL', value=sauce.urls[0], inline=False)
        embed.set_image(url=sauce.thumbnail)
        embed.set_footer(text=f'Similarity: {sauce.similarity}%')

        return embed, components