Update api/funkwhale_api/playlists/utils.py

This commit is contained in:
petitminion 2021-06-09 13:48:59 +00:00
parent 706815115a
commit 53e4a2464c

View file

@ -18,7 +18,7 @@ logger = logging.getLogger(__name__)
def clean_namespace_xspf(xspf_file): def clean_namespace_xspf(xspf_file):
""" """
This will delete any namaespace found in the xspf file. It will also delete any encoding info. This will delete any namaespace found in the xspf file. It will also delete any encoding info.
This way xspf file will be compatible with our get_playlist_metadata_from_xspf function. This way xspf file will be compatible with our get_track_id_from_xspf function.
""" """
file = open(xspf_file) file = open(xspf_file)
with file as f: with file as f:
@ -44,36 +44,24 @@ def get_track_id_from_xspf(xspf_file):
xspf_file_clean = clean_namespace_xspf(xspf_file) xspf_file_clean = clean_namespace_xspf(xspf_file)
tree = etree.parse(xspf_file_clean) tree = etree.parse(xspf_file_clean)
tracks = tree.findall(".//track") tracks = tree.findall(".//track")
total_track_count = 0 added_track_count = 0
added_file_count = 0
for track in tracks: for track in tracks:
track_id = "" track_id = ""
total_track_count = total_track_count + 1
total = str(total_track_count)
# Getting metadata of the xspf file # Getting metadata of the xspf file
try: try:
artist = track.find(".//creator").text artist = track.find(".//creator").text
except Exception as e:
logger.info("Error while parsing Xml file :%s" % e)
try:
title = track.find(".//title").text title = track.find(".//title").text
except Exception as e:
logger.info("Error while parsing Xml file :%s" % e)
try:
album = track.find(".//album").text album = track.find(".//album").text
except Exception as e: except Exception as e:
logger.info("Error while parsing Xml file :%s" % e) logger.info(f"Error while parsing Xml file : {e!r}")
# Finding track id in the db # Finding track id in the db
try: try:
artist_id = Artist.objects.get(name=artist) artist_id = Artist.objects.get(name=artist)
except Exception as e:
logger.info("Error while quering database : %s" % e)
try:
album_id = Album.objects.get(title=album) album_id = Album.objects.get(title=album)
except Exception as e: except Exception as e:
logger.info("Error while quering database :%s" % e) logger.info(f"Error while quering database : {e!r}")
try: try:
track_id = Track.objects.get( track_id = Track.objects.get(
title=title, artist=artist_id.id, album=album_id.id title=title, artist=artist_id.id, album=album_id.id
@ -83,15 +71,15 @@ def get_track_id_from_xspf(xspf_file):
try: try:
track_id = Track.objects.get(title=title, artist=artist_id.id) track_id = Track.objects.get(title=title, artist=artist_id.id)
except Exception as e: except Exception as e:
logger.info("Error while quering database :%s" % e) logger.info(f"Error while quering database : {e!r}")
if track_id: if track_id:
track_list.append(track_id.id) track_list.append(track_id.id)
added_file_count = added_file_count + 1 added_track_count = added_track_count + 1
logger.info( logger.info(
str(total) str(len(tracks))
+ " tracks where found in xspf file. " + " tracks where found in xspf file. "
+ str(added_file_count) + str(added_track_count)
+ " are gonna be added to playlist." + " are gonna be added to playlist."
) )
return track_list return track_list