mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-05 04:49:24 +02:00
Cleaning exeptions
This commit is contained in:
parent
7af11d3fdb
commit
3f619c1f00
1 changed files with 58 additions and 50 deletions
|
@ -1,11 +1,13 @@
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
from datetime import datetime
|
||||||
# /!\ The next import have xml vulnerabilities but this shouldn't have security implication in funkwhale
|
# /!\ The next import have xml vulnerabilities but this shouldn't have security implication in funkwhale
|
||||||
# since there are only used to generate xspf file.
|
# since there are only used to generate xspf file.
|
||||||
from xml.etree.ElementTree import Element, SubElement
|
from xml.etree.ElementTree import Element, SubElement
|
||||||
|
|
||||||
from defusedxml import ElementTree as etree
|
from defusedxml import ElementTree as etree
|
||||||
from defusedxml import minidom
|
from defusedxml import minidom
|
||||||
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
|
||||||
from funkwhale_api.music.models import Album, Artist, Track
|
from funkwhale_api.music.models import Album, Artist, Track
|
||||||
from funkwhale_api.playlists.models import Playlist
|
from funkwhale_api.playlists.models import Playlist
|
||||||
|
@ -45,8 +47,9 @@ def get_track_id_from_xspf(xspf_file):
|
||||||
artist = track.find(".//creator").text
|
artist = track.find(".//creator").text
|
||||||
title = track.find(".//title").text
|
title = track.find(".//title").text
|
||||||
album = track.find(".//album").text
|
album = track.find(".//album").text
|
||||||
except Exception as e:
|
except AttributeError as e:
|
||||||
logger.info(f"Error while parsing Xml file : {e!r}")
|
logger.info(f"Couldn't find the following attribute while parsing the xml file : {e!r}")
|
||||||
|
continue
|
||||||
# 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)
|
||||||
|
@ -57,12 +60,11 @@ def get_track_id_from_xspf(xspf_file):
|
||||||
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
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except ObjectDoesNotExist:
|
||||||
if e:
|
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 ObjectDoesNotExist as e:
|
||||||
except Exception as e:
|
logger.info(f"Couldn't find track in the database : {e!r}")
|
||||||
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_track_count = added_track_count + 1
|
added_track_count = added_track_count + 1
|
||||||
|
@ -81,59 +83,65 @@ def generate_xspf_from_playlist(playlist_id):
|
||||||
This returns a string containing playlist data in xspf format
|
This returns a string containing playlist data in xspf format
|
||||||
"""
|
"""
|
||||||
fw_playlist = Playlist.objects.get(id=playlist_id)
|
fw_playlist = Playlist.objects.get(id=playlist_id)
|
||||||
plt_tracks = fw_playlist.playlist_tracks.prefetch_related('track')
|
plt_tracks = fw_playlist.playlist_tracks.prefetch_related('track')
|
||||||
top = Element("playlist")
|
xpsf_playlist = Element("playlist")
|
||||||
top.set("version", "1")
|
xpsf_tracklist = write_xpsf_headers(xpsf_playlist, fw_playlist.name, str(fw_playlist.creation_date))
|
||||||
title_xspf = SubElement(top, "title")
|
|
||||||
title_xspf.text = fw_playlist.name
|
|
||||||
date_xspf = SubElement(top, "date")
|
|
||||||
date_xspf.text = str(fw_playlist.creation_date)
|
|
||||||
trackList_xspf = SubElement(top, "trackList")
|
|
||||||
|
|
||||||
for plt_track in plt_tracks:
|
for plt_track in plt_tracks:
|
||||||
track = plt_track.track
|
track = plt_track.track
|
||||||
track_xspf = SubElement(trackList_xspf, "track")
|
write_xspf_track_data(track, xpsf_tracklist)
|
||||||
location_xspf = SubElement(track_xspf, "location")
|
return prettify(xpsf_playlist)
|
||||||
location_xspf.text = "https://" + track.domain_name + track.listen_url
|
|
||||||
title_xspf = SubElement(track_xspf, "title")
|
|
||||||
title_xspf.text = str(track.title)
|
|
||||||
creator_xspf = SubElement(track_xspf, "creator")
|
|
||||||
creator_xspf.text = str(track.artist)
|
|
||||||
if str(track.album) == "[non-album tracks]":
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
album_xspf = SubElement(track_xspf, "album")
|
|
||||||
album_xspf.text = str(track.album)
|
|
||||||
return prettify(top)
|
|
||||||
|
|
||||||
|
|
||||||
def generate_xspf_from_tracks_ids(tracks_ids):
|
def generate_xspf_from_tracks_ids(tracks_ids):
|
||||||
"""
|
"""
|
||||||
This returns a string containing playlist data in xspf format. It's used for test purposes.
|
This returns a string containing playlist data in xspf format. It's used for test purposes.
|
||||||
"""
|
"""
|
||||||
|
xspf_title = "An automated generated playlist"
|
||||||
top = Element("playlist")
|
now = datetime.now()
|
||||||
top.set("version", "1")
|
xpsf_date = now.strftime("%m/%d/%Y")
|
||||||
# top.append(Element.fromstring('version="1"'))
|
xpsf_playlist = Element("playlist")
|
||||||
title_xspf = SubElement(top, "title")
|
xpsf_tracklist = write_xpsf_headers(xpsf_playlist, xspf_title, xpsf_date)
|
||||||
title_xspf.text = "An automated generated playlist"
|
|
||||||
trackList_xspf = SubElement(top, "trackList")
|
|
||||||
|
|
||||||
for track_id in tracks_ids:
|
for track_id in tracks_ids:
|
||||||
track = Track.objects.get(id=track_id)
|
try:
|
||||||
track_xspf = SubElement(trackList_xspf, "track")
|
track = Track.objects.get(id=track_id)
|
||||||
location_xspf = SubElement(track_xspf, "location")
|
write_xspf_track_data(track, xpsf_tracklist)
|
||||||
location_xspf.text = "https://" + track.domain_name + track.listen_url
|
except ObjectDoesNotExist as e:
|
||||||
title_xspf = SubElement(track_xspf, "title")
|
logger.info(f"Error while quering database : {e!r}")
|
||||||
title_xspf.text = str(track.title)
|
return prettify(xpsf_playlist)
|
||||||
creator_xspf = SubElement(track_xspf, "creator")
|
|
||||||
creator_xspf.text = str(track.artist)
|
|
||||||
if str(track.album) == "[non-album tracks]":
|
def write_xpsf_headers(xpsf_playlist, xpsf_title, xpsf_date):
|
||||||
continue
|
"""
|
||||||
else:
|
This generate the playlist metadata and return a trackList subelement used to insert each track
|
||||||
album_xspf = SubElement(track_xspf, "album")
|
into the playlist
|
||||||
album_xspf.text = str(track.album)
|
"""
|
||||||
return prettify(top)
|
xpsf_playlist.set("version", "1")
|
||||||
|
title_xspf = SubElement(xpsf_playlist, "title")
|
||||||
|
title_xspf.text = xpsf_title
|
||||||
|
date_xspf = SubElement(xpsf_playlist, "date")
|
||||||
|
date_xspf.text = xpsf_date
|
||||||
|
trackList_xspf = SubElement(xpsf_playlist, "trackList")
|
||||||
|
return trackList_xspf
|
||||||
|
|
||||||
|
|
||||||
|
def write_xspf_track_data(track, trackList_xspf):
|
||||||
|
"""
|
||||||
|
Insert a track into the trackList subelement of a xspf file
|
||||||
|
"""
|
||||||
|
track_xspf = SubElement(trackList_xspf, "track")
|
||||||
|
location_xspf = SubElement(track_xspf, "location")
|
||||||
|
location_xspf.text = "https://" + track.domain_name + track.listen_url
|
||||||
|
title_xspf = SubElement(track_xspf, "title")
|
||||||
|
title_xspf.text = str(track.title)
|
||||||
|
creator_xspf = SubElement(track_xspf, "creator")
|
||||||
|
creator_xspf.text = str(track.artist)
|
||||||
|
if str(track.album) == "[non-album tracks]":
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
album_xspf = SubElement(track_xspf, "album")
|
||||||
|
album_xspf.text = str(track.album)
|
||||||
|
|
||||||
|
|
||||||
def prettify(elem):
|
def prettify(elem):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue