clening exeptions

This commit is contained in:
Petitminion 2021-09-09 11:57:26 +02:00
parent 5555382f64
commit 2830134a75
2 changed files with 74 additions and 20 deletions

View file

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