mirror of
https://github.com/beetbox/beets.git
synced 2026-02-26 09:11:32 +01:00
musicbrainz: access the custom server directly, if configured
This commit is contained in:
parent
6b034da147
commit
ca0b3171cc
2 changed files with 21 additions and 19 deletions
|
|
@ -20,6 +20,7 @@ import operator
|
|||
import traceback
|
||||
from collections import Counter
|
||||
from contextlib import suppress
|
||||
from dataclasses import dataclass
|
||||
from functools import cached_property, singledispatchmethod
|
||||
from itertools import groupby, product
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
|
@ -122,16 +123,18 @@ BROWSE_CHUNKSIZE = 100
|
|||
BROWSE_MAXTRACKS = 500
|
||||
|
||||
|
||||
@dataclass
|
||||
class MusicBrainzAPI:
|
||||
api_url = "https://musicbrainz.org/ws/2"
|
||||
api_host: str
|
||||
rate_limit: float
|
||||
|
||||
@cached_property
|
||||
def session(self) -> LimiterTimeoutSession:
|
||||
return LimiterTimeoutSession(per_second=1)
|
||||
return LimiterTimeoutSession(per_second=self.rate_limit)
|
||||
|
||||
def _get(self, entity: str, **kwargs) -> JSONDict:
|
||||
return self.session.get(
|
||||
f"{self.api_url}/{entity}", params={**kwargs, "fmt": "json"}
|
||||
f"{self.api_host}/ws/2/{entity}", params={**kwargs, "fmt": "json"}
|
||||
).json()
|
||||
|
||||
def get_release(self, id_: str) -> JSONDict:
|
||||
|
|
@ -420,7 +423,17 @@ class MusicBrainzPlugin(MetadataSourcePlugin):
|
|||
|
||||
@cached_property
|
||||
def api(self) -> MusicBrainzAPI:
|
||||
return MusicBrainzAPI()
|
||||
hostname = self.config["host"].as_str()
|
||||
if hostname == "musicbrainz.org":
|
||||
hostname, rate_limit = "https://musicbrainz.org", 1.0
|
||||
else:
|
||||
https = self.config["https"].get(bool)
|
||||
hostname = f"http{'s' if https else ''}://{hostname}"
|
||||
rate_limit = (
|
||||
self.config["ratelimit"].get(int)
|
||||
/ self.config["ratelimit_interval"].as_number()
|
||||
)
|
||||
return MusicBrainzAPI(hostname, rate_limit)
|
||||
|
||||
def __init__(self):
|
||||
"""Set up the python-musicbrainz-ngs module according to settings
|
||||
|
|
@ -455,16 +468,6 @@ class MusicBrainzPlugin(MetadataSourcePlugin):
|
|||
"'musicbrainz.searchlimit' configuration option",
|
||||
"'musicbrainz.search_limit'",
|
||||
)
|
||||
hostname = self.config["host"].as_str()
|
||||
https = self.config["https"].get(bool)
|
||||
# Only call set_hostname when a custom server is configured. Since
|
||||
# musicbrainz-ngs connects to musicbrainz.org with HTTPS by default
|
||||
if hostname != "musicbrainz.org":
|
||||
musicbrainzngs.set_hostname(hostname, https)
|
||||
musicbrainzngs.set_rate_limit(
|
||||
self.config["ratelimit_interval"].as_number(),
|
||||
self.config["ratelimit"].get(int),
|
||||
)
|
||||
|
||||
def track_info(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -69,15 +69,14 @@ Default
|
|||
.. conf:: ratelimit
|
||||
:default: 1
|
||||
|
||||
Controls the number of Web service requests per second.
|
||||
|
||||
**Do not change the rate limit setting** if you're using the main MusicBrainz
|
||||
server---on this public server, you're limited_ to one request per second.
|
||||
Controls the number of Web service requests per second. This setting applies only
|
||||
to custom servers. The official MusicBrainz server enforces a rate limit of 1
|
||||
request per second.
|
||||
|
||||
.. conf:: ratelimit_interval
|
||||
:default: 1.0
|
||||
|
||||
The time interval (in seconds) for the rate limit.
|
||||
The time interval (in seconds) for the rate limit. Only applies to custom servers.
|
||||
|
||||
.. conf:: enabled
|
||||
:default: yes
|
||||
|
|
|
|||
Loading…
Reference in a new issue