Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
funkwhale
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
petitminion
funkwhale
Commits
4186e65b
Commit
4186e65b
authored
3 years ago
by
petitminion
Browse files
Options
Downloads
Patches
Plain Diff
Update utils.py
parent
53e4a246
No related branches found
No related tags found
No related merge requests found
Pipeline
#15192
failed
3 years ago
Stage: review
Stage: lint
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
api/funkwhale_api/playlists/utils.py
+32
-12
32 additions, 12 deletions
api/funkwhale_api/playlists/utils.py
with
32 additions
and
12 deletions
api/funkwhale_api/playlists/utils.py
+
32
−
12
View file @
4186e65b
import
logging
import
logging
import
os
import
re
import
re
# /!\ 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
...
@@ -27,13 +25,7 @@ def clean_namespace_xspf(xspf_file):
...
@@ -27,13 +25,7 @@ def clean_namespace_xspf(xspf_file):
# This is needed because lxml error : "ValueError: Unicode strings with encoding declaration are
# This is needed because lxml error : "ValueError: Unicode strings with encoding declaration are
# not supported. Please use bytes input or XML fragments without declaration."
# not supported. Please use bytes input or XML fragments without declaration."
xspf_data
=
re
.
sub
(
"'
encoding=
'
.
'"
,
""
,
xspf_data
)
xspf_data
=
re
.
sub
(
"'
encoding=
'
.
'"
,
""
,
xspf_data
)
xspf_file_clean
=
xspf_file
+
"
2
"
return
xspf_data
if
os
.
path
.
exists
(
xspf_file_clean
):
os
.
remove
(
xspf_file_clean
)
file
=
open
(
xspf_file_clean
,
"
x
"
)
file
.
write
(
xspf_data
)
return
xspf_file_clean
def
get_track_id_from_xspf
(
xspf_file
):
def
get_track_id_from_xspf
(
xspf_file
):
...
@@ -41,8 +33,8 @@ def get_track_id_from_xspf(xspf_file):
...
@@ -41,8 +33,8 @@ 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.
Return a list of funkwhale tracks id from a xspf file. Tracks not found in database are ignored.
"""
"""
track_list
=
[]
track_list
=
[]
xspf_
file
_clean
=
clean_namespace_xspf
(
xspf_file
)
xspf_
data
_clean
=
clean_namespace_xspf
(
xspf_file
)
tree
=
etree
.
parse
(
xspf_
file
_clean
)
tree
=
etree
.
fromstring
(
xspf_
data
_clean
)
tracks
=
tree
.
findall
(
"
.//track
"
)
tracks
=
tree
.
findall
(
"
.//track
"
)
added_track_count
=
0
added_track_count
=
0
...
@@ -55,7 +47,6 @@ def get_track_id_from_xspf(xspf_file):
...
@@ -55,7 +47,6 @@ def get_track_id_from_xspf(xspf_file):
album
=
track
.
find
(
"
.//album
"
).
text
album
=
track
.
find
(
"
.//album
"
).
text
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
info
(
f
"
Error while parsing Xml file :
{
e
!r}
"
)
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
)
...
@@ -118,6 +109,35 @@ def generate_xspf_from_playlist(playlist_id):
...
@@ -118,6 +109,35 @@ def generate_xspf_from_playlist(playlist_id):
return
prettify
(
top
)
return
prettify
(
top
)
def
generate_xspf_from_tracks_ids
(
tracks_ids
):
"""
This returns a string containing playlist data in xspf format. It
'
s used for test purposes.
"""
top
=
Element
(
"
playlist
"
)
top
.
set
(
"
version
"
,
"
1
"
)
# top.append(Element.fromstring('version="1"'))
title_xspf
=
SubElement
(
top
,
"
title
"
)
title_xspf
.
text
=
"
An automated generated playlist
"
trackList_xspf
=
SubElement
(
top
,
"
trackList
"
)
for
track_id
in
tracks_ids
:
track
=
Track
.
objects
.
get
(
id
=
track_id
)
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]
"
:
continue
else
:
album_xspf
=
SubElement
(
track_xspf
,
"
album
"
)
album_xspf
.
text
=
str
(
track
.
album
)
return
prettify
(
top
)
def
prettify
(
elem
):
def
prettify
(
elem
):
"""
"""
Return a pretty-printed XML string for the Element.
Return a pretty-printed XML string for the Element.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment