Switch to unified view

a/rdpl2stream/AsxPlaylistDecoder.py b/rdpl2stream/AsxPlaylistDecoder.py
...
...
17
# along with Radio Tray.  If not, see <http://www.gnu.org/licenses/>.
17
# along with Radio Tray.  If not, see <http://www.gnu.org/licenses/>.
18
#
18
#
19
##########################################################################
19
##########################################################################
20
import urllib2
20
import urllib2
21
from lib.common import USER_AGENT
21
from lib.common import USER_AGENT
22
from lxml import etree
22
import xml.etree.ElementTree as ET
23
from lxml import objectify
24
from StringIO import StringIO
23
from StringIO import StringIO
25
import logging
24
import logging
25
import re
26
26
27
class AsxPlaylistDecoder:
27
class AsxPlaylistDecoder:
28
28
29
    def __init__(self):
29
    def __init__(self):
30
        self.log = logging.getLogger('radiotray')
30
        self.log = logging.getLogger('radiotray')
...
...
51
        f.close()
51
        f.close()
52
52
53
        self.log.info('Playlist downloaded')
53
        self.log.info('Playlist downloaded')
54
        self.log.info('Decoding playlist...')
54
        self.log.info('Decoding playlist...')
55
55
56
        parser = etree.XMLParser(recover=True)
56
        try:
57
        root = etree.parse(StringIO(str),parser)
57
            root = ET.parse(StringIO(str))
58
58
        except:
59
            # Last ditch: try to fix docs with mismatched tag name case
60
            str = re.sub('''<([A-Za-z0-9/]+)''', \
61
                         lambda m: "<" + m.group(1).lower(),
62
                         str)
63
            root = ET.parse(StringIO(str))
64
            
59
        #ugly hack to normalize the XML
65
        #ugly hack to normalize the XML
60
        for element in root.iter():
66
        for element in root.iter():
61
67
62
            tmp = element.tag
68
            tmp = element.tag
63
            element.tag = tmp.lower()
69
            element.tag = tmp.lower()
64
70
65
            for key in element.attrib.iterkeys():
71
            keys = element.attrib.keys()
72
            for key in keys:
66
                element.attrib[key.lower()] = element.attrib[key]
73
                element.attrib[key.lower()] = element.attrib[key]
67
74
75
        elts = root.findall(".//ref/[@href]")
68
76
69
        result = root.xpath("//ref/@href")
77
        result = []
78
        for elt in elts:
79
            tmp = elt.attrib['href']
80
            if (tmp.endswith("?MSWMExt=.asf")):
81
                tmp = tmp.replace("http", "mms")
82
            result.append(tmp)
70
83
71
        if (len(result) > 0):
72
73
            for i in range(1,len(result)):
74
75
                tmp = result[i]
76
                if (tmp.endswith("?MSWMExt=.asf")):
77
                    result[i] = tmp.replace("http", "mms")
78
            return result
84
        return result
79
        else:
80
            return []