diff --git a/api/funkwhale_api/music/management/commands/wikidata_far_right.py b/api/funkwhale_api/music/management/commands/wikidata_far_right.py new file mode 100644 index 000000000..a735cf3cb --- /dev/null +++ b/api/funkwhale_api/music/management/commands/wikidata_far_right.py @@ -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() diff --git a/api/funkwhale_api/music/wikidata.py b/api/funkwhale_api/music/wikidata.py index 0f2f480e9..deaef4fd4 100644 --- a/api/funkwhale_api/music/wikidata.py +++ b/api/funkwhale_api/music/wikidata.py @@ -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 diff --git a/changes/changelog.d/2395.feature b/changes/changelog.d/2395.feature new file mode 100644 index 000000000..d8ca7cd1f --- /dev/null +++ b/changes/changelog.d/2395.feature @@ -0,0 +1 @@ +Far right / fascist music filter based on wikidata (#2395)