Cambios para satisfacer a pylint

This commit is contained in:
Daniel Cortes
2020-06-07 18:37:29 -04:00
parent 5a5a96a365
commit 70b9c10229
6 changed files with 207 additions and 130 deletions

View File

@@ -1,19 +1,19 @@
"""
Modulo que se encarga de realizar requests a musicbrainz, mayormente devuelve el resultado que musicbrainz entrega
exceptuando los errores.
Modulo que se encarga de realizar requests a musicbrainz, mayormente devuelve el resultado que
musicbrainz entrega exceptuando los errores.
"""
from urllib.parse import quote, urlencode
import logging
import requests
from ratelimit import limits, sleep_and_retry
from urllib.parse import quote, urlencode
from utils import replace_key, sanitize_keys, pretty_print_json
from utils import sanitize_keys
from utils.cache import Cache as cache
_headers = {'accept': 'application/json', 'user-agent': 'MusicList/1.0 (danielcortes.xyz)'}
_mb_host = 'https://musicbrainz.org/ws/2'
_ca_host = 'http://coverartarchive.org'
HEADERS = {'accept': 'application/json', 'user-agent': 'MusicList/1.0 (danielcortes.xyz)'}
MB_HOST = 'https://musicbrainz.org/ws/2'
CA_HOST = 'http://coverartarchive.org'
_log = logging.getLogger(__name__)
_log.addHandler(logging.NullHandler())
@@ -29,10 +29,10 @@ def _do_request(url):
if not url:
raise ValueError('URL cant be empty')
_log.info(f'Doing request to "{url}" with headers {_headers}')
response = requests.get(url, headers=_headers)
_log.info('Doing request to "%s" with headers %s', url, HEADERS)
response = requests.get(url, headers=HEADERS)
_log.info(f'Request returned with status code {response.status_code}')
_log.info('Request returned with status code %s', response.status_code)
return response
@@ -52,17 +52,17 @@ def _do_request_mb(url):
:return: The dictionary with the response or the status and his an error message
"""
if not _headers['user-agent']:
if not HEADERS['user-agent']:
raise ValueError('User Agent isn\'t set')
r = _do_request(url)
response = _do_request(url)
if r.status_code == 200:
response = r.json(object_hook=sanitize_keys)
elif r.status_code == 500:
response = {'status': r.status_code, 'error': f'Error del servidor'}
if response.status_code == 200:
response = response.json(object_hook=sanitize_keys)
elif response.status_code == 500:
response = {'status': response.status_code, 'error': 'Error del servidor'}
else:
response = {'status': r.status_code, 'error': r.json()['error']}
response = {'status': response.status_code, 'error': response.json()['error']}
return response
@@ -79,32 +79,32 @@ def _do_request_ca(url):
:raises ValueError when user-agent isn't set
:return: The dictionary with the response or the status and his an error message
"""
if not _headers['user-agent']:
if not HEADERS['user-agent']:
raise ValueError('User Agent isn\'t set')
r = _do_request(url)
response = _do_request(url)
if r.status_code == 200:
response = r.json(object_hook=sanitize_keys)
elif r.status_code == 500:
response = {'status': r.status_code, 'error': f'Error del servidor'}
elif 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'}
if response.status_code == 200:
response = response.json(object_hook=sanitize_keys)
elif response.status_code == 500:
response = {'status': response.status_code, 'error': 'Error del servidor'}
elif response.status_code == 400:
response = {'status': response.status_code, 'error': 'El mbid es invalido'}
elif response.status_code == 404:
response = {'status': response.status_code, 'error': 'No encontrado'}
elif response.status_code == 405:
response = {'status': response.status_code, 'error': 'Metodo erroneo'}
elif response.status_code == 503:
response = {'status': response.status_code, 'error': 'Rate limit exedido'}
else:
response = {'status': r.status_code, 'error': r.json()['error']}
response = {'status': response.status_code, 'error': response.json()['error']}
return response
def _get(entity_type, mbid, includes=None):
"""Does a get entity query to musicbrainz
:param str entity_type: Type of the entity (artist, release, recording...)
:param str mbid: MBID of the entity to get
:param list includes: List of include parameters (recording, releases...)
@@ -114,19 +114,19 @@ def _get(entity_type, mbid, includes=None):
"""
if includes is None:
_log.info(f'Getting {entity_type} of mbid {mbid}')
_log.info('Getting %s of mbid %s', entity_type, mbid)
return _do_request_mb(f'{_mb_host}/{entity_type}/{mbid}')
else:
_log.info(f'Getting {entity_type} of mbid {mbid} including {includes}')
_includes = quote('+'.join(includes))
return _do_request_mb(f'{MB_HOST}/{entity_type}/{mbid}')
return _do_request_mb(f'{_mb_host}/{entity_type}/{mbid}?inc={_includes}')
_log.info('Getting %s of mbid %s including %s', entity_type, mbid, includes)
_includes = quote('+'.join(includes))
return _do_request_mb(f'{MB_HOST}/{entity_type}/{mbid}?inc={_includes}')
def _search(entity_type, query, includes=None, limit=25, offset=0):
"""Does a search of an entity to musicbrainz
:param str entity_type: Type of the entity (artist, release, recording...)
:param str query: The search string
:param int limit: Limit of the search, defaults to 25 and has a max of 100
@@ -136,7 +136,8 @@ def _search(entity_type, query, includes=None, limit=25, offset=0):
:rtype: dict
"""
_log.info(f'Searching {entity_type} with query "{query}" at offset {offset} with limit of {limit} with includes {includes}')
_log.info('Searching %s with query "%s" at offset %s with limit of %s with includes %s',
entity_type, query, offset, limit, includes)
if limit >= 0 and offset >= 0:
_query = {'query': query, 'limit': limit, 'offset': offset}
@@ -146,12 +147,12 @@ def _search(entity_type, query, includes=None, limit=25, offset=0):
if includes is not None:
_query['inc'] = '+'.join(includes)
return _do_request_mb(f'{_mb_host}/{entity_type}/?{urlencode(_query)}')
return _do_request_mb(f'{MB_HOST}/{entity_type}/?{urlencode(_query)}')
def _browse(entity_type, params, includes=None, limit=25, offset=0):
"""Browses entities of a type with the given parameters
:param str entity_type: Type of the entity (artist, release, recording...)
:param dict params: Dictionary with the parameters to do the search
:param list includes: List of include parameters (recording, releases...)
@@ -168,13 +169,15 @@ def _browse(entity_type, params, includes=None, limit=25, offset=0):
raise ValueError('Params must be a dictionary')
if includes is None:
_log.info(f'Browsing {entity_type} with parameters {params} at offset {offset} with limit of {limit}')
_log.info('Browsing %s with parameters %s at offset %s with limit of %s',
entity_type, params, offset, limit)
_query = urlencode({**params, 'limit': limit, 'offset': offset})
else:
_log.info(f'Browsing {entity_type} with parameters {params} including {includes} at offset {offset} with limit of {limit}')
_log.info('Browsing %s with parameters %s including %s at offset %s with limit of %s',
entity_type, params, includes, offset, limit)
_query = urlencode({**params, 'inc': '+'.join(includes), 'limit': limit, 'offset': offset})
return _do_request_mb(f'{_mb_host}/{entity_type}?{_query}')
return _do_request_mb(f'{MB_HOST}/{entity_type}?{_query}')
def _ca(entity_type, mbid):
@@ -186,9 +189,9 @@ def _ca(entity_type, mbid):
:return: The url of the cover art
"""
_log.info(f'Obtaining the cover art of the entity with type {entity_type} and mbid {mbid}')
_log.info('Obtaining the cover art of the entity with type %s and mbid %s', entity_type, mbid)
_url = f'{_ca_host}/{entity_type}/{mbid}'
_url = f'{CA_HOST}/{entity_type}/{mbid}'
return _do_request_ca(_url)
@@ -240,7 +243,7 @@ def get_recording_by_mbid(mbid, includes=None):
@cache
def search_artist(query, includes=None, limit=25, offset=0):
"""Search an artist by a query string
:param str query: Query string
:param list includes: List of include parameters
:param int limit: Limit of the search, defaults to 25 and has a max of 100
@@ -253,7 +256,7 @@ def search_artist(query, includes=None, limit=25, offset=0):
@cache
def search_release(query, includes=None, limit=25, offset=0):
"""Search a release by a query string
:param str query: Query string
:param list includes: List of include parameters
:param int limit: Limit of the search, defaults to 25 and has a max of 100
@@ -280,20 +283,20 @@ def search_release_group(query, includes=None, limit=25, offset=0):
@cache
def search_recording(query, includes=None, limit=25, offset=0):
"""Search a recording by a query string
:param str query: Query string
:param list includes: List of include parameters
:param int limit: Limit of the search, defaults to 25 and has a max of 100
:param int offset: Offset of the search for paging purposes
:return: dictionary with the response
"""
return _search('recording', query, includes,limit, offset)
return _search('recording', query, includes, limit, offset)
@cache
def browse_artists(params, includes=None, limit=25, offset=0):
"""Browse an artist given some params
:param dict params: Parameters to do a search
:param list includes: List of include parameters
:param int limit: Limit of the search, defaults to 25 and has a max of 100
@@ -306,7 +309,7 @@ def browse_artists(params, includes=None, limit=25, offset=0):
@cache
def browse_recordings(params, includes=None, limit=25, offset=0):
"""Browse through recordings given some params
:param dict params: Parameters to do a search
:param list includes: List of include parameters
:param int limit: Limit of the search, defaults to 25 and has a max of 100
@@ -319,7 +322,7 @@ def browse_recordings(params, includes=None, limit=25, offset=0):
@cache
def browse_releases(params, includes=None, limit=25, offset=0):
"""Browse through releases given some params
:param dict params: Parameters to do a search
:param list includes: List of include parameters
:param int limit: Limit of the search, defaults to 25 and has a max of 100
@@ -332,7 +335,7 @@ def browse_releases(params, includes=None, limit=25, offset=0):
@cache
def browse_release_groups(params, includes=None, limit=25, offset=0):
"""Browse through release groups given some params
:param dict params: Parameters to do a search
:param list includes: List of include parameters
:param int limit: Limit of the search, defaults to 25 and has a max of 100