tilie_server.musicbrainz#

A client of the MusicBrainz web service, within its rules of use.

MusicBrainz asks every client of https://musicbrainz.org/ws/2/ for at most one request per second and a User-Agent naming the application and where to learn about it. This client keeps both for the whole server: one MusicBrainz per application sends its requests one after the other, each at least MusicBrainz.MIN_INTERVAL seconds after the previous one, and answers a URL it has fetched before from its cache without asking again. The User-Agent names the server and its repository, never a person.

The fetch, the clock and the sleep are injected, so the rules can be proved without the network and without waiting.

tilie_server.musicbrainz.Fetch#

Fetch a URL with the given headers; answer the status and the body.

A fetch that gets no answer raises OSError (urllib’s URLError and TimeoutError are both).

alias of Callable[[str, Mapping[str, str]], tuple[int, bytes]]

tilie_server.musicbrainz.BASE_URL = 'https://musicbrainz.org/ws/2/'#

The MusicBrainz web service.

tilie_server.musicbrainz.USER_AGENT = 'tilie-server/0.3.0 ( https://github.com/TimeToAlign/TimeLineEditor )'#

The application and where to learn about it; no personal contact data.

tilie_server.musicbrainz.TIMEOUT = 10.0#

Seconds a request may wait for an answer.

tilie_server.musicbrainz.ENTITY_TYPES: Mapping[str, str] = {'artist': 'artists', 'recording': 'recordings', 'release-group': 'release-groups', 'work': 'works'}#

The entity types searched, each with the member listing its search results.

tilie_server.musicbrainz.LUCENE_SPECIAL = frozenset({'!', '"', '&', '(', ')', '*', '+', '-', '/', ':', '?', '[', '\\', ']', '^', '{', '|', '}', '~'})#

The characters the search syntax gives a meaning, each escaped with \.

exception tilie_server.musicbrainz.UpstreamError[source]#

Bases: Exception

MusicBrainz could not answer a request; the message says why.

tilie_server.musicbrainz.lucene_escaped(text)[source]#

Escape every character the search syntax gives a meaning.

The text is then searched for as written, never read as a query.

Parameters:

text (str)

Return type:

str

tilie_server.musicbrainz.search_url(entity_type, text, limit, offset)[source]#

The URL that searches entity_type for text.

Parameters:
Return type:

str

tilie_server.musicbrainz.lookup_url(entity_type, mbid)[source]#

The URL of one entity with its URL relationships.

Parameters:
  • entity_type (str)

  • mbid (str)

Return type:

str

tilie_server.musicbrainz.urllib_fetch(url, headers)[source]#

Fetch url with urllib, waiting at most TIMEOUT seconds.

An answer of any status is returned; no answer raises OSError.

Parameters:
Return type:

tuple[int, bytes]

class tilie_server.musicbrainz.MusicBrainz(fetch=<function urllib_fetch>, *, clock=<built-in function monotonic>, sleep=<function sleep>, user_agent='tilie-server/0.3.0 ( https://github.com/TimeToAlign/TimeLineEditor )')[source]#

Bases: object

The server’s one way to MusicBrainz: paced, identified and cached.

Variables:
  • MIN_INTERVAL (ClassVar[float]) – Seconds between the starts of two upstream requests.

  • CACHE_SIZE (ClassVar[int]) – Answers kept for the run; the least recently used goes first.

Parameters:
MIN_INTERVAL: ClassVar[float] = 1.0#
CACHE_SIZE: ClassVar[int] = 1024#
async get(url)[source]#

Answer the JSON at url, from the cache or from MusicBrainz.

Raises:

UpstreamError – If MusicBrainz answers another status than 200, answers no JSON, or cannot be reached. Failures are not cached.

Parameters:

url (str)

Return type:

Any