Merge pull request #750 from adamcik/fix/gh-687-playlist-handling

Fix/gh 687 playlist handling
This commit is contained in:
Stein Magnus Jodal 2014-06-20 22:11:12 +02:00
commit 0fa47c86bb
2 changed files with 39 additions and 20 deletions

View File

@ -18,16 +18,16 @@ except ImportError:
# TODO: make detect_FOO_header reusable in general mopidy code.
# i.e. give it just a "peek" like function.
def detect_m3u_header(typefind):
return typefind.peek(0, 8) == b'#EXTM3U\n'
return typefind.peek(0, 7).upper() == b'#EXTM3U'
def detect_pls_header(typefind):
return typefind.peek(0, 11).lower() == b'[playlist]\n'
return typefind.peek(0, 10).lower() == b'[playlist]'
def detect_xspf_header(typefind):
data = typefind.peek(0, 150)
if b'xspf' not in data:
if b'xspf' not in data.lower():
return False
try:
@ -41,7 +41,7 @@ def detect_xspf_header(typefind):
def detect_asx_header(typefind):
data = typefind.peek(0, 50)
if b'asx' not in data:
if b'asx' not in data.lower():
return False
try:
@ -82,6 +82,7 @@ def parse_pls(data):
def parse_xspf(data):
try:
# Last element will be root.
for event, element in elementtree.iterparse(data):
element.tag = element.tag.lower() # normalize
except elementtree.ParseError:
@ -94,14 +95,18 @@ def parse_xspf(data):
def parse_asx(data):
try:
# Last element will be root.
for event, element in elementtree.iterparse(data):
element.tag = element.tag.lower() # normalize
except elementtree.ParseError:
return
for ref in element.findall('entry/ref'):
for ref in element.findall('entry/ref[@href]'):
yield ref.get('href', '').strip()
for entry in element.findall('entry[@href]'):
yield entry.get('href', '').strip()
def parse_urilist(data):
for line in data.readlines():

View File

@ -33,21 +33,28 @@ Length3=213
Version=2
"""
ASX = b"""<asx version="3.0">
<title>Example</title>
<entry>
<title>Sample Title</title>
<ref href="file:///tmp/foo" />
</entry>
<entry>
<title>Example title</title>
<ref href="file:///tmp/bar" />
</entry>
<entry>
<title>Other title</title>
<ref href="file:///tmp/baz" />
</entry>
</asx>
ASX = b"""<ASX version="3.0">
<TITLE>Example</TITLE>
<ENTRY>
<TITLE>Sample Title</TITLE>
<REF href="file:///tmp/foo" />
</ENTRY>
<ENTRY>
<TITLE>Example title</TITLE>
<REF href="file:///tmp/bar" />
</ENTRY>
<ENTRY>
<TITLE>Other title</TITLE>
<REF href="file:///tmp/baz" />
</ENTRY>
</ASX>
"""
SIMPLE_ASX = b"""<ASX version="3.0">
<ENTRY href="file:///tmp/foo" />
<ENTRY href="file:///tmp/bar" />
<ENTRY href="file:///tmp/baz" />
</ASX>
"""
XSPF = b"""<?xml version="1.0" encoding="UTF-8"?>
@ -121,6 +128,13 @@ class AsxPlsPlaylistTest(BasePlaylistTest, unittest.TestCase):
parse = staticmethod(playlists.parse_asx)
class SimpleAsxPlsPlaylistTest(BasePlaylistTest, unittest.TestCase):
valid = SIMPLE_ASX
invalid = BAD
detect = staticmethod(playlists.detect_asx_header)
parse = staticmethod(playlists.parse_asx)
class XspfPlaylistTest(BasePlaylistTest, unittest.TestCase):
valid = XSPF
invalid = BAD