Skip to content

Conversation

@obnoxiousmods
Copy link

@obnoxiousmods obnoxiousmods commented Sep 14, 2024

I was tired of streamrip crashing all the time from Tidal's 429

So I made this, it will automatically retry when a 429 is detected, after waiting 5 seconds anyways

Might be better to add max_retries and retry_delay to the config

( I did not test this too much, but it was working for me )

import asyncio
from typing import Any

import aiohttp
import aiohttp.log


class StreamRipClient(aiohttp.ClientSession):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.max_retries = 5
        self.retry_delay = 5

    async def _request(self, method: str, url: str, **kwargs) -> aiohttp.ClientResponse:
        for attempt in range(self.max_retries):
            try:
                response = await super()._request(method, url, **kwargs)
                if response.status != 429:
                    return response
                else:
                    aiohttp.log.client_logger.warning(
                        f"Rate limited. Retrying in {self.retry_delay} seconds..."
                    )
                    await asyncio.sleep(self.retry_delay)
            except aiohttp.ClientError:
                if attempt == self.max_retries - 1:
                    raise
                aiohttp.log.client_logger.warning(
                    f"Request failed. Retrying in {self.retry_delay} seconds..."
                )
                await asyncio.sleep(self.retry_delay)

        raise aiohttp.ClientError(f"Max retries ({self.max_retries}) exceeded")

@obnoxiousmods
Copy link
Author

rip_url_httpslisten tidal comartist4761990_2024_09_13_18_06_04

Can confirm it seems to work :) If other services dont 429 for rate limiting they will need more code

@nathom
Copy link
Owner

nathom commented Dec 7, 2024

This is great! Can you put the class in client.py? And rename it StreamripClientSession?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants