|
a/rdpl2stream/XspfPlaylistDecoder.py |
|
b/rdpl2stream/XspfPlaylistDecoder.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 |
import xml.etree.ElementTree as ET
|
29 |
import xml.etree.ElementTree as ET
|
22 |
from StringIO import StringIO
|
30 |
from io import BytesIO
|
23 |
from lib.common import USER_AGENT
|
31 |
from lib.common import USER_AGENT, Logger
|
24 |
import logging
|
|
|
25 |
|
32 |
|
26 |
class XspfPlaylistDecoder:
|
33 |
class XspfPlaylistDecoder:
|
27 |
|
|
|
28 |
def __init__(self):
|
34 |
def __init__(self):
|
29 |
self.log = logging.getLogger('radiotray')
|
35 |
self.log = Logger()
|
30 |
self.log.debug('XSPF playlist decoder')
|
|
|
31 |
|
|
|
32 |
|
36 |
|
33 |
def isStreamValid(self, contentType, firstBytes):
|
37 |
def isStreamValid(self, contentType, firstBytes):
|
34 |
|
|
|
35 |
if('application/xspf+xml' in contentType):
|
38 |
if 'application/xspf+xml' in contentType:
|
36 |
self.log.info('Stream is readable by XSPF Playlist Decoder')
|
39 |
self.log.info('Stream is readable by XSPF Playlist Decoder')
|
37 |
return True
|
40 |
return True
|
38 |
else:
|
41 |
else:
|
39 |
return False
|
42 |
return False
|
40 |
|
|
|
41 |
|
43 |
|
42 |
|
44 |
|
43 |
def extractPlaylist(self, url):
|
45 |
def extractPlaylist(self, url):
|
44 |
|
|
|
45 |
self.log.info('Downloading playlist...')
|
46 |
self.log.info('XSPF: downloading playlist...')
|
46 |
|
|
|
47 |
req = urllib2.Request(url)
|
47 |
req = UrlRequest(url)
|
48 |
req.add_header('User-Agent', USER_AGENT)
|
48 |
req.add_header('User-Agent', USER_AGENT)
|
49 |
f = urllib2.urlopen(req)
|
49 |
f = urlUrlopen(req)
|
50 |
str = f.read()
|
50 |
str = f.read()
|
51 |
f.close()
|
51 |
f.close()
|
|
|
52 |
self.log.info('XSPF: playlist downloaded, decoding...')
|
52 |
|
53 |
|
53 |
self.log.info('Playlist downloaded')
|
|
|
54 |
self.log.info('Decoding playlist...')
|
|
|
55 |
|
|
|
56 |
root = ET.parse(StringIO(str))
|
54 |
root = ET.parse(BytesIO(str))
|
57 |
|
|
|
58 |
ns = {'xspf':'http://xspf.org/ns/0/'}
|
55 |
ns = {'xspf':'http://xspf.org/ns/0/'}
|
59 |
elements = root.findall(".//xspf:track/xspf:location", ns)
|
56 |
elements = root.findall(".//xspf:track/xspf:location", ns)
|
60 |
|
57 |
|
61 |
result = []
|
58 |
result = []
|
62 |
for r in elements:
|
59 |
for r in elements:
|