improve cli code
This commit is contained in:
parent
c72bc7a2f0
commit
0b9dbb4e50
|
@ -0,0 +1,13 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
|
||||
from funkwhale_api.music import wikidata
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = """Get the list of far right artists from wikidata and save it into db.
|
||||
Display missing MBID and missing english labels.
|
||||
Be aware this is querying wikidata you could be rate limited.
|
||||
"""
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
wikidata.get_far_right_artists()
|
|
@ -57,10 +57,15 @@ def get_far_right_artists():
|
|||
for result in results["results"]["bindings"]:
|
||||
item_id = result["itemLabel"]["value"]
|
||||
wkd_artist = wbi.item.get(item_id).get_json()
|
||||
if wkd_artist["labels"].get("en", False):
|
||||
artist_name = wkd_artist["labels"]["en"]["value"]
|
||||
else:
|
||||
raise ValueError(f"Artist {item_id} has no English label")
|
||||
artist_name = (
|
||||
wkd_artist["labels"].get("mul", {}).get("value")
|
||||
or wkd_artist["labels"].get("en", {}).get("value")
|
||||
or next(iter(wkd_artist["labels"].values()), False)
|
||||
)
|
||||
|
||||
if not artist_name:
|
||||
logger.info(f"Artist {item_id} has no label, skipping")
|
||||
continue
|
||||
|
||||
query = Q(name=artist_name)
|
||||
|
||||
|
@ -78,7 +83,7 @@ def get_far_right_artists():
|
|||
default = {"name": artist_name, "mbid": artist_mbid, "far_right": item_id}
|
||||
|
||||
artist, created = get_best_candidate_or_create(
|
||||
Artist, query, default, sort_fields=["name"]
|
||||
Artist, query, default, sort_fields=["name", "mbid"]
|
||||
)
|
||||
|
||||
if not created:
|
||||
|
@ -88,4 +93,10 @@ def get_far_right_artists():
|
|||
artists.append(artist)
|
||||
|
||||
logger.info(f"Found {len(artists)} far right artists : {[a.name for a in artists]}")
|
||||
|
||||
# Remove far right flag from artists that are not in the list
|
||||
Artist.objects.filter(far_right__isnull=False).exclude(
|
||||
id__in=[a.id for a in artists]
|
||||
).update(far_right=None)
|
||||
|
||||
return artists
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Far right / fascist music filter based on wikidata (#2395)
|
Loading…
Reference in New Issue