Extrayendo funcionalidad de cache en su propio modulo
This commit is contained in:
79
fetcher/cache.py
Normal file
79
fetcher/cache.py
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from fetcher import jobs
|
||||||
|
from utils import get_redis_connection
|
||||||
|
|
||||||
|
_log = logging.getLogger('fetcher_cache')
|
||||||
|
_log.addHandler(logging.NullHandler())
|
||||||
|
|
||||||
|
|
||||||
|
def get_artist(mbid):
|
||||||
|
_log.info('Intentando obtener artista %s desde redis', mbid)
|
||||||
|
|
||||||
|
with get_redis_connection() as redis:
|
||||||
|
artist = redis.get(f'artist:{mbid}')
|
||||||
|
if not artist:
|
||||||
|
_log.info('El artista no estaba en redis')
|
||||||
|
jobs.load_artist_on_cache.delay(mbid)
|
||||||
|
return None
|
||||||
|
|
||||||
|
_log.info('El artista se encontro en redis')
|
||||||
|
return json.loads(artist)
|
||||||
|
|
||||||
|
|
||||||
|
def get_disc(mbid):
|
||||||
|
_log.info('Intentando obtener disco %s desde redis', mbid)
|
||||||
|
|
||||||
|
with get_redis_connection() as redis:
|
||||||
|
disc = redis.get(f'release_group:{mbid}')
|
||||||
|
if not disc:
|
||||||
|
_log.info('El disco no estaba en redis')
|
||||||
|
jobs.load_entities_of_release_group.delay(mbid)
|
||||||
|
return None
|
||||||
|
|
||||||
|
_log.info('El disco se encontro en redis')
|
||||||
|
return json.loads(disc)
|
||||||
|
|
||||||
|
|
||||||
|
def get_discs_of_artist(mbid, limit, offset):
|
||||||
|
_log.info('Intentando obtener los discos del arista %s', mbid)
|
||||||
|
|
||||||
|
with get_redis_connection() as redis:
|
||||||
|
key_releases = f'artist:{mbid}:release_groups'
|
||||||
|
|
||||||
|
if key_releases not in redis:
|
||||||
|
_log.debug('%s no existe en redis', key_releases)
|
||||||
|
jobs.load_artist_on_cache.delay(mbid)
|
||||||
|
return None, 0
|
||||||
|
|
||||||
|
count = int(redis.get(f'{key_releases}:count'))
|
||||||
|
if count != redis.zcard(key_releases):
|
||||||
|
_log.debug('La cantidad de release_groups del artista %s no coinciden con el total',
|
||||||
|
mbid)
|
||||||
|
jobs.load_artist_on_cache.delay(mbid)
|
||||||
|
return None, 0
|
||||||
|
|
||||||
|
release_ids = redis.zrange(key_releases, offset, limit)
|
||||||
|
keys = [f'release_group:{mbid}' for mbid in release_ids]
|
||||||
|
if redis.exists(*keys) != len(keys):
|
||||||
|
_log.debug('Aun no se cargan todas las release_groups del artista %s', mbid)
|
||||||
|
jobs.load_artist_on_cache.delay(mbid)
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
_log.info('Se encontraron los discos en redis')
|
||||||
|
return [json.loads(disc) for disc in redis.mget(keys)]
|
||||||
|
|
||||||
|
|
||||||
|
def get_artist_of_disc(mbid):
|
||||||
|
_log.info('Intentando obtener el artista del disco %s en redis', mbid)
|
||||||
|
|
||||||
|
with get_redis_connection() as redis:
|
||||||
|
artist_id = redis.get(f'release_group:{mbid}:artist')
|
||||||
|
if not artist_id:
|
||||||
|
_log.debug('No se encontro el artista')
|
||||||
|
jobs.load_entities_of_release_group.delay(mbid)
|
||||||
|
return None
|
||||||
|
|
||||||
|
_log.debug('Se encontro el artista')
|
||||||
|
return json.loads(redis.get(f'artist:{artist_id}'))
|
||||||
@@ -11,7 +11,7 @@ from math import ceil
|
|||||||
from country_list import countries_for_language
|
from country_list import countries_for_language
|
||||||
|
|
||||||
import fetcher.musicbrainz as mb
|
import fetcher.musicbrainz as mb
|
||||||
from fetcher import jobs
|
from fetcher import jobs, cache
|
||||||
from utils import get_redis_connection
|
from utils import get_redis_connection
|
||||||
|
|
||||||
_log = logging.getLogger('fetcher_medium')
|
_log = logging.getLogger('fetcher_medium')
|
||||||
@@ -170,95 +170,57 @@ def get_artist(mbid):
|
|||||||
"""Obtiene un artista desde musicbrainz incluyendo sus tags"""
|
"""Obtiene un artista desde musicbrainz incluyendo sus tags"""
|
||||||
_log.info('Obteniendo artista con id %s', mbid)
|
_log.info('Obteniendo artista con id %s', mbid)
|
||||||
|
|
||||||
with get_redis_connection() as redis:
|
mb_artist = cache.get_artist(mbid)
|
||||||
_log.debug('Intentando obtener artista %s desde redis', mbid)
|
if mb_artist:
|
||||||
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)
|
|
||||||
|
|
||||||
return map_artist(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):
|
def get_disc(mbid):
|
||||||
"""Obtiene un disco desde musicbrainz"""
|
"""Obtiene un disco desde musicbrainz"""
|
||||||
_log.info('Obteniendo disco con id %s', mbid)
|
_log.info('Obteniendo disco con id %s', mbid)
|
||||||
|
|
||||||
with get_redis_connection() as redis:
|
mb_disc = cache.get_disc(mbid)
|
||||||
_log.debug('Intentando obtener disco %s desde redis', mbid)
|
if mb_disc:
|
||||||
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)
|
|
||||||
return map_disc(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):
|
def get_discs_of_artist(mbid, limit, page):
|
||||||
"""Obtiene los discos de un artista desde musicbrainz incluyendo"""
|
"""Obtiene los discos de un artista desde musicbrainz incluyendo"""
|
||||||
_log.debug("Obteniendo los discos del artista %s en la pagina %s con limite %s",
|
_log.info('Obteniendo los discos del artista %s', mbid)
|
||||||
mbid, limit, page)
|
|
||||||
|
|
||||||
offset = limit * (page - 1)
|
offset = limit * (page - 1)
|
||||||
|
|
||||||
mb_discs = []
|
mb_discs, total = cache.get_discs_of_artist(mbid, limit, offset)
|
||||||
total = 0
|
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
|
mb_discs_browse = mb.browse_release_groups(params={'artist': mbid},
|
||||||
# Si es que coinciden significa que se cargaron todos los discos, pero si no, quizás aun no
|
includes=['artist-credits'],
|
||||||
# terminan de guardarse, por lo que salto el código de obtención y voy directo a musicbrainz
|
limit=limit, offset=offset)
|
||||||
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)
|
|
||||||
|
|
||||||
if len(mb_discs) == 0:
|
if 'error' in mb_discs_browse:
|
||||||
_log.debug('Cargar desde musicbrainz las release groups de %s', mbid)
|
_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
|
mb_discs = mb_discs_browse.get('release_groups')
|
||||||
# en cache antes
|
total = mb_discs_browse.get('release_group_count')
|
||||||
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')
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'paginate': paginate(total, limit, page),
|
'paginate': paginate(total, limit, page),
|
||||||
@@ -269,32 +231,22 @@ def get_discs_of_artist(mbid, limit, page):
|
|||||||
def get_artist_of_disc(mbid):
|
def get_artist_of_disc(mbid):
|
||||||
"""Obtiene el artista de un disco"""
|
"""Obtiene el artista de un disco"""
|
||||||
_log.info('Obteniendo artista del disco %s', mbid)
|
_log.info('Obteniendo artista del disco %s', mbid)
|
||||||
mb_artist = None
|
|
||||||
|
|
||||||
with get_redis_connection() as redis:
|
mb_artist = cache.get_artist_of_disc(mbid)
|
||||||
_log.debug('Intentando obtener el artista del disco %s desde redis', mbid)
|
if mb_artist:
|
||||||
if f'release_group:{mbid}:artist' in redis:
|
return map_artist(mb_artist)
|
||||||
_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)
|
|
||||||
|
|
||||||
if mb_artist is None:
|
mb_artist_browse = mb.browse_artists(params={'release-group': mbid},
|
||||||
_log.debug('Obteniendo el artista del disco %s desde musicbrainz', mbid)
|
includes=['tags'],
|
||||||
mb_artist_browse = mb.browse_artists(params={'release-group': mbid},
|
limit=1, offset=0)
|
||||||
includes=['tags'],
|
|
||||||
limit=1, offset=0)
|
|
||||||
|
|
||||||
if 'error' in mb_artist_browse:
|
if 'error' in mb_artist_browse:
|
||||||
_log.debug('Error en el browse de artista %s', mb_artist_browse)
|
_log.debug('Error al buscar %s', mb_artist_browse)
|
||||||
return mb_artist_browse
|
return mb_artist_browse
|
||||||
|
|
||||||
mb_artist = mb_artist_browse.get('artists')[0]
|
mb_artist = mb_artist_browse.get('artists')[0]
|
||||||
jobs.load_artist_on_cache.delay(mb_artist)
|
|
||||||
|
|
||||||
return {
|
return map_artist(mb_artist)
|
||||||
'artist': map_artist(mb_artist)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|||||||
Reference in New Issue
Block a user