From c87684aa6b1f04296f060aef60bfa76f42201a71 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Wed, 19 Feb 2014 23:48:09 +0100 Subject: [PATCH 1/2] audio: Handle all caps ASX tags. Also add support for simple asx format. --- mopidy/audio/playlists.py | 13 +++++++---- tests/audio/test_playlists.py | 44 +++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/mopidy/audio/playlists.py b/mopidy/audio/playlists.py index 59236dba..fe37d4a7 100644 --- a/mopidy/audio/playlists.py +++ b/mopidy/audio/playlists.py @@ -18,7 +18,7 @@ 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, 8).upper() == b'#EXTM3U\n' def detect_pls_header(typefind): @@ -27,7 +27,7 @@ def detect_pls_header(typefind): 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(): diff --git a/tests/audio/test_playlists.py b/tests/audio/test_playlists.py index 26a8634e..51c36eac 100644 --- a/tests/audio/test_playlists.py +++ b/tests/audio/test_playlists.py @@ -33,21 +33,28 @@ Length3=213 Version=2 """ -ASX = b""" - Example - - Sample Title - - - - Example title - - - - Other title - - - +ASX = b""" + Example + + Sample Title + + + + Example title + + + + Other title + + + +""" + +SIMPLE_ASX = b""" + + + + """ XSPF = b""" @@ -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 From e7b39bd051d199b7117ca450e0acbfdd3b9473f7 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Fri, 20 Jun 2014 21:31:12 +0200 Subject: [PATCH 2/2] audio: Handle carriage return in playlist detection --- mopidy/audio/playlists.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mopidy/audio/playlists.py b/mopidy/audio/playlists.py index fe37d4a7..35e0800d 100644 --- a/mopidy/audio/playlists.py +++ b/mopidy/audio/playlists.py @@ -18,11 +18,11 @@ 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).upper() == 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):