Agregando jobs para rellenar el cache de datos
This commit is contained in:
39
fetcher/jobs.py
Normal file
39
fetcher/jobs.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""Jobs a ejecutar en el fondo"""
|
||||
import logging
|
||||
import json
|
||||
|
||||
import django_rq
|
||||
from django.core.cache import cache
|
||||
|
||||
from fetcher import musicbrainz as mb
|
||||
|
||||
_log = logging.getLogger('fetcher_jobs')
|
||||
_log.addHandler(logging.NullHandler())
|
||||
|
||||
|
||||
@django_rq.job
|
||||
def load_entities_of_release_group(mbid): # pylint: disable=unused-argument
|
||||
"""Carga en cache un release group y sus entidades relacionadas"""
|
||||
|
||||
|
||||
@django_rq.job
|
||||
def load_artist_on_cache(mbid):
|
||||
"""Carga en cache a un artista y todas sus entidades"""
|
||||
|
||||
if cache.ttl(f'artist_{mbid}') != 0:
|
||||
_log.info('El artista ya se habia procesado anteriormente')
|
||||
return
|
||||
|
||||
artista = mb.get_artist_by_mbid(mbid, includes=['tags'])
|
||||
cache.set(f'artist_{mbid}', json.dumps(artista))
|
||||
_log.info('Artista %s almacenado en cache', mbid)
|
||||
|
||||
offset = 0
|
||||
while True:
|
||||
release_groups = mb.browse_release_groups({'artist': mbid}, limit=100, offset=offset)
|
||||
for basic_release_group in release_groups.get('release_groups'):
|
||||
load_entities_of_release_group(basic_release_group.get('id'))
|
||||
|
||||
offset += 100
|
||||
if offset > release_groups.get('release-group-count', 0):
|
||||
break
|
||||
@@ -4,9 +4,14 @@ Su objetivo es modificar los datos que entrega musicbrainz para que sean
|
||||
correspondientes con lo que debe entregar la api, se encarga de casos como
|
||||
traducción.
|
||||
"""
|
||||
import json
|
||||
from math import ceil
|
||||
|
||||
from django.core.cache import cache
|
||||
from country_list import countries_for_language
|
||||
|
||||
import fetcher.musicbrainz as mb
|
||||
from fetcher import jobs
|
||||
|
||||
|
||||
###
|
||||
@@ -150,11 +155,17 @@ def map_coverart(mb_cover):
|
||||
|
||||
def get_artist(mbid):
|
||||
"""Obtiene un artista desde musicbrainz incluyendo sus tags"""
|
||||
mb_artist = mb.get_artist_by_mbid(mbid, includes=['tags'])
|
||||
|
||||
mb_artist = cache.get(f'artist_{mbid}')
|
||||
if mb_artist is None:
|
||||
mb_artist = mb.get_artist_by_mbid(mbid, includes=['tags'])
|
||||
else:
|
||||
mb_artist = json.loads(mb_artist)
|
||||
if 'error' in mb_artist:
|
||||
return mb_artist
|
||||
|
||||
jobs.load_artist_on_cache.delay(mbid)
|
||||
|
||||
return map_artist(mb_artist)
|
||||
|
||||
|
||||
|
||||
@@ -78,4 +78,3 @@ def sanitize_keys(dictionary):
|
||||
new_key = key.replace('-', '_')
|
||||
replace_key(dictionary, key, new_key)
|
||||
return dictionary
|
||||
|
||||
|
||||
Reference in New Issue
Block a user