Commit 2830134a authored by petitminion's avatar petitminion
Browse files

clening exeptions

parent 5555382f
from xml.etree.ElementTree import Element
from django.db.models.fields import CharField, IntegerField
from rest_framework import serializers
from funkwhale_api.federation import serializers as federation_serializers
......@@ -110,7 +112,30 @@ class PlaylistSerializer(serializers.ModelSerializer):
full_urls.append(url)
return full_urls
def generate_xspf_from_playlist(playlist_id):
class PlaylistAddManySerializer(serializers.Serializer):
tracks = serializers.PrimaryKeyRelatedField(
many=True, queryset=Track.objects.for_nested_serialization()
)
allow_duplicates = serializers.BooleanField(required=False)
class Meta:
fields = "allow_duplicates"
class XspfSerializer(serializers.Serializer):
title = CharField()
playlist_id = IntegerField()
class Meta:
fields = (
"title",
"playlist_id",
)
def get_title():
return "test"
def generate_xspf_from_playlist():
"""
This returns a string containing playlist data in xspf format
"""
......@@ -126,12 +151,3 @@ class PlaylistSerializer(serializers.ModelSerializer):
utils.write_xspf_track_data(track, xspf_tracklist)
return utils.prettify(xspf_playlist)
class PlaylistAddManySerializer(serializers.Serializer):
tracks = serializers.PrimaryKeyRelatedField(
many=True, queryset=Track.objects.for_nested_serialization()
)
allow_duplicates = serializers.BooleanField(required=False)
class Meta:
fields = "allow_duplicates"
......@@ -8,7 +8,7 @@ from xml.etree.ElementTree import Element, SubElement
from defusedxml import ElementTree as etree
from defusedxml import minidom
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from funkwhale_api.music.models import Album, Artist, Track
......@@ -30,9 +30,30 @@ def clean_namespace_xspf(xspf_file):
return xspf_data
def album_exist(track, artist_id):
try:
album = track.find(".//album").text
except AttributeError as e:
logger.info(
f"Couldn't find the following attribute while parsing the xml : {e!r}. No album information."
)
return
try:
album_id = Album.objects.get(title=album, artist_id=artist_id)
except Exception as e:
logger.info(f"Error while quering database for album : {e!r}")
return
except MultipleObjectsReturned as e:
album_id = Album.objects.filter(title=album, artist_id=artist_id).first
return album_id
return album_id
def get_track_id_from_xspf(xspf_file):
"""
Return a list of funkwhale tracks id from a xspf file. Tracks not found in database are ignored.
Usefull to generate playlist from xspf files.
"""
track_list = []
xspf_data_clean = clean_namespace_xspf(xspf_file)
......@@ -46,27 +67,44 @@ def get_track_id_from_xspf(xspf_file):
try:
artist = track.find(".//creator").text
title = track.find(".//title").text
album = track.find(".//album").text
except AttributeError as e:
logger.info(
f"Couldn't find the following attribute while parsing the xml file : {e!r}"
f"Couldn't find the following attribute while parsing the xml file for artist and title data : {e!r}. \
Switching to next track..."
)
continue
# Finding track id in the db
try:
artist_id = Artist.objects.get(name=artist)
album_id = Album.objects.get(title=album)
except Exception as e:
logger.info(f"Error while quering database : {e!r}")
try:
track_id = Track.objects.get(
title=title, artist=artist_id.id, album=album_id.id
)
except ObjectDoesNotExist:
logger.info(f"Error while quering database : {e!r}. Switching to next track.")
continue
except MultipleObjectsReturned as e:
artist_id = Artist.objects.filter(name=artist).first()
album_id = album_exist(track, artist_id)
if album_id:
try:
track_id = Track.objects.get(
title=title, artist=artist_id.id, album=album_id.id
)
except ObjectDoesNotExist as e :
logger.info(f"Couldn't find track in the database : {e!r}. Trying without album...")
except MultipleObjectsReturned as e:
track_id = Track.objects.filter(
title=title, artist=artist_id.id, album=album_id.id
).first()
else:
try:
track_id = Track.objects.get(title=title, artist=artist_id.id)
except ObjectDoesNotExist as e:
logger.info(f"Couldn't find track in the database : {e!r}")
continue
except MultipleObjectsReturned as e:
track_id = Track.objects.filter(title=title, artist=artist_id.id).first()
if track_id:
track_list.append(track_id.id)
added_track_count = added_track_count + 1
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment