|
a/rdpl2stream/AsxPlaylistDecoder.py |
|
b/rdpl2stream/AsxPlaylistDecoder.py |
|
... |
|
... |
15 |
#
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
16 |
# You should have received a copy of the GNU General Public License
|
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 sys
|
|
|
21 |
PY3 = sys.version > '3'
|
|
|
22 |
if PY3:
|
|
|
23 |
from urllib.request import Request as UrlRequest
|
|
|
24 |
from urllib.request import urlopen as urlUrlopen
|
|
|
25 |
else:
|
|
|
26 |
from urllib2 import Request as UrlRequest
|
|
|
27 |
from urllib2 import urlopen as urlUrlopen
|
|
|
28 |
|
21 |
from lib.common import USER_AGENT
|
29 |
from lib.common import USER_AGENT, Logger
|
22 |
import xml.etree.ElementTree as ET
|
30 |
import xml.etree.ElementTree as ET
|
23 |
from StringIO import StringIO
|
31 |
from io import BytesIO
|
24 |
import logging
|
|
|
25 |
import re
|
32 |
import re
|
26 |
|
33 |
|
27 |
class AsxPlaylistDecoder:
|
34 |
class AsxPlaylistDecoder:
|
28 |
|
|
|
29 |
def __init__(self):
|
35 |
def __init__(self):
|
30 |
self.log = logging.getLogger('radiotray')
|
36 |
self.log = Logger()
|
31 |
self.log.debug('ASX-familiy playlist decoder')
|
|
|
32 |
|
37 |
|
33 |
|
38 |
|
34 |
def isStreamValid(self, contentType, firstBytes):
|
39 |
def isStreamValid(self, contentType, firstBytes):
|
35 |
|
40 |
if ('audio/x-ms-wax' in contentType or \
|
36 |
if(('audio/x-ms-wax' in contentType or 'video/x-ms-wvx' in contentType or 'video/x-ms-asf' in contentType or 'video/x-ms-wmv' in contentType) and firstBytes.strip().lower().startswith('<asx')):
|
41 |
'video/x-ms-wvx' in contentType or \
|
|
|
42 |
'video/x-ms-asf' in contentType or \
|
|
|
43 |
'video/x-ms-wmv' in contentType) and \
|
|
|
44 |
firstBytes.strip().lower().startswith(b'<asx'):
|
37 |
self.log.info('Stream is readable by ASX Playlist Decoder')
|
45 |
self.log.info('Stream is readable by ASX Playlist Decoder')
|
38 |
return True
|
46 |
return True
|
39 |
else:
|
47 |
else:
|
40 |
return False
|
48 |
return False
|
41 |
|
49 |
|
42 |
|
50 |
|
43 |
def extractPlaylist(self, url):
|
51 |
def extractPlaylist(self, url):
|
|
|
52 |
self.log.info('ASX: Downloading playlist...')
|
44 |
|
53 |
|
45 |
self.log.info('Downloading playlist...')
|
|
|
46 |
|
|
|
47 |
req = urllib2.Request(url)
|
54 |
req = UrlRequest(url)
|
48 |
req.add_header('User-Agent', USER_AGENT)
|
55 |
req.add_header('User-Agent', USER_AGENT)
|
49 |
f = urllib2.urlopen(req)
|
56 |
f = urlUrlopen(req)
|
50 |
str = f.read()
|
57 |
str = f.read()
|
51 |
f.close()
|
58 |
f.close()
|
52 |
|
59 |
|
53 |
self.log.info('Playlist downloaded')
|
60 |
self.log.info('ASX: playlist downloaded, decoding...')
|
54 |
self.log.info('Decoding playlist...')
|
|
|
55 |
|
61 |
|
56 |
try:
|
62 |
try:
|
57 |
root = ET.parse(StringIO(str))
|
63 |
root = ET.parse(BytesIO(str))
|
58 |
except:
|
64 |
except:
|
59 |
# Last ditch: try to fix docs with mismatched tag name case
|
65 |
# Last ditch: try to fix docs with mismatched tag name case
|
60 |
str = re.sub('''<([A-Za-z0-9/]+)''', \
|
66 |
str = re.sub('''<([A-Za-z0-9/]+)''', \
|
61 |
lambda m: "<" + m.group(1).lower(),
|
67 |
lambda m: "<" + m.group(1).lower(),
|
62 |
str)
|
68 |
str)
|
63 |
root = ET.parse(StringIO(str))
|
69 |
root = ET.parse(BytesIO(str))
|
64 |
|
70 |
|
65 |
#ugly hack to normalize the XML
|
71 |
#ugly hack to normalize the XML
|
66 |
for element in root.iter():
|
72 |
for element in root.iter():
|
67 |
|
|
|
68 |
tmp = element.tag
|
73 |
tmp = element.tag
|
69 |
element.tag = tmp.lower()
|
74 |
element.tag = tmp.lower()
|
70 |
|
75 |
|
71 |
keys = element.attrib.keys()
|
76 |
keys = element.attrib.keys()
|
72 |
for key in keys:
|
77 |
for key in keys:
|