improve cli code

This commit is contained in:
Petitminion 2025-02-10 18:06:36 +01:00
parent c72bc7a2f0
commit 0b9dbb4e50
3 changed files with 30 additions and 5 deletions

View File

@ -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()

View File

@ -57,10 +57,15 @@ def get_far_right_artists():
for result in results["results"]["bindings"]: for result in results["results"]["bindings"]:
item_id = result["itemLabel"]["value"] item_id = result["itemLabel"]["value"]
wkd_artist = wbi.item.get(item_id).get_json() wkd_artist = wbi.item.get(item_id).get_json()
if wkd_artist["labels"].get("en", False): artist_name = (
artist_name = wkd_artist["labels"]["en"]["value"] wkd_artist["labels"].get("mul", {}).get("value")
else: or wkd_artist["labels"].get("en", {}).get("value")
raise ValueError(f"Artist {item_id} has no English label") 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) 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} default = {"name": artist_name, "mbid": artist_mbid, "far_right": item_id}
artist, created = get_best_candidate_or_create( artist, created = get_best_candidate_or_create(
Artist, query, default, sort_fields=["name"] Artist, query, default, sort_fields=["name", "mbid"]
) )
if not created: if not created:
@ -88,4 +93,10 @@ def get_far_right_artists():
artists.append(artist) artists.append(artist)
logger.info(f"Found {len(artists)} far right artists : {[a.name for a in artists]}") 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 return artists

View File

@ -0,0 +1 @@
Far right / fascist music filter based on wikidata (#2395)