Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions beetsplug/_utils/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ def setup_adapter(self) -> None:
self.mount("https://", adapter)
self.mount("http://", adapter)

# Genius answers "429 Too Many Requests" when queried too fast, so it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong file for that logic. Genius rate limit should be defined the plugins code if possible. A bit of a strange coupling here.


Do we have any documentation for the rate limit? Is it really static? If it is not I would prefer an immediate retry and than an exponential backoff. We should have utils for this already too.

# gets its own, slower rate limit (with its own timer).
self.mount(
"https://api.genius.com/",
RateLimitAdapter(rate_limit=1.0, max_retries=retry),
)

def request(self, *args, **kwargs) -> requests.Response:
"""Execute HTTP request with automatic timeout and status validation.

Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ New features
Bug fixes
~~~~~~~~~

- :doc:`plugins/lyrics`: Requests to Genius now use their own, slower rate
limit, to reduce ``429 Too Many Requests`` errors during large imports.
:bug:`7226`
- Import: ``duplicate_keys`` that use library field names such as ``mb_albumid``
or ``mb_trackid`` now detect duplicates when a metadata-source match is
applied, instead of treating the key as missing. :bug:`7067`
Expand Down
15 changes: 14 additions & 1 deletion test/plugins/utils/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
import requests

from beetsplug._utils.requests import RateLimitAdapter
from beetsplug._utils.requests import RateLimitAdapter, TimeoutAndRetrySession


def _prepared_request(
Expand Down Expand Up @@ -41,3 +41,16 @@ def test_send_sleeps_for_remaining_time(

assert sleep_mock.call_count == 1
assert sleep_mock.call_args.args[0] == pytest.approx(expected_sleep)


class TestTimeoutAndRetrySession:
def test_genius_has_its_own_slower_rate_limit(self):
session = TimeoutAndRetrySession()

genius = session.get_adapter("https://api.genius.com/search?q=abc")
other = session.get_adapter("https://example.com/")

assert isinstance(genius, RateLimitAdapter)
assert isinstance(other, RateLimitAdapter)
assert genius is not other
assert genius.rate_limit > other.rate_limit
Loading