Cambiada la estructura de la api

No era muy util como estaba antes, por lo que siplifique los modelos y
proceso levemente lo que entrega musicbrainz
This commit is contained in:
Daniel Cortes
2020-05-22 06:28:39 -04:00
parent 3568abfbc7
commit a37d51b120
4 changed files with 293 additions and 39 deletions

View File

@@ -3,7 +3,7 @@ import requests
from ratelimit import limits, sleep_and_retry
from urllib.parse import quote, urlencode
from utils import replace_key, sanitize_keys
from utils import replace_key, sanitize_keys, pretty_print_json
from utils.cache import Cache as cache
_headers = {'accept': 'application/json', 'user-agent': 'MusicList/1.0 (danielcortes.xyz)'}
@@ -16,7 +16,7 @@ _log.addHandler(logging.NullHandler())
@sleep_and_retry
@limits(calls=1, period=1)
def _do_request(url, allow_redirects=True):
def _do_request(url, host=_mb_host):
"""Does a request to a path and returns the dictionary representing the json response
If the response is 200 it will return the full dictionary of the json that the response
@@ -42,20 +42,25 @@ def _do_request(url, allow_redirects=True):
raise ValueError('URL cant be empty')
_log.info(f'Doing request to "{url}" with headers {_headers}')
r = requests.get(url, headers=_headers, allow_redirects=allow_redirects)
r = requests.get(url, headers=_headers)
_log.info(f'Request returned with status code {r.status_code}')
if r.status_code == 200:
response = r.json(object_hook=sanitize_keys)
elif r.status_code == 307:
response = {'link': r.headers['location']}
elif r.status_code == 404:
response = {'status': r.status_code, 'error': f'No encontrado'}
elif r.status_code == 400:
response = {'status': r.status_code, 'error': f'Query erronea'}
elif r.status_code == 500:
response = {'status': r.status_code, 'error': f'Error del servidor'}
elif host == _ca_host:
if r.status_code == 400:
response = {'status': r.status_code, 'error': f'El mbid es invalido'}
elif r.status_code == 404:
response = {'status': r.status_code, 'error': f'No encontrado'}
elif r.status_code == 405:
response = {'status': r.status_code, 'error': f'Metodo erroneo'}
elif r.status_code == 503:
response = {'status': r.status_code, 'error': f'Rate limit exedido'}
else:
response = {'status': r.status_code, 'error': f'Error desconocido de musicbrainz'}
response = {'status': r.status_code, 'error': r.json()['error']}
return response
@@ -132,24 +137,20 @@ def _browse(entity_type, params, includes=None, limit=25, offset=0):
return _do_request(f'{_mb_host}/{entity_type}?{_query}')
def _ca(entity_type, mbid, size=None):
def _ca(entity_type, mbid):
"""Gets the url of the cover art of a release or release-group
:param str entity_type: Type of the entity, could be release or release-group
:param str mbid: MBID of the entity of whom is the cover art
:param int size: Optional size of the cover art, could be 250, 500 or 1200
any other value will be ignored
:return: The url of the cover art
"""
_log.info(f'Obtaining the cover art of the entity with type {entity_type} and mbid {mbid} with size {size}')
_log.info(f'Obtaining the cover art of the entity with type {entity_type} and mbid {mbid}')
_url = f'{_ca_host}/{entity_type}/{mbid}/front'
if size in (250, 500, 1200):
_url += f'-{size}'
_url = f'{_ca_host}/{entity_type}/{mbid}'
return _do_request(_url, allow_redirects=False)
return _do_request(_url, host=_ca_host)
@cache
@@ -321,24 +322,22 @@ def browse_release_groups(params, includes=None, limit=25, offset=0):
@cache
def get_release_cover_art(mbid, size=None):
def get_release_cover_art(mbid):
"""Gets the url of the cover art of a release
:param str mbid: MBID of the release of whom is the cover art
:param int size: Optional size of the cover art, could be 250, 500 or 1200
:return: dictionary with the response
"""
return _ca('release', mbid, size)
return _ca('release', mbid)
@cache
def get_release_group_cover_art(mbid, size=None):
def get_release_group_cover_art(mbid):
"""Gets the url of the cover art of a release group
:param str mbid: MBID of the release group of whom is the cover art
:param int size: Optional size of the cover art, could be 250, 500 or 1200
:return: dictionary with the response
"""
return _ca('release-group', mbid, size)
return _ca('release-group', mbid)