Extrayendo funcionalidad de cache en su propio modulo

This commit is contained in:
Daniel Cortes
2020-06-12 02:40:26 -04:00
parent bb27c57c98
commit b8c79da9ca
2 changed files with 124 additions and 93 deletions

View File

@@ -11,7 +11,7 @@ from math import ceil
from country_list import countries_for_language
import fetcher.musicbrainz as mb
from fetcher import jobs
from fetcher import jobs, cache
from utils import get_redis_connection
_log = logging.getLogger('fetcher_medium')
@@ -170,95 +170,57 @@ def get_artist(mbid):
"""Obtiene un artista desde musicbrainz incluyendo sus tags"""
_log.info('Obteniendo artista con id %s', mbid)
with get_redis_connection() as redis:
_log.debug('Intentando obtener artista %s desde redis', mbid)
mb_artist = redis.get(f'artist:{mbid}')
if mb_artist is None:
_log.debug('El artista %s no estaba en redis, obteniendo desde musicbrainz', mbid)
mb_artist = mb.get_artist_by_mbid(mbid, includes=['tags'])
else:
_log.debug('El artista %s fue encontrado en redis', mbid)
mb_artist = json.loads(mb_artist)
if 'error' in mb_artist:
_log.debug('Error en artista %s', mbid)
return mb_artist
jobs.load_artist_on_cache.delay(mbid)
mb_artist = cache.get_artist(mbid)
if mb_artist:
return map_artist(mb_artist)
mb_artist = mb.get_artist_by_mbid(mbid, includes=['tags'])
if 'error' in mb_artist:
_log.debug('Error en artista %s', mb_artist)
return mb_artist
return map_artist(mb_artist)
def get_disc(mbid):
"""Obtiene un disco desde musicbrainz"""
_log.info('Obteniendo disco con id %s', mbid)
with get_redis_connection() as redis:
_log.debug('Intentando obtener disco %s desde redis', mbid)
mb_disc = redis.get(f'release_group:{mbid}')
if mb_disc is None:
_log.debug('El disco %s no estaba en redis, obteniendo desde musicbrainz', mbid)
mb_disc = mb.get_release_group_by_mbid(mbid, ['artists'])
else:
_log.debug('El disco %s fue encontrado en redis', mbid)
mb_disc = json.loads(mb_disc)
if 'error' in mb_disc:
_log.debug('El disco tiene un error %s', mb_disc)
return mb_disc
jobs.load_entities_of_release_group.delay(mbid)
mb_disc = cache.get_disc(mbid)
if mb_disc:
return map_disc(mb_disc)
mb_disc = mb.get_release_group_by_mbid(mbid, ['artists'])
if 'error' in mb_disc:
_log.debug('El disco tiene un error %s', mb_disc)
return mb_disc
return map_disc(mb_disc)
def get_discs_of_artist(mbid, limit, page):
"""Obtiene los discos de un artista desde musicbrainz incluyendo"""
_log.debug("Obteniendo los discos del artista %s en la pagina %s con limite %s",
mbid, limit, page)
_log.info('Obteniendo los discos del artista %s', mbid)
offset = limit * (page - 1)
mb_discs = []
total = 0
mb_discs, total = cache.get_discs_of_artist(mbid, limit, offset)
if mb_discs and total:
return {
'paginate': paginate(total, limit, page),
'discs': {map_disc(disc) for disc in mb_discs}
}
# Si es que tengo un set de release_groups en redis me fijo si es que sus counts coinciden
# Si es que coinciden significa que se cargaron todos los discos, pero si no, quizás aun no
# terminan de guardarse, por lo que salto el código de obtención y voy directo a musicbrainz
with get_redis_connection() as redis:
_log.debug('Intentando encontrar en cache los discos de %s', mbid)
key_releases = f'artist:{mbid}:release_groups'
if key_releases in redis:
if int(redis.get(f'{key_releases}:count')) == redis.zcard(key_releases):
release_ids = redis.zrange(key_releases, offset, limit)
keys = [f'release_group:{mbid}' for mbid in release_ids]
if redis.exists(*keys) == len(release_ids):
_log.debug('Encontrados los discos de %s', mbid)
mb_discs = [get_disc(mbid) for mbid in release_ids]
total = redis.zcard(key_releases)
else:
_log.debug('Aun no se cargan todas las release_groups de %s', mbid)
else:
_log.debug('La cantidad de release_groups que hay almacenadas para %s no coinciden '
'con las totales', key_releases)
else:
_log.debug('%s no se encontraba en redis, saltando código', key_releases)
mb_discs_browse = mb.browse_release_groups(params={'artist': mbid},
includes=['artist-credits'],
limit=limit, offset=offset)
if len(mb_discs) == 0:
_log.debug('Cargar desde musicbrainz las release groups de %s', mbid)
if 'error' in mb_discs_browse:
_log.error('Error al buscar %s', mb_discs_browse)
return mb_discs_browse
# Si es que no había ningún disco, enviar a cargar al artista, quizás nunca se a guardado
# en cache antes
jobs.load_artist_on_cache.delay(mbid)
mb_discs_browse = mb.browse_release_groups(params={'artist': mbid},
includes=['artist-credits'],
limit=limit, offset=offset)
if 'error' in mb_discs_browse:
_log.error('Error al hacer browse de %s', mb_discs_browse)
return mb_discs_browse
mb_discs = mb_discs_browse.get('release_groups')
total = mb_discs_browse.get('release_group_count')
mb_discs = mb_discs_browse.get('release_groups')
total = mb_discs_browse.get('release_group_count')
return {
'paginate': paginate(total, limit, page),
@@ -269,32 +231,22 @@ def get_discs_of_artist(mbid, limit, page):
def get_artist_of_disc(mbid):
"""Obtiene el artista de un disco"""
_log.info('Obteniendo artista del disco %s', mbid)
mb_artist = None
with get_redis_connection() as redis:
_log.debug('Intentando obtener el artista del disco %s desde redis', mbid)
if f'release_group:{mbid}:artist' in redis:
_log.debug('Se encontró el artista del disco %s en redis', mbid)
mb_artist = get_artist(redis.get(f'release_group:{mbid}:artist'))
else:
_log.debug('El artista del disco %s no estaba en redis', mbid)
mb_artist = cache.get_artist_of_disc(mbid)
if mb_artist:
return map_artist(mb_artist)
if mb_artist is None:
_log.debug('Obteniendo el artista del disco %s desde musicbrainz', mbid)
mb_artist_browse = mb.browse_artists(params={'release-group': mbid},
includes=['tags'],
limit=1, offset=0)
mb_artist_browse = mb.browse_artists(params={'release-group': mbid},
includes=['tags'],
limit=1, offset=0)
if 'error' in mb_artist_browse:
_log.debug('Error en el browse de artista %s', mb_artist_browse)
return mb_artist_browse
if 'error' in mb_artist_browse:
_log.debug('Error al buscar %s', mb_artist_browse)
return mb_artist_browse
mb_artist = mb_artist_browse.get('artists')[0]
jobs.load_artist_on_cache.delay(mb_artist)
mb_artist = mb_artist_browse.get('artists')[0]
return {
'artist': map_artist(mb_artist)
}
return map_artist(mb_artist)
##